Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/GeometryUtils.java @ 3301:c8f670ae96e8
Create extent of a map layer in the correct coordinate reference system.
flys-artifacts/trunk@4986 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 13 Jul 2012 10:25:24 +0000 |
parents | f062b5a90e26 |
children | 96daa8a4c48f |
comparison
equal
deleted
inserted
replaced
3300:e1cf76b3ecb4 | 3301:c8f670ae96e8 |
---|---|
26 import org.geotools.feature.FeatureCollection; | 26 import org.geotools.feature.FeatureCollection; |
27 import org.geotools.feature.FeatureIterator; | 27 import org.geotools.feature.FeatureIterator; |
28 import org.geotools.feature.simple.SimpleFeatureTypeBuilder; | 28 import org.geotools.feature.simple.SimpleFeatureTypeBuilder; |
29 import org.geotools.geojson.feature.FeatureJSON; | 29 import org.geotools.geojson.feature.FeatureJSON; |
30 import org.geotools.geometry.jts.JTS; | 30 import org.geotools.geometry.jts.JTS; |
31 import org.geotools.geometry.jts.ReferencedEnvelope; | |
31 import org.geotools.referencing.CRS; | 32 import org.geotools.referencing.CRS; |
32 import org.opengis.feature.simple.SimpleFeature; | 33 import org.opengis.feature.simple.SimpleFeature; |
33 import org.opengis.feature.simple.SimpleFeatureType; | 34 import org.opengis.feature.simple.SimpleFeatureType; |
34 import org.opengis.referencing.FactoryException; | 35 import org.opengis.referencing.FactoryException; |
35 import org.opengis.referencing.NoSuchAuthorityCodeException; | 36 import org.opengis.referencing.NoSuchAuthorityCodeException; |
36 import org.opengis.referencing.crs.CoordinateReferenceSystem; | 37 import org.opengis.referencing.crs.CoordinateReferenceSystem; |
37 import org.opengis.referencing.operation.MathTransform; | 38 import org.opengis.referencing.operation.MathTransform; |
38 import org.opengis.referencing.operation.TransformException; | 39 import org.opengis.referencing.operation.TransformException; |
39 | 40 |
40 | |
41 public class GeometryUtils { | 41 public class GeometryUtils { |
42 | 42 |
43 private static final Logger logger = Logger.getLogger(GeometryUtils.class); | 43 private static final Logger logger = Logger.getLogger(GeometryUtils.class); |
44 | 44 |
45 public static final String PREFIX_EPSG = "EPSG:"; | |
46 | |
45 public static final String DEFAULT_EPSG = "EPSG:31467"; | 47 public static final String DEFAULT_EPSG = "EPSG:31467"; |
46 | 48 |
47 | |
48 private GeometryUtils() { | 49 private GeometryUtils() { |
49 } | 50 } |
50 | |
51 | 51 |
52 public static Envelope getRiverBoundary(String rivername) { | 52 public static Envelope getRiverBoundary(String rivername) { |
53 List<RiverAxis> axes = RiverAxis.getRiverAxis(rivername); | 53 List<RiverAxis> axes = RiverAxis.getRiverAxis(rivername); |
54 if (axes != null && axes.size() > 0) { | 54 if (axes != null && axes.size() > 0) { |
55 Envelope max = null; | 55 Envelope max = null; |
71 } | 71 } |
72 | 72 |
73 return null; | 73 return null; |
74 } | 74 } |
75 | 75 |
76 | |
77 public static String getRiverBounds(String rivername) { | 76 public static String getRiverBounds(String rivername) { |
78 Envelope env = getRiverBoundary(rivername); | 77 Envelope env = getRiverBoundary(rivername); |
79 | 78 |
80 if (env == null) { | 79 if (env == null) { |
81 return jtsBoundsToOLBounds(env); | 80 return jtsBoundsToOLBounds(env); |
82 } | 81 } |
83 | 82 |
84 return null; | 83 return null; |
85 } | 84 } |
86 | 85 |
87 | |
88 /** | 86 /** |
89 * Returns the boundary of Envelope <i>env</i> in OpenLayers | 87 * Returns the boundary of Envelope <i>env</i> in OpenLayers representation. |
90 * representation. | |
91 * | 88 * |
92 * @param env The envelope of a geometry. | 89 * @param env The envelope of a geometry. |
93 * | 90 * |
94 * @return the OpenLayers boundary of <i>env</i>. | 91 * @return the OpenLayers boundary of <i>env</i>. |
95 */ | 92 */ |
99 env.getMinY() + " " + | 96 env.getMinY() + " " + |
100 env.getMaxX() + " " + | 97 env.getMaxX() + " " + |
101 env.getMaxY(); | 98 env.getMaxY(); |
102 } | 99 } |
103 | 100 |
104 | |
105 public static String createOLBounds(Geometry a, Geometry b) { | 101 public static String createOLBounds(Geometry a, Geometry b) { |
106 Coordinate[] ca = a.getCoordinates(); | 102 Coordinate[] ca = a.getCoordinates(); |
107 Coordinate[] cb = b.getCoordinates(); | 103 Coordinate[] cb = b.getCoordinates(); |
108 | 104 |
109 double lowerX = Double.MAX_VALUE; | 105 double lowerX = Double.MAX_VALUE; |
128 } | 124 } |
129 | 125 |
130 return "" + lowerX + " " + lowerY + " " + upperX + " " + upperY; | 126 return "" + lowerX + " " + lowerY + " " + upperX + " " + upperY; |
131 } | 127 } |
132 | 128 |
133 | |
134 public static SimpleFeatureType buildFeatureType( | 129 public static SimpleFeatureType buildFeatureType( |
135 String name, String srs, Class<?> geometryType) | 130 String name, String srs, Class<?> geometryType) |
136 { | 131 { |
137 return buildFeatureType(name, srs, geometryType, null); | 132 return buildFeatureType(name, srs, geometryType, null); |
138 } | 133 } |
139 | 134 |
140 | |
141 /** | 135 /** |
142 * Creates a new SimpleFeatureType using a SimpleFeatureTypeBuilder. | 136 * Creates a new SimpleFeatureType using a SimpleFeatureTypeBuilder. |
143 * | 137 * |
144 * @param name The name of the FeatureType. | 138 * @param name The name of the FeatureType. |
145 * @param srs The SRS (e.g. "EPSG:31466"). | 139 * @param srs The SRS (e.g. "EPSG:31466"). |
146 * @param geometryType The geometry type's class (e.g. Polygon.class). | 140 * @param geometryType The geometry type's class (e.g. Polygon.class). |
147 * @param attrs Optional. An object with attribute-name/attribute-class | 141 * @param attrs Optional. An object with attribute-name/attribute-class pairs |
148 * pairs where index 0 specifies the name as string and index 1 the type | 142 * where index 0 specifies the name as string and index 1 the |
149 * as class. | 143 * ype as class. |
150 * | 144 * |
151 * @return a new SimpleFeatureType. | 145 * @return a new SimpleFeatureType. |
152 */ | 146 */ |
153 public static SimpleFeatureType buildFeatureType( | 147 public static SimpleFeatureType buildFeatureType(String name, String srs, |
154 String name, String srs, Class<?> geometryType, Object[][] attrs) | 148 Class<?> geometryType, Object[][] attrs) { |
155 { | |
156 try { | 149 try { |
157 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); | 150 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); |
158 CoordinateReferenceSystem crs = CRS.decode(srs); | 151 CoordinateReferenceSystem crs = CRS.decode(srs); |
159 | 152 |
160 builder.setName("flys"); | 153 builder.setName("flys"); |
178 } | 171 } |
179 | 172 |
180 return null; | 173 return null; |
181 } | 174 } |
182 | 175 |
183 | |
184 public static List<SimpleFeature> parseGeoJSON( | 176 public static List<SimpleFeature> parseGeoJSON( |
185 String geojson, SimpleFeatureType ft | 177 String geojson, SimpleFeatureType ft |
186 ) { | 178 ) { |
187 List<SimpleFeature> collection = new ArrayList<SimpleFeature>(); | 179 List<SimpleFeature> collection = new ArrayList<SimpleFeature>(); |
188 | 180 |
203 | 195 |
204 return collection; | 196 return collection; |
205 } | 197 } |
206 | 198 |
207 | 199 |
208 public static boolean writeShapefile( | 200 /** |
209 File shape, | 201 * This method returns the {@link CoordinateReferenceSystem} by the |
210 SimpleFeatureType featureType, | 202 * {@link String} <i>epsg</i>. |
211 FeatureCollection collection | 203 * |
204 * @param epsg An EPSG code like <b>EPSG:31466</b> | |
205 * | |
206 * @return the {@link CoordinateReferenceSystem} specified by <i>epsg</i>. | |
207 */ | |
208 public static CoordinateReferenceSystem getCoordinateReferenceSystem( | |
209 String epsg | |
212 ) { | 210 ) { |
213 return writeShapefile( | 211 if (epsg == null) { |
214 shape, featureType, collection, featureType.getCoordinateReferenceSystem()); | 212 logger.warn("cannot create CoordinateReferenceSystem with null"); |
215 } | 213 return null; |
216 | 214 } |
217 | 215 |
218 public static boolean writeShapefile( | 216 if (!epsg.startsWith(PREFIX_EPSG)) { |
219 File shape, | 217 epsg = PREFIX_EPSG + epsg; |
220 SimpleFeatureType featureType, | 218 } |
221 FeatureCollection collection, | 219 |
222 CoordinateReferenceSystem crs | 220 try { |
221 return CRS.decode(epsg); | |
222 } | |
223 catch (FactoryException fe) { | |
224 logger.error( | |
225 "unable to get CoordinateReferenceSystem for: " + epsg, | |
226 fe); | |
227 } | |
228 | |
229 return null; | |
230 } | |
231 | |
232 | |
233 public static Envelope transform(Envelope orig, String targetSrs) { | |
234 return transform(orig, targetSrs, DEFAULT_EPSG); | |
235 } | |
236 | |
237 | |
238 public static Envelope transform( | |
239 Envelope orig, | |
240 String targetSrs, | |
241 String origSrs | |
223 ) { | 242 ) { |
243 if (targetSrs == null || orig == null || origSrs == null) { | |
244 logger.warn("unable to transform envelope: empty parameters"); | |
245 return orig; | |
246 } | |
247 | |
248 logger.debug("Transform envlope to '" + targetSrs + "'"); | |
249 try { | |
250 CoordinateReferenceSystem sourceCRS = | |
251 getCoordinateReferenceSystem(origSrs); | |
252 | |
253 CoordinateReferenceSystem targetCRS = | |
254 getCoordinateReferenceSystem(targetSrs); | |
255 | |
256 if (sourceCRS != null && targetCRS != null) { | |
257 ReferencedEnvelope tmpEnv = | |
258 new ReferencedEnvelope(orig, CRS.decode(origSrs)); | |
259 | |
260 Envelope target = tmpEnv.transform(targetCRS, false); | |
261 | |
262 if (logger.isDebugEnabled()) { | |
263 logger.debug(" orig envelope : " + orig); | |
264 logger.debug(" transformed envelope: " + target); | |
265 } | |
266 | |
267 return target; | |
268 } | |
269 } | |
270 catch (NoSuchAuthorityCodeException nsae) { | |
271 logger.error("Cannot get CoordinateReferenceSystem!", nsae); | |
272 } | |
273 catch (FactoryException fe) { | |
274 logger.error("Cannot get CoordinateReferenceSystem!", fe); | |
275 } | |
276 catch (TransformException te) { | |
277 logger.error("Cannot transform envelope from source " | |
278 + origSrs + " to target srs " + targetSrs); | |
279 } | |
280 | |
281 return null; | |
282 } | |
283 | |
284 | |
285 public static boolean writeShapefile(File shape, | |
286 SimpleFeatureType featureType, FeatureCollection collection) { | |
287 return writeShapefile(shape, featureType, collection, | |
288 featureType.getCoordinateReferenceSystem()); | |
289 } | |
290 | |
291 | |
292 public static boolean writeShapefile(File shape, | |
293 SimpleFeatureType featureType, FeatureCollection collection, | |
294 CoordinateReferenceSystem crs) { | |
224 if (collection.isEmpty()) { | 295 if (collection.isEmpty()) { |
225 logger.warn("Shapefile is not written - no features given!"); | 296 logger.warn("Shapefile is not written - no features given!"); |
226 return false; | 297 return false; |
227 } | 298 } |
228 | 299 |