I'm using Geotools 14.0 for Java and I'm trying to generate shapefile based on data from GeoJSON. Here is my code:
try { String url = "geojson.json"; File geojson = new File(url); File shpFile = new File("test.shp"); ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); Map params = new HashMap(); params.put("url", shpFile.toURI().toURL()); params.put("create spatial index", Boolean.TRUE); ShapefileDataStore shpDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); InputStream in = new FileInputStream(geojson); int decimals = 15; GeometryJSON gjson = new GeometryJSON(decimals); FeatureJSON fjson = new FeatureJSON(gjson); FeatureCollection fc = fjson.readFeatureCollection(in); SimpleFeatureType type = (SimpleFeatureType) fc.getSchema(); shpDataStore.createSchema(type); Transaction transaction = new DefaultTransaction("create"); String typeName = shpDataStore.getTypeNames()[0]; SimpleFeatureSource featureSource = shpDataStore.getFeatureSource(typeName); if (featureSource instanceof FeatureStore) { SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; featureStore.setTransaction(transaction); try { featureStore.addFeatures(fc); for(Object c : featureStore.getFeatures().toArray()){ System.out.println("C: " + ((SimpleFeatureImpl)c).toString()); } transaction.commit(); } catch (Exception ex) { ex.printStackTrace(); transaction.rollback(); } finally { transaction.close(); } } else { System.out.println(typeName + " does not support read/write access"); } } catch (Exception e) { return "Exception " + e.getLocalizedMessage(); }And after this, i get test.shp file but it does not contain any geometry data. If I convert it back to geojson I get this:
Is there any way to do this in a proper way? Thank you in advance
أكثر...
try { String url = "geojson.json"; File geojson = new File(url); File shpFile = new File("test.shp"); ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); Map params = new HashMap(); params.put("url", shpFile.toURI().toURL()); params.put("create spatial index", Boolean.TRUE); ShapefileDataStore shpDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); InputStream in = new FileInputStream(geojson); int decimals = 15; GeometryJSON gjson = new GeometryJSON(decimals); FeatureJSON fjson = new FeatureJSON(gjson); FeatureCollection fc = fjson.readFeatureCollection(in); SimpleFeatureType type = (SimpleFeatureType) fc.getSchema(); shpDataStore.createSchema(type); Transaction transaction = new DefaultTransaction("create"); String typeName = shpDataStore.getTypeNames()[0]; SimpleFeatureSource featureSource = shpDataStore.getFeatureSource(typeName); if (featureSource instanceof FeatureStore) { SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; featureStore.setTransaction(transaction); try { featureStore.addFeatures(fc); for(Object c : featureStore.getFeatures().toArray()){ System.out.println("C: " + ((SimpleFeatureImpl)c).toString()); } transaction.commit(); } catch (Exception ex) { ex.printStackTrace(); transaction.rollback(); } finally { transaction.close(); } } else { System.out.println(typeName + " does not support read/write access"); } } catch (Exception e) { return "Exception " + e.getLocalizedMessage(); }And after this, i get test.shp file but it does not contain any geometry data. If I convert it back to geojson I get this:
{ "type": "Feature", "properties": { }, "id": "test.1" }
And this is my original geojson file (geojson.json):{ "type": "FeatureCollection", "features": [ { "properties": { "version": 0 }, "id": "1", "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [ 1868732.467515988, 6594375.3042187244 ], [ 1868732.467515988, 5165920.1196253505 ], [ 3297187.6521093622, 5165920.1196253505 ], [ 3297187.6521093622, 6594375.3042187244 ], [ 1868732.467515988, 6594375.3042187244 ] ] ] } } ] }
أكثر...