11package com .baeldung .geotools ;
22
3- import java .io .File ;
4- import java .io .Serializable ;
5- import java .util .ArrayList ;
6- import java .util .HashMap ;
7- import java .util .List ;
8- import java .util .Map ;
9-
3+ import com .vividsolutions .jts .geom .Coordinate ;
4+ import com .vividsolutions .jts .geom .GeometryFactory ;
5+ import com .vividsolutions .jts .geom .Point ;
106import org .geotools .data .DataUtilities ;
117import org .geotools .data .DefaultTransaction ;
128import org .geotools .data .Transaction ;
2319import org .opengis .feature .simple .SimpleFeature ;
2420import org .opengis .feature .simple .SimpleFeatureType ;
2521
26- import com .vividsolutions .jts .geom .Coordinate ;
27- import com .vividsolutions .jts .geom .GeometryFactory ;
28- import com .vividsolutions .jts .geom .Point ;
22+ import java .io .File ;
23+ import java .io .Serializable ;
24+ import java .util .ArrayList ;
25+ import java .util .HashMap ;
26+ import java .util .List ;
27+ import java .util .Map ;
28+ import java .util .function .Function ;
2929
3030public class ShapeFile {
3131
@@ -52,73 +52,67 @@ public static void main(String[] args) throws Exception {
5252 ShapefileDataStore dataStore = setDataStoreParams (dataStoreFactory , params , shapeFile , CITY );
5353
5454 writeToFile (dataStore , collection );
55-
5655 }
5756
58- public static SimpleFeatureType createFeatureType () {
57+ static SimpleFeatureType createFeatureType () {
5958
6059 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder ();
6160 builder .setName ("Location" );
6261 builder .setCRS (DefaultGeographicCRS .WGS84 );
6362
6463 builder .add ("Location" , Point .class );
6564 builder .length (15 )
66- .add ("Name" , String .class );
67-
68- SimpleFeatureType CITY = builder .buildFeatureType ();
65+ .add ("Name" , String .class );
6966
70- return CITY ;
67+ return builder . buildFeatureType () ;
7168 }
7269
73- public static void addLocations (SimpleFeatureType CITY , DefaultFeatureCollection collection ) {
70+ static void addLocations (SimpleFeatureType CITY , DefaultFeatureCollection collection ) {
7471
7572 Map <String , List <Double >> locations = new HashMap <>();
7673
7774 double lat = 13.752222 ;
7875 double lng = 100.493889 ;
79- String name = "Bangkok" ;
80- addToLocationMap (name , lat , lng , locations );
76+ addToLocationMap ("Bangkok" , lat , lng , locations );
8177
8278 lat = 53.083333 ;
8379 lng = -0.15 ;
84- name = "New York" ;
85- addToLocationMap (name , lat , lng , locations );
80+ addToLocationMap ("New York" , lat , lng , locations );
8681
8782 lat = -33.925278 ;
8883 lng = 18.423889 ;
89- name = "Cape Town" ;
90- addToLocationMap (name , lat , lng , locations );
84+ addToLocationMap ("Cape Town" , lat , lng , locations );
9185
9286 lat = -33.859972 ;
9387 lng = 151.211111 ;
94- name = "Sydney" ;
95- addToLocationMap (name , lat , lng , locations );
88+ addToLocationMap ("Sydney" , lat , lng , locations );
9689
9790 lat = 45.420833 ;
9891 lng = -75.69 ;
99- name = "Ottawa" ;
100- addToLocationMap (name , lat , lng , locations );
92+ addToLocationMap ("Ottawa" , lat , lng , locations );
10193
10294 lat = 30.07708 ;
10395 lng = 31.285909 ;
104- name = "Cairo" ;
105- addToLocationMap (name , lat , lng , locations );
96+ addToLocationMap ("Cairo" , lat , lng , locations );
10697
10798 GeometryFactory geometryFactory = JTSFactoryFinder .getGeometryFactory (null );
10899
109- for (Map .Entry <String , List <Double >> location : locations .entrySet ()) {
110- Point point = geometryFactory .createPoint (new Coordinate (location .getValue ()
111- .get (0 ),
112- location .getValue ()
113- .get (1 )));
100+ locations .entrySet ().stream ()
101+ .map (toFeature (CITY , geometryFactory ))
102+ .forEach (collection ::add );
103+ }
104+
105+ private static Function <Map .Entry <String , List <Double >>, SimpleFeature > toFeature (SimpleFeatureType CITY , GeometryFactory geometryFactory ) {
106+ return location -> {
107+ Point point = geometryFactory .createPoint (
108+ new Coordinate (location .getValue ()
109+ .get (0 ), location .getValue ().get (1 )));
114110
115111 SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder (CITY );
116112 featureBuilder .add (point );
117113 featureBuilder .add (location .getKey ());
118- SimpleFeature feature = featureBuilder .buildFeature (null );
119- collection .add (feature );
120- }
121-
114+ return featureBuilder .buildFeature (null );
115+ };
122116 }
123117
124118 private static void addToLocationMap (String name , double lat , double lng , Map <String , List <Double >> locations ) {
@@ -142,14 +136,12 @@ private static File getNewShapeFile() {
142136 System .exit (0 );
143137 }
144138
145- File shapeFile = chooser .getSelectedFile ();
146-
147- return shapeFile ;
139+ return chooser .getSelectedFile ();
148140 }
149141
150142 private static ShapefileDataStore setDataStoreParams (ShapefileDataStoreFactory dataStoreFactory , Map <String , Serializable > params , File shapeFile , SimpleFeatureType CITY ) throws Exception {
151143 params .put ("url" , shapeFile .toURI ()
152- .toURL ());
144+ .toURL ());
153145 params .put ("create spatial index" , Boolean .TRUE );
154146
155147 ShapefileDataStore dataStore = (ShapefileDataStore ) dataStoreFactory .createNewDataStore (params );
@@ -176,11 +168,9 @@ private static void writeToFile(ShapefileDataStore dataStore, DefaultFeatureColl
176168 try {
177169 featureStore .addFeatures (collection );
178170 transaction .commit ();
179-
180171 } catch (Exception problem ) {
181172 problem .printStackTrace ();
182173 transaction .rollback ();
183-
184174 } finally {
185175 transaction .close ();
186176 }
@@ -190,5 +180,4 @@ private static void writeToFile(ShapefileDataStore dataStore, DefaultFeatureColl
190180 System .exit (1 );
191181 }
192182 }
193-
194183}
0 commit comments