annotate flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 1121:66783d957201

Close open shapefile transactions properly. flys-artifacts/trunk@2628 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 01 Sep 2011 07:27:44 +0000
parents faca1825818e
children c07e9e9c7482
rev   line source
1052
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.utils;
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
2
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
3 import java.io.IOException;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
4 import java.io.File;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
5 import java.io.Serializable;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
6 import java.net.MalformedURLException;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
7 import java.util.ArrayList;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
8 import java.util.HashMap;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
9 import java.util.List;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
10 import java.util.Map;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
11
1121
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
12 import org.apache.log4j.Logger;
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
13
1052
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
14 import com.vividsolutions.jts.geom.Coordinate;
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
15 import com.vividsolutions.jts.geom.Geometry;
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
16
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
17 import org.opengis.feature.simple.SimpleFeature;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
18 import org.opengis.feature.simple.SimpleFeatureType;
1108
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
19 import org.opengis.referencing.FactoryException;
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
20 import org.opengis.referencing.NoSuchAuthorityCodeException;
1107
005c2964f9af Use the correct SRID while reading GeoJSON barriers and writing line/polygon shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1106
diff changeset
21 import org.opengis.referencing.crs.CoordinateReferenceSystem;
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
22
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
23 import org.geotools.data.DataStoreFactorySpi;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
24 import org.geotools.data.FeatureStore;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
25 import org.geotools.data.DefaultTransaction;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
26 import org.geotools.data.Transaction;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
27 import org.geotools.data.shapefile.ShapefileDataStore;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
28 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
29 import org.geotools.feature.FeatureIterator;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
30 import org.geotools.feature.FeatureCollection;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
31 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
32 import org.geotools.geojson.feature.FeatureJSON;
1108
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
33 import org.geotools.referencing.CRS;
1052
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
34
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
35
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
36 public class GeometryUtils {
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
37
1121
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
38 private static final Logger logger = Logger.getLogger(GeometryUtils.class);
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
39
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
40
1052
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
41 private GeometryUtils() {
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
42 }
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
43
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
44
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
45 /**
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
46 * Returns the boundary of Geometry <i>geom</i> in OpenLayers
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
47 * representation.
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
48 *
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
49 * @param geom The geometry.
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
50 *
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
51 * @return the OpenLayers boundary of <i>geom</i>.
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
52 */
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
53 public static String jtsBoundsToOLBounds(Geometry geom) {
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
54 Coordinate[] c = geom != null ? geom.getCoordinates() : null;
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
55
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
56 if (c == null || c.length < 2) {
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
57 return null;
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
58 }
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
59
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
60 return "" + c[0].x + " " + c[1].y + " " + c[1].x + " " + c[0].y;
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
61 }
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
62
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
63
1107
005c2964f9af Use the correct SRID while reading GeoJSON barriers and writing line/polygon shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1106
diff changeset
64 public static SimpleFeatureType buildFeatureType(
1108
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
65 String name, String srs, Class geometryType)
1107
005c2964f9af Use the correct SRID while reading GeoJSON barriers and writing line/polygon shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1106
diff changeset
66 {
1119
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
67 return buildFeatureType(name, srs, geometryType, null);
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
68 }
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
69
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
70
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
71 /**
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
72 * Creates a new SimpleFeatureType using a SimpleFeatureTypeBuilder.
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
73 *
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
74 * @param name The name of the FeatureType.
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
75 * @param srs The SRS (e.g. "EPSG:31466").
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
76 * @param geometryType The geometry type's class (e.g. Polygon.class).
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
77 * @param attrs Optional. An object with attribute-name/attribute-class
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
78 * pairs where index 0 specifies the name as string and index 1 the type
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
79 * as class.
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
80 *
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
81 * @return a new SimpleFeatureType.
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
82 */
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
83 public static SimpleFeatureType buildFeatureType(
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
84 String name, String srs, Class geometryType, Object[][] attrs)
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
85 {
1108
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
86 try {
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
87 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
88 CoordinateReferenceSystem crs = CRS.decode(srs);
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
89
1108
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
90 builder.setName("flys");
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
91 builder.setNamespaceURI("http://www.intevation.de/");
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
92 builder.setCRS(crs);
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
93 builder.setSRS(srs);
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
94
1108
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
95 builder.add("geometry", geometryType, crs);
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
96
1119
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
97 if (attrs != null) {
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
98 for (Object[] attr: attrs) {
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
99 builder.add((String) attr[0], (Class) attr[1]);
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
100 }
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
101 }
faca1825818e Write additional attributes ELEVATION and KILOMETER into crosssection track shapefiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1108
diff changeset
102
1108
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
103 return builder.buildFeatureType();
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
104 }
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
105 catch (NoSuchAuthorityCodeException nsae) {
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
106 }
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
107 catch (FactoryException fe) {
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
108 }
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
109
5b1198c27d43 Export riveraxis and crosssections to shapefiles for WSPLGEN.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1107
diff changeset
110 return null;
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
111 }
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
112
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
113
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
114 public static List<SimpleFeature> parseGeoJSON(
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
115 String geojson, SimpleFeatureType ft
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
116 ) {
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
117 List<SimpleFeature> collection = new ArrayList<SimpleFeature>();
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
118
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
119 try {
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
120 FeatureJSON fjson = new FeatureJSON();
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
121 fjson.setFeatureType(ft);
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
122
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
123 FeatureIterator<SimpleFeature> iterator =
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
124 fjson.streamFeatureCollection(geojson);
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
125
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
126 while (iterator.hasNext()) {
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
127 collection.add(iterator.next());
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
128 }
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
129 }
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
130 catch (IOException ioe) {
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
131 // TODO handle exception
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
132 }
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
133
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
134 return collection;
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
135 }
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
136
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
137
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
138 public static boolean writeShapefile(
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
139 File shape,
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
140 SimpleFeatureType featureType,
1121
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
141 FeatureCollection collection
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
142 ) {
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
143 Transaction transaction = null;
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
144
1121
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
145 try {
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
146 Map<String, Serializable> params =
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
147 new HashMap<String, Serializable>();
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
148
1121
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
149 params.put("url", shape.toURI().toURL());
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
150 params.put("create spatial index", Boolean.TRUE);
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
151
1121
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
152 DataStoreFactorySpi dataStoreFactory =
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
153 new ShapefileDataStoreFactory();
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
154
1121
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
155 ShapefileDataStore newDataStore =
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
156 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
157 newDataStore.createSchema(featureType);
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
158
1121
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
159 transaction = new DefaultTransaction("create");
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
160
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
161 String typeName = newDataStore.getTypeNames()[0];
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
162
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
163 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
164 (FeatureStore<SimpleFeatureType, SimpleFeature>)
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
165 newDataStore.getFeatureSource(typeName);
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
166
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
167 featureStore.setTransaction(transaction);
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
168
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
169 featureStore.addFeatures(collection);
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
170 transaction.commit();
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
171
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
172 return true;
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
173 }
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
174 catch (MalformedURLException mue) {
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
175 logger.error("Unable to prepare shapefile: " + mue.getMessage());
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
176 }
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
177 catch (IOException ioe) {
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
178 logger.error("Unable to write shapefile: " + ioe.getMessage());
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
179 }
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
180 finally {
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
181 if (transaction != null) {
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
182 try {
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
183 logger.debug("XXX CLOSE TRANSACTION!");
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
184 transaction.close();
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
185 }
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
186 catch (IOException ioe) { /* do nothing */ }
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
187 }
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
188 }
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
189
66783d957201 Close open shapefile transactions properly.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1119
diff changeset
190 return false;
1106
e9f66d63bdd0 Write user defined barriers into a shapefile that is placed in the Artifact's directory.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1052
diff changeset
191 }
1052
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
192 }
eeebf8514c7f The boundary (extent) for the river axis is determined based on its geometry - this extent is written into the wms facet.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
193 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org