/
GamaShapeFile.java
412 lines (377 loc) · 14.6 KB
/
GamaShapeFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*******************************************************************************************************
*
* msi.gama.util.file.GamaShapeFile.java, in plugin msi.gama.core, is part of the source code of the GAMA modeling and
* simulation platform (v. 1.8.1)
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/SU & Partners
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package msi.gama.util.file;
import static msi.gama.runtime.GAMA.reportError;
import static msi.gama.runtime.exceptions.GamaRuntimeException.warning;
import static org.apache.commons.lang.StringUtils.join;
import static org.apache.commons.lang.StringUtils.splitByWholeSeparatorPreserveAllTokens;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.LinkedHashMap;
import java.util.Map;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Geometry;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryType;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import msi.gama.common.geometry.Envelope3D;
import msi.gama.common.geometry.GeometryUtils;
import msi.gama.common.util.GISUtils;
import msi.gama.metamodel.shape.GamaGisGeometry;
import msi.gama.metamodel.shape.IShape;
import msi.gama.metamodel.topology.projection.ProjectionFactory;
import msi.gama.precompiler.GamlAnnotations.doc;
import msi.gama.precompiler.GamlAnnotations.example;
import msi.gama.precompiler.GamlAnnotations.file;
import msi.gama.precompiler.IConcept;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.GamaListFactory;
import msi.gama.util.IList;
import msi.gaml.operators.Strings;
import msi.gaml.types.IType;
import msi.gaml.types.Types;
import ummisco.gama.dev.utils.DEBUG;
/**
* Written by drogoul Modified on 13 nov. 2011
*
* @todo Description
*
*/
@file (
name = "shape",
extensions = { "shp" },
buffer_type = IType.LIST,
buffer_content = IType.GEOMETRY,
buffer_index = IType.INT,
concept = { IConcept.SHAPEFILE, IConcept.FILE },
doc = @doc ("Represents a shape file as defined by the ESRI standard. See https://en.wikipedia.org/wiki/Shapefile for more information."))
@SuppressWarnings ({ "unchecked", "rawtypes" })
public class GamaShapeFile extends GamaGisFile {
public static class ShapeInfo extends GamaFileMetaData {
final int itemNumber;
final CoordinateReferenceSystem crs;
final double width;
final double height;
final Map<String, String> attributes = new LinkedHashMap();
public ShapeInfo(final IScope scope, final URL url, final long modificationStamp) {
super(modificationStamp);
ShapefileDataStore store = null;
ReferencedEnvelope env = new ReferencedEnvelope();
CoordinateReferenceSystem crs1 = null;
int number = 0;
try {
store = getDataStore(url);
final SimpleFeatureSource source = store.getFeatureSource();
final SimpleFeatureCollection features = source.getFeatures();
try {
crs1 = source.getInfo().getCRS();
} catch (final Exception e) {
DEBUG.ERR("Ignored exception in ShapeInfo getCRS:" + e.getMessage());
}
env = source.getBounds();
if (crs1 == null) {
crs1 = GISUtils.manageGoogleCRS(url);
if (crs1 != null) { env = new ReferencedEnvelope(env, crs1); }
}
if (crs1 != null) {
try {
env = env.transform(new ProjectionFactory().getTargetCRS(scope), true);
} catch (final Exception e) {
throw e;
}
}
try {
number = features.size();
} catch (final Exception e) {
DEBUG.ERR("Error in loading shapefile: " + e.getMessage());
}
final java.util.List<AttributeDescriptor> att_list = store.getSchema().getAttributeDescriptors();
for (final AttributeDescriptor desc : att_list) {
String type;
if (desc.getType() instanceof GeometryType) {
type = "geometry";
} else {
type = Types.get(desc.getType().getBinding()).toString();
}
attributes.put(desc.getName().getLocalPart(), type);
}
} catch (final Exception e) {
DEBUG.ERR("Error in reading metadata of " + url);
e.printStackTrace();
} finally {
width = env.getWidth();
height = env.getHeight();
itemNumber = number;
this.crs = crs1;
if (store != null) { store.dispose(); }
}
}
public CoordinateReferenceSystem getCRS() {
return crs;
}
public ShapeInfo(final String propertiesString) {
super(propertiesString);
final String[] segments = split(propertiesString);
itemNumber = Integer.parseInt(segments[1]);
final String crsString = segments[2];
CoordinateReferenceSystem theCRS;
if ("null".equals(crsString) || crsString.startsWith("Unknown")) {
theCRS = null;
} else {
try {
theCRS = CRS.parseWKT(crsString);
} catch (final Exception e) {
theCRS = null;
}
}
crs = theCRS;
width = Double.parseDouble(segments[3]);
height = Double.parseDouble(segments[4]);
if (segments.length > 5) {
final String[] names = splitByWholeSeparatorPreserveAllTokens(segments[5], SUB_DELIMITER);
final String[] types = splitByWholeSeparatorPreserveAllTokens(segments[6], SUB_DELIMITER);
for (int i = 0; i < names.length; i++) {
attributes.put(names[i], types[i]);
}
}
}
/**
* Method getSuffix()
*
* @see msi.gama.util.file.GamaFileMetaInformation#getSuffix()
*/
@Override
public String getSuffix() {
final StringBuilder sb = new StringBuilder();
appendSuffix(sb);
return sb.toString();
}
@Override
public void appendSuffix(final StringBuilder sb) {
sb.append(itemNumber).append(" object");
if (itemNumber > 1) { sb.append("s"); }
sb.append(SUFFIX_DEL);
sb.append(crs == null ? "Unknown CRS" : crs.getName().getCode());
sb.append(SUFFIX_DEL);
sb.append(Math.round(width)).append("m x ");
sb.append(Math.round(height)).append("m");
}
@Override
public String getDocumentation() {
final StringBuilder sb = new StringBuilder();
sb.append("Shapefile").append(Strings.LN);
sb.append(itemNumber).append(" objects").append(Strings.LN);
sb.append("Dimensions: ").append(Math.round(width) + "m x " + Math.round(height) + "m").append(Strings.LN);
sb.append("Coordinate Reference System: ").append(crs == null ? "Unknown CRS" : crs.getName().getCode())
.append(Strings.LN);
if (!attributes.isEmpty()) {
sb.append("Attributes: ").append(Strings.LN);
attributes.forEach((k, v) -> sb.append("<li>").append(k).append(" (" + v + ")").append("</li>"));
}
return sb.toString();
}
public Map<String, String> getAttributes() {
return attributes;
}
@Override
public String toPropertyString() {
// See Issue #1603: .toWKT() && pa can sometimes cause problem with
// certain projections.
String system = crs == null ? "Unknown projection" : crs.toWKT();
try {
CRS.parseWKT(system);
} catch (final Exception e) {
// The toWKT()/parseWKT() pair has a problem
String srs = CRS.toSRS(crs);
if (srs == null && crs != null) { srs = crs.getName().getCode(); }
system = "Unknown projection " + srs;
}
final String attributeNames = join(attributes.keySet(), SUB_DELIMITER);
final String types = join(attributes.values(), SUB_DELIMITER);
final Object[] toSave =
new Object[] { super.toPropertyString(), itemNumber, system, width, height, attributeNames, types };
return join(toSave, DELIMITER);
}
}
/**
* @throws GamaRuntimeException
* @param scope
* @param pathName
*/
@doc (
value = "This file constructor allows to read a shapefile (.shp) file",
examples = { @example (
value = "file f <- shape_file(\"file.shp\");",
isExecutable = false) })
public GamaShapeFile(final IScope scope, final String pathName) throws GamaRuntimeException {
super(scope, pathName, (Integer) null);
}
@doc (
value = "This file constructor allows to read a shapefile (.shp) file and specifying the coordinates system code, as an int (epsg code)",
examples = { @example (
value = "file f <- shape_file(\"file.shp\", \"32648\");",
isExecutable = false) })
public GamaShapeFile(final IScope scope, final String pathName, final Integer code) throws GamaRuntimeException {
super(scope, pathName, code);
}
@doc (
value = "This file constructor allows to read a shapefile (.shp) file and specifying the coordinates system code (epg,...,), as a string",
examples = { @example (
value = "file f <- shape_file(\"file.shp\", \"EPSG:32648\");",
isExecutable = false) })
public GamaShapeFile(final IScope scope, final String pathName, final String code) throws GamaRuntimeException {
super(scope, pathName, code);
}
@doc (
value = "This file constructor allows to read a shapefile (.shp) file and take a potential z value (not taken in account by default)",
examples = { @example (
value = "file f <- shape_file(\"file.shp\", true);",
isExecutable = false) })
public GamaShapeFile(final IScope scope, final String pathName, final boolean with3D) throws GamaRuntimeException {
super(scope, pathName, (Integer) null, with3D);
}
@doc (
value = "This file constructor allows to read a shapefile (.shp) file and specifying the coordinates system code, as an int (epsg code) and take a potential z value (not taken in account by default)",
examples = { @example (
value = "file f <- shape_file(\"file.shp\", \"32648\", true);",
isExecutable = false) })
public GamaShapeFile(final IScope scope, final String pathName, final Integer code, final boolean with3D)
throws GamaRuntimeException {
super(scope, pathName, code, with3D);
}
@doc (
value = "This file constructor allows to read a shapefile (.shp) file and specifying the coordinates system code (epg,...,), as a string and take a potential z value (not taken in account by default)",
examples = { @example (
value = "file f <- shape_file(\"file.shp\", \"EPSG:32648\",true);",
isExecutable = false) })
public GamaShapeFile(final IScope scope, final String pathName, final String code, final boolean with3D)
throws GamaRuntimeException {
super(scope, pathName, code, with3D);
}
/**
* @see msi.gama.util.GamaFile#fillBuffer()
*/
@Override
protected void fillBuffer(final IScope scope) throws GamaRuntimeException {
if (getBuffer() != null) return;
setBuffer(GamaListFactory.<IShape> create(Types.GEOMETRY));
readShapes(scope);
}
@Override
public IList<String> getAttributes(final IScope scope) {
ShapeInfo s;
final IFileMetaDataProvider p = scope.getGui().getMetaDataProvider();
if (p != null) {
s = (ShapeInfo) p.getMetaData(getFile(scope), false, true);
} else {
try {
s = new ShapeInfo(scope, getFile(scope).toURI().toURL(), 0);
} catch (final MalformedURLException e) {
return GamaListFactory.EMPTY_LIST;
}
}
return GamaListFactory.wrap(Types.STRING, s.attributes.keySet());
}
@Override
protected CoordinateReferenceSystem getOwnCRS(final IScope scope) {
ShapefileDataStore store = null;
try {
final URL url = getFile(scope).toURI().toURL();
store = getDataStore(url);
CoordinateReferenceSystem crs = store.getFeatureSource().getInfo().getCRS();
if (crs == null) { crs = GISUtils.manageGoogleCRS(url); }
return crs;
} catch (final IOException e) {
return null;
} finally {
if (store != null) { store.dispose(); }
}
}
static ShapefileDataStore getDataStore(final URL url) {
final ShapefileDataStore store = new ShapefileDataStore(url);
store.setGeometryFactory(GeometryUtils.GEOMETRY_FACTORY);
store.setBufferCachingEnabled(true);
store.setMemoryMapped(true);
store.setCharset(Charset.forName("UTF8"));
return store;
}
protected void readShapes(final IScope scope) {
ProgressCounter counter = new ProgressCounter(scope, "Reading file " + getName(scope));
FileDataStore store = null;
final File file = getFile(scope);
final IList list = getBuffer();
try {
store = FileDataStoreFinder.getDataStore(file);
final SimpleFeatureSource source = store.getFeatureSource();
final Envelope3D env = Envelope3D.of(source.getBounds());
computeProjection(scope, env);
SimpleFeatureCollection collection = source.getFeatures();
collection.accepts(feature -> {
Geometry g = (Geometry) feature.getDefaultGeometryProperty().getValue();
System.out.println(g.getCoordinate());
}, counter);
collection.accepts(feature -> {
Geometry g = (Geometry) feature.getDefaultGeometryProperty().getValue();
if (g != null && !g.isEmpty() /* Fix for Issue 725 && 677 */ ) {
if (!with3D && !g.isValid()) { g = GeometryUtils.cleanGeometry(g); }
g = gis.transform(g);
if (!with3D) {
g.apply(ZERO_Z);
g.geometryChanged();
}
g = multiPolygonManagement(g);
GamaGisGeometry gt = new GamaGisGeometry(g, feature);
if (gt.getInnerGeometry() != null) { list.add(gt); }
} else if (g == null) {
// See Issue 725
reportError(scope,
warning("geometry could not be added as it is " + "nil: " + feature.getIdentifier(),
scope),
false);
}
}, counter);
} catch (final IOException e) {
throw GamaRuntimeException.create(e, scope);
} finally {
if (store != null) { store.dispose(); }
}
// if (size > list.size()) {
// reportError(scope, warning("Problem with file " + getFile(scope) + ": only " + list.size() + " of the "
// + size + " geometries could be added", scope), false);
// }
}
@Override
public Envelope3D computeEnvelope(final IScope scope) {
if (gis == null) {
ShapefileDataStore store = null;
try {
store = getDataStore(getFile(scope).toURI().toURL());
final Envelope3D env = Envelope3D.of(store.getFeatureSource().getBounds());
computeProjection(scope, env);
} catch (final IOException e) {
return Envelope3D.EMPTY;
} finally {
if (store != null) { store.dispose(); }
}
}
return gis.getProjectedEnvelope();
}
}