Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Sep 26, 2023
1 parent c0fa519 commit f77abb7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,15 @@ public static List<VectorTile.Feature> mergeMultiLineString(List<VectorTile.Feat
return mergeGeometries(features, GeometryType.LINE);
}

private static final Comparator<WithIndex<?>> BY_HILBERT_INDEX =
(o1, o2) -> Integer.compare(o1.hilbert(), o2.hilbert());

private record WithIndex<T> (T feature, int hilbert) {}

private static List<VectorTile.Feature> mergeGeometries(
List<VectorTile.Feature> features,
GeometryType geometryType
) {
features = features.stream().sorted(Comparator.comparingInt(f -> f.geometry().index())).toList();
List<VectorTile.Feature> result = new ArrayList<>(features.size());
var groupedByAttrs = groupByAttrs(features, result, geometryType);
for (List<VectorTile.Feature> groupedFeatures : groupedByAttrs) {
Expand All @@ -127,9 +131,11 @@ private static List<VectorTile.Feature> mergeGeometries(
result.add(feature1);
} else {
VectorTile.VectorGeometryMerger combined = VectorTile.newMerger(geometryType);
for (var feature : groupedFeatures) {
combined.accept(feature.geometry());
}
groupedFeatures.stream()
.map(f -> new WithIndex<>(f, f.geometry().hilbertIndex()))
.sorted(BY_HILBERT_INDEX)
.map(d -> d.feature.geometry())
.forEachOrdered(combined);
result.add(feature1.copyWithNewGeometry(combined.finish()));
}
}
Expand Down Expand Up @@ -193,6 +199,7 @@ public static List<VectorTile.Feature> mergeLineStrings(List<VectorTile.Feature>
}
}
if (!outputSegments.isEmpty()) {
outputSegments = sortByHilbertIndex(outputSegments);
Geometry newGeometry = GeoUtils.combineLineStrings(outputSegments);
result.add(feature1.copyWithNewGeometry(newGeometry));
}
Expand Down Expand Up @@ -334,13 +341,22 @@ public static List<VectorTile.Feature> mergeNearbyPolygons(List<VectorTile.Featu
extractPolygons(merged, outPolygons, minArea, minHoleArea);
}
if (!outPolygons.isEmpty()) {
outPolygons = sortByHilbertIndex(outPolygons);
Geometry combined = GeoUtils.combinePolygons(outPolygons);
result.add(feature1.copyWithNewGeometry(combined));
}
}
return result;
}

private static <G extends Geometry> List<G> sortByHilbertIndex(List<G> geometries) {
return geometries.stream()
.map(p -> new WithIndex<>(p, VectorTile.hilbertIndex(p)))
.sorted(BY_HILBERT_INDEX)
.map(d -> d.feature)
.toList();
}

public static List<VectorTile.Feature> mergeNearbyPolygons(List<VectorTile.Feature> features, double minArea,
double minHoleArea, double minDist, double buffer) throws GeometryException {
return mergeNearbyPolygons(features, minArea, minHoleArea, minDist, buffer, DefaultStats.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.stream.Stream;
import javax.annotation.concurrent.NotThreadSafe;
import org.locationtech.jts.algorithm.Orientation;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateSequence;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
Expand Down Expand Up @@ -420,6 +421,13 @@ public static VectorGeometryMerger newMerger(GeometryType geometryType) {
return new VectorGeometryMerger(geometryType);
}

public static int hilbertIndex(Geometry g) {
Coordinate coord = g.getCoordinate();
int x = zigZagEncode((int) Math.round(coord.x * 4096 / 256));
int y = zigZagEncode((int) Math.round(coord.y * 4096 / 256));
return Hilbert.hilbertXYToIndex(15, x, y);
}

/**
* Adds features in a layer to this tile.
*
Expand Down Expand Up @@ -588,9 +596,9 @@ public static class VectorGeometryMerger implements Consumer<VectorGeometry> {
// the sequence

private final GeometryType geometryType;
private final IntArrayList result = new IntArrayList();
private int overallX = 0;
private int overallY = 0;
private final IntArrayList result = new IntArrayList();

private VectorGeometryMerger(GeometryType geometryType) {
this.geometryType = geometryType;
Expand Down Expand Up @@ -925,12 +933,15 @@ public VectorGeometry filterPointsOutsideBuffer(double buffer) {
}
}

public int index() {
// 0 -> 4096*2*2
public int hilbertIndex() {
if (commands.length < 3) {
return 0;
}
int x = commands[1];
int y = commands[2];
return Hilbert.hilbertXYToIndex(14, x, y);
return Hilbert.hilbertXYToIndex(15, x >> scale, y >> scale);
}

}

/**
Expand Down

0 comments on commit f77abb7

Please sign in to comment.