Hello, I have array of com.vividsolutions.jts.geom.LineSegment objects and i want to create new shapefile from it to display it on map.
I follow lab on http://docs.codehaus.org/display/GEOTDOC/07+FeatureStore but when i try to do following: FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection(); org.opengis.feature.simple.SimpleFeatureType featureType; MultiLineString line; ... collection.add(featureType.create(new Object[] { line })); i found out that org.opengis.feature.simple.SimpleFeatureType don't have method create at all. I try to cast it to org.geotools.feature.SimpleFeatureType but got exception: java.lang.ClassCastException: org.geotools.feature.simple.SimpleFeatureTypeImpl cannot be cast to org.geotools.feature.SimpleFeatureType I hope so someone can help me about this. Here is my complete code: public static void writeToShapefile(LineSegment[] lineSegments) { DefaultTransaction transaction = new DefaultTransaction("Example1"); try { FileDataStoreFactorySpi factory = new IndexedShapefileDataStoreFactory(); File file = new File("myShp.shp"); //Map map = Collections.singletonMap("shapefile url", file.toURI().toURL()); Map map = Collections.singletonMap("url", file.toURI().toURL()); ShapefileDataStore myDataStore = (ShapefileDataStore)factory.createNewDataStore(map); myDataStore.forceSchemaCRS(CRS.decode("EPSG:4326")); SimpleFeatureType featureType = DataUtilities.createType("best_path", "geom:MultiLineString"); myDataStore.createSchema(featureType); /* populate collection from line segments */ FeatureCollection<SimpleFeatureType, SimpleFeature> fc = populate(featureType, lineSegments); /* write features to shape file */ FeatureSource<SimpleFeatureType, SimpleFeature> source = myDataStore.getFeatureSource("best_path"); if (source instanceof FeatureStore) { // you have write access FeatureStore<SimpleFeatureType, SimpleFeature> store = (FeatureStore<SimpleFeatureType, SimpleFeature>) source; store.setTransaction(transaction); try { store.addFeatures(fc); transaction.commit(); transaction.close(); } catch (Exception eek) { try { transaction.rollback(); } catch (IOException e) { e.printStackTrace(); } } } else { // read-only } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static FeatureCollection<SimpleFeatureType, SimpleFeature> populate(SimpleFeatureType featureType, LineSegment[] lineSegments) { FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection(); try { GeometryFactory gf = new GeometryFactory(); for (int i = 0; i < lineSegments.length; i++) { LineString[] lsTemp = new LineString[1]; Coordinate c0 = lineSegments[i].p0; Coordinate c1 = lineSegments[i].p1; Coordinate[] coords = new Coordinate[2]; coords[0] = c0; coords[1] = c1; LineString ls = gf.createLineString(coords); lsTemp[0] = ls; MultiLineString line = gf.createMultiLineString(lsTemp); collection.add(featureType.create(new Object[] { line })); } return collection; } catch (Exception iae) { iae.printStackTrace(); return null; } } -- View this message in context: http://n2.nabble.com/WriteToShapefile-tp3403703p3403703.html Sent from the geotools-gt2-users mailing list archive at Nabble.com. ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ Geotools-gt2-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
