Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/FloodMapState.java @ 1123:be9bb260b835
Set the Z values of line and polygon barrier geometries used for WSPLGEN.
flys-artifacts/trunk@2632 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 01 Sep 2011 12:23:23 +0000 |
parents | 66783d957201 |
children | 28a595b32980 |
comparison
equal
deleted
inserted
replaced
1122:111794adf285 | 1123:be9bb260b835 |
---|---|
2 | 2 |
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.FileNotFoundException; | 4 import java.io.FileNotFoundException; |
5 import java.io.FileOutputStream; | 5 import java.io.FileOutputStream; |
6 import java.io.IOException; | 6 import java.io.IOException; |
7 import java.util.ArrayList; | |
7 import java.util.List; | 8 import java.util.List; |
8 | 9 |
9 import javax.xml.xpath.XPathConstants; | 10 import javax.xml.xpath.XPathConstants; |
10 | 11 |
12 import com.vividsolutions.jts.geom.Coordinate; | |
11 import com.vividsolutions.jts.geom.Geometry; | 13 import com.vividsolutions.jts.geom.Geometry; |
12 import com.vividsolutions.jts.geom.LineString; | 14 import com.vividsolutions.jts.geom.LineString; |
13 import com.vividsolutions.jts.geom.MultiPolygon; | 15 import com.vividsolutions.jts.geom.MultiPolygon; |
14 import com.vividsolutions.jts.geom.Polygon; | 16 import com.vividsolutions.jts.geom.Polygon; |
15 | 17 |
286 protected void setLine(FLYSArtifact artifact, File dir, WSPLGENJob job) { | 288 protected void setLine(FLYSArtifact artifact, File dir, WSPLGENJob job) { |
287 String geoJSON = artifact.getDataAsString("uesk.barriers"); | 289 String geoJSON = artifact.getDataAsString("uesk.barriers"); |
288 String srid = FLYSUtils.getRiverSrid(artifact); | 290 String srid = FLYSUtils.getRiverSrid(artifact); |
289 String srs = "EPSG:" + srid; | 291 String srs = "EPSG:" + srid; |
290 | 292 |
291 List<SimpleFeature> features = getBarriersFeatures(geoJSON, srs); | 293 SimpleFeatureType ft = getBarriersFeatureType( |
294 "barriers", srs, Geometry.class); | |
295 | |
296 List<SimpleFeature> features = GeometryUtils.parseGeoJSON(geoJSON, ft); | |
292 if (features == null || features.size() == 0) { | 297 if (features == null || features.size() == 0) { |
293 logger.debug("No barrier features extracted."); | 298 logger.debug("No barrier features extracted."); |
294 return; | 299 return; |
295 } | 300 } |
296 | 301 |
303 shapeLines, | 308 shapeLines, |
304 GeometryUtils.buildFeatureType("lines", srs, LineString.class), | 309 GeometryUtils.buildFeatureType("lines", srs, LineString.class), |
305 fcs[0]); | 310 fcs[0]); |
306 | 311 |
307 if (l) { | 312 if (l) { |
313 logger.debug( | |
314 "Successfully created barrier line shapefile. " + | |
315 "Write shapefile path into WSPLGEN job."); | |
308 job.addLin(shapeLines.getAbsolutePath()); | 316 job.addLin(shapeLines.getAbsolutePath()); |
309 } | 317 } |
310 | 318 |
311 boolean p = GeometryUtils.writeShapefile( | 319 boolean p = GeometryUtils.writeShapefile( |
312 shapePolys, | 320 shapePolys, |
313 GeometryUtils.buildFeatureType("polygons", srs, Polygon.class), | 321 GeometryUtils.buildFeatureType("polygons", srs, Polygon.class), |
314 fcs[1]); | 322 fcs[1]); |
315 | 323 |
316 if (p) { | 324 if (p) { |
325 logger.debug( | |
326 "Successfully created barrier polygon shapefile. " + | |
327 "Write shapefile path into WSPLGEN job."); | |
317 job.addLin(shapePolys.getAbsolutePath()); | 328 job.addLin(shapePolys.getAbsolutePath()); |
318 } | 329 } |
319 } | 330 } |
320 | 331 |
321 | 332 |
322 protected List<SimpleFeature> getBarriersFeatures(String json, String srs) { | 333 protected SimpleFeatureType getBarriersFeatureType( |
323 SimpleFeatureType ft = GeometryUtils.buildFeatureType( | 334 String name, |
324 "barriers", srs,Geometry.class); | 335 String srs, |
325 | 336 Class type |
326 return GeometryUtils.parseGeoJSON(json, ft); | 337 ) { |
338 Object[][] attrs = new Object[2][]; | |
339 attrs[0] = new Object[] { "typ", String.class }; | |
340 attrs[1] = new Object[] { "elevation", Double.class }; | |
341 | |
342 return GeometryUtils.buildFeatureType(name, srs, type, attrs); | |
327 } | 343 } |
328 | 344 |
329 | 345 |
330 protected FeatureCollection[] splitLinesAndPolygons(List<SimpleFeature> f) { | 346 protected FeatureCollection[] splitLinesAndPolygons(List<SimpleFeature> f) { |
331 FeatureCollection lines = FeatureCollections.newCollection(); | 347 FeatureCollection lines = FeatureCollections.newCollection(); |
332 FeatureCollection polygons = FeatureCollections.newCollection(); | 348 FeatureCollection polygons = FeatureCollections.newCollection(); |
333 | 349 |
334 for (SimpleFeature feature: f) { | 350 for (SimpleFeature feature: f) { |
335 Geometry geom = (Geometry) feature.getDefaultGeometry(); | 351 Geometry geom = (Geometry) feature.getDefaultGeometry(); |
336 | 352 |
353 | |
337 if (geom instanceof LineString) { | 354 if (geom instanceof LineString) { |
355 geom = applyElevationAttribute(feature, (LineString) geom); | |
338 lines.add(feature); | 356 lines.add(feature); |
339 } | 357 } |
340 else if (geom instanceof Polygon) { | 358 else if (geom instanceof Polygon) { |
359 geom = applyElevationAttribute(feature, (Polygon) geom); | |
341 polygons.add(feature); | 360 polygons.add(feature); |
342 } | 361 } |
343 else { | 362 else { |
344 logger.warn("Feature not supported: " + geom.getClass()); | 363 logger.warn("Feature not supported: " + geom.getClass()); |
345 } | 364 } |
347 | 366 |
348 logger.debug("Found " + lines.size() + " barrier lines."); | 367 logger.debug("Found " + lines.size() + " barrier lines."); |
349 logger.debug("Found " + polygons.size() + " barrier polygons."); | 368 logger.debug("Found " + polygons.size() + " barrier polygons."); |
350 | 369 |
351 return new FeatureCollection[] { lines, polygons }; | 370 return new FeatureCollection[] { lines, polygons }; |
371 } | |
372 | |
373 | |
374 protected static Geometry applyElevationAttribute( | |
375 SimpleFeature feature, | |
376 Geometry geom | |
377 ) { | |
378 logger.debug("Apply elevations for: " + geom.getClass()); | |
379 | |
380 List<Double> elevations = extractElevations(feature); | |
381 int numPoints = geom.getNumPoints(); | |
382 int numElevation = elevations.size(); | |
383 | |
384 String typ = (String) feature.getAttribute("typ"); | |
385 | |
386 if (numPoints > numElevation) { | |
387 logger.warn("More vertices in Geometry than elevations given."); | |
388 } | |
389 | |
390 Coordinate[] c = geom.getCoordinates(); | |
391 for (int i = 0; i < numPoints; i++) { | |
392 if (i < numElevation) { | |
393 c[i].z = elevations.get(i); | |
394 } | |
395 else if (typ != null && typ.equals("Graben")) { | |
396 c[i].z = -9999d; | |
397 } | |
398 else { | |
399 c[i].z = 9999d; | |
400 } | |
401 } | |
402 | |
403 return geom; | |
404 } | |
405 | |
406 | |
407 protected static List<Double> extractElevations(SimpleFeature feature) { | |
408 String tmp = (String) feature.getAttribute("elevation"); | |
409 String typ = (String) feature.getAttribute("typ"); | |
410 | |
411 String[] elevations = tmp == null ? null : tmp.split(" "); | |
412 | |
413 int num = elevations != null ? elevations.length : 0; | |
414 | |
415 List<Double> list = new ArrayList<Double>(num); | |
416 | |
417 for (int i = 0; i < num; i++) { | |
418 try { | |
419 list.add(Double.parseDouble(elevations[i])); | |
420 } | |
421 catch (NumberFormatException nfe) { | |
422 logger.warn("Error while parsing elevation at pos: " + i); | |
423 if (typ != null && typ.equals("Graben")) { | |
424 list.add(new Double(-9999.0)); | |
425 } | |
426 else { | |
427 list.add(new Double(9999.0)); | |
428 } | |
429 } | |
430 } | |
431 | |
432 return list; | |
352 } | 433 } |
353 | 434 |
354 | 435 |
355 protected void setAxis(FLYSArtifact artifact, File dir, WSPLGENJob job) { | 436 protected void setAxis(FLYSArtifact artifact, File dir, WSPLGENJob job) { |
356 String river = artifact.getDataAsString("river"); | 437 String river = artifact.getDataAsString("river"); |