Repository navigation
Description
Constructors.polygonFromEnvelope (behind ST_MakeEnvelope and ST_PolygonFromEnvelope) uses the same Coordinate object for the first and the last point of the ring. Functions that edit coordinates in place then change that point twice, so the result is a different polygon.
Expected behavior
translate(makeEnvelope(0, 0, 2, 1), 10, 20) should be POLYGON ((10 20, 10 21, 12 21, 12 20, 10 20)) with area 2, the same as translating the equal WKT polygon POLYGON ((0 0, 0 1, 2 1, 2 0, 0 0)).
Actual behavior
translate(makeEnvelope(0, 0, 2, 1), 10, 20) -> POLYGON ((20 40, 10 21, 12 21, 12 20, 20 40)), area 23.0
translate(POLYGON ((0 0, 0 1, 2 1, 2 0, 0 0)), 10, 20) -> POLYGON ((10 20, 10 21, 12 21, 12 20, 10 20)), area 2.0
affine(makeEnvelope(0, 0, 2, 1), 1, 0, 0, 1, 10, 20) -> POLYGON ((20 40, 10 21, 12 21, 12 20, 20 40)), area 23.0
flipCoordinates(polygonFromEnvelope(1, 2, 2, 4)) -> POLYGON ((1 2, 4 1, 4 2, 2 2, 1 2)), area 1.5
Steps to reproduce
Build common (JDK 17, Maven 3.9):
git clone https://github.com/apache/sedona.git sedona
cd sedona
git checkout --detach 1288854605d462a774f4c6bbf644e75dc05d51d4
mvn -B -q -T 2 -pl common -am package \
-Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dspotless.skip=true
mvn -B -q -pl common dependency:build-classpath -Dmdep.outputFile="$PWD/common.cp"Save as Repro.java (reproducer code is CC0-1.0):
import org.apache.sedona.common.Constructors;
import org.apache.sedona.common.Functions;
import org.locationtech.jts.geom.Geometry;
public class Repro {
static void show(String name, Geometry g) {
System.out.println(name + " -> " + Functions.asWKT(g) + ", area " + Functions.area(g));
}
public static void main(String[] args) throws Exception {
show("translate(makeEnvelope(0, 0, 2, 1), 10, 20)",
Functions.translate(Constructors.makeEnvelope(0, 0, 2, 1), 10, 20));
show("translate(POLYGON ((0 0, 0 1, 2 1, 2 0, 0 0)), 10, 20)",
Functions.translate(Constructors.geomFromWKT("POLYGON ((0 0, 0 1, 2 1, 2 0, 0 0))", 0), 10, 20));
show("affine(makeEnvelope(0, 0, 2, 1), 1, 0, 0, 1, 10, 20)",
Functions.affine(Constructors.makeEnvelope(0, 0, 2, 1), 1, 0, 0, 1, 10, 20));
show("flipCoordinates(polygonFromEnvelope(1, 2, 2, 4))",
Functions.flipCoordinates(Constructors.polygonFromEnvelope(1, 2, 2, 4)));
}
}Run it:
CP="$PWD/common/target/sedona-common-2.0.0-SNAPSHOT.jar:$(cat common.cp)"
mkdir -p repro-classes
javac -proc:none -cp "$CP" -d repro-classes Repro.java
java -cp "repro-classes:$CP" ReproConstructors.polygonFromEnvelope sets coordinates[4] = coordinates[0], and the polygon keeps that array, so the first and last ring positions are one object. From reading the code (not run), nested Spark SQL calls such as ST_Translate(ST_MakeEnvelope(0, 0, 2, 1), 10, 20) pass the geometry object straight to the next function, so SQL should be affected the same way. Using a copy of the first coordinate as the closing point fixes it. Happy to send a fix.
Sedona version
2.0.0-SNAPSHOT, commit 1288854605d462a774f4c6bbf644e75dc05d51d4
API type
Java (org.apache.sedona.common.Constructors / Functions)
JRE version
Temurin 17.0.21.1
Environment
Standalone JVM (no Spark), Windows 11 x64
Existing issues
- I searched the existing issues and did not find a duplicate.
Nathan, ChipLab
Activity
[GH-3592] Stop envelope polygons from sharing their closing coordinate (