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