comparison flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 1190:f514894ec2fd

merged flys-artifacts/2.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:17 +0200
parents 0398c2b8dbaf
children eb3ab28d1c21
comparison
equal deleted inserted replaced
917:b48c36076e17 1190:f514894ec2fd
1 package de.intevation.flys.utils;
2
3 import java.io.IOException;
4 import java.io.File;
5 import java.io.Serializable;
6 import java.net.MalformedURLException;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import org.apache.log4j.Logger;
13
14 import com.vividsolutions.jts.geom.Coordinate;
15 import com.vividsolutions.jts.geom.Geometry;
16
17 import org.opengis.feature.simple.SimpleFeature;
18 import org.opengis.feature.simple.SimpleFeatureType;
19 import org.opengis.referencing.FactoryException;
20 import org.opengis.referencing.NoSuchAuthorityCodeException;
21 import org.opengis.referencing.crs.CoordinateReferenceSystem;
22
23 import org.geotools.data.DataStoreFactorySpi;
24 import org.geotools.data.FeatureStore;
25 import org.geotools.data.DefaultTransaction;
26 import org.geotools.data.Transaction;
27 import org.geotools.data.shapefile.ShapefileDataStore;
28 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
29 import org.geotools.feature.FeatureIterator;
30 import org.geotools.feature.FeatureCollection;
31 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
32 import org.geotools.geojson.feature.FeatureJSON;
33 import org.geotools.referencing.CRS;
34
35 import de.intevation.flys.model.RiverAxis;
36
37
38 public class GeometryUtils {
39
40 private static final Logger logger = Logger.getLogger(GeometryUtils.class);
41
42
43 private GeometryUtils() {
44 }
45
46
47 public static String getRiverBounds(String rivername) {
48 RiverAxis axis = RiverAxis.getRiverAxis(rivername);
49 if (axis != null) {
50 // TODO Take the correct EPSG into account. Maybe, we need to
51 // reproject the geometry.
52 Geometry geom = axis.getGeom().getBoundary();
53 return jtsBoundsToOLBounds(geom);
54 }
55
56 return null;
57 }
58
59
60 /**
61 * Returns the boundary of Geometry <i>geom</i> in OpenLayers
62 * representation.
63 *
64 * @param geom The geometry.
65 *
66 * @return the OpenLayers boundary of <i>geom</i>.
67 */
68 public static String jtsBoundsToOLBounds(Geometry geom) {
69 Coordinate[] c = geom != null ? geom.getCoordinates() : null;
70
71 if (c == null || c.length < 2) {
72 return null;
73 }
74
75 return "" + c[0].x + " " + c[1].y + " " + c[1].x + " " + c[0].y;
76 }
77
78
79 public static SimpleFeatureType buildFeatureType(
80 String name, String srs, Class geometryType)
81 {
82 return buildFeatureType(name, srs, geometryType, null);
83 }
84
85
86 /**
87 * Creates a new SimpleFeatureType using a SimpleFeatureTypeBuilder.
88 *
89 * @param name The name of the FeatureType.
90 * @param srs The SRS (e.g. "EPSG:31466").
91 * @param geometryType The geometry type's class (e.g. Polygon.class).
92 * @param attrs Optional. An object with attribute-name/attribute-class
93 * pairs where index 0 specifies the name as string and index 1 the type
94 * as class.
95 *
96 * @return a new SimpleFeatureType.
97 */
98 public static SimpleFeatureType buildFeatureType(
99 String name, String srs, Class geometryType, Object[][] attrs)
100 {
101 try {
102 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
103 CoordinateReferenceSystem crs = CRS.decode(srs);
104
105 builder.setName("flys");
106 builder.setNamespaceURI("http://www.intevation.de/");
107 builder.setCRS(crs);
108 builder.setSRS(srs);
109
110 builder.add("geometry", geometryType, crs);
111
112 if (attrs != null) {
113 for (Object[] attr: attrs) {
114 builder.add((String) attr[0], (Class) attr[1]);
115 }
116 }
117
118 return builder.buildFeatureType();
119 }
120 catch (NoSuchAuthorityCodeException nsae) {
121 }
122 catch (FactoryException fe) {
123 }
124
125 return null;
126 }
127
128
129 public static List<SimpleFeature> parseGeoJSON(
130 String geojson, SimpleFeatureType ft
131 ) {
132 List<SimpleFeature> collection = new ArrayList<SimpleFeature>();
133
134 try {
135 FeatureJSON fjson = new FeatureJSON();
136 fjson.setFeatureType(ft);
137
138 FeatureIterator<SimpleFeature> iterator =
139 fjson.streamFeatureCollection(geojson);
140
141 while (iterator.hasNext()) {
142 collection.add(iterator.next());
143 }
144 }
145 catch (IOException ioe) {
146 // TODO handle exception
147 }
148
149 return collection;
150 }
151
152
153 public static boolean writeShapefile(
154 File shape,
155 SimpleFeatureType featureType,
156 FeatureCollection collection
157 ) {
158 if (collection.isEmpty()) {
159 logger.warn("Shapefile is not written - no features given!");
160 return false;
161 }
162
163 Transaction transaction = null;
164
165 try {
166 Map<String, Serializable> params =
167 new HashMap<String, Serializable>();
168
169 params.put("url", shape.toURI().toURL());
170 params.put("create spatial index", Boolean.TRUE);
171
172 DataStoreFactorySpi dataStoreFactory =
173 new ShapefileDataStoreFactory();
174
175 ShapefileDataStore newDataStore =
176 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
177 newDataStore.createSchema(featureType);
178
179 transaction = new DefaultTransaction("create");
180
181 String typeName = newDataStore.getTypeNames()[0];
182
183 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore =
184 (FeatureStore<SimpleFeatureType, SimpleFeature>)
185 newDataStore.getFeatureSource(typeName);
186
187 featureStore.setTransaction(transaction);
188
189 featureStore.addFeatures(collection);
190 transaction.commit();
191
192 return true;
193 }
194 catch (MalformedURLException mue) {
195 logger.error("Unable to prepare shapefile: " + mue.getMessage());
196 }
197 catch (IOException ioe) {
198 logger.error("Unable to write shapefile: " + ioe.getMessage());
199 }
200 finally {
201 if (transaction != null) {
202 try {
203 logger.debug("XXX CLOSE TRANSACTION!");
204 transaction.close();
205 }
206 catch (IOException ioe) { /* do nothing */ }
207 }
208 }
209
210 return false;
211 }
212 }
213 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org