Geotools refactor (#2626)

This commit is contained in:
Grzegorz Piwowarek 2017-09-16 09:14:03 +03:00 committed by GitHub
parent 02c5135867
commit 4846177a2a
2 changed files with 34 additions and 48 deletions

View File

@ -1,12 +1,8 @@
package com.baeldung.geotools; package com.baeldung.geotools;
import java.io.File; import com.vividsolutions.jts.geom.Coordinate;
import java.io.Serializable; import com.vividsolutions.jts.geom.GeometryFactory;
import java.util.ArrayList; import com.vividsolutions.jts.geom.Point;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.geotools.data.DataUtilities; import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction; import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction; import org.geotools.data.Transaction;
@ -23,9 +19,13 @@ import org.geotools.swing.data.JFileDataStoreChooser;
import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.feature.simple.SimpleFeatureType;
import com.vividsolutions.jts.geom.Coordinate; import java.io.File;
import com.vividsolutions.jts.geom.GeometryFactory; import java.io.Serializable;
import com.vividsolutions.jts.geom.Point; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class ShapeFile { public class ShapeFile {
@ -52,10 +52,9 @@ public class ShapeFile {
ShapefileDataStore dataStore = setDataStoreParams(dataStoreFactory, params, shapeFile, CITY); ShapefileDataStore dataStore = setDataStoreParams(dataStoreFactory, params, shapeFile, CITY);
writeToFile(dataStore, collection); writeToFile(dataStore, collection);
} }
public static SimpleFeatureType createFeatureType() { static SimpleFeatureType createFeatureType() {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("Location"); builder.setName("Location");
@ -65,60 +64,55 @@ public class ShapeFile {
builder.length(15) builder.length(15)
.add("Name", String.class); .add("Name", String.class);
SimpleFeatureType CITY = builder.buildFeatureType(); return builder.buildFeatureType();
return CITY;
} }
public static void addLocations(SimpleFeatureType CITY, DefaultFeatureCollection collection) { static void addLocations(SimpleFeatureType CITY, DefaultFeatureCollection collection) {
Map<String, List<Double>> locations = new HashMap<>(); Map<String, List<Double>> locations = new HashMap<>();
double lat = 13.752222; double lat = 13.752222;
double lng = 100.493889; double lng = 100.493889;
String name = "Bangkok"; addToLocationMap("Bangkok", lat, lng, locations);
addToLocationMap(name, lat, lng, locations);
lat = 53.083333; lat = 53.083333;
lng = -0.15; lng = -0.15;
name = "New York"; addToLocationMap("New York", lat, lng, locations);
addToLocationMap(name, lat, lng, locations);
lat = -33.925278; lat = -33.925278;
lng = 18.423889; lng = 18.423889;
name = "Cape Town"; addToLocationMap("Cape Town", lat, lng, locations);
addToLocationMap(name, lat, lng, locations);
lat = -33.859972; lat = -33.859972;
lng = 151.211111; lng = 151.211111;
name = "Sydney"; addToLocationMap("Sydney", lat, lng, locations);
addToLocationMap(name, lat, lng, locations);
lat = 45.420833; lat = 45.420833;
lng = -75.69; lng = -75.69;
name = "Ottawa"; addToLocationMap("Ottawa", lat, lng, locations);
addToLocationMap(name, lat, lng, locations);
lat = 30.07708; lat = 30.07708;
lng = 31.285909; lng = 31.285909;
name = "Cairo"; addToLocationMap("Cairo", lat, lng, locations);
addToLocationMap(name, lat, lng, locations);
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
for (Map.Entry<String, List<Double>> location : locations.entrySet()) { locations.entrySet().stream()
Point point = geometryFactory.createPoint(new Coordinate(location.getValue() .map(toFeature(CITY, geometryFactory))
.get(0), .forEach(collection::add);
location.getValue() }
.get(1)));
private static Function<Map.Entry<String, List<Double>>, SimpleFeature> toFeature(SimpleFeatureType CITY, GeometryFactory geometryFactory) {
return location -> {
Point point = geometryFactory.createPoint(
new Coordinate(location.getValue()
.get(0), location.getValue().get(1)));
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(CITY); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(CITY);
featureBuilder.add(point); featureBuilder.add(point);
featureBuilder.add(location.getKey()); featureBuilder.add(location.getKey());
SimpleFeature feature = featureBuilder.buildFeature(null); return featureBuilder.buildFeature(null);
collection.add(feature); };
}
} }
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) {
@ -142,9 +136,7 @@ public class ShapeFile {
System.exit(0); System.exit(0);
} }
File shapeFile = chooser.getSelectedFile(); return chooser.getSelectedFile();
return shapeFile;
} }
private static ShapefileDataStore setDataStoreParams(ShapefileDataStoreFactory dataStoreFactory, Map<String, Serializable> params, File shapeFile, SimpleFeatureType CITY) throws Exception { private static ShapefileDataStore setDataStoreParams(ShapefileDataStoreFactory dataStoreFactory, Map<String, Serializable> params, File shapeFile, SimpleFeatureType CITY) throws Exception {
@ -176,11 +168,9 @@ public class ShapeFile {
try { try {
featureStore.addFeatures(collection); featureStore.addFeatures(collection);
transaction.commit(); transaction.commit();
} catch (Exception problem) { } catch (Exception problem) {
problem.printStackTrace(); problem.printStackTrace();
transaction.rollback(); transaction.rollback();
} finally { } finally {
transaction.close(); transaction.close();
} }
@ -190,5 +180,4 @@ public class ShapeFile {
System.exit(1); System.exit(1);
} }
} }
} }

View File

@ -11,7 +11,6 @@ public class GeoToolsUnitTest {
@Test @Test
public void givenFeatureType_whenAddLocations_returnFeatureCollection() { public void givenFeatureType_whenAddLocations_returnFeatureCollection() {
DefaultFeatureCollection collection = new DefaultFeatureCollection(); DefaultFeatureCollection collection = new DefaultFeatureCollection();
SimpleFeatureType CITY = ShapeFile.createFeatureType(); SimpleFeatureType CITY = ShapeFile.createFeatureType();
@ -19,7 +18,5 @@ public class GeoToolsUnitTest {
ShapeFile.addLocations(CITY, collection); ShapeFile.addLocations(CITY, collection);
assertNotNull(collection); assertNotNull(collection);
} }
} }