Mercurial > dive4elements > gnv-client
diff 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 |
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java Tue Apr 06 11:05:00 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java Tue Apr 06 11:56:53 2010 +0000 @@ -1,49 +1,38 @@ package de.intevation.gnv.state.layer; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Collection; +import java.util.Iterator; + +import org.apache.log4j.Logger; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + import com.vividsolutions.jts.geom.Geometry; - import com.vividsolutions.jts.io.ParseException; import com.vividsolutions.jts.io.WKTReader; import de.intevation.artifactdatabase.Config; import de.intevation.artifactdatabase.XMLUtils; - import de.intevation.artifacts.ArtifactNamespaceContext; import de.intevation.artifacts.CallContext; - import de.intevation.gnv.artifacts.context.GNVArtifactContext; - import de.intevation.gnv.geobackend.base.Result; - import de.intevation.gnv.geobackend.base.query.QueryExecutor; import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; - import de.intevation.gnv.geobackend.base.query.exception.QueryException; - import de.intevation.gnv.state.InputData; import de.intevation.gnv.state.OutputStateBase; - import de.intevation.gnv.state.exception.StateException; - import de.intevation.gnv.utils.ArtifactXMLUtilities; import de.intevation.gnv.utils.FileUtils; import de.intevation.gnv.utils.MapfileGenerator; import de.intevation.gnv.utils.MetaWriter; import de.intevation.gnv.utils.ShapeFileWriter; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; - -import java.util.Collection; -import java.util.Iterator; - -import org.apache.log4j.Logger; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; - /** * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a> * @@ -76,6 +65,8 @@ private String columnQueryID = null; + private String geometryTypeQueryID = null; + /** * The ID for the Value which will hold the Geometrie-Value */ @@ -85,10 +76,10 @@ private String shapeFilePath; + private String templateID = null; + private String geometryType = null; - private String templateID = null; - public static final String SHAPEFILE_NAME = "data.shp"; /** @@ -113,7 +104,8 @@ if (outputMode.equalsIgnoreCase("wms")) { Collection<Result> data = this.fetchData(); if (data != null && !data.isEmpty()){ - XMLUtils.toStream(this.getWMS(uuid, callContext, data), + XMLUtils.toStream(this.getWMS(uuid, callContext, + data, geometryType), outputStream); }else{ this.writeExceptionReport2Stream(outputStream); @@ -121,7 +113,8 @@ }else if (outputMode.equalsIgnoreCase("zip")){ Collection<Result> data = this.fetchData(); if (data != null && !data.isEmpty()){ - this.writeZip(uuid, callContext, outputStream, data); + this.writeZip(uuid, callContext, outputStream, + data,geometryType); }else{ this.writeExceptionReport2Stream(outputStream); } @@ -158,6 +151,9 @@ if (it.hasNext()){ Result resultValue = it.next(); String table = resultValue.getString(0); + + this.geometryType = this.getGeometryType(table, queryExecutor); + String where = resultValue.getString(1); String columns = this.fetchColumns(table); @@ -250,6 +246,46 @@ return data; } + private String getGeometryType(String tableName, + QueryExecutor queryExecutor){ + String returnValue = null; + String[] tables = tableName.toUpperCase().split(","); + String[] filter = tables[0].split("\\."); + + try { + Collection<Result> result = + queryExecutor.executeQuery(this.geometryTypeQueryID, filter); + if (result != null && !result.isEmpty()) + { + int geometryCode = result.iterator().next().getInteger(0); + if (geometryCode == 11 || + geometryCode == 10){ + returnValue = "MultiPolygon"; + }else if (geometryCode == 9 || + geometryCode == 8){ + returnValue = "MultiLineString"; + }else if (geometryCode == 7){ + returnValue = "MultiPoint"; + }else if (geometryCode == 6){ + returnValue = "GeometryCollection"; + }else if (geometryCode == 5 || + geometryCode == 4){ + returnValue = "Polygon"; + }else if (geometryCode == 3 || + geometryCode == 2){ + returnValue = "LineString"; + }else if (geometryCode == 1){ + returnValue = "Point"; + }else if (geometryCode == 0){ + returnValue = "Geometry"; + } + } + } catch (QueryException e) { + log.error(e,e); + } + + return returnValue; + } private String fetchColumns(String tableName){ String returnValue = null; try { @@ -295,12 +331,14 @@ this.columnQueryID = "layer_colums"; //Config.getStringXPath(configuration, // "queryID-columns"); + this.geometryTypeQueryID = "geometry_type"; } protected String writeToShapeFile( String uuid, Collection<Result> data, - CallContext callContext + CallContext callContext, + String geometryType ) { File baseDir = shapefileDirectory(callContext); @@ -325,7 +363,7 @@ } File shapeFile = new File(shapeDir, SHAPEFILE_NAME); - if ((geometryType = ShapeFileWriter.writeDataToFile(shapeFile, "data", data)) == null){ + if (!ShapeFileWriter.writeDataToFile(shapeFile, "data", data,geometryType)){ log.error("writing data into shapefile failed"); return null; } @@ -348,7 +386,8 @@ String uuid, CallContext callContext, OutputStream output, - Collection<Result> data + Collection<Result> data, + String geometryType ) throws StateException { @@ -362,7 +401,7 @@ } else { - if ((p = writeToShapeFile(uuid, data, callContext)) != null) { + if ((p = writeToShapeFile(uuid, data, callContext,geometryType)) != null) { FileUtils.createZipArchive(new File(p), output); } } @@ -431,14 +470,14 @@ synchronized (shapeFileLock) { String path = shapeFilePath; shapeFilePath = null; - geometryType = null; templateID = null; return path; } } protected Document getWMS(String uuid, CallContext callContext, - Collection<Result> data) + Collection<Result> data, + String geometryType) throws StateException { // TODO: Do the real WMS publishing here! @@ -455,17 +494,18 @@ else { if (data != null && - (path = writeToShapeFile(uuid, data, callContext)) != null) { + (path = writeToShapeFile(uuid, data, callContext,geometryType)) != null) { String paramType = LAYER_MODEL+"_"+templateID; if (!MapfileGenerator.getInstance().templateExists(paramType)){ // If the template doesn't exist the Defaulttemplates will be used. - paramType = LAYER_MODEL+"_"+this.geometryType.toLowerCase(); + paramType = LAYER_MODEL+"_"+ + this.determineDefaultTemplateName(geometryType); } Document meta = MetaWriter.writeLayerMeta(callContext, uuid, path, paramType, - this.determineGeometryType()); + this.determineGeometryType(geometryType)); if (meta != null) { MapfileGenerator.getInstance().update(); return meta; @@ -478,12 +518,30 @@ return document; } - private String determineGeometryType(){ + private String determineGeometryType(String geometryType){ - String returnValue = this.geometryType.toLowerCase(); + String returnValue = geometryType.toLowerCase(); if (returnValue.equalsIgnoreCase("linestring")){ returnValue = "Line"; + }else if (returnValue.equalsIgnoreCase("multilinestring")){ + returnValue ="Line"; + }else if (returnValue.equalsIgnoreCase("multipolygon")){ + returnValue = "Polygon"; + } + return returnValue; + } + + private String determineDefaultTemplateName(String geometryType){ + + String returnValue = geometryType.toLowerCase(); + + if (returnValue.equalsIgnoreCase("multilinestring")){ + returnValue ="linestring"; + }else if (returnValue.equalsIgnoreCase("multipolygon")){ + returnValue = "polygon"; + }else if (returnValue.equalsIgnoreCase("multipoint")){ + returnValue = "point"; } return returnValue; }