protected Geometry transformGeometryCollection(GeometryCollection geom, Geometry parent) { List transGeomList = new ArrayList(); for (int i = 0; i < geom.getNumGeometries(); i++) { Geometry transformGeom = transform(geom.getGeometryN(i)); if (transformGeom == null) continue; if (pruneEmptyGeometry && transformGeom.isEmpty()) continue; transGeomList.add(transformGeom); } if (preserveGeometryCollectionType) return factory.createGeometryCollection(GeometryFactory.toGeometryArray(transGeomList)); return factory.buildGeometry(transGeomList); }
/** * Main method - write the featurecollection to a shapefile (2d, 3d or 4d). * * @param featureCollection collection to write * @param dp 'OutputFile' or 'DefaultValue' to specify where to write, and 'ShapeType' to specify * dimentionality. */ public void write(FeatureCollection featureCollection, DriverProperties dp) throws IllegalParametersException, Exception { String shpfileName; String dbffname; String shxfname; String path; String fname; String fname_withoutextention; int shapeType; int loc; GeometryCollection gc; shpfileName = dp.getProperty(FILE_PROPERTY_KEY); if (shpfileName == null) { shpfileName = dp.getProperty(DEFAULT_VALUE_PROPERTY_KEY); } if (shpfileName == null) { throw new IllegalParametersException("no output filename specified"); } loc = shpfileName.lastIndexOf(File.separatorChar); if (loc == -1) { // loc = 0; // no path - ie. "hills.shp" // path = ""; // fname = shpfileName; // probably using the wrong path separator character. throw new Exception( "couldn't find the path separator character '" + File.separatorChar + "' in your shape file name. This you're probably using the unix (or dos) one."); } else { path = shpfileName.substring(0, loc + 1); // ie. "/data1/hills.shp" -> "/data1/" fname = shpfileName.substring(loc + 1); // ie. "/data1/hills.shp" -> "hills.shp" } loc = fname.lastIndexOf("."); if (loc == -1) { throw new IllegalParametersException("Filename must end in '.shp'"); } fname_withoutextention = fname.substring(0, loc); // ie. "hills.shp" -> "hills." dbffname = path + fname_withoutextention + ".dbf"; writeDbf(featureCollection, dbffname); // this gc will be a collection of either multi-points, multi-polygons, or multi-linestrings // polygons will have the rings in the correct order gc = makeSHAPEGeometryCollection(featureCollection); shapeType = 2; // x,y if (dp.getProperty(SHAPE_TYPE_PROPERTY_KEY) != null) { String st = dp.getProperty(SHAPE_TYPE_PROPERTY_KEY); if (st.equalsIgnoreCase("xy")) { shapeType = 2; } else if (st.equalsIgnoreCase("xym")) { shapeType = 3; } else if (st.equalsIgnoreCase("xymz")) { shapeType = 4; } else if (st.equalsIgnoreCase("xyzm")) { shapeType = 4; } else if (st.equalsIgnoreCase("xyz")) { shapeType = 4; } else { throw new IllegalParametersException( "ShapefileWriter.write() - dataproperties has a 'ShapeType' that isnt 'xy', 'xym', or 'xymz'"); } } else { if (gc.getNumGeometries() > 0) { shapeType = guessCoorinateDims(gc.getGeometryN(0)); } } URL url = new URL("file", "localhost", shpfileName); Shapefile myshape = new Shapefile(url); myshape.write(gc, shapeType); shxfname = path + fname_withoutextention + ".shx"; BufferedOutputStream in = new BufferedOutputStream(new FileOutputStream(shxfname)); EndianDataOutputStream sfile = new EndianDataOutputStream(in); myshape.writeIndex(gc, sfile, shapeType); }