Skip to content

Commit

Permalink
Unchecked and deprecation warnings in shapefile handling (#285)
Browse files Browse the repository at this point in the history
* Unchecked and deprecation warnings in shapefile handling
* Remove redundant multipolygon construction in shp polygon handler

Signed-off-by: Will Cohen <[email protected]>
  • Loading branch information
willcohen authored and dr-jts committed May 28, 2018
1 parent 08fca1f commit 95fbe34
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
* Wrapper for a Shapefile arc.
*/
public class MultiLineHandler implements ShapeHandler{

int myShapeType= -1;

int myShapeType = -1;
private PrecisionModel precisionModel = new PrecisionModel();
private GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 0);


public MultiLineHandler()
{
Expand Down Expand Up @@ -60,7 +63,7 @@ public Geometry read( EndianDataInputStream file , GeometryFactory geometryFacto

if (shapeType ==0)
{
return new MultiLineString(null,new PrecisionModel(),0); //null shape
return geometryFactory.createMultiLineString(null); //null shape
}

if (shapeType != myShapeType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
*/
public class MultiPointHandler implements ShapeHandler {
int myShapeType= -1;
private PrecisionModel precisionModel = new PrecisionModel();
private GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 0);

/** Creates new MultiPointHandler */
public MultiPointHandler() {
Expand All @@ -60,7 +62,7 @@ public Geometry read(EndianDataInputStream file,GeometryFactory geometryFactory,
actualReadWords += 2;

if (shapeType ==0)
return new MultiPoint(null,new PrecisionModel(),0);
return geometryFactory.createMultiPointFromCoords(null);
if (shapeType != myShapeType)
{
throw new InvalidShapefileException("Multipointhandler.read() - expected type code "+myShapeType+" but got "+shapeType);
Expand Down Expand Up @@ -134,7 +136,7 @@ public Geometry read(EndianDataInputStream file,GeometryFactory geometryFactory,
actualReadWords += 1;
}

return geometryFactory.createMultiPoint(coords);
return geometryFactory.createMultiPointFromCoords(coords);
}

double[] zMinMax(Geometry g)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
*/
public class PolygonHandler implements ShapeHandler{
int myShapeType;
private PrecisionModel precisionModel = new PrecisionModel();
private GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 0);

public PolygonHandler()
{
Expand Down Expand Up @@ -87,7 +89,7 @@ public Geometry read( EndianDataInputStream file , GeometryFactory geometryFacto

if (shapeType ==0)
{
return new MultiPolygon(null,new PrecisionModel(),0); //null shape
return geometryFactory.createMultiPolygon(null); //null shape
}

if ( shapeType != myShapeType ) {
Expand Down Expand Up @@ -118,8 +120,8 @@ public Geometry read( EndianDataInputStream file , GeometryFactory geometryFacto
}

//LinearRing[] rings = new LinearRing[numParts];
ArrayList shells = new ArrayList();
ArrayList holes = new ArrayList();
ArrayList<LinearRing> shells = new ArrayList<LinearRing>();
ArrayList<LinearRing> holes = new ArrayList<LinearRing>();
Coordinate[] coords = new Coordinate[numPoints];

for(int t=0;t<numPoints;t++)
Expand Down Expand Up @@ -200,12 +202,12 @@ public Geometry read( EndianDataInputStream file , GeometryFactory geometryFacto
}
}

ArrayList holesForShells = assignHolesToShells(shells, holes);
ArrayList<ArrayList<LinearRing>> holesForShells = assignHolesToShells(shells, holes);

Polygon[] polygons = new Polygon[shells.size()];
for (int i = 0; i < shells.size(); i++) {
polygons[i] = geometryFactory.createPolygon((LinearRing) shells.get(i),
(LinearRing[]) ((ArrayList) holesForShells.get(i))
(LinearRing[]) ((ArrayList<LinearRing>) holesForShells.get(i))
.toArray(new LinearRing[0]));
}

Expand All @@ -224,12 +226,12 @@ public Geometry read( EndianDataInputStream file , GeometryFactory geometryFacto
return result;
}

private ArrayList assignHolesToShells(ArrayList shells, ArrayList holes)
private ArrayList<ArrayList<LinearRing>> assignHolesToShells(ArrayList<LinearRing> shells, ArrayList<LinearRing> holes)
{
// now we have a list of all shells and all holes
ArrayList holesForShells = new ArrayList(shells.size());
ArrayList<ArrayList<LinearRing>> holesForShells = new ArrayList<ArrayList<LinearRing>>(shells.size());
for (int i = 0; i < shells.size(); i++) {
holesForShells.add(new ArrayList());
holesForShells.add(new ArrayList<LinearRing>());
}

// find homes
Expand Down Expand Up @@ -269,7 +271,7 @@ private ArrayList assignHolesToShells(ArrayList shells, ArrayList holes)
}
else {
// ((ArrayList)holesForShells.get(shells.indexOf(minShell))).add(testRing);
((ArrayList) holesForShells.get(findIndex(shells, minShell)))
((ArrayList<LinearRing>) holesForShells.get(findIndex(shells, minShell)))
.add(testHole);
}
}
Expand Down Expand Up @@ -299,24 +301,16 @@ public int getShapeType(){
}
public int getLength(Geometry geometry){

MultiPolygon multi;
if(geometry instanceof MultiPolygon){
multi = (MultiPolygon)geometry;
}
else{
multi = new MultiPolygon(new Polygon[]{(Polygon)geometry},geometry.getPrecisionModel(),geometry.getSRID());
}

int nrings=0;

for (int t=0;t<multi.getNumGeometries();t++)
for (int t=0;t<geometry.getNumGeometries();t++)
{
Polygon p;
p = (Polygon) multi.getGeometryN(t);
p = (Polygon) geometry.getGeometryN(t);
nrings = nrings + 1 + p.getNumInteriorRing();
}

int npoints = multi.getNumPoints();
int npoints = geometry.getNumPoints();

if (myShapeType == 15)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public GeometryCollection read(GeometryFactory geometryFactory) throws IOExcepti
if(mainHeader.getVersion() > VERSION){System.err.println("Sf-->Warning, Shapefile format ("+mainHeader.getVersion()+") newer that supported ("+VERSION+"), attempting to read anyway");}

Geometry body;
ArrayList list = new ArrayList();
ArrayList<Geometry> list = new ArrayList<Geometry>();
int type=mainHeader.getShapeType();
ShapeHandler handler = getShapeHandler(type);
if(handler==null)throw new ShapeTypeNotSupportedException("Unsuported shape type:"+type);
Expand All @@ -123,13 +123,15 @@ public GeometryCollection read(GeometryFactory geometryFactory) throws IOExcepti
list.add(body);
// System.out.println("Done record: " + recordNumber);
}catch(IllegalArgumentException r2d2){
geomFactory = new GeometryFactory(null, -1);
//System.out.println("Record " +recordNumber+ " has is NULL Shape");
list.add(new GeometryCollection(null,null,-1));
list.add(geomFactory.createGeometryCollection(null));
}catch(Exception c3p0){
geomFactory = new GeometryFactory(null, -1);
System.out.println("Error processing record (a):" +recordNumber);
System.out.println(c3p0.getMessage());
c3p0.printStackTrace();
list.add(new GeometryCollection(null,null,-1));
list.add(geomFactory.createGeometryCollection(null));
}
// System.out.println("processing:" +recordNumber);
}
Expand Down Expand Up @@ -184,12 +186,14 @@ public Geometry next()
// System.out.println("Done record: " + recordNumber);
}catch(IllegalArgumentException r2d2){
//System.out.println("Record " +recordNumber+ " has is NULL Shape");
geom = new GeometryCollection(null,null,-1);
geomFactory = new GeometryFactory(null, -1);
geom = geomFactory.createGeometryCollection(null);
}catch(Exception c3p0){
geomFactory = new GeometryFactory(null, -1);
System.out.println("Error processing record (a):" +recordNumber);
System.out.println(c3p0.getMessage());
c3p0.printStackTrace();
geom = new GeometryCollection(null,null,-1);
geom = geomFactory.createGeometryCollection(null);
}
// System.out.println("processing:" +recordNumber);
}
Expand Down

0 comments on commit 95fbe34

Please sign in to comment.