# HG changeset patch # User Tim Englich # Date 1268057068 0 # Node ID 6eccb68a8b9955b8694d38a64d8d0b967f904d28 # Parent a6081d76fa1caf9ec0ec76c3013516e7b8ba963d Added WMS-Publishing to Product Layer gnv-artifacts/trunk@747 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r a6081d76fa1c -r 6eccb68a8b99 gnv-artifacts/ChangeLog --- a/gnv-artifacts/ChangeLog Mon Mar 08 10:51:01 2010 +0000 +++ b/gnv-artifacts/ChangeLog Mon Mar 08 14:04:28 2010 +0000 @@ -1,3 +1,26 @@ +2010-03-08 Tim Englich + + * doc/conf/maptemplates/layer*.vm: + Added some basetemplates for Lines, Points and Polygons that will be used + if an WMS-layer of the Product Layer will be created. + + * src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java (writeDataToFile): + Changed Method in taht way that it now will return the geometrytype of + this Layer instead of an boolean-value. + Null will determine that an Exception occured during the + Shapefilegeneration. + + * src/main/java/de/intevation/gnv/utils/MetaWriter.java (writeLayerMeta): + Added a new Method for writing the Metadatafile for WMS published by + the Product Layer. + Done som Refactoring work for reusing som Code-Snippets. + + * src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java (getWMS): + Added the Basic-Support for publishing an WMS-Layer. + The Lookup of the Attributes which has to be put into the Shapefile and + the lookup of the Template which schould be used to symbolize the Layer + has to be implemented. + 2010-03-08 Ingo Weinzierl Issue195 diff -r a6081d76fa1c -r 6eccb68a8b99 gnv-artifacts/doc/conf/maptemplates/layer_linestring.vm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/doc/conf/maptemplates/layer_linestring.vm Mon Mar 08 14:04:28 2010 +0000 @@ -0,0 +1,13 @@ +LAYER + NAME "$info.getName()" + TYPE $info.getType() + DATA "${info.getName()}/${info.getData()}" + STATUS $info.getStatus() + + CLASS + NAME "Layer" + STYLE + COLOR "#870087" + END + END +END diff -r a6081d76fa1c -r 6eccb68a8b99 gnv-artifacts/doc/conf/maptemplates/layer_point.vm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/doc/conf/maptemplates/layer_point.vm Mon Mar 08 14:04:28 2010 +0000 @@ -0,0 +1,13 @@ +LAYER + NAME "$info.getName()" + TYPE $info.getType() + DATA "${info.getName()}/${info.getData()}" + STATUS $info.getStatus() + + CLASS + NAME "Layer" + STYLE + COLOR "#870087" + END + END +END diff -r a6081d76fa1c -r 6eccb68a8b99 gnv-artifacts/doc/conf/maptemplates/layer_polygon.vm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv-artifacts/doc/conf/maptemplates/layer_polygon.vm Mon Mar 08 14:04:28 2010 +0000 @@ -0,0 +1,13 @@ +LAYER + NAME "$info.getName()" + TYPE $info.getType() + DATA "${info.getName()}/${info.getData()}" + STATUS $info.getStatus() + + CLASS + NAME "Layer" + STYLE + COLOR "#870087" + END + END +END diff -r a6081d76fa1c -r 6eccb68a8b99 gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java --- a/gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java Mon Mar 08 10:51:01 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/state/layer/LayerOutputState.java Mon Mar 08 14:04:28 2010 +0000 @@ -7,10 +7,13 @@ import java.io.IOException; import java.io.OutputStream; import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; 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; @@ -26,11 +29,13 @@ 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.raster.PaletteManager; import de.intevation.gnv.state.InputData; import de.intevation.gnv.state.OutputStateBase; import de.intevation.gnv.state.exception.StateException; import de.intevation.gnv.utils.FileUtils; import de.intevation.gnv.utils.MapfileGenerator; +import de.intevation.gnv.utils.MetaWriter; import de.intevation.gnv.utils.ShapeFileWriter; /** @@ -48,6 +53,9 @@ * The UID of this Class. */ private static final long serialVersionUID = 9180957321704424049L; + + // TODO: Replace + public static final String LAYER_MODEL = "layer"; /** * The ID for the Query fetching the Layer from the DB @@ -69,6 +77,8 @@ private String shapeFilePath; + private String geometryType = null; + public static final String SHAPEFILE_NAME = "data.shp"; /** @@ -92,7 +102,7 @@ format, XPATH_OUTPUT_MODE, ArtifactNamespaceContext.INSTANCE); if (outputMode.equalsIgnoreCase("wms")) { Collection data = this.fetchData(); - // TODO USE ME + XMLUtils.toStream(this.getWMS(uuid, callContext, data),outputStream); }else if (outputMode.equalsIgnoreCase("zip")){ Collection data = this.fetchData(); this.writeZip(uuid, callContext, outputStream, data); @@ -227,7 +237,7 @@ } File shapeFile = new File(shapeDir, SHAPEFILE_NAME); - if (!ShapeFileWriter.writeDataToFile(shapeFile, "data", data)){ + if ((geometryType = ShapeFileWriter.writeDataToFile(shapeFile, "data", data)) == null){ log.error("writing data into shapefile failed"); return null; } @@ -333,9 +343,70 @@ synchronized (shapeFileLock) { String path = shapeFilePath; shapeFilePath = null; + geometryType = null; return path; } } + protected Document getWMS(String uuid, + CallContext callContext, + Collection data) + throws StateException + { + // TODO: Do the real WMS publishing here! + Document document = XMLUtils.newDocument(); + + Element pathElement = document.createElement("path"); + document.appendChild(pathElement); + + String path = getShapeFilePath(); + + if (path != null && new File(path).isDirectory()) { + pathElement.setTextContent(path); + } + else { + + if (data != null && + (path = writeToShapeFile(uuid, data, callContext)) != null) { + + String paramType = LAYER_MODEL+"_"+this.geometryType.toLowerCase(); + + Document meta = MetaWriter.writeLayerMeta(callContext, uuid, + path, paramType, + this.determineGeometryType()); + if (meta != null) { + MapfileGenerator.getInstance().update(); + return meta; + } + + pathElement.setTextContent(path); + } + } + + return document; + } + private String determineGeometryType(){ + + String returnValue = this.geometryType.toLowerCase(); + + if (returnValue.equalsIgnoreCase("linestring")){ + returnValue = "Line"; + } + return returnValue; + } + + private static Map getPalettes( + CallContext callContext + ) { + //TODO: customize for product Layer + GNVArtifactContext context = + (GNVArtifactContext)callContext.globalContext(); + Map palettes = + (Map)context.get( + GNVArtifactContext.PALETTES_KEY); + return palettes != null + ? palettes + : new HashMap(); + } } diff -r a6081d76fa1c -r 6eccb68a8b99 gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java --- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java Mon Mar 08 10:51:01 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/MetaWriter.java Mon Mar 08 14:04:28 2010 +0000 @@ -1,13 +1,5 @@ package de.intevation.gnv.utils; -import de.intevation.artifacts.ArtifactNamespaceContext; -import de.intevation.artifacts.CallContext; - -import de.intevation.artifactdatabase.XMLUtils; - -import de.intevation.gnv.artifacts.context.GNVArtifactContext; -import de.intevation.gnv.wms.LayerInfo; - import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -15,10 +7,15 @@ import java.io.OutputStream; import org.apache.log4j.Logger; - import org.w3c.dom.Document; import org.w3c.dom.Element; +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.wms.LayerInfo; + /** * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) */ @@ -33,9 +30,34 @@ public static final String META_FILE_NAME = "meta.xml"; public static final String ISOLINES_NAME = "isolines.shp"; public static final String POLYGON_NAME = "polygons.shp"; + public static final String LAYER_DATA_NAME = "data.shp"; private MetaWriter() { } + + public static Document writeLayerMeta(CallContext context, + String uuid, + String path, + String paramType, + String layerType){ + Document meta = XMLUtils.newDocument(); + XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( + meta, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + Element root = creator.create("meta"); + meta.appendChild(root); + + writeAbstractMeta(context, meta, root); + writeLayerMeta(context, meta, root, uuid, paramType,layerType); + boolean success = writeMetaFile(path, meta); + + if (success){ + return meta; + }else{ + return null; + } + } public static Document writeHorizontalcrosssectionMeta( CallContext context, @@ -56,12 +78,26 @@ writePolygonMeta(context, meta, root, uuid, path, paramType); writeIsolineMeta(context, meta, root, uuid, path, paramType); + boolean success = writeMetaFile(path, meta); + + if (success){ + return meta; + }else{ + return null; + } + } + + /** + * @param path + * @param meta + */ + private static boolean writeMetaFile(String path, Document meta) { try { File metaFile = new File(path, META_FILE_NAME); if (!metaFile.createNewFile() || !metaFile.canWrite()) { logger.error("Error while writing meta file: "+metaFile.toString()); - return null; + return false; } OutputStream out = null; @@ -81,16 +117,16 @@ metaFile.delete(); } - return success ? meta : null; + return success; } catch (FileNotFoundException fnfe) { logger.error(fnfe); + return false; } catch (IOException ioe) { logger.error(ioe, ioe); + return false; } - - return meta; } @@ -128,7 +164,44 @@ meta.appendChild(mapserver); } + protected static void writeLayerMeta( + CallContext context, + Document document, + Element meta, + String uuid, + String paramType, + String layerType + ) { + XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( + document, + ArtifactNamespaceContext.NAMESPACE_URI, + ArtifactNamespaceContext.NAMESPACE_PREFIX); + Element layer = creator.create(LayerInfo.LAYER); + Element model = creator.create(LayerInfo.LAYER_MODEL); + Element name = creator.create(LayerInfo.LAYER_NAME); + Element type = creator.create(LayerInfo.LAYER_TYPE); + Element status = creator.create(LayerInfo.LAYER_STATUS); + Element data = creator.create(LayerInfo.LAYER_DATA); + + model.setTextContent(paramType); + name.setTextContent(uuid); + type.setTextContent(layerType); + status.setTextContent("OFF"); + data.setTextContent(LAYER_DATA_NAME); + + layer.appendChild(model); + layer.appendChild(name); + layer.appendChild(type); + layer.appendChild(status); + layer.appendChild(data); + + meta.appendChild(layer); + } + + + + public static void writePolygonMeta( CallContext context, Document document, @@ -152,7 +225,7 @@ model.setTextContent(paramType); name.setTextContent(uuid); type.setTextContent("POLYGON"); - status.setTextContent("DEFAULT"); + status.setTextContent("OFF"); data.setTextContent(POLYGON_NAME); layer.appendChild(model); @@ -188,7 +261,7 @@ model.setTextContent(paramType+"_isolines"); name.setTextContent(uuid); type.setTextContent("LINE"); - status.setTextContent("DEFAULT"); + status.setTextContent("OFF"); data.setTextContent(ISOLINES_NAME); layer.appendChild(model); diff -r a6081d76fa1c -r 6eccb68a8b99 gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java --- a/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java Mon Mar 08 10:51:01 2010 +0000 +++ b/gnv-artifacts/src/main/java/de/intevation/gnv/utils/ShapeFileWriter.java Mon Mar 08 14:04:28 2010 +0000 @@ -178,9 +178,11 @@ } - public static boolean writeDataToFile(File shapeFile, + public static String writeDataToFile(File shapeFile, String name, Collection data){ + String geomType = null; + WKTReader wktReader = new WKTReader(); Map params = new HashMap(); @@ -190,7 +192,7 @@ } catch (MalformedURLException mue) { log.error(mue.getLocalizedMessage(), mue); - return false; + return null; } params.put("create spatial index", Boolean.TRUE); @@ -210,6 +212,7 @@ Geometry g = wktReader.read(result.getString(0)); if (type == null){ try { + geomType = g.getGeometryType().toUpperCase(); type = DataUtilities.createType( name, "geom:"+g.getGeometryType()+":srid=4326"); @@ -217,7 +220,7 @@ } catch (SchemaException se) { log.error(se.getLocalizedMessage(), se); - return false; + return null; } featureBuilder = new SimpleFeatureBuilder(type); } @@ -271,7 +274,7 @@ } } - return success; + return geomType; }