Skip to content

Commit 8ac2322

Browse files
dimitarsazdovskipivovarit
authored andcommitted
Bael 817 refactor (eugenp#2575)
* Code refactor according to comments * Moved to new module * Changes in pom file
1 parent ba9d832 commit 8ac2322

5 files changed

Lines changed: 126 additions & 73 deletions

File tree

geotools/pom.xml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>geotools</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>geotools</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<dependencies>
14+
<dependency>
15+
<groupId>junit</groupId>
16+
<artifactId>junit</artifactId>
17+
<version>4.12</version>
18+
<scope>test</scope>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.geotools</groupId>
22+
<artifactId>gt-shapefile</artifactId>
23+
<version>${geotools-shapefile.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.geotools</groupId>
27+
<artifactId>gt-epsg-hsql</artifactId>
28+
<version>${geotools.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.geotools</groupId>
32+
<artifactId>gt-swing</artifactId>
33+
<version>${geotools-swing.version}</version>
34+
</dependency>
35+
</dependencies>
36+
<repositories>
37+
<repository>
38+
<id>maven2-repository.dev.java.net</id>
39+
<name>Java.net repository</name>
40+
<url>http://download.java.net/maven/2</url>
41+
</repository>
42+
<repository>
43+
<id>osgeo</id>
44+
<name>Open Source Geospatial Foundation Repository</name>
45+
<url>http://download.osgeo.org/webdav/geotools/</url>
46+
</repository>
47+
<repository>
48+
<snapshots>
49+
<enabled>true</enabled>
50+
</snapshots>
51+
<id>opengeo</id>
52+
<name>OpenGeo Maven Repository</name>
53+
<url>http://repo.opengeo.org</url>
54+
</repository>
55+
</repositories>
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<inherited>true</inherited>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-compiler-plugin</artifactId>
62+
<configuration>
63+
<source>1.8</source>
64+
<target>1.8</target>
65+
</configuration>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
<properties>
70+
<geotools.version>15.2</geotools.version>
71+
<geotools-swing.version>15.2</geotools-swing.version>
72+
<geotools-shapefile.version>15.2</geotools-shapefile.version>
73+
</properties>
74+
</project>

libraries/src/main/java/com/baeldung/geotools/ShapeFile.java renamed to geotools/src/main/java/com/baeldung/geotools/ShapeFile.java

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

libraries/src/test/java/com/baeldung/geotools/GeoToolsUnitTestTest.java renamed to geotools/src/test/java/com/baeldung/geotools/GeoToolsUnitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.junit.Test;
88
import org.opengis.feature.simple.SimpleFeatureType;
99

10-
public class GeoToolsUnitTestTest {
10+
public class GeoToolsUnitTest {
1111

1212
@Test
1313
public void givenFeatureType_whenAddLocations_returnFeatureCollection() {

libraries/pom.xml

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
<artifactId>unit-ri</artifactId>
141141
<version>1.0.3</version>
142142
</dependency>
143-
<dependency>
143+
<dependency>
144144
<groupId>org.apache.commons</groupId>
145145
<artifactId>commons-collections4</artifactId>
146146
<version>${commons.collections.version}</version>
@@ -487,21 +487,7 @@
487487
<artifactId>vavr</artifactId>
488488
<version>${vavr.version}</version>
489489
</dependency>
490-
<dependency>
491-
<groupId>org.geotools</groupId>
492-
<artifactId>gt-shapefile</artifactId>
493-
<version>${geotools.version}</version>
494-
</dependency>
495-
<dependency>
496-
<groupId>org.geotools</groupId>
497-
<artifactId>gt-epsg-hsql</artifactId>
498-
<version>${geotools.version}</version>
499-
</dependency>
500-
<dependency>
501-
<groupId>org.geotools</groupId>
502-
<artifactId>gt-swing</artifactId>
503-
<version>${geotools.version}</version>
504-
</dependency>
490+
505491
<!-- Retrofit -->
506492
<dependency>
507493
<groupId>com.squareup.retrofit2</groupId>
@@ -581,19 +567,6 @@
581567
<name>Java.net repository</name>
582568
<url>http://download.java.net/maven/2</url>
583569
</repository>
584-
<repository>
585-
<id>osgeo</id>
586-
<name>Open Source Geospatial Foundation Repository</name>
587-
<url>http://download.osgeo.org/webdav/geotools/</url>
588-
</repository>
589-
<repository>
590-
<snapshots>
591-
<enabled>true</enabled>
592-
</snapshots>
593-
<id>opengeo</id>
594-
<name>OpenGeo Maven Repository</name>
595-
<url>http://repo.opengeo.org</url>
596-
</repository>
597570
<repository>
598571
<snapshots>
599572
<enabled>false</enabled>
@@ -647,13 +620,12 @@
647620
<eclipse-collections.version>8.2.0</eclipse-collections.version>
648621
<streamex.version>0.6.5</streamex.version>
649622
<vavr.version>0.9.0</vavr.version>
650-
<geotools.version>15.2</geotools.version>
651623
<joda-time.version>2.9.9</joda-time.version>
652624
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
653625
<retrofit.version>2.3.0</retrofit.version>
654626
<joda-time.version>2.9.9</joda-time.version>
655627
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
656-
<protonpack.version>1.14</protonpack.version>
628+
<protonpack.version>1.14</protonpack.version>
657629
<unit-ri.version>1.0.3</unit-ri.version>
658630
<cache.version>1.0.0</cache.version>
659631
<hazelcast.version>3.8.4</hazelcast.version>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
<!-- <module>gatling</module> --> <!-- not meant to run as part of the standard build -->
5959

60-
60+
<module>geotools</module>
6161
<module>groovy-spock</module>
6262
<module>gson</module>
6363
<module>guava</module>

0 commit comments

Comments
 (0)