]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.core/src/org/argeo/app/geo/GeoToolsTest.java
Adapt to changes in Argeo Commons.
[gpl/argeo-suite.git] / org.argeo.app.core / src / org / argeo / app / geo / GeoToolsTest.java
1 package org.argeo.app.geo;
2
3 import java.io.BufferedReader;
4 import java.io.File;
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;
11 import java.util.Map;
12
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;
32
33 public class GeoToolsTest {
34 public GeoToolsTest() {
35
36 }
37
38 public void init() {
39 try {
40 main(null);
41 } catch (Exception e) {
42 e.printStackTrace();
43 }
44 }
45
46 public void destroy() {
47
48 }
49
50 public static void main(String[] args) throws Exception {
51 final SimpleFeatureType TYPE = DataUtilities.createType("Location", "the_geom:Point:srid=4326," + // <- the
52 // geometry
53 // attribute:
54 // Point
55 // type
56 "name:String," + // <- a String attribute
57 "number:Integer" // a number attribute
58 );
59 final SimpleFeatureType TYPE_HULL = DataUtilities.createType("Hull", "the_geom:MultiPolygon:srid=4326");
60 System.out.println("TYPE:" + TYPE);
61
62 /*
63 * A list to collect features as we create them.
64 */
65 List<SimpleFeature> features = new ArrayList<>();
66 List<Coordinate> coordinates = new ArrayList<>();
67
68 /*
69 * GeometryFactory will be used to create the geometry attribute of each
70 * feature, using a Point object for the location.
71 */
72 GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
73
74 SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
75
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);
81
82 for (line = reader.readLine(); line != null; line = reader.readLine()) {
83 if (line.trim().length() > 0) { // skip blank lines
84 String[] tokens = line.split("\\,");
85
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());
90
91 /* Longitude (= x coord) first ! */
92 Coordinate coordinate = new Coordinate(longitude, latitude);
93 coordinates.add(coordinate);
94 Point point = geometryFactory.createPoint(coordinate);
95
96 featureBuilder.add(point);
97 featureBuilder.add(name);
98 featureBuilder.add(number);
99 SimpleFeature feature = featureBuilder.buildFeature(null);
100 features.add(feature);
101 }
102 }
103 }
104
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);
112
113 /*
114 * Get an output file name and create the new shapefile
115 */
116 File newFile = getNewShapeFile();
117
118 ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
119
120 Map<String, Serializable> params = new HashMap<>();
121 params.put("url", newFile.toURI().toURL());
122 params.put("create spatial index", Boolean.TRUE);
123
124 ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
125
126 /*
127 * TYPE is used as a template to describe the file contents
128 */
129 newDataStore.createSchema(TYPE_HULL);
130
131 /*
132 * Write the features to the shapefile
133 */
134 Transaction transaction = new DefaultTransaction("create");
135
136 String typeName = newDataStore.getTypeNames()[0];
137 SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
138 SimpleFeatureType SHAPE_TYPE = featureSource.getSchema();
139 /*
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
144 * Date)
145 *
146 * Each data store has different limitations so check the resulting
147 * SimpleFeatureType.
148 */
149 System.out.println("SHAPE:" + SHAPE_TYPE);
150
151 if (featureSource instanceof SimpleFeatureStore) {
152 SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
153 /*
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.
157 */
158 SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, Collections.singletonList(hull));
159 featureStore.setTransaction(transaction);
160 try {
161 featureStore.addFeatures(collection);
162 transaction.commit();
163 } catch (Exception problem) {
164 problem.printStackTrace();
165 transaction.rollback();
166 } finally {
167 transaction.close();
168 }
169 } else {
170 System.out.println(typeName + " does not support read/write access");
171 }
172 // if (featureSource instanceof SimpleFeatureStore) {
173 // SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
174 // /*
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.
178 // */
179 // SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
180 // featureStore.setTransaction(transaction);
181 // try {
182 // featureStore.addFeatures(collection);
183 // transaction.commit();
184 // } catch (Exception problem) {
185 // problem.printStackTrace();
186 // transaction.rollback();
187 // } finally {
188 // transaction.close();
189 // }
190 // } else {
191 // System.out.println(typeName + " does not support read/write access");
192 // }
193 }
194
195 /**
196 * Prompt the user for the name and path to use for the output shapefile
197 *
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
200 */
201 private static File getNewShapeFile() {
202 // String path = csvFile.getAbsolutePath();
203 // String newPath = path.substring(0, path.length() - 4) + ".shp";
204
205 JFileDataStoreChooser chooser = new JFileDataStoreChooser("shp");
206 chooser.setDialogTitle("Save shapefile");
207 // chooser.setSelectedFile(new File(newPath));
208
209 int returnVal = chooser.showSaveDialog(null);
210
211 if (returnVal != JFileDataStoreChooser.APPROVE_OPTION) {
212 // the user cancelled the dialog
213 System.exit(0);
214 }
215
216 File newFile = chooser.getSelectedFile();
217
218 return newFile;
219 }
220
221 }