1 package org
.argeo
.app
.geo
;
3 import java
.io
.BufferedReader
;
5 import java
.io
.InputStreamReader
;
6 import java
.io
.Serializable
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Collections
;
9 import java
.util
.HashMap
;
10 import java
.util
.List
;
13 import org
.geotools
.data
.DataUtilities
;
14 import org
.geotools
.data
.DefaultTransaction
;
15 import org
.geotools
.data
.Transaction
;
16 import org
.geotools
.data
.collection
.ListFeatureCollection
;
17 import org
.geotools
.data
.shapefile
.ShapefileDataStore
;
18 import org
.geotools
.data
.shapefile
.ShapefileDataStoreFactory
;
19 import org
.geotools
.data
.simple
.SimpleFeatureCollection
;
20 import org
.geotools
.data
.simple
.SimpleFeatureSource
;
21 import org
.geotools
.data
.simple
.SimpleFeatureStore
;
22 import org
.geotools
.feature
.simple
.SimpleFeatureBuilder
;
23 import org
.geotools
.geometry
.jts
.JTSFactoryFinder
;
24 import org
.geotools
.swing
.data
.JFileDataStoreChooser
;
25 import org
.locationtech
.jts
.geom
.Coordinate
;
26 import org
.locationtech
.jts
.geom
.Geometry
;
27 import org
.locationtech
.jts
.geom
.GeometryFactory
;
28 import org
.locationtech
.jts
.geom
.LineString
;
29 import org
.locationtech
.jts
.geom
.Point
;
30 import org
.opengis
.feature
.simple
.SimpleFeature
;
31 import org
.opengis
.feature
.simple
.SimpleFeatureType
;
33 public class GeoToolsTest
{
34 public GeoToolsTest() {
41 } catch (Exception e
) {
46 public void destroy() {
50 public static void main(String
[] args
) throws Exception
{
51 final SimpleFeatureType TYPE
= DataUtilities
.createType("Location", "the_geom:Point:srid=4326," + // <- the
56 "name:String," + // <- a String attribute
57 "number:Integer" // a number attribute
59 final SimpleFeatureType TYPE_HULL
= DataUtilities
.createType("Hull", "the_geom:MultiPolygon:srid=4326");
60 System
.out
.println("TYPE:" + TYPE
);
63 * A list to collect features as we create them.
65 List
<SimpleFeature
> features
= new ArrayList
<>();
66 List
<Coordinate
> coordinates
= new ArrayList
<>();
69 * GeometryFactory will be used to create the geometry attribute of each
70 * feature, using a Point object for the location.
72 GeometryFactory geometryFactory
= JTSFactoryFinder
.getGeometryFactory();
74 SimpleFeatureBuilder featureBuilder
= new SimpleFeatureBuilder(TYPE
);
76 try (BufferedReader reader
= new BufferedReader(
77 new InputStreamReader(GeoToolsTest
.class.getResourceAsStream("/org/djapps/on/apaf/locations.csv")))) {
78 /* First line of the data file is the header */
79 String line
= reader
.readLine();
80 System
.out
.println("Header: " + line
);
82 for (line
= reader
.readLine(); line
!= null; line
= reader
.readLine()) {
83 if (line
.trim().length() > 0) { // skip blank lines
84 String
[] tokens
= line
.split("\\,");
86 double latitude
= Double
.parseDouble(tokens
[0]);
87 double longitude
= Double
.parseDouble(tokens
[1]);
88 String name
= tokens
[2].trim();
89 int number
= Integer
.parseInt(tokens
[3].trim());
91 /* Longitude (= x coord) first ! */
92 Coordinate coordinate
= new Coordinate(longitude
, latitude
);
93 coordinates
.add(coordinate
);
94 Point point
= geometryFactory
.createPoint(coordinate
);
96 featureBuilder
.add(point
);
97 featureBuilder
.add(name
);
98 featureBuilder
.add(number
);
99 SimpleFeature feature
= featureBuilder
.buildFeature(null);
100 features
.add(feature
);
105 LineString lineString
= geometryFactory
106 .createLineString(coordinates
.toArray(new Coordinate
[coordinates
.size()]));
107 Geometry convexHull
= lineString
.convexHull();
108 System
.out
.println(convexHull
.toText());
109 SimpleFeatureBuilder hullFeatureBuilder
= new SimpleFeatureBuilder(TYPE_HULL
);
110 hullFeatureBuilder
.add(convexHull
);
111 SimpleFeature hull
= hullFeatureBuilder
.buildFeature(null);
114 * Get an output file name and create the new shapefile
116 File newFile
= getNewShapeFile();
118 ShapefileDataStoreFactory dataStoreFactory
= new ShapefileDataStoreFactory();
120 Map
<String
, Serializable
> params
= new HashMap
<>();
121 params
.put("url", newFile
.toURI().toURL());
122 params
.put("create spatial index", Boolean
.TRUE
);
124 ShapefileDataStore newDataStore
= (ShapefileDataStore
) dataStoreFactory
.createNewDataStore(params
);
127 * TYPE is used as a template to describe the file contents
129 newDataStore
.createSchema(TYPE_HULL
);
132 * Write the features to the shapefile
134 Transaction transaction
= new DefaultTransaction("create");
136 String typeName
= newDataStore
.getTypeNames()[0];
137 SimpleFeatureSource featureSource
= newDataStore
.getFeatureSource(typeName
);
138 SimpleFeatureType SHAPE_TYPE
= featureSource
.getSchema();
140 * The Shapefile format has a couple limitations: - "the_geom" is always first,
141 * and used for the geometry attribute name - "the_geom" must be of type Point,
142 * MultiPoint, MuiltiLineString, MultiPolygon - Attribute names are limited in
143 * length - Not all data types are supported (example Timestamp represented as
146 * Each data store has different limitations so check the resulting
149 System
.out
.println("SHAPE:" + SHAPE_TYPE
);
151 if (featureSource
instanceof SimpleFeatureStore
) {
152 SimpleFeatureStore featureStore
= (SimpleFeatureStore
) featureSource
;
154 * SimpleFeatureStore has a method to add features from a
155 * SimpleFeatureCollection object, so we use the ListFeatureCollection class to
156 * wrap our list of features.
158 SimpleFeatureCollection collection
= new ListFeatureCollection(TYPE
, Collections
.singletonList(hull
));
159 featureStore
.setTransaction(transaction
);
161 featureStore
.addFeatures(collection
);
162 transaction
.commit();
163 } catch (Exception problem
) {
164 problem
.printStackTrace();
165 transaction
.rollback();
170 System
.out
.println(typeName
+ " does not support read/write access");
172 // if (featureSource instanceof SimpleFeatureStore) {
173 // SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
175 // * SimpleFeatureStore has a method to add features from a
176 // * SimpleFeatureCollection object, so we use the ListFeatureCollection class to
177 // * wrap our list of features.
179 // SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
180 // featureStore.setTransaction(transaction);
182 // featureStore.addFeatures(collection);
183 // transaction.commit();
184 // } catch (Exception problem) {
185 // problem.printStackTrace();
186 // transaction.rollback();
188 // transaction.close();
191 // System.out.println(typeName + " does not support read/write access");
196 * Prompt the user for the name and path to use for the output shapefile
198 * @param csvFile the input csv file used to create a default shapefile name
199 * @return name and path for the shapefile as a new File object
201 private static File
getNewShapeFile() {
202 // String path = csvFile.getAbsolutePath();
203 // String newPath = path.substring(0, path.length() - 4) + ".shp";
205 JFileDataStoreChooser chooser
= new JFileDataStoreChooser("shp");
206 chooser
.setDialogTitle("Save shapefile");
207 // chooser.setSelectedFile(new File(newPath));
209 int returnVal
= chooser
.showSaveDialog(null);
211 if (returnVal
!= JFileDataStoreChooser
.APPROVE_OPTION
) {
212 // the user cancelled the dialog
216 File newFile
= chooser
.getSelectedFile();