Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 3818:dc18457b1cef
merged flys-artifacts/pre2.7-2012-03-16
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:59 +0200 |
parents | cbeeaaad1056 |
children | 33aa37e6d98f |
comparison
equal
deleted
inserted
replaced
2456:60ab1054069d | 3818:dc18457b1cef |
---|---|
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.Envelope; | |
16 import com.vividsolutions.jts.geom.Geometry; | |
17 | |
18 import org.opengis.feature.simple.SimpleFeature; | |
19 import org.opengis.feature.simple.SimpleFeatureType; | |
20 import org.opengis.referencing.FactoryException; | |
21 import org.opengis.referencing.NoSuchAuthorityCodeException; | |
22 import org.opengis.referencing.crs.CoordinateReferenceSystem; | |
23 | |
24 import org.geotools.data.DataStoreFactorySpi; | |
25 import org.geotools.data.FeatureStore; | |
26 import org.geotools.data.DefaultTransaction; | |
27 import org.geotools.data.Transaction; | |
28 import org.geotools.data.shapefile.ShapefileDataStore; | |
29 import org.geotools.data.shapefile.ShapefileDataStoreFactory; | |
30 import org.geotools.feature.FeatureIterator; | |
31 import org.geotools.feature.FeatureCollection; | |
32 import org.geotools.feature.simple.SimpleFeatureTypeBuilder; | |
33 import org.geotools.geojson.feature.FeatureJSON; | |
34 import org.geotools.referencing.CRS; | |
35 | |
36 import de.intevation.flys.model.RiverAxis; | |
37 | |
38 | |
39 public class GeometryUtils { | |
40 | |
41 private static final Logger logger = Logger.getLogger(GeometryUtils.class); | |
42 | |
43 | |
44 private GeometryUtils() { | |
45 } | |
46 | |
47 | |
48 public static Envelope getRiverBoundary(String rivername) { | |
49 List<RiverAxis> axes = RiverAxis.getRiverAxis(rivername); | |
50 if (axes != null && axes.size() > 0) { | |
51 Envelope max = null; | |
52 | |
53 for (RiverAxis axis: axes) { | |
54 // TODO Take the correct EPSG into account. Maybe, we need to | |
55 // reproject the geometry. | |
56 Envelope env = axis.getGeom().getEnvelopeInternal(); | |
57 | |
58 if (max == null) { | |
59 max = env; | |
60 } | |
61 else { | |
62 max.expandToInclude(env); | |
63 } | |
64 } | |
65 | |
66 return max; | |
67 } | |
68 | |
69 return null; | |
70 } | |
71 | |
72 | |
73 public static String getRiverBounds(String rivername) { | |
74 Envelope env = getRiverBoundary(rivername); | |
75 | |
76 if (env == null) { | |
77 return jtsBoundsToOLBounds(env); | |
78 } | |
79 | |
80 return null; | |
81 } | |
82 | |
83 | |
84 /** | |
85 * Returns the boundary of Envelope <i>env</i> in OpenLayers | |
86 * representation. | |
87 * | |
88 * @param env The envelope of a geometry. | |
89 * | |
90 * @return the OpenLayers boundary of <i>env</i>. | |
91 */ | |
92 public static String jtsBoundsToOLBounds(Envelope env) { | |
93 return "" + | |
94 env.getMinX() + " " + | |
95 env.getMinY() + " " + | |
96 env.getMaxX() + " " + | |
97 env.getMaxY(); | |
98 } | |
99 | |
100 | |
101 public static String createOLBounds(Geometry a, Geometry b) { | |
102 Coordinate[] ca = a.getCoordinates(); | |
103 Coordinate[] cb = b.getCoordinates(); | |
104 | |
105 double lowerX = Double.MAX_VALUE; | |
106 double lowerY = Double.MAX_VALUE; | |
107 double upperX = -Double.MAX_VALUE; | |
108 double upperY = -Double.MAX_VALUE; | |
109 | |
110 for (Coordinate c: ca) { | |
111 lowerX = lowerX < c.x ? lowerX : c.x; | |
112 lowerY = lowerY < c.y ? lowerY : c.y; | |
113 | |
114 upperX = upperX > c.x ? upperX : c.x; | |
115 upperY = upperY > c.y ? upperY : c.y; | |
116 } | |
117 | |
118 for (Coordinate c: cb) { | |
119 lowerX = lowerX < c.x ? lowerX : c.x; | |
120 lowerY = lowerY < c.y ? lowerY : c.y; | |
121 | |
122 upperX = upperX > c.x ? upperX : c.x; | |
123 upperY = upperY > c.y ? upperY : c.y; | |
124 } | |
125 | |
126 return "" + lowerX + " " + lowerY + " " + upperX + " " + upperY; | |
127 } | |
128 | |
129 | |
130 public static SimpleFeatureType buildFeatureType( | |
131 String name, String srs, Class geometryType) | |
132 { | |
133 return buildFeatureType(name, srs, geometryType, null); | |
134 } | |
135 | |
136 | |
137 /** | |
138 * Creates a new SimpleFeatureType using a SimpleFeatureTypeBuilder. | |
139 * | |
140 * @param name The name of the FeatureType. | |
141 * @param srs The SRS (e.g. "EPSG:31466"). | |
142 * @param geometryType The geometry type's class (e.g. Polygon.class). | |
143 * @param attrs Optional. An object with attribute-name/attribute-class | |
144 * pairs where index 0 specifies the name as string and index 1 the type | |
145 * as class. | |
146 * | |
147 * @return a new SimpleFeatureType. | |
148 */ | |
149 public static SimpleFeatureType buildFeatureType( | |
150 String name, String srs, Class geometryType, Object[][] attrs) | |
151 { | |
152 try { | |
153 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); | |
154 CoordinateReferenceSystem crs = CRS.decode(srs); | |
155 | |
156 builder.setName("flys"); | |
157 builder.setNamespaceURI("http://www.intevation.de/"); | |
158 builder.setCRS(crs); | |
159 builder.setSRS(srs); | |
160 | |
161 builder.add("geometry", geometryType, crs); | |
162 | |
163 if (attrs != null) { | |
164 for (Object[] attr: attrs) { | |
165 builder.add((String) attr[0], (Class) attr[1]); | |
166 } | |
167 } | |
168 | |
169 return builder.buildFeatureType(); | |
170 } | |
171 catch (NoSuchAuthorityCodeException nsae) { | |
172 } | |
173 catch (FactoryException fe) { | |
174 } | |
175 | |
176 return null; | |
177 } | |
178 | |
179 | |
180 public static List<SimpleFeature> parseGeoJSON( | |
181 String geojson, SimpleFeatureType ft | |
182 ) { | |
183 List<SimpleFeature> collection = new ArrayList<SimpleFeature>(); | |
184 | |
185 try { | |
186 FeatureJSON fjson = new FeatureJSON(); | |
187 fjson.setFeatureType(ft); | |
188 | |
189 FeatureIterator<SimpleFeature> iterator = | |
190 fjson.streamFeatureCollection(geojson); | |
191 | |
192 while (iterator.hasNext()) { | |
193 collection.add(iterator.next()); | |
194 } | |
195 } | |
196 catch (IOException ioe) { | |
197 // TODO handle exception | |
198 } | |
199 | |
200 return collection; | |
201 } | |
202 | |
203 | |
204 public static boolean writeShapefile( | |
205 File shape, | |
206 SimpleFeatureType featureType, | |
207 FeatureCollection collection | |
208 ) { | |
209 if (collection.isEmpty()) { | |
210 logger.warn("Shapefile is not written - no features given!"); | |
211 return false; | |
212 } | |
213 | |
214 Transaction transaction = null; | |
215 | |
216 try { | |
217 Map<String, Serializable> params = | |
218 new HashMap<String, Serializable>(); | |
219 | |
220 params.put("url", shape.toURI().toURL()); | |
221 params.put("create spatial index", Boolean.TRUE); | |
222 | |
223 DataStoreFactorySpi dataStoreFactory = | |
224 new ShapefileDataStoreFactory(); | |
225 | |
226 ShapefileDataStore newDataStore = | |
227 (ShapefileDataStore)dataStoreFactory.createNewDataStore(params); | |
228 newDataStore.createSchema(featureType); | |
229 | |
230 transaction = new DefaultTransaction("create"); | |
231 | |
232 String typeName = newDataStore.getTypeNames()[0]; | |
233 | |
234 FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = | |
235 (FeatureStore<SimpleFeatureType, SimpleFeature>) | |
236 newDataStore.getFeatureSource(typeName); | |
237 | |
238 featureStore.setTransaction(transaction); | |
239 | |
240 featureStore.addFeatures(collection); | |
241 transaction.commit(); | |
242 | |
243 return true; | |
244 } | |
245 catch (MalformedURLException mue) { | |
246 logger.error("Unable to prepare shapefile: " + mue.getMessage()); | |
247 } | |
248 catch (IOException ioe) { | |
249 logger.error("Unable to write shapefile: " + ioe.getMessage()); | |
250 } | |
251 finally { | |
252 if (transaction != null) { | |
253 try { | |
254 logger.debug("XXX CLOSE TRANSACTION!"); | |
255 transaction.close(); | |
256 } | |
257 catch (IOException ioe) { /* do nothing */ } | |
258 } | |
259 } | |
260 | |
261 return false; | |
262 } | |
263 } | |
264 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |