Bael 817 refactor (#2575)
* Code refactor according to comments * Moved to new module * Changes in pom file
This commit is contained in:
parent
ba9d8324ee
commit
8ac23221fe
74
geotools/pom.xml
Normal file
74
geotools/pom.xml
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>geotools</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>geotools</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-shapefile</artifactId>
|
||||||
|
<version>${geotools-shapefile.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-epsg-hsql</artifactId>
|
||||||
|
<version>${geotools.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.geotools</groupId>
|
||||||
|
<artifactId>gt-swing</artifactId>
|
||||||
|
<version>${geotools-swing.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>maven2-repository.dev.java.net</id>
|
||||||
|
<name>Java.net repository</name>
|
||||||
|
<url>http://download.java.net/maven/2</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>osgeo</id>
|
||||||
|
<name>Open Source Geospatial Foundation Repository</name>
|
||||||
|
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<snapshots>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</snapshots>
|
||||||
|
<id>opengeo</id>
|
||||||
|
<name>OpenGeo Maven Repository</name>
|
||||||
|
<url>http://repo.opengeo.org</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<inherited>true</inherited>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<properties>
|
||||||
|
<geotools.version>15.2</geotools.version>
|
||||||
|
<geotools-swing.version>15.2</geotools-swing.version>
|
||||||
|
<geotools-shapefile.version>15.2</geotools-shapefile.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -50,42 +50,10 @@ public class ShapeFile {
|
|||||||
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
|
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
|
||||||
|
|
||||||
Map<String, Serializable> params = new HashMap<String, Serializable>();
|
Map<String, Serializable> params = new HashMap<String, Serializable>();
|
||||||
params.put("url", shapeFile.toURI()
|
|
||||||
.toURL());
|
|
||||||
params.put("create spatial index", Boolean.TRUE);
|
|
||||||
|
|
||||||
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
|
ShapefileDataStore dataStore = setDataStoreParams(dataStoreFactory, params, shapeFile, CITY);
|
||||||
dataStore.createSchema(CITY);
|
|
||||||
|
|
||||||
// If you decide to use the TYPE type and create a Data Store with it,
|
writeToFile(dataStore, collection);
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,7 +120,7 @@ public class ShapeFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addToLocationMap(String name, double lat, double lng, Map<String, List<Double>> locations) {
|
private static void addToLocationMap(String name, double lat, double lng, Map<String, List<Double>> locations) {
|
||||||
List<Double> coordinates = new ArrayList<>();
|
List<Double> coordinates = new ArrayList<>();
|
||||||
|
|
||||||
@ -163,7 +131,6 @@ public class ShapeFile {
|
|||||||
|
|
||||||
private static File getNewShapeFile() {
|
private static File getNewShapeFile() {
|
||||||
String filePath = new File(".").getAbsolutePath() + FILE_NAME;
|
String filePath = new File(".").getAbsolutePath() + FILE_NAME;
|
||||||
|
|
||||||
|
|
||||||
JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
|
JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
|
||||||
chooser.setDialogTitle("Save shapefile");
|
chooser.setDialogTitle("Save shapefile");
|
||||||
@ -176,12 +143,52 @@ public class ShapeFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
File shapeFile = chooser.getSelectedFile();
|
File shapeFile = chooser.getSelectedFile();
|
||||||
if (shapeFile.equals(filePath)) {
|
|
||||||
System.out.println("Error: cannot replace " + filePath);
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return shapeFile;
|
return shapeFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static ShapefileDataStore setDataStoreParams(ShapefileDataStoreFactory dataStoreFactory, Map<String, Serializable> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -7,7 +7,7 @@ import org.geotools.feature.simple.SimpleFeatureBuilder;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.opengis.feature.simple.SimpleFeatureType;
|
import org.opengis.feature.simple.SimpleFeatureType;
|
||||||
|
|
||||||
public class GeoToolsUnitTestTest {
|
public class GeoToolsUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFeatureType_whenAddLocations_returnFeatureCollection() {
|
public void givenFeatureType_whenAddLocations_returnFeatureCollection() {
|
@ -140,7 +140,7 @@
|
|||||||
<artifactId>unit-ri</artifactId>
|
<artifactId>unit-ri</artifactId>
|
||||||
<version>1.0.3</version>
|
<version>1.0.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-collections4</artifactId>
|
<artifactId>commons-collections4</artifactId>
|
||||||
<version>${commons.collections.version}</version>
|
<version>${commons.collections.version}</version>
|
||||||
@ -487,21 +487,7 @@
|
|||||||
<artifactId>vavr</artifactId>
|
<artifactId>vavr</artifactId>
|
||||||
<version>${vavr.version}</version>
|
<version>${vavr.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.geotools</groupId>
|
|
||||||
<artifactId>gt-shapefile</artifactId>
|
|
||||||
<version>${geotools.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.geotools</groupId>
|
|
||||||
<artifactId>gt-epsg-hsql</artifactId>
|
|
||||||
<version>${geotools.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.geotools</groupId>
|
|
||||||
<artifactId>gt-swing</artifactId>
|
|
||||||
<version>${geotools.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<!-- Retrofit -->
|
<!-- Retrofit -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.squareup.retrofit2</groupId>
|
<groupId>com.squareup.retrofit2</groupId>
|
||||||
@ -581,19 +567,6 @@
|
|||||||
<name>Java.net repository</name>
|
<name>Java.net repository</name>
|
||||||
<url>http://download.java.net/maven/2</url>
|
<url>http://download.java.net/maven/2</url>
|
||||||
</repository>
|
</repository>
|
||||||
<repository>
|
|
||||||
<id>osgeo</id>
|
|
||||||
<name>Open Source Geospatial Foundation Repository</name>
|
|
||||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
|
||||||
<snapshots>
|
|
||||||
<enabled>true</enabled>
|
|
||||||
</snapshots>
|
|
||||||
<id>opengeo</id>
|
|
||||||
<name>OpenGeo Maven Repository</name>
|
|
||||||
<url>http://repo.opengeo.org</url>
|
|
||||||
</repository>
|
|
||||||
<repository>
|
<repository>
|
||||||
<snapshots>
|
<snapshots>
|
||||||
<enabled>false</enabled>
|
<enabled>false</enabled>
|
||||||
@ -647,13 +620,12 @@
|
|||||||
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
||||||
<streamex.version>0.6.5</streamex.version>
|
<streamex.version>0.6.5</streamex.version>
|
||||||
<vavr.version>0.9.0</vavr.version>
|
<vavr.version>0.9.0</vavr.version>
|
||||||
<geotools.version>15.2</geotools.version>
|
|
||||||
<joda-time.version>2.9.9</joda-time.version>
|
<joda-time.version>2.9.9</joda-time.version>
|
||||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||||
<retrofit.version>2.3.0</retrofit.version>
|
<retrofit.version>2.3.0</retrofit.version>
|
||||||
<joda-time.version>2.9.9</joda-time.version>
|
<joda-time.version>2.9.9</joda-time.version>
|
||||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||||
<protonpack.version>1.14</protonpack.version>
|
<protonpack.version>1.14</protonpack.version>
|
||||||
<unit-ri.version>1.0.3</unit-ri.version>
|
<unit-ri.version>1.0.3</unit-ri.version>
|
||||||
<cache.version>1.0.0</cache.version>
|
<cache.version>1.0.0</cache.version>
|
||||||
<hazelcast.version>3.8.4</hazelcast.version>
|
<hazelcast.version>3.8.4</hazelcast.version>
|
||||||
|
2
pom.xml
2
pom.xml
@ -57,7 +57,7 @@
|
|||||||
|
|
||||||
<!-- <module>gatling</module> --> <!-- not meant to run as part of the standard build -->
|
<!-- <module>gatling</module> --> <!-- not meant to run as part of the standard build -->
|
||||||
|
|
||||||
|
<module>geotools</module>
|
||||||
<module>groovy-spock</module>
|
<module>groovy-spock</module>
|
||||||
<module>gson</module>
|
<module>gson</module>
|
||||||
<module>guava</module>
|
<module>guava</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user