@@ -50,42 +50,10 @@ public static void main(String[] args) throws Exception {
5050 ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory ();
5151
5252 Map <String , Serializable > params = new HashMap <String , Serializable >();
53- params .put ("url" , shapeFile .toURI ()
54- .toURL ());
55- params .put ("create spatial index" , Boolean .TRUE );
56-
57- ShapefileDataStore dataStore = (ShapefileDataStore ) dataStoreFactory .createNewDataStore (params );
58- dataStore .createSchema (CITY );
59-
60- // If you decide to use the TYPE type and create a Data Store with it,
61- // You will need to uncomment this line to set the Coordinate Reference System
62- // newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
6353
64- Transaction transaction = new DefaultTransaction ( "create" );
54+ ShapefileDataStore dataStore = setDataStoreParams ( dataStoreFactory , params , shapeFile , CITY );
6555
66- String typeName = dataStore .getTypeNames ()[0 ];
67- SimpleFeatureSource featureSource = dataStore .getFeatureSource (typeName );
68-
69- if (featureSource instanceof SimpleFeatureStore ) {
70- SimpleFeatureStore featureStore = (SimpleFeatureStore ) featureSource ;
71-
72- featureStore .setTransaction (transaction );
73- try {
74- featureStore .addFeatures (collection );
75- transaction .commit ();
76-
77- } catch (Exception problem ) {
78- problem .printStackTrace ();
79- transaction .rollback ();
80-
81- } finally {
82- transaction .close ();
83- }
84- System .exit (0 ); // success!
85- } else {
86- System .out .println (typeName + " does not support read/write access" );
87- System .exit (1 );
88- }
56+ writeToFile (dataStore , collection );
8957
9058 }
9159
@@ -152,7 +120,7 @@ public static void addLocations(SimpleFeatureBuilder featureBuilder, DefaultFeat
152120 }
153121
154122 }
155-
123+
156124 private static void addToLocationMap (String name , double lat , double lng , Map <String , List <Double >> locations ) {
157125 List <Double > coordinates = new ArrayList <>();
158126
@@ -163,7 +131,6 @@ private static void addToLocationMap(String name, double lat, double lng, Map<St
163131
164132 private static File getNewShapeFile () {
165133 String filePath = new File ("." ).getAbsolutePath () + FILE_NAME ;
166-
167134
168135 JFileDataStoreChooser chooser = new JFileDataStoreChooser ("shp" );
169136 chooser .setDialogTitle ("Save shapefile" );
@@ -176,12 +143,52 @@ private static File getNewShapeFile() {
176143 }
177144
178145 File shapeFile = chooser .getSelectedFile ();
179- if (shapeFile .equals (filePath )) {
180- System .out .println ("Error: cannot replace " + filePath );
181- System .exit (0 );
182- }
183146
184147 return shapeFile ;
185148 }
186149
150+ private static ShapefileDataStore setDataStoreParams (ShapefileDataStoreFactory dataStoreFactory , Map <String , Serializable > params , File shapeFile , SimpleFeatureType CITY ) throws Exception {
151+ params .put ("url" , shapeFile .toURI ()
152+ .toURL ());
153+ params .put ("create spatial index" , Boolean .TRUE );
154+
155+ ShapefileDataStore dataStore = (ShapefileDataStore ) dataStoreFactory .createNewDataStore (params );
156+ dataStore .createSchema (CITY );
157+
158+ return dataStore ;
159+ }
160+
161+ private static void writeToFile (ShapefileDataStore dataStore , DefaultFeatureCollection collection ) throws Exception {
162+
163+ // If you decide to use the TYPE type and create a Data Store with it,
164+ // You will need to uncomment this line to set the Coordinate Reference System
165+ // newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
166+
167+ Transaction transaction = new DefaultTransaction ("create" );
168+
169+ String typeName = dataStore .getTypeNames ()[0 ];
170+ SimpleFeatureSource featureSource = dataStore .getFeatureSource (typeName );
171+
172+ if (featureSource instanceof SimpleFeatureStore ) {
173+ SimpleFeatureStore featureStore = (SimpleFeatureStore ) featureSource ;
174+
175+ featureStore .setTransaction (transaction );
176+ try {
177+ featureStore .addFeatures (collection );
178+ transaction .commit ();
179+
180+ } catch (Exception problem ) {
181+ problem .printStackTrace ();
182+ transaction .rollback ();
183+
184+ } finally {
185+ transaction .close ();
186+ }
187+ System .exit (0 ); // success!
188+ } else {
189+ System .out .println (typeName + " does not support read/write access" );
190+ System .exit (1 );
191+ }
192+ }
193+
187194}
0 commit comments