changeset 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 6cff63d0c434
children db5b04ecb426
files gnv-artifacts/ChangeLog gnv-artifacts/doc/conf/queries.properties gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java
diffstat 4 files changed, 136 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Tue Apr 06 11:05:00 2010 +0000
+++ b/gnv-artifacts/ChangeLog	Tue Apr 06 11:56:53 2010 +0000
@@ -1,3 +1,21 @@
+2010-04-06  Tim Englich  <tim.englich@intevation.de>
+	  ISSUE213: Wrong Geometrytype used for the generation of an Layer with 
+	            Multipolygon-Geometries
+
+	* src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java (getWMS): 
+	  Integrated lookup of the Geometrytypes using a separat SQL-Statement.
+	  Also added a separat Function for determining the Defaulttemplate for the 
+	  Mapfile-Generation.
+	* src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java (writeDataToFile): 
+	  Changed Methodsignature. Now it's possible to put the Geometrytype which 
+	  is required to generate the Layer and not use the Geometrytype of the 
+	  first Geometry of the Resultvalues.
+	  This was nessessary because we can have several kinds of geometrytypes
+	  (e.g. MultiPolygons and Polygons) in one Layer. In that case we have to 
+	  use the Multi* (e.g. MultiPolygon) as Geometrytype.
+	* doc/conf/queries.properties: 
+	  Added Query for determining the geometryType of a Layer.
+
 2010-04-06	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/gnv/**/*.java: Fixed vim modeline.
--- a/gnv-artifacts/doc/conf/queries.properties	Tue Apr 06 11:05:00 2010 +0000
+++ b/gnv-artifacts/doc/conf/queries.properties	Tue Apr 06 11:56:53 2010 +0000
@@ -1360,7 +1360,7 @@
          
 #############################################
 #############################################
-######### Layer Contis und Nauthis ##########
+# Layer Contis, Nauthis  and Marinefeatures #
 #############################################
 #############################################
 
@@ -1395,3 +1395,9 @@
     WHERE COLUMN_NAME NOT LIKE 'SHAPE' AND \
           OWNER = '?' AND \
           TABLE_NAME = '?'
+          
+geometry_type = select geometry_type \
+    from sde.geometry_columns \
+    where f_table_schema = '?' and \
+          f_table_name = '?' and \
+          f_geometry_column='SHAPE'
\ No newline at end of file
--- 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;
     }
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java	Tue Apr 06 11:05:00 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java	Tue Apr 06 11:56:53 2010 +0000
@@ -1,23 +1,10 @@
 package de.intevation.gnv.utils;
 
-import com.vividsolutions.jts.geom.Geometry;
-import com.vividsolutions.jts.geom.MultiLineString;
-import com.vividsolutions.jts.geom.MultiPolygon;
-
-import com.vividsolutions.jts.io.ParseException;
-import com.vividsolutions.jts.io.WKTReader;
-
-import de.intevation.gnv.geobackend.base.Result;
-import de.intevation.gnv.geobackend.base.ResultDescriptor;
-
 import java.io.File;
 import java.io.IOException;
 import java.io.Serializable;
-
 import java.net.MalformedURLException;
-
 import java.text.NumberFormat;
-
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
@@ -25,27 +12,30 @@
 import java.util.Map;
 
 import org.apache.log4j.Logger;
-
 import org.geotools.data.DataStoreFactorySpi;
 import org.geotools.data.DataUtilities;
 import org.geotools.data.DefaultTransaction;
 import org.geotools.data.FeatureStore;
 import org.geotools.data.Transaction;
-
 import org.geotools.data.shapefile.ShapefileDataStore;
 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
-
 import org.geotools.feature.FeatureCollection;
 import org.geotools.feature.FeatureCollections;
 import org.geotools.feature.SchemaException;
-
 import org.geotools.feature.simple.SimpleFeatureBuilder;
-
 import org.geotools.referencing.crs.DefaultGeographicCRS;
-
 import org.opengis.feature.simple.SimpleFeature;
 import org.opengis.feature.simple.SimpleFeatureType;
 
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.MultiLineString;
+import com.vividsolutions.jts.geom.MultiPolygon;
+import com.vividsolutions.jts.io.ParseException;
+import com.vividsolutions.jts.io.WKTReader;
+
+import de.intevation.gnv.geobackend.base.Result;
+import de.intevation.gnv.geobackend.base.ResultDescriptor;
+
 /**
  * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
  */
@@ -200,10 +190,10 @@
     }
 
 
-    public static String writeDataToFile(File shapeFile,
+    public static boolean writeDataToFile(File shapeFile,
                                           String name,
-                                          Collection<Result> data){
-        String geomType = null;
+                                          Collection<Result> data,
+                                          String geometryType){
 
         WKTReader wktReader = new WKTReader();
 
@@ -214,7 +204,7 @@
         }
         catch (MalformedURLException mue) {
             log.error(mue.getLocalizedMessage(), mue);
-            return null;
+            return false;
         }
 
         params.put("create spatial index", Boolean.TRUE);
@@ -237,8 +227,7 @@
                 int columns = rd.getColumnCount();
                 if (type == null){
                     try {
-                        geomType = g.getGeometryType().toUpperCase();
-                        String schema = "geom:"+g.getGeometryType()+":srid=4326";
+                        String schema = "geom:"+geometryType+":srid=4326";
                         for (int i = 1; i < columns; i++){
                             schema+=","+rd.getColumnName(i)+
                                     ":"+rd.getColumnClassName(i);
@@ -247,7 +236,7 @@
                     }
                     catch (SchemaException se) {
                         log.error(se.getLocalizedMessage(), se);
-                        return null;
+                        return false;
                     }
                     featureBuilder = new SimpleFeatureBuilder(type);
                 }
@@ -304,7 +293,7 @@
             }
         }
 
-        return geomType;
+        return true;
 
     }
 

http://dive4elements.wald.intevation.org