Skip to content

Commit

Permalink
Add firstCoordinate vector tile feature helper (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored Feb 3, 2024
1 parent 7c03592 commit f7a3b62
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.locationtech.jts.algorithm.Orientation;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateSequence;
import org.locationtech.jts.geom.CoordinateXY;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
Expand Down Expand Up @@ -987,6 +988,20 @@ public int hilbertIndex() {
return Hilbert.hilbertXYToIndex(15, x >> scale, y >> scale);
}


/**
* Returns the coordinate of the first point in this geometry in tile pixel coordinates from (0,0) at the top left
* to (256,256) at the bottom right.
*/
public CoordinateXY firstCoordinate() {
if (commands.length < 3) {
return null;
}
double factor = 1 << scale;
double x = zigZagDecode(commands[1]) * SIZE / EXTENT / factor;
double y = zigZagDecode(commands[2]) * SIZE / EXTENT / factor;
return new CoordinateXY(x, y);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,65 @@ record Case(int expected, Geometry geom) {}
}));
}

@ParameterizedTest
@CsvSource({
"0,0",
"1,1",
"-10,-1",
"300.25,200.75",
})
void firstCoordinateOfPoint(double x, double y) {
for (int scale = 0; scale < 10; scale++) {
assertEquals(new CoordinateXY(x, y), VectorTile.encodeGeometry(newPoint(x, y), scale).firstCoordinate(),
"scale=" + scale);
}
}

@ParameterizedTest
@CsvSource({
"0,0",
"1,1",
"-10,-1",
"300.25,200.75",
})
void firstCoordinateOfMultiPoint(double x, double y) {
for (int scale = 0; scale < 10; scale++) {
assertEquals(new CoordinateXY(x, y),
VectorTile.encodeGeometry(newMultiPoint(newPoint(x, y), newPoint(0, 0)), scale).firstCoordinate(),
"scale=" + scale);
}
}

@ParameterizedTest
@CsvSource({
"0,0",
"1,1",
"-10,-1",
"300.25,200.75",
})
void firstCoordinateOfLine(double x, double y) {
for (int scale = 0; scale < 10; scale++) {
assertEquals(new CoordinateXY(x, y),
VectorTile.encodeGeometry(newLineString(x, y, x + 1, y + 1), scale).firstCoordinate(),
"scale=" + scale);
}
}

@ParameterizedTest
@CsvSource({
"0,0",
"1,1",
"-10,-1",
"300.25,200.75",
})
void firstCoordinateOfPolygon(double x, double y) {
for (int scale = 0; scale < 10; scale++) {
assertEquals(new CoordinateXY(x, y),
VectorTile.encodeGeometry(newPolygon(x, y, x + 1, y, x + 1, y + 1, x, y + 1, x, y), scale).firstCoordinate(),
"scale=" + scale);
}
}

private static void assertArrayEquals(int[] a, int[] b) {
assertEquals(
IntStream.of(a).boxed().toList(),
Expand Down

0 comments on commit f7a3b62

Please sign in to comment.