# HG changeset patch # User Sascha L. Teichmann # Date 1336411087 0 # Node ID 47d07709ba094ea141df504c6378edcfa7869329 # Parent 87ac5c5325230957663c884d46c6a0f30570927f Printing: extract layer order and visibility from collections describe. flys-client/trunk@4353 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 87ac5c532523 -r 47d07709ba09 flys-client/ChangeLog --- a/flys-client/ChangeLog Mon May 07 13:22:43 2012 +0000 +++ b/flys-client/ChangeLog Mon May 07 17:18:07 2012 +0000 @@ -1,3 +1,10 @@ +2012-05-07 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java: + Extract layer order and visibility from the describe document of + the collection. TODO: Generate legend by inserting icon urls to + GETLEGENDGRAPHIC of the layers. + 2012-05-07 Raimund Renkert First implementation for fixing analysis parameter. @@ -37,7 +44,7 @@ 2012-05-04 Sascha L. Teichmann * src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java: - Dynamically generate spec from artifact. TODO: Interfere with collecttion. + Dynamically generate spec from artifact. TODO: Interfere with collection. * src/main/webapp/WEB-INF/config.yaml: Simplified a bit. diff -r 87ac5c532523 -r 47d07709ba09 flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java --- a/flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java Mon May 07 13:22:43 2012 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java Mon May 07 17:18:07 2012 +0000 @@ -1,25 +1,5 @@ package de.intevation.flys.client.server; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.FileOutputStream; -import java.io.FileInputStream; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.OutputStream; - -import java.util.List; -import java.util.Map; -import java.util.ArrayList; -import java.util.LinkedHashMap; - -import javax.servlet.ServletException; - -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import de.intevation.artifacts.common.ArtifactNamespaceContext; import de.intevation.artifacts.common.utils.ClientProtocolUtils; @@ -32,8 +12,31 @@ import de.intevation.artifacts.httpclient.http.HttpClient; import de.intevation.artifacts.httpclient.http.HttpClientImpl; +import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler; + import de.intevation.flys.client.shared.model.MapConfig; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletException; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import org.apache.log4j.Logger; import org.mapfish.print.MapPrinter; @@ -53,43 +56,107 @@ private static final Logger log = Logger.getLogger(MapPrintServiceImpl.class); - protected static String generateSpec( - Document document, - double minX, double minY, - double maxX, double maxY - ) { - System.err.println(XMLUtils.toString(document)); - MapConfig mapConfig = MapHelper.parseConfig(document); + protected static class Layer implements Comparable { - Map spec = new LinkedHashMap(); - spec.put("layout", "A4 portrait"); - spec.put("title", "FLYS Druck"); - spec.put("srs", "EPSG:" + mapConfig.getSrid()); - spec.put("dpi", Integer.valueOf(254)); - spec.put("units", "degrees"); - spec.put("outputFormat", "pdf"); + protected int pos; + protected String url; + protected String layers; + protected String description; - List layers = new ArrayList(); - - String ns = ArtifactNamespaceContext.NAMESPACE_URI; + public Layer() { + } - NodeList facets = document.getElementsByTagNameNS(ns, "facet"); + public boolean setup(Element element) { - for (int i = 0, N = facets.getLength(); i < N; ++i) { - Element element = (Element)facets.item(i); - if (!element.getParentNode().getLocalName().equals("layers")) { - continue; + Element parent = (Element)element.getParentNode(); + String parentName = parent.getAttribute("name"); + if (!(parentName.equals("map") + || parentName.equals("floodmap"))) { + return false; } + + String ns = ArtifactNamespaceContext.NAMESPACE_URI; + + String visible = element.getAttributeNS(ns, "visible"); + String active = element.getAttributeNS(ns, "active"); + + if (visible.equals("0") || active.equals("0")) { + return false; + } + + url = element.getAttributeNS(ns, "url"); + layers = element.getAttributeNS(ns, "layers"); + description = element.getAttributeNS(ns, "description"); + + try { + pos = Integer.parseInt(element.getAttributeNS(ns, "pos")); + } + catch (NumberFormatException nfe) { + return false; + } + + return true; + } + + public Map toMap() { Map layer = new LinkedHashMap(); layer.put("type", "WMS"); List subLayers = new ArrayList(1); - subLayers.add(element.getAttributeNS(ns, "layers")); + subLayers.add(layers); layer.put("layers", subLayers); - layer.put("baseURL", element.getAttributeNS(ns, "url")); - layer.put("format", "image/png"); + layer.put("baseURL", url); + layer.put("format", "image/png"); // TODO: Make configurable. - layers.add(layer); + return layer; + } + + @Override + public int compareTo(Layer other) { + int d = pos - other.pos; + if (d < 0) return -1; + return d > 0 ? +1 : 0; + } + } // class Layer + + protected static String generateSpec( + Document descDocument, + Document outDocument, + double minX, double minY, + double maxX, double maxY + ) { + MapConfig mapConfig = MapHelper.parseConfig(outDocument); + + Map spec = new LinkedHashMap(); + spec.put("layout", "A4 portrait"); + spec.put("title", "FLYS Druck"); + spec.put("srs", "EPSG:" + mapConfig.getSrid()); + spec.put("dpi", Integer.valueOf(254)); + spec.put("units", "degrees"); + spec.put("outputFormat", "pdf"); + + + List ls = new ArrayList(); + Layer l = new Layer(); + + String ns = ArtifactNamespaceContext.NAMESPACE_URI; + NodeList facets = descDocument.getElementsByTagNameNS(ns, "facet"); + + for (int i = 0, N = facets.getLength(); i < N; ++i) { + Element element = (Element)facets.item(i); + if (l.setup(element)) { + ls.add(l); + l = new Layer(); + } + } + + // Establish Z order. + Collections.sort(ls); + + List layers = new ArrayList(ls.size()); + + for (Layer layer: ls) { + layers.add(layer.toMap()); } spec.put("layers", layers); @@ -161,19 +228,31 @@ String url = getURL(); - Document request = ClientProtocolUtils.newOutCollectionDocument( - uuid, mapType, mapType); + Document requestOut = + ClientProtocolUtils.newOutCollectionDocument( + uuid, mapType, mapType); + Document requestDesc = + ClientProtocolUtils.newDescribeCollectionDocument(uuid); - Document result; + Document outDocument; + Document descDocument; + try { HttpClient client = new HttpClientImpl(url); - InputStream is = client.collectionOut(request, uuid, mapType); + + InputStream is = client.collectionOut( + requestOut, uuid, mapType); + try { - result = XMLUtils.parseDocument(is); + outDocument = XMLUtils.parseDocument(is); } finally { is.close(); + is = null; } + + descDocument = (Document)client.doCollectionAction( + requestDesc, uuid, new DocumentResponseHandler()); } catch (ConnectionException ce) { log.error(ce); @@ -181,7 +260,8 @@ } String spec = generateSpec( - result, + descDocument, + outDocument, minX, minY, maxX, maxY);