Skip to content

Commit a1af661

Browse files
authored
Fix exception in WKB export for polygon with first ring of zero area (#276)
1 parent c0c6217 commit a1af661

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

src/main/java/com/esri/core/geometry/MultiPathImpl.java

+25-18
Original file line numberDiff line numberDiff line change
@@ -2133,27 +2133,34 @@ int getOGCPolygonCount() {
21332133
protected void _updateOGCFlags() {
21342134
if (_hasDirtyFlag(DirtyFlags.DirtyOGCFlags)) {
21352135
_updateRingAreas2D();
2136-
2137-
int pathCount = getPathCount();
2138-
if (pathCount > 0 && (m_pathFlags == null || m_pathFlags.size() < pathCount))
2139-
m_pathFlags = (AttributeStreamOfInt8) AttributeStreamBase
2140-
.createByteStream(pathCount + 1);
2141-
2142-
int firstSign = 1;
2143-
for (int ipath = 0; ipath < pathCount; ipath++) {
2144-
double area = m_cachedRingAreas2D.read(ipath);
2145-
if (ipath == 0)
2146-
firstSign = area > 0 ? 1 : -1;
2147-
if (area * firstSign > 0.0)
2148-
m_pathFlags.setBits(ipath,
2149-
(byte) PathFlags.enumOGCStartPolygon);
2150-
else
2151-
m_pathFlags.clearBits(ipath,
2152-
(byte) PathFlags.enumOGCStartPolygon);
2153-
}
2136+
_updateOGCFlagsHelper();
21542137
_setDirtyFlag(DirtyFlags.DirtyOGCFlags, false);
21552138
}
21562139
}
2140+
2141+
private void _updateOGCFlagsHelper() {
2142+
int pathCount = getPathCount();
2143+
if (pathCount > 0 && (m_pathFlags == null || m_pathFlags.size() < pathCount))
2144+
m_pathFlags = (AttributeStreamOfInt8) AttributeStreamBase.createByteStream(pathCount + 1);
2145+
2146+
// firstSign is the sign of first ring.
2147+
// a first ring with non zero area defines the
2148+
// value. First zero area rings are written out as enumOGCStartPolygon.
2149+
int firstSign = 0;
2150+
for (int ipath = 0; ipath < pathCount; ipath++) {
2151+
double area = m_cachedRingAreas2D.read(ipath);
2152+
if (firstSign == 0) {
2153+
// if the first ring is inverted we assume that the
2154+
// whole polygon is inverted.
2155+
firstSign = MathUtils.sign(area);
2156+
}
2157+
2158+
if (area * firstSign > 0.0 || firstSign == 0)
2159+
m_pathFlags.setBits(ipath, (byte) PathFlags.enumOGCStartPolygon);
2160+
else
2161+
m_pathFlags.clearBits(ipath, (byte) PathFlags.enumOGCStartPolygon);
2162+
}
2163+
}
21572164

21582165
public int getPathIndexFromPointIndex(int pointIndex) {
21592166
int positionHint = m_currentPathIndex;// in case of multithreading

src/test/java/com/esri/core/geometry/TestImportExport.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,6 @@ protected void tearDown() throws Exception {
4444

4545
@Test
4646
public static void testImportExportShapePolygon() {
47-
// {
48-
// String s = "MULTIPOLYGON (((-1.4337158203098852 53.42590083930004, -1.4346462383651897 53.42590083930004, -1.4349713164114632 53.42426406667512, -1.4344808816770183 53.42391134176576, -1.4337158203098852 53.424339319373516, -1.4337158203098852 53.42590083930004, -1.4282226562499147 53.42590083930004, -1.4282226562499147 53.42262754610009, -1.423659941537096 53.42262754610009, -1.4227294921872726 53.42418897437618, -1.4199829101572732 53.42265258737483, -1.4172363281222147 53.42418897437334, -1.4144897460898278 53.42265258737625, -1.4144897460898278 53.42099079900008, -1.4117431640598568 53.42099079712516, -1.4117431640598568 53.41849780932388, -1.4112778948070286 53.41771711805022, -1.4114404909237805 53.41689867267529, -1.411277890108579 53.416080187950215, -1.4117431640598568 53.4152995338453, -1.4117431657531654 53.40953184824072, -1.41723632610001 53.40953184402311, -1.4172363281199125 53.406257299700044, -1.4227294921899158 53.406257299700044, -1.4227294921899158 53.40789459668797, -1.4254760767598498 53.40789460061099, -1.4262193642339867 53.40914148401417, -1.4273828468095076 53.409531853100034, -1.4337158203098852 53.409531790075235, -1.4337158203098852 53.41280609140024, -1.4392089843723568 53.41280609140024, -1.439208984371362 53.41608014067522, -1.441160015802268 53.41935368587538, -1.4427511170075604 53.41935368587538, -1.4447021484373863 53.42099064750012, -1.4501953124999432 53.42099064750012, -1.4501953124999432 53.43214683850347, -1.4513643355446106 53.434108816701794, -1.4502702625278232 53.43636597733034, -1.4494587195580948 53.437354845300334, -1.4431075935937656 53.437354845300334, -1.4372459179209045 53.43244635455021, -1.433996276212838 53.42917388040006, -1.4337158203098852 53.42917388040006, -1.4337158203098852 53.42590083930004)))";
49-
// Geometry g = OperatorImportFromWkt.local().execute(0, Geometry.Type.Unknown, s, null);
50-
// boolean result1 = OperatorSimplify.local().isSimpleAsFeature(g, null, null);
51-
// boolean result2 = OperatorSimplifyOGC.local().isSimpleOGC(g, null, true, null, null);
52-
// Geometry simple = OperatorSimplifyOGC.local().execute(g, null, true, null);
53-
// OperatorFactoryLocal.saveToWKTFileDbg("c:/temp/simplifiedeeee", simple, null);
54-
// int i = 0;
55-
// }
5647
OperatorExportToESRIShape exporterShape = (OperatorExportToESRIShape) OperatorFactoryLocal.getInstance().getOperator(Operator.Type.ExportToESRIShape);
5748
OperatorImportFromESRIShape importerShape = (OperatorImportFromESRIShape) OperatorFactoryLocal.getInstance().getOperator(Operator.Type.ImportFromESRIShape);
5849

@@ -1754,6 +1745,20 @@ public static void testImportGeoJsonSpatialReference() throws Exception {
17541745
assertTrue(mapGeometry4326.getGeometry().equals(mapGeometry3857.getGeometry()));
17551746
}
17561747

1748+
@Test
1749+
public void testZeroRingWkb() {
1750+
//https://github.com/Esri/geometry-api-java/issues/275
1751+
Polygon poly = new Polygon();
1752+
poly.startPath(0, 0);
1753+
poly.lineTo(0, 10);
1754+
poly.lineTo(0, 10);
1755+
poly.addEnvelope(new Envelope(1, 1, 3, 3), false);
1756+
1757+
ByteBuffer bb = OperatorExportToWkb.local().execute(0, poly, null);
1758+
Geometry res = OperatorImportFromWkb.local().execute(0, Geometry.Type.Unknown, bb, null);
1759+
assertTrue(res.equals(poly));
1760+
}
1761+
17571762
public static Polygon makePolygon() {
17581763
Polygon poly = new Polygon();
17591764
poly.startPath(0, 0);

0 commit comments

Comments
 (0)