diff --git a/geotools/pom.xml b/geotools/pom.xml new file mode 100644 index 0000000000..37b4a2338a --- /dev/null +++ b/geotools/pom.xml @@ -0,0 +1,74 @@ + + 4.0.0 + + com.baeldung + geotools + 0.0.1-SNAPSHOT + jar + + geotools + http://maven.apache.org + + + + junit + junit + 4.12 + test + + + org.geotools + gt-shapefile + ${geotools-shapefile.version} + + + org.geotools + gt-epsg-hsql + ${geotools.version} + + + org.geotools + gt-swing + ${geotools-swing.version} + + + + + maven2-repository.dev.java.net + Java.net repository + http://download.java.net/maven/2 + + + osgeo + Open Source Geospatial Foundation Repository + http://download.osgeo.org/webdav/geotools/ + + + + true + + opengeo + OpenGeo Maven Repository + http://repo.opengeo.org + + + + + + true + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + 15.2 + 15.2 + 15.2 + + diff --git a/libraries/src/main/java/com/baeldung/geotools/ShapeFile.java b/geotools/src/main/java/com/baeldung/geotools/ShapeFile.java similarity index 92% rename from libraries/src/main/java/com/baeldung/geotools/ShapeFile.java rename to geotools/src/main/java/com/baeldung/geotools/ShapeFile.java index 77c67abc84..e195989489 100644 --- a/libraries/src/main/java/com/baeldung/geotools/ShapeFile.java +++ b/geotools/src/main/java/com/baeldung/geotools/ShapeFile.java @@ -50,42 +50,10 @@ public class ShapeFile { ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory(); Map params = new HashMap(); - params.put("url", shapeFile.toURI() - .toURL()); - params.put("create spatial index", Boolean.TRUE); - ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); - dataStore.createSchema(CITY); + ShapefileDataStore dataStore = setDataStoreParams(dataStoreFactory, params, shapeFile, CITY); - // If you decide to use the TYPE type and create a Data Store with it, - // You will need to uncomment this line to set the Coordinate Reference System - // newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); - - Transaction transaction = new DefaultTransaction("create"); - - String typeName = dataStore.getTypeNames()[0]; - SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName); - - if (featureSource instanceof SimpleFeatureStore) { - SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; - - featureStore.setTransaction(transaction); - try { - featureStore.addFeatures(collection); - transaction.commit(); - - } catch (Exception problem) { - problem.printStackTrace(); - transaction.rollback(); - - } finally { - transaction.close(); - } - System.exit(0); // success! - } else { - System.out.println(typeName + " does not support read/write access"); - System.exit(1); - } + writeToFile(dataStore, collection); } @@ -152,7 +120,7 @@ public class ShapeFile { } } - + private static void addToLocationMap(String name, double lat, double lng, Map> locations) { List coordinates = new ArrayList<>(); @@ -163,7 +131,6 @@ public class ShapeFile { private static File getNewShapeFile() { String filePath = new File(".").getAbsolutePath() + FILE_NAME; - JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp"); chooser.setDialogTitle("Save shapefile"); @@ -176,12 +143,52 @@ public class ShapeFile { } File shapeFile = chooser.getSelectedFile(); - if (shapeFile.equals(filePath)) { - System.out.println("Error: cannot replace " + filePath); - System.exit(0); - } return shapeFile; } + private static ShapefileDataStore setDataStoreParams(ShapefileDataStoreFactory dataStoreFactory, Map params, File shapeFile, SimpleFeatureType CITY) throws Exception { + params.put("url", shapeFile.toURI() + .toURL()); + params.put("create spatial index", Boolean.TRUE); + + ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params); + dataStore.createSchema(CITY); + + return dataStore; + } + + private static void writeToFile(ShapefileDataStore dataStore, DefaultFeatureCollection collection) throws Exception { + + // If you decide to use the TYPE type and create a Data Store with it, + // You will need to uncomment this line to set the Coordinate Reference System + // newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84); + + Transaction transaction = new DefaultTransaction("create"); + + String typeName = dataStore.getTypeNames()[0]; + SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName); + + if (featureSource instanceof SimpleFeatureStore) { + SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource; + + featureStore.setTransaction(transaction); + try { + featureStore.addFeatures(collection); + transaction.commit(); + + } catch (Exception problem) { + problem.printStackTrace(); + transaction.rollback(); + + } finally { + transaction.close(); + } + System.exit(0); // success! + } else { + System.out.println(typeName + " does not support read/write access"); + System.exit(1); + } + } + } diff --git a/libraries/src/test/java/com/baeldung/geotools/GeoToolsUnitTestTest.java b/geotools/src/test/java/com/baeldung/geotools/GeoToolsUnitTest.java similarity index 95% rename from libraries/src/test/java/com/baeldung/geotools/GeoToolsUnitTestTest.java rename to geotools/src/test/java/com/baeldung/geotools/GeoToolsUnitTest.java index 44cd47edc3..fad8740598 100644 --- a/libraries/src/test/java/com/baeldung/geotools/GeoToolsUnitTestTest.java +++ b/geotools/src/test/java/com/baeldung/geotools/GeoToolsUnitTest.java @@ -7,7 +7,7 @@ import org.geotools.feature.simple.SimpleFeatureBuilder; import org.junit.Test; import org.opengis.feature.simple.SimpleFeatureType; -public class GeoToolsUnitTestTest { +public class GeoToolsUnitTest { @Test public void givenFeatureType_whenAddLocations_returnFeatureCollection() { diff --git a/libraries/pom.xml b/libraries/pom.xml index 8534b6d0ff..77e7c2634a 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -140,7 +140,7 @@ unit-ri 1.0.3 - + org.apache.commons commons-collections4 ${commons.collections.version} @@ -487,21 +487,7 @@ vavr ${vavr.version} - - org.geotools - gt-shapefile - ${geotools.version} - - - org.geotools - gt-epsg-hsql - ${geotools.version} - - - org.geotools - gt-swing - ${geotools.version} - + com.squareup.retrofit2 @@ -581,19 +567,6 @@ Java.net repository http://download.java.net/maven/2 - - osgeo - Open Source Geospatial Foundation Repository - http://download.osgeo.org/webdav/geotools/ - - - - true - - opengeo - OpenGeo Maven Repository - http://repo.opengeo.org - false @@ -647,13 +620,12 @@ 8.2.0 0.6.5 0.9.0 - 15.2 2.9.9 1.5.1 2.3.0 2.9.9 1.5.1 - 1.14 + 1.14 1.0.3 1.0.0 3.8.4 diff --git a/pom.xml b/pom.xml index dfa7e09acc..c501dddd3c 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ - + geotools groovy-spock gson guava