Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Locale;
import java.util.regex.Pattern;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.Lineal;
import org.locationtech.jts.geom.Polygonal;
import org.locationtech.jts.geom.Puntal;
Expand Down Expand Up @@ -37,6 +38,19 @@ public Expression featureTest() {
}

public static GeometryType typeOf(Geometry geom) {
if (geom instanceof GeometryCollection collection && collection.getNumGeometries() >= 1) {
var result = typeOf(collection.getGeometryN(0));
for (int i = 1; i < collection.getNumGeometries(); i++) {
if (!result.equals(typeOf(collection.getGeometryN(i)))) {
return UNKNOWN;
}
}
return result;
}
return typeOfPrimitive(geom);
}

private static GeometryType typeOfPrimitive(Geometry geom) {
return geom instanceof Puntal ? POINT : geom instanceof Lineal ? LINE : geom instanceof Polygonal ? POLYGON :
UNKNOWN;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.onthegomap.planetiler.VectorTile;
import com.onthegomap.planetiler.geo.GeoUtils;
import com.onthegomap.planetiler.geo.GeometryType;
import com.onthegomap.planetiler.reader.osm.OsmElement;
import com.onthegomap.planetiler.reader.osm.OsmReader;
import com.onthegomap.planetiler.reader.osm.OsmRelationInfo;
Expand All @@ -11,10 +12,8 @@
import java.util.Objects;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Lineal;
import org.locationtech.jts.geom.MultiLineString;
import org.locationtech.jts.geom.Polygonal;
import org.locationtech.jts.geom.Puntal;

/**
* An input feature read from a data source with geometry and tags known at creation-time.
Expand Down Expand Up @@ -193,17 +192,17 @@ public Map<String, Object> tags() {

@Override
public boolean isPoint() {
return latLonGeometry instanceof Puntal || worldGeometry instanceof Puntal;
return GeometryType.POINT.equals(GeometryType.typeOf(latLonGeometry != null ? latLonGeometry : worldGeometry));
}

@Override
public boolean canBePolygon() {
return latLonGeometry instanceof Polygonal || worldGeometry instanceof Polygonal;
return GeometryType.POLYGON.equals(GeometryType.typeOf(latLonGeometry != null ? latLonGeometry : worldGeometry));
}

@Override
public boolean canBeLine() {
return latLonGeometry instanceof Lineal || worldGeometry instanceof Lineal;
return GeometryType.LINE.equals(GeometryType.typeOf(latLonGeometry != null ? latLonGeometry : worldGeometry));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
Expand Down Expand Up @@ -235,7 +236,7 @@ private PlanetilerResults runWithOsmElements(
);
}

private SimpleFeature newReaderFeature(Geometry geometry, Map<String, Object> attrs) {
private static SimpleFeature newReaderFeature(Geometry geometry, Map<String, Object> attrs) {
return SimpleFeature.create(geometry, attrs);
}

Expand Down Expand Up @@ -450,6 +451,75 @@ void testMultiPoint() throws Exception {
), results.tiles);
}

static Stream<org.junit.jupiter.params.provider.Arguments> pointCollections() {
double x1 = 0.5 + Z14_WIDTH / 2;
double y1 = 0.5 + Z14_WIDTH / 2;
double x2 = x1 + Z13_WIDTH / 256d;
double y2 = y1 + Z13_WIDTH / 256d;
double lat1 = GeoUtils.getWorldLat(y1);
double lng1 = GeoUtils.getWorldLon(x1);
double lat2 = GeoUtils.getWorldLat(y2);
double lng2 = GeoUtils.getWorldLon(x2);

return Stream.of(
// one collection with several points
org.junit.jupiter.params.provider.Arguments.of(List.of(
newReaderFeature(GeoUtils.JTS_FACTORY.createGeometryCollection(Set.of(
newPoint(lng1, lat1),
newPoint(lng2, lat2)
).toArray(Geometry[]::new)), Map.of(
"attr", "value"
))
)),
// nested collections, i.e. several collection, each with one point, in one collection
org.junit.jupiter.params.provider.Arguments.of(List.of(
newReaderFeature(GeoUtils.JTS_FACTORY.createGeometryCollection(Set.of(
GeoUtils.JTS_FACTORY.createGeometryCollection(Set.of(newPoint(lng1, lat1)).toArray(Geometry[]::new)),
GeoUtils.JTS_FACTORY.createGeometryCollection(Set.of(newPoint(lng2, lat2)).toArray(Geometry[]::new))
).toArray(Geometry[]::new)), Map.of(
"attr", "value"
))
))
);
}

// note: Same as testMultiPoint() but we get list of points in the result, not one multipoint
@ParameterizedTest
@MethodSource("pointCollections")
void testPointCollection(List<SimpleFeature> points) throws Exception {
var results = runWithReaderFeatures(
Map.of("threads", "1"),
points,
(in, features) -> features.point("layer")
.setZoomRange(13, 14)
.setAttr("name", "name value")
.inheritAttrFromSource("attr")
);

assertSubmap(Map.of(
TileCoord.ofXYZ(Z14_TILES / 2, Z14_TILES / 2, 14), List.of(
feature(newPoint(128, 128), Map.of(
"attr", "value",
"name", "name value"
)),
feature(newPoint(130, 130), Map.of(
"attr", "value",
"name", "name value"
))
),
TileCoord.ofXYZ(Z13_TILES / 2, Z13_TILES / 2, 13), List.of(
feature(newPoint(64, 64), Map.of(
"attr", "value",
"name", "name value"
)),
feature(newPoint(65, 65), Map.of(
"attr", "value",
"name", "name value"
))
)
), results.tiles);
}

@Test
void testLabelGridLimit() throws Exception {
double y = 0.5 + Z14_WIDTH / 2;
Expand Down Expand Up @@ -717,8 +787,7 @@ void testNumPointsAttr() throws Exception {
), results.tiles);
}

@Test
void testMultiLineString() throws Exception {
static Stream<org.junit.jupiter.params.provider.Arguments> lineCollections() {
double x1 = 0.5 + Z14_WIDTH / 2;
double y1 = 0.5 + Z14_WIDTH / 2;
double x2 = x1 + Z14_WIDTH;
Expand All @@ -728,16 +797,45 @@ void testMultiLineString() throws Exception {
double lat2 = GeoUtils.getWorldLat(y2);
double lng2 = GeoUtils.getWorldLon(x2);

var results = runWithReaderFeatures(
Map.of("threads", "1"),
List.of(
return Stream.of(
// simple multiline
org.junit.jupiter.params.provider.Arguments.of(List.of(
newReaderFeature(newMultiLineString(
newLineString(lng1, lat1, lng2, lat2),
newLineString(lng2, lat2, lng1, lat1)
), Map.of(
"attr", "value"
))
),
)),
// one collection with several lines
org.junit.jupiter.params.provider.Arguments.of(List.of(
newReaderFeature(GeoUtils.JTS_FACTORY.createGeometryCollection(Set.of(
newLineString(lng1, lat1, lng2, lat2),
newLineString(lng2, lat2, lng1, lat1)
).toArray(Geometry[]::new)), Map.of(
"attr", "value"
))
)),
// nested collections, i.e. several collection, each with one line, in one collection
org.junit.jupiter.params.provider.Arguments.of(List.of(
newReaderFeature(GeoUtils.JTS_FACTORY.createGeometryCollection(Set.of(
GeoUtils.JTS_FACTORY
.createGeometryCollection(Set.of(newLineString(lng1, lat1, lng2, lat2)).toArray(Geometry[]::new)),
GeoUtils.JTS_FACTORY
.createGeometryCollection(Set.of(newLineString(lng2, lat2, lng1, lat1)).toArray(Geometry[]::new))
).toArray(Geometry[]::new)), Map.of(
"attr", "value"
))
))
);
}

@ParameterizedTest
@MethodSource("lineCollections")
void testMultiLineString(List<SimpleFeature> lines) throws Exception {
var results = runWithReaderFeatures(
Map.of("threads", "1"),
lines,
(in, features) -> features.line("layer")
.setZoomRange(13, 14)
.setBufferPixels(4)
Expand Down Expand Up @@ -1441,6 +1539,93 @@ record TestRelationInfo(long id, String name) implements OsmRelationInfo {}
), results.tiles);
}

static Stream<org.junit.jupiter.params.provider.Arguments> polygonCollections() {
double x1 = 0.125;
double y1 = 0.125;
double x2 = 0.875;
double y2 = 0.875;
double x3 = 0.25;
double y3 = 0.25;
double x4 = 0.75;
double y4 = 0.75;
double x5 = 0.375;
double y5 = 0.375;
double x6 = 0.625;
double y6 = 0.625;
double lat1 = GeoUtils.getWorldLat(y1);
double lng1 = GeoUtils.getWorldLon(x1);
double lat2 = GeoUtils.getWorldLat(y2);
double lng2 = GeoUtils.getWorldLon(x2);
double lat3 = GeoUtils.getWorldLat(y3);
double lng3 = GeoUtils.getWorldLon(x3);
double lat4 = GeoUtils.getWorldLat(y4);
double lng4 = GeoUtils.getWorldLon(x4);
double lat5 = GeoUtils.getWorldLat(y5);
double lng5 = GeoUtils.getWorldLon(x5);
double lat6 = GeoUtils.getWorldLat(y6);
double lng6 = GeoUtils.getWorldLon(x6);

var polygon1 = newPolygon(
rectangleCoordList(lng1, lat1, lng2, lat2),
List.of(rectangleCoordList(lng3, lat3, lng4, lat4))
);
var polygon2 = rectangle(lng5, lat5, lng6, lat6);

return Stream.of(
// one collection with several polygons
org.junit.jupiter.params.provider.Arguments.of(List.of(
newReaderFeature(GeoUtils.JTS_FACTORY.createGeometryCollection(Set.of(
polygon1,
polygon2
).toArray(Geometry[]::new)), Map.of(
"attr", "value"
))
)),
// nested collections, i.e. several collection, each with one polygon, in one collection
org.junit.jupiter.params.provider.Arguments.of(List.of(
newReaderFeature(GeoUtils.JTS_FACTORY.createGeometryCollection(Set.of(
GeoUtils.JTS_FACTORY
.createGeometryCollection(Set.of(polygon1).toArray(Geometry[]::new)),
GeoUtils.JTS_FACTORY
.createGeometryCollection(Set.of(polygon2).toArray(Geometry[]::new))
).toArray(Geometry[]::new)), Map.of(
"attr", "value"
))
))
);
}

@ParameterizedTest
@MethodSource("polygonCollections")
void testPolygonCollection(List<SimpleFeature> polygons) throws Exception {
var results = runWithReaderFeatures(
Map.of("threads", "1"),
polygons,
(in, features) -> features.polygon("layer")
.setZoomRange(0, 0)
.setAttr("name", "name value")
.inheritAttrFromSource("attr")
);

assertSubmap(Map.of(
TileCoord.ofXYZ(0, 0, 0), List.of(
feature(rectangle(0.375 * 256, 0.625 * 256), Map.of(
"attr", "value",
"name", "name value"
)),
feature(newPolygon(
rectangleCoordList(0.125 * 256, 0.875 * 256),
List.of(
rectangleCoordList(0.25 * 256, 0.75 * 256)
)
), Map.of(
"attr", "value",
"name", "name value"
))
)
), results.tiles);
}

Comment thread
phanecak-maptiler marked this conversation as resolved.
@Test
void testOsmLineInRelation() throws Exception {
record TestRelationInfo(long id, String name) implements OsmRelationInfo {}
Expand Down
Loading
Loading