comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java @ 799:feeaf5aec552

ISSUE213: Wrong Geometrytype used for the generation of an Layer with Multipolygon-Geometries gnv-artifacts/trunk@881 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 06 Apr 2010 11:56:53 +0000
parents c4156275c1e1
children feae2f9d6c6f
comparison
equal deleted inserted replaced
798:6cff63d0c434 799:feeaf5aec552
1 package de.intevation.gnv.state.layer; 1 package de.intevation.gnv.state.layer;
2 2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.OutputStream;
6 import java.util.Collection;
7 import java.util.Iterator;
8
9 import org.apache.log4j.Logger;
10 import org.w3c.dom.Document;
11 import org.w3c.dom.Element;
12 import org.w3c.dom.Node;
13
3 import com.vividsolutions.jts.geom.Geometry; 14 import com.vividsolutions.jts.geom.Geometry;
4
5 import com.vividsolutions.jts.io.ParseException; 15 import com.vividsolutions.jts.io.ParseException;
6 import com.vividsolutions.jts.io.WKTReader; 16 import com.vividsolutions.jts.io.WKTReader;
7 17
8 import de.intevation.artifactdatabase.Config; 18 import de.intevation.artifactdatabase.Config;
9 import de.intevation.artifactdatabase.XMLUtils; 19 import de.intevation.artifactdatabase.XMLUtils;
10
11 import de.intevation.artifacts.ArtifactNamespaceContext; 20 import de.intevation.artifacts.ArtifactNamespaceContext;
12 import de.intevation.artifacts.CallContext; 21 import de.intevation.artifacts.CallContext;
13
14 import de.intevation.gnv.artifacts.context.GNVArtifactContext; 22 import de.intevation.gnv.artifacts.context.GNVArtifactContext;
15
16 import de.intevation.gnv.geobackend.base.Result; 23 import de.intevation.gnv.geobackend.base.Result;
17
18 import de.intevation.gnv.geobackend.base.query.QueryExecutor; 24 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
19 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; 25 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
20
21 import de.intevation.gnv.geobackend.base.query.exception.QueryException; 26 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
22
23 import de.intevation.gnv.state.InputData; 27 import de.intevation.gnv.state.InputData;
24 import de.intevation.gnv.state.OutputStateBase; 28 import de.intevation.gnv.state.OutputStateBase;
25
26 import de.intevation.gnv.state.exception.StateException; 29 import de.intevation.gnv.state.exception.StateException;
27
28 import de.intevation.gnv.utils.ArtifactXMLUtilities; 30 import de.intevation.gnv.utils.ArtifactXMLUtilities;
29 import de.intevation.gnv.utils.FileUtils; 31 import de.intevation.gnv.utils.FileUtils;
30 import de.intevation.gnv.utils.MapfileGenerator; 32 import de.intevation.gnv.utils.MapfileGenerator;
31 import de.intevation.gnv.utils.MetaWriter; 33 import de.intevation.gnv.utils.MetaWriter;
32 import de.intevation.gnv.utils.ShapeFileWriter; 34 import de.intevation.gnv.utils.ShapeFileWriter;
33 35
34 import java.io.File;
35 import java.io.IOException;
36 import java.io.OutputStream;
37
38 import java.util.Collection;
39 import java.util.Iterator;
40
41 import org.apache.log4j.Logger;
42
43 import org.w3c.dom.Document;
44 import org.w3c.dom.Element;
45 import org.w3c.dom.Node;
46
47 /** 36 /**
48 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a> 37 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
49 * 38 *
50 */ 39 */
51 public class LayerOutputState extends OutputStateBase { 40 public class LayerOutputState extends OutputStateBase {
74 */ 63 */
75 private String geometryQueryID = null; 64 private String geometryQueryID = null;
76 65
77 private String columnQueryID = null; 66 private String columnQueryID = null;
78 67
68 private String geometryTypeQueryID = null;
69
79 /** 70 /**
80 * The ID for the Value which will hold the Geometrie-Value 71 * The ID for the Value which will hold the Geometrie-Value
81 */ 72 */
82 private String geometryID = null; 73 private String geometryID = null;
83 74
84 private Boolean shapeFileLock = new Boolean(true); 75 private Boolean shapeFileLock = new Boolean(true);
85 76
86 private String shapeFilePath; 77 private String shapeFilePath;
87 78
79 private String templateID = null;
80
88 private String geometryType = null; 81 private String geometryType = null;
89
90 private String templateID = null;
91 82
92 public static final String SHAPEFILE_NAME = "data.shp"; 83 public static final String SHAPEFILE_NAME = "data.shp";
93 84
94 /** 85 /**
95 * Constructor 86 * Constructor
111 String outputMode = XMLUtils.xpathString( 102 String outputMode = XMLUtils.xpathString(
112 format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE); 103 format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE);
113 if (outputMode.equalsIgnoreCase("wms")) { 104 if (outputMode.equalsIgnoreCase("wms")) {
114 Collection<Result> data = this.fetchData(); 105 Collection<Result> data = this.fetchData();
115 if (data != null && !data.isEmpty()){ 106 if (data != null && !data.isEmpty()){
116 XMLUtils.toStream(this.getWMS(uuid, callContext, data), 107 XMLUtils.toStream(this.getWMS(uuid, callContext,
108 data, geometryType),
117 outputStream); 109 outputStream);
118 }else{ 110 }else{
119 this.writeExceptionReport2Stream(outputStream); 111 this.writeExceptionReport2Stream(outputStream);
120 } 112 }
121 }else if (outputMode.equalsIgnoreCase("zip")){ 113 }else if (outputMode.equalsIgnoreCase("zip")){
122 Collection<Result> data = this.fetchData(); 114 Collection<Result> data = this.fetchData();
123 if (data != null && !data.isEmpty()){ 115 if (data != null && !data.isEmpty()){
124 this.writeZip(uuid, callContext, outputStream, data); 116 this.writeZip(uuid, callContext, outputStream,
117 data,geometryType);
125 }else{ 118 }else{
126 this.writeExceptionReport2Stream(outputStream); 119 this.writeExceptionReport2Stream(outputStream);
127 } 120 }
128 121
129 } 122 }
156 Iterator<Result> it = result.iterator(); 149 Iterator<Result> it = result.iterator();
157 String[] queryValues = null; 150 String[] queryValues = null;
158 if (it.hasNext()){ 151 if (it.hasNext()){
159 Result resultValue = it.next(); 152 Result resultValue = it.next();
160 String table = resultValue.getString(0); 153 String table = resultValue.getString(0);
154
155 this.geometryType = this.getGeometryType(table, queryExecutor);
156
161 String where = resultValue.getString(1); 157 String where = resultValue.getString(1);
162 String columns = this.fetchColumns(table); 158 String columns = this.fetchColumns(table);
163 159
164 templateID = resultValue.getString(2); 160 templateID = resultValue.getString(2);
165 if (this.geometryID != null){ 161 if (this.geometryID != null){
248 } 244 }
249 } 245 }
250 return data; 246 return data;
251 } 247 }
252 248
249 private String getGeometryType(String tableName,
250 QueryExecutor queryExecutor){
251 String returnValue = null;
252 String[] tables = tableName.toUpperCase().split(",");
253 String[] filter = tables[0].split("\\.");
254
255 try {
256 Collection<Result> result =
257 queryExecutor.executeQuery(this.geometryTypeQueryID, filter);
258 if (result != null && !result.isEmpty())
259 {
260 int geometryCode = result.iterator().next().getInteger(0);
261 if (geometryCode == 11 ||
262 geometryCode == 10){
263 returnValue = "MultiPolygon";
264 }else if (geometryCode == 9 ||
265 geometryCode == 8){
266 returnValue = "MultiLineString";
267 }else if (geometryCode == 7){
268 returnValue = "MultiPoint";
269 }else if (geometryCode == 6){
270 returnValue = "GeometryCollection";
271 }else if (geometryCode == 5 ||
272 geometryCode == 4){
273 returnValue = "Polygon";
274 }else if (geometryCode == 3 ||
275 geometryCode == 2){
276 returnValue = "LineString";
277 }else if (geometryCode == 1){
278 returnValue = "Point";
279 }else if (geometryCode == 0){
280 returnValue = "Geometry";
281 }
282 }
283 } catch (QueryException e) {
284 log.error(e,e);
285 }
286
287 return returnValue;
288 }
253 private String fetchColumns(String tableName){ 289 private String fetchColumns(String tableName){
254 String returnValue = null; 290 String returnValue = null;
255 try { 291 try {
256 String[] tables = tableName.toUpperCase().split(","); 292 String[] tables = tableName.toUpperCase().split(",");
257 String[] filter = tables[0].split("\\."); 293 String[] filter = tables[0].split("\\.");
293 this.geometryQueryID = Config.getStringXPath(configuration, 329 this.geometryQueryID = Config.getStringXPath(configuration,
294 "queryID-geometry"); 330 "queryID-geometry");
295 331
296 this.columnQueryID = "layer_colums"; //Config.getStringXPath(configuration, 332 this.columnQueryID = "layer_colums"; //Config.getStringXPath(configuration,
297 // "queryID-columns"); 333 // "queryID-columns");
334 this.geometryTypeQueryID = "geometry_type";
298 } 335 }
299 336
300 protected String writeToShapeFile( 337 protected String writeToShapeFile(
301 String uuid, 338 String uuid,
302 Collection<Result> data, 339 Collection<Result> data,
303 CallContext callContext 340 CallContext callContext,
341 String geometryType
304 ) { 342 ) {
305 File baseDir = shapefileDirectory(callContext); 343 File baseDir = shapefileDirectory(callContext);
306 344
307 File shapeDir = new File(baseDir, uuid); 345 File shapeDir = new File(baseDir, uuid);
308 boolean success = false; 346 boolean success = false;
323 } 361 }
324 createdDir = true; 362 createdDir = true;
325 } 363 }
326 364
327 File shapeFile = new File(shapeDir, SHAPEFILE_NAME); 365 File shapeFile = new File(shapeDir, SHAPEFILE_NAME);
328 if ((geometryType = ShapeFileWriter.writeDataToFile(shapeFile, "data", data)) == null){ 366 if (!ShapeFileWriter.writeDataToFile(shapeFile, "data", data,geometryType)){
329 log.error("writing data into shapefile failed"); 367 log.error("writing data into shapefile failed");
330 return null; 368 return null;
331 } 369 }
332 370
333 shapeFilePath = shapeDir.getAbsolutePath(); 371 shapeFilePath = shapeDir.getAbsolutePath();
346 384
347 protected void writeZip( 385 protected void writeZip(
348 String uuid, 386 String uuid,
349 CallContext callContext, 387 CallContext callContext,
350 OutputStream output, 388 OutputStream output,
351 Collection<Result> data 389 Collection<Result> data,
390 String geometryType
352 ) 391 )
353 throws StateException 392 throws StateException
354 { 393 {
355 try { 394 try {
356 String p = getShapeFilePath(); 395 String p = getShapeFilePath();
360 FileUtils.createZipArchive(dir, output); 399 FileUtils.createZipArchive(dir, output);
361 } 400 }
362 } 401 }
363 else { 402 else {
364 403
365 if ((p = writeToShapeFile(uuid, data, callContext)) != null) { 404 if ((p = writeToShapeFile(uuid, data, callContext,geometryType)) != null) {
366 FileUtils.createZipArchive(new File(p), output); 405 FileUtils.createZipArchive(new File(p), output);
367 } 406 }
368 } 407 }
369 } 408 }
370 catch (IOException ioe) { 409 catch (IOException ioe) {
429 468
430 public String resetShapeFilePath() { 469 public String resetShapeFilePath() {
431 synchronized (shapeFileLock) { 470 synchronized (shapeFileLock) {
432 String path = shapeFilePath; 471 String path = shapeFilePath;
433 shapeFilePath = null; 472 shapeFilePath = null;
434 geometryType = null;
435 templateID = null; 473 templateID = null;
436 return path; 474 return path;
437 } 475 }
438 } 476 }
439 protected Document getWMS(String uuid, 477 protected Document getWMS(String uuid,
440 CallContext callContext, 478 CallContext callContext,
441 Collection<Result> data) 479 Collection<Result> data,
480 String geometryType)
442 throws StateException 481 throws StateException
443 { 482 {
444 // TODO: Do the real WMS publishing here! 483 // TODO: Do the real WMS publishing here!
445 Document document = XMLUtils.newDocument(); 484 Document document = XMLUtils.newDocument();
446 485
453 pathElement.setTextContent(path); 492 pathElement.setTextContent(path);
454 } 493 }
455 else { 494 else {
456 495
457 if (data != null && 496 if (data != null &&
458 (path = writeToShapeFile(uuid, data, callContext)) != null) { 497 (path = writeToShapeFile(uuid, data, callContext,geometryType)) != null) {
459 498
460 String paramType = LAYER_MODEL+"_"+templateID; 499 String paramType = LAYER_MODEL+"_"+templateID;
461 500
462 if (!MapfileGenerator.getInstance().templateExists(paramType)){ 501 if (!MapfileGenerator.getInstance().templateExists(paramType)){
463 // If the template doesn't exist the Defaulttemplates will be used. 502 // If the template doesn't exist the Defaulttemplates will be used.
464 paramType = LAYER_MODEL+"_"+this.geometryType.toLowerCase(); 503 paramType = LAYER_MODEL+"_"+
504 this.determineDefaultTemplateName(geometryType);
465 } 505 }
466 Document meta = MetaWriter.writeLayerMeta(callContext, uuid, 506 Document meta = MetaWriter.writeLayerMeta(callContext, uuid,
467 path, paramType, 507 path, paramType,
468 this.determineGeometryType()); 508 this.determineGeometryType(geometryType));
469 if (meta != null) { 509 if (meta != null) {
470 MapfileGenerator.getInstance().update(); 510 MapfileGenerator.getInstance().update();
471 return meta; 511 return meta;
472 } 512 }
473 513
476 } 516 }
477 517
478 return document; 518 return document;
479 } 519 }
480 520
481 private String determineGeometryType(){ 521 private String determineGeometryType(String geometryType){
482 522
483 String returnValue = this.geometryType.toLowerCase(); 523 String returnValue = geometryType.toLowerCase();
484 524
485 if (returnValue.equalsIgnoreCase("linestring")){ 525 if (returnValue.equalsIgnoreCase("linestring")){
486 returnValue = "Line"; 526 returnValue = "Line";
527 }else if (returnValue.equalsIgnoreCase("multilinestring")){
528 returnValue ="Line";
529 }else if (returnValue.equalsIgnoreCase("multipolygon")){
530 returnValue = "Polygon";
487 } 531 }
488 return returnValue; 532 return returnValue;
489 } 533 }
534
535 private String determineDefaultTemplateName(String geometryType){
536
537 String returnValue = geometryType.toLowerCase();
538
539 if (returnValue.equalsIgnoreCase("multilinestring")){
540 returnValue ="linestring";
541 }else if (returnValue.equalsIgnoreCase("multipolygon")){
542 returnValue = "polygon";
543 }else if (returnValue.equalsIgnoreCase("multipoint")){
544 returnValue = "point";
545 }
546 return returnValue;
547 }
490 } 548 }

http://dive4elements.wald.intevation.org