Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: Halil İbrahim Şener <[email protected]>
  • Loading branch information
hisener committed Jan 19, 2021
1 parent f1261ac commit b542462
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
33 changes: 15 additions & 18 deletions modules/core/src/main/java/org/locationtech/jts/io/WKBReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ private static int hexToInt(char hex)
private PrecisionModel precisionModel;
// default dimension - will be set on read
private int inputDimension = 2;
private boolean hasSRID = false;
private int SRID = 0;
/**
* true if structurally invalid input should be reported rather than repaired.
* At some point this could be made client-controllable.
Expand Down Expand Up @@ -156,11 +154,10 @@ public Geometry read(InStream is)
throws IOException, ParseException
{
dis.setInStream(is);
Geometry g = readGeometry();
return g;
return readGeometry(0);
}

private Geometry readGeometry()
private Geometry readGeometry(int SRID)
throws IOException, ParseException
{

Expand Down Expand Up @@ -201,7 +198,7 @@ else if(isStrict)
inputDimension = 2 + (hasZ?1:0) + (hasM?1:0);

// determine if SRIDs are present
hasSRID = (typeInt & 0x20000000) != 0;
boolean hasSRID = (typeInt & 0x20000000) != 0;
if (hasSRID) {
SRID = dis.readInt();
}
Expand All @@ -222,16 +219,16 @@ else if(isStrict)
geom = readPolygon();
break;
case WKBConstants.wkbMultiPoint :
geom = readMultiPoint();
geom = readMultiPoint(SRID);
break;
case WKBConstants.wkbMultiLineString :
geom = readMultiLineString();
geom = readMultiLineString(SRID);
break;
case WKBConstants.wkbMultiPolygon :
geom = readMultiPolygon();
geom = readMultiPolygon(SRID);
break;
case WKBConstants.wkbGeometryCollection :
geom = readGeometryCollection();
geom = readGeometryCollection(SRID);
break;
default:
throw new ParseException("Unknown WKB type " + geometryType);
Expand Down Expand Up @@ -291,52 +288,52 @@ private Polygon readPolygon() throws IOException
return factory.createPolygon(shell, holes);
}

private MultiPoint readMultiPoint() throws IOException, ParseException
private MultiPoint readMultiPoint(int SRID) throws IOException, ParseException
{
int numGeom = dis.readInt();
Point[] geoms = new Point[numGeom];
for (int i = 0; i < numGeom; i++) {
Geometry g = readGeometry();
Geometry g = readGeometry(SRID);
if (! (g instanceof Point))
throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiPoint");
geoms[i] = (Point) g;
}
return factory.createMultiPoint(geoms);
}

private MultiLineString readMultiLineString() throws IOException, ParseException
private MultiLineString readMultiLineString(int SRID) throws IOException, ParseException
{
int numGeom = dis.readInt();
LineString[] geoms = new LineString[numGeom];
for (int i = 0; i < numGeom; i++) {
Geometry g = readGeometry();
Geometry g = readGeometry(SRID);
if (! (g instanceof LineString))
throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiLineString");
geoms[i] = (LineString) g;
}
return factory.createMultiLineString(geoms);
}

private MultiPolygon readMultiPolygon() throws IOException, ParseException
private MultiPolygon readMultiPolygon(int SRID) throws IOException, ParseException
{
int numGeom = dis.readInt();
Polygon[] geoms = new Polygon[numGeom];

for (int i = 0; i < numGeom; i++) {
Geometry g = readGeometry();
Geometry g = readGeometry(SRID);
if (! (g instanceof Polygon))
throw new ParseException(INVALID_GEOM_TYPE_MSG + "MultiPolygon");
geoms[i] = (Polygon) g;
}
return factory.createMultiPolygon(geoms);
}

private GeometryCollection readGeometryCollection() throws IOException, ParseException
private GeometryCollection readGeometryCollection(int SRID) throws IOException, ParseException
{
int numGeom = dis.readInt();
Geometry[] geoms = new Geometry[numGeom];
for (int i = 0; i < numGeom; i++) {
geoms[i] = readGeometry();
geoms[i] = readGeometry(SRID);
}
return factory.createGeometryCollection(geoms);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;

import junit.framework.TestCase;
import junit.textui.TestRunner;
Expand Down Expand Up @@ -193,6 +194,23 @@ public void testSRIDInSubGeometry() throws ParseException {
checkSRID("0107000020E61000000900000001010000000000000000000000000000000000F03F01010000000000000000000000000000000000F03F01010000000000000000000040000000000000084001020000000200000000000000000000400000000000000840000000000000104000000000000014400102000000020000000000000000000000000000000000F03F000000000000004000000000000008400102000000020000000000000000001040000000000000144000000000000018400000000000001C4001030000000200000005000000000000000000000000000000000000000000000000000000000000000000244000000000000024400000000000002440000000000000244000000000000000000000000000000000000000000000000005000000000000000000F03F000000000000F03F000000000000F03F0000000000002240000000000000224000000000000022400000000000002240000000000000F03F000000000000F03F000000000000F03F01030000000200000005000000000000000000000000000000000000000000000000000000000000000000244000000000000024400000000000002440000000000000244000000000000000000000000000000000000000000000000005000000000000000000F03F000000000000F03F000000000000F03F0000000000002240000000000000224000000000000022400000000000002240000000000000F03F000000000000F03F000000000000F03F0103000000010000000500000000000000000022C0000000000000000000000000000022C00000000000002440000000000000F0BF0000000000002440000000000000F0BF000000000000000000000000000022C00000000000000000", 4326);
}

public void testInvalidWkbShouldBeReadable() throws ParseException {
WKBReader wkbReader = new WKBReader(geomFactory);
// The last sub-geometry uses 2029 unlike others.
Geometry geometry = wkbReader.read(WKBReader.hexToBytes("0107000020E6100000090000000101000020E61000000000000000000000000000000000F03F0101000020E61000000000000000000000000000000000F03F0101000020E6100000000000000000004000000000000008400102000020E61000000200000000000000000000400000000000000840000000000000104000000000000014400102000020E6100000020000000000000000000000000000000000F03F000000000000004000000000000008400102000020E6100000020000000000000000001040000000000000144000000000000018400000000000001C400103000020E61000000200000005000000000000000000000000000000000000000000000000000000000000000000244000000000000024400000000000002440000000000000244000000000000000000000000000000000000000000000000005000000000000000000F03F000000000000F03F000000000000F03F0000000000002240000000000000224000000000000022400000000000002240000000000000F03F000000000000F03F000000000000F03F0103000020E61000000200000005000000000000000000000000000000000000000000000000000000000000000000244000000000000024400000000000002440000000000000244000000000000000000000000000000000000000000000000005000000000000000000F03F000000000000F03F000000000000F03F0000000000002240000000000000224000000000000022400000000000002240000000000000F03F000000000000F03F000000000000F03F0103000020ED070000010000000500000000000000000022C0000000000000000000000000000022C00000000000002440000000000000F0BF0000000000002440000000000000F0BF000000000000000000000000000022C00000000000000000"));

assertTrue(geometry instanceof GeometryCollection);
assertEquals(4326, geometry.getSRID());

GeometryCollection geometryCollection = (GeometryCollection) geometry;
for(int i = 0; i < geometryCollection.getNumGeometries() - 1; i++) {
assertEquals(4326, geometryCollection.getGeometryN(i).getSRID());
}
Geometry lastSubGeometry = geometryCollection.getGeometryN(geometryCollection.getNumGeometries() - 1);
assertTrue(lastSubGeometry instanceof Polygon);
assertEquals(2029, lastSubGeometry.getSRID());
}

/**
* Not yet implemented satisfactorily.
*
Expand Down Expand Up @@ -236,7 +254,7 @@ private void checkSRID(String wkbHex, int expectedSrid) throws ParseException {

GeometryCollection geometryCollection = (GeometryCollection) geometry;
for(int i = 0; i < geometryCollection.getNumGeometries(); i++) {
assertEquals(geometryCollection.getGeometryN(i).getSRID(), expectedSrid);
assertEquals(expectedSrid, geometryCollection.getGeometryN(i).getSRID());
}
}
}

0 comments on commit b542462

Please sign in to comment.