Skip to content

Commit c3b7436

Browse files
committed
fix: simplify the implementation, remove BBox implementation
1 parent 0c576f4 commit c3b7436

File tree

2 files changed

+31
-99
lines changed

2 files changed

+31
-99
lines changed

common/src/main/java/org/apache/sedona/common/Functions.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,38 @@ public static Geometry expand(Geometry geometry, double deltaX, double deltaY, d
106106
return geometry;
107107
}
108108

109-
BBox bBox = new BBox(geometry);
110-
double minX = bBox.getStartLon() - deltaX;
111-
double maxX = bBox.getEndLon() + deltaX;
112-
double minY = bBox.getStartLat() - deltaY;
113-
double maxY = bBox.getEndLat() + deltaY;
114-
double minZ, maxZ;
109+
Coordinate[] coordinates = geometry.getCoordinates();
110+
double minX = Double.MAX_VALUE;
111+
double maxX = -Double.MAX_VALUE;
112+
double minY = Double.MAX_VALUE;
113+
double maxY = -Double.MAX_VALUE;
114+
double minZ = Double.MAX_VALUE;
115+
double maxZ = -Double.MAX_VALUE;
116+
117+
for (int i = 0; i < coordinates.length; i++) {
118+
minX = Math.min(minX, coordinates[i].x);
119+
maxX = Math.max(maxX, coordinates[i].x);
120+
minY = Math.min(minY, coordinates[i].y);
121+
maxY = Math.max(maxY, coordinates[i].y);
122+
minZ = Math.min(minZ, coordinates[i].z);
123+
maxZ = Math.max(maxZ, coordinates[i].z);
124+
}
125+
126+
minX = minX - deltaX;
127+
maxX = maxX + deltaX;
128+
minY = minY - deltaY;
129+
maxY = maxY + deltaY;
130+
115131
if (Functions.hasZ(geometry)) {
116-
minZ = bBox.getMinZ() - deltaZ;
117-
maxZ = bBox.getMaxZ() + deltaZ;
118-
Coordinate[] coordinates = new Coordinate[5];
119-
coordinates[0] = new Coordinate(minX, minY, minZ);
120-
coordinates[1] = new Coordinate(minX, maxY, minZ);
121-
coordinates[2] = new Coordinate(maxX, maxY, maxZ);
122-
coordinates[3] = new Coordinate(maxX, minY, maxZ);
123-
coordinates[4] = coordinates[0];
124-
return geometry.getFactory().createPolygon(coordinates);
132+
minZ = minZ - deltaZ;
133+
maxZ = maxZ + deltaZ;
134+
Coordinate[] newCoords = new Coordinate[5];
135+
newCoords[0] = new Coordinate(minX, minY, minZ);
136+
newCoords[1] = new Coordinate(minX, maxY, minZ);
137+
newCoords[2] = new Coordinate(maxX, maxY, maxZ);
138+
newCoords[3] = new Coordinate(maxX, minY, maxZ);
139+
newCoords[4] = newCoords[0];
140+
return geometry.getFactory().createPolygon(newCoords);
125141
}
126142
Geometry result = Constructors.polygonFromEnvelope(minX, minY, maxX, maxY);
127143
result.setSRID(geometry.getSRID());

common/src/main/java/org/apache/sedona/common/utils/BBox.java

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ public class BBox {
2525
double endLon;
2626
double startLat;
2727
double endLat;
28-
double minZ;
29-
double maxZ;
3028

3129
static GeometryFactory geometryFactory = new GeometryFactory();
3230

@@ -37,64 +35,6 @@ public BBox(double startLon, double endLon, double startLat, double endLat) {
3735
this.endLat = endLat;
3836
}
3937

40-
public BBox(Geometry geom) {
41-
startLon = Double.MAX_VALUE;
42-
endLon = -Double.MAX_VALUE;
43-
startLat = Double.MAX_VALUE;
44-
endLat = -Double.MAX_VALUE;
45-
minZ = Double.MAX_VALUE;
46-
maxZ = -Double.MAX_VALUE;
47-
processEnvelope(geom);
48-
}
49-
50-
private void processEnvelope(Geometry geom) {
51-
if (geom.getClass().getSimpleName().equals(Geometry.TYPENAME_POINT)) {
52-
processEnvelopePoint((Point) geom);
53-
} else if (geom.getClass().getSimpleName().equals(Geometry.TYPENAME_LINESTRING)) {
54-
processEnvelopeCoordinate(geom.getCoordinates());
55-
} else if (geom.getClass().getSimpleName().equals(Geometry.TYPENAME_POLYGON)) {
56-
processEnvelopeCoordinate(((Polygon) geom).getExteriorRing().getCoordinates());
57-
} else if (geom.getClass().getSimpleName().equals(Geometry.TYPENAME_MULTIPOINT)) {
58-
for (int i = 0; i < geom.getNumGeometries(); i++) {
59-
processEnvelope(geom.getGeometryN(i));
60-
}
61-
} else if (geom.getClass().getSimpleName().equals(Geometry.TYPENAME_MULTILINESTRING)) {
62-
for (int i = 0; i < geom.getNumGeometries(); i++) {
63-
processEnvelope(geom.getGeometryN(i));
64-
}
65-
} else if (geom.getClass().getSimpleName().equals(Geometry.TYPENAME_MULTIPOLYGON)) {
66-
for (int i = 0; i < geom.getNumGeometries(); i++) {
67-
processEnvelope(geom.getGeometryN(i));
68-
}
69-
} else if (geom.getClass().getSimpleName().equals(Geometry.TYPENAME_GEOMETRYCOLLECTION)) {
70-
for (int i = 0; i < geom.getNumGeometries(); i++) {
71-
processEnvelope(geom.getGeometryN(i));
72-
}
73-
} else {
74-
throw new RuntimeException("Unknown geometry type: " + geom.getGeometryType());
75-
}
76-
}
77-
78-
private void processEnvelopeCoordinate(Coordinate[] coords) {
79-
for (Coordinate coord : coords) {
80-
startLon = Math.min(startLon, coord.x);
81-
endLon = Math.max(endLon, coord.x);
82-
startLat = Math.min(startLat, coord.y);
83-
endLat = Math.max(endLat, coord.y);
84-
minZ = Math.min(minZ, coord.z);
85-
maxZ = Math.max(maxZ, coord.z);
86-
}
87-
}
88-
89-
private void processEnvelopePoint(Point geom) {
90-
startLon = Math.min(startLon, geom.getCoordinates()[0].getX());
91-
endLon = Math.max(endLon, geom.getCoordinates()[0].getX());
92-
startLat = Math.min(startLat, geom.getCoordinates()[0].getY());
93-
endLat = Math.max(endLat, geom.getCoordinates()[0].getY());
94-
minZ = Math.min(minZ, geom.getCoordinates()[0].getZ());
95-
maxZ = Math.max(maxZ, geom.getCoordinates()[0].getZ());
96-
}
97-
9838
public BBox(BBox other) {
9939
this(other.startLon, other.endLon, other.startLat, other.endLat);
10040
}
@@ -115,28 +55,4 @@ public Polygon toPolygon() {
11555
new Coordinate(this.startLon, this.startLat)
11656
});
11757
}
118-
119-
public double getStartLon() {
120-
return startLon;
121-
}
122-
123-
public double getEndLon() {
124-
return endLon;
125-
}
126-
127-
public double getStartLat() {
128-
return startLat;
129-
}
130-
131-
public double getEndLat() {
132-
return endLat;
133-
}
134-
135-
public double getMinZ() {
136-
return minZ;
137-
}
138-
139-
public double getMaxZ() {
140-
return maxZ;
141-
}
14258
}

0 commit comments

Comments
 (0)