Hello John, Below is your code which I've modified it somewhat - removing unused methods and trying to simplify the source a little. I've just done a very quick hack (ie. no attempt made to make it pretty or efficient, that's your job :).
I tested it with a polygon file: the "bc_voting_areas" shapefile which is included in the uDig sample data: http://udig.refractions.net/docs/data-v1_2.zip I used GeoTools 2.7.0 (just released). All worked fine: the new line shapefile was created and displayed in a JMapFrame with your fluro green style ! I also tested the Csv2Shape example app using version 2.7.0 and the source linked to from the web page. That also ran fine for me. Michael package org.geotools.teststuff; import java.awt.Color; import java.io.File; import java.io.Serializable; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import org.geotools.data.DefaultTransaction; import org.geotools.data.FileDataStore; import org.geotools.data.FileDataStoreFinder; import org.geotools.data.Transaction; import org.geotools.data.shapefile.ShapefileDataStore; import org.geotools.data.shapefile.ShapefileDataStoreFactory; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.feature.FeatureCollections; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.map.DefaultMapContext; import org.geotools.styling.FeatureTypeStyle; import org.geotools.styling.LineSymbolizer; import org.geotools.styling.Style; import org.geotools.styling.StyleBuilder; import org.geotools.swing.JMapFrame; import org.geotools.swing.data.JFileDataStoreChooser; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.LineString; import com.vividsolutions.jts.geom.MultiPolygon; /** * @author John * Convert a multi-polygon shape file to a line-string shape file * * This displays the new collection okay, but blows up making the shape file */ public class TestConvertGeoTypes { private static final boolean NEW = true; // If true, convert with new type. //If false, create new collection, but use old type private static final String TEMP_SHAPE_FILE = "/tmp/foo.shp"; // Mainline public static void main(String[] args) throws Exception { // GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null ); // Coordinate[] coords = new Coordinate[] { // new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6), // new Coordinate(-2, 2), new Coordinate(-2, 0), new Coordinate(-8, 6), // }; // LineString line = geometryFactory.createLineString(coords); // Read shapefile composed of multipolygons File file = JFileDataStoreChooser.showOpenFile("shp", null); if (file == null) { return; } ShapefileDataStore oldStore = getDataStoreFromShapeFile(file.getPath()); SimpleFeatureSource oldSrc = oldStore.getFeatureSource(); SimpleFeatureCollection oldCol = oldSrc.getFeatures(); // Convert the polygons to linestrings LineString[] lines = polys2lines(oldCol); ShapefileDataStore shpFile = createOutputShapefile( TEMP_SHAPE_FILE, oldSrc.getSchema(), lines); // // Display on a map (this works) // DefaultMapContext map = new DefaultMapContext(); map.setTitle("Try Geo Conversion"); StyleBuilder sbldr = new StyleBuilder(); Style style = sbldr.createStyle(); LineSymbolizer liner = sbldr.createLineSymbolizer(Color.GREEN, 1); // Rule rule = sbldr.createRule(line); FeatureTypeStyle fts = sbldr.createFeatureTypeStyle(liner); style.featureTypeStyles().add(fts); map.addLayer(shpFile.getFeatureSource(), (Style) style); JMapFrame.showMap(map); } // // Convert mulipolygon features to array of linestrings // private static LineString[] polys2lines(SimpleFeatureCollection oldCol) { SimpleFeatureIterator it = oldCol.features(); GeometryFactory fact = new GeometryFactory(); List<LineString> al = new LinkedList<LineString>(); while (it.hasNext()) { SimpleFeature feature = it.next(); MultiPolygon mp = (MultiPolygon) feature.getAttribute(0); int n = mp.getNumGeometries(); for (int i = 0; i < n; i++) { Geometry g = mp.getGeometryN(i); Coordinate[] coords = g.getCoordinates(); LineString ls = fact.createLineString(coords); al.add(ls); } } return al.toArray(new LineString[al.size()]); } // // Make a new feature source, based on old type but convert to new type // private static ShapefileDataStore createOutputShapefile(String fileName, SimpleFeatureType oldType, Geometry[] geoms) throws Exception { SimpleFeatureTypeBuilder ftbuilder = new SimpleFeatureTypeBuilder(); ftbuilder.setName("linetype"); ftbuilder.setCRS(oldType.getCoordinateReferenceSystem()); ftbuilder.add("the_geom", LineString.class); ftbuilder.add("id", Integer.class); final SimpleFeatureType TYPE = ftbuilder.buildFeatureType(); Integer i = 0; SimpleFeatureCollection newCollection = FeatureCollections.newCollection(); for (Geometry g : geoms) { SimpleFeatureBuilder builder = new SimpleFeatureBuilder(TYPE); builder.add(g); // add geometry attribute builder.add(++i); // add id attribute // add whatever other attribute objects here... SimpleFeature feature = builder.buildFeature(i.toString()); newCollection.add(feature); } File newFile = new File(fileName); ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put("url", newFile.toURI().toURL()); params.put("create spatial index", Boolean.TRUE); ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); newDataStore.createSchema(TYPE); // docs break transaction /* * Write the features to the shapefile */ Transaction transaction = new DefaultTransaction("create"); String typeName = newDataStore.getTypeNames()[0]; SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName); if (featureSource instanceof SimpleFeatureStore) { SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; featureStore.setTransaction(transaction); try { featureStore.addFeatures(newCollection); transaction.commit(); } catch (Exception problem) { problem.printStackTrace(); transaction.rollback(); return null; } finally { transaction.close(); } } else { throw new RuntimeException(typeName + " does not support read/write access"); } return newDataStore; } // // Make data store based on a shape file public static ShapefileDataStore getDataStoreFromShapeFile(String fnameIn) throws Exception { FileDataStore fds = FileDataStoreFinder.getDataStore(new File(fnameIn)); ShapefileDataStore shpStore = (ShapefileDataStore) fds; return shpStore; } } ------------------------------------------------------------------------------ Enable your software for Intel(R) Active Management Technology to meet the growing manageability and security demands of your customers. Businesses are taking advantage of Intel(R) vPro (TM) technology - will your software be a part of the solution? Download the Intel(R) Manageability Checker today! http://p.sf.net/sfu/intel-dev2devmar _______________________________________________ Geotools-gt2-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
