Mercurial > dive4elements > river
changeset 2504:425bc486a40f
Generate map print spec dynamically from artifact
flys-client/trunk@4349 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 04 May 2012 18:31:03 +0000 |
parents | e3bd1f412421 |
children | 87ac5c532523 |
files | flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java flys-client/src/main/webapp/WEB-INF/config.yaml |
diffstat | 3 files changed, 156 insertions(+), 178 deletions(-) [+] |
line wrap: on
line diff
--- a/flys-client/ChangeLog Fri May 04 17:03:04 2012 +0000 +++ b/flys-client/ChangeLog Fri May 04 18:31:03 2012 +0000 @@ -1,3 +1,10 @@ +2012-05-04 Sascha L. Teichmann <sascha.teichmann@intevation.de> + + * src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java: + Dynamically generate spec from artifact. TODO: Interfere with collecttion. + + * src/main/webapp/WEB-INF/config.yaml: Simplified a bit. + 2012-05-04 Sascha L. Teichmann <sascha.teichmann@intevation.de> * src/main/java/de/intevation/flys/client/server/MapHelper.java:
--- a/flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java Fri May 04 17:03:04 2012 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java Fri May 04 18:31:03 2012 +0000 @@ -9,12 +9,31 @@ 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; +import de.intevation.artifacts.common.utils.JSON; +import de.intevation.artifacts.common.utils.StringUtils; +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.artifacts.httpclient.exceptions.ConnectionException; + +import de.intevation.artifacts.httpclient.http.HttpClient; +import de.intevation.artifacts.httpclient.http.HttpClientImpl; + +import de.intevation.flys.client.shared.model.MapConfig; + import org.apache.log4j.Logger; import org.mapfish.print.MapPrinter; @@ -24,17 +43,9 @@ import org.mapfish.print.utils.PJsonObject; -import de.intevation.artifacts.common.utils.StringUtils; -import de.intevation.artifacts.common.utils.XMLUtils; -import de.intevation.artifacts.common.utils.ClientProtocolUtils; - -import de.intevation.artifacts.httpclient.http.HttpClient; -import de.intevation.artifacts.httpclient.http.HttpClientImpl; -import de.intevation.artifacts.httpclient.exceptions.ConnectionException; - import org.w3c.dom.Document; - -import de.intevation.flys.client.shared.model.MapConfig; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; public class MapPrintServiceImpl extends HttpServlet @@ -42,101 +53,70 @@ private static final Logger log = Logger.getLogger(MapPrintServiceImpl.class); - private static final String DEMO_JSON = - "{" + - "\"layout\": \"A4 portrait\"," + - "\"title\": \"A simple example\"," + - "\"srs\": \"EPSG:4326\"," + - "\"dpi\": 254," + - "\"units\": \"degrees\"," + - "\"outputFormat\": \"pdf\"," + - "\"layers\": [{" + - " \"type\": \"WMS\"," + - " \"layers\": [\"basic\"]," + - " \"baseURL\": \"http://labs.metacarta.com/wms/vmap0\"," + - " \"format\": \"image/jpeg\"" + - "}," + - "{" + - " \"type\": \"WMS\"," + - " \"layers\": [\"routes\"]," + - " \"baseURL\": \"http://www.camptocamp.org/cgi-bin/mapserv_c2corg\"," + - " \"format\": \"image/png\"" + - "}, {" + - "\"opacity\": 0.5," + - "\"customParams\": {}," + - "\"type\": \"Vector\"," + - "\"styles\": {" + - " \"1\": {" + - " \"fillColor\": \"red\"," + - " \"strokeColor\": \"red\"," + - " \"fillOpacity\": 0.4000000000000001," + - " \"hoverFillColor\": \"white\"," + - " \"hoverFillOpacity\": 0.8000000000000002," + - " \"strokeOpacity\": 1," + - " \"strokeWidth\": 0.5," + - " \"strokeLinecap\": \"round\"," + - " \"strokeDashstyle\": \"solid\"," + - " \"hoverStrokeColor\": \"red\"," + - " \"hoverStrokeOpacity\": 1.0," + - " \"hoverStrokeWidth\": 0.2," + - " \"pointRadius\": 6," + - " \"hoverPointRadius\": 1," + - " \"hoverPointUnit\": \"%\"," + - " \"pointerEvents\": \"visiblePainted\"," + - " \"cursor\": \"inherit\"" + - " }" + - "}," + - "\"styleProperty\": \"_style\"," + - "\"geoJson\": {" + - " \"type\": \"FeatureCollection\"," + - " \"features\": [{" + - " \"type\": \"Feature\"," + - " \"id\": \"OpenLayers.Feature.Vector_243\"," + - " \"properties\": {" + - " \"_style\": 1," + - " \"name\": \"\"," + - " \"description\": \"\"," + - " \"ext-comp-1048\": \"default\"" + - " }," + - " \"geometry\": {" + - " \"type\": \"Polygon\"," + - " \"coordinates\": [[[5, 45], [5, 47], [7, 47], [7, 45], [5, 45]]]" + - " }" + - " }]" + - "}," + - "\"name\": \"Cosmetic\"" + - " }" + - "]," + - "\"pages\": [" + - " {" + - " \"center\": [6, 45.5]," + - " \"scale\": 4000000," + - " \"mapTitle\": \"First map\"," + - " \"comment\": \"This is the first page selected by the user.\"," + - " \"rotation\": 0," + - " \"data\": {" + - " \"data\": [" + - " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," + - " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" + - " ]," + - " \"columns\": [\"id\", \"name\", \"icon\"]" + - " }" + - " },{" + - " \"center\": [6, 45.5]," + - " \"scale\": 4000000," + - " \"mapTitle\": \"First map\"," + - " \"comment\": \"This is the first page selected by the user.\"," + - " \"rotation\": 0," + - " \"data\": {" + - " \"data\": [" + - " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," + - " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" + - " ]," + - " \"columns\": [\"id\", \"name\", \"icon\"]" + - " }" + - " }" + - "]" + - "}"; + 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); + + Map<String, Object> spec = new LinkedHashMap<String, Object>(); + 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<Object> layers = new ArrayList<Object>(); + + String ns = ArtifactNamespaceContext.NAMESPACE_URI; + + NodeList facets = document.getElementsByTagNameNS(ns, "facet"); + + for (int i = 0, N = facets.getLength(); i < N; ++i) { + Element element = (Element)facets.item(i); + if (!element.getParentNode().getLocalName().equals("layers")) { + continue; + } + Map<String, Object> layer = new LinkedHashMap<String, Object>(); + + layer.put("type", "WMS"); + List<Object> subLayers = new ArrayList<Object>(1); + subLayers.add(element.getAttributeNS(ns, "layers")); + layer.put("layers", subLayers); + layer.put("baseURL", element.getAttributeNS(ns, "url")); + layer.put("format", "image/png"); + + layers.add(layer); + } + + spec.put("layers", layers); + spec.put("name", "Name"); + + List<Object> pages = new ArrayList<Object>(1); + + spec.put("pages", pages); + + Map<String, Object> page = new LinkedHashMap<String, Object>(); + + List<Object> bounds = new ArrayList<Object>(4); + bounds.add(Double.valueOf(minX)); + bounds.add(Double.valueOf(minY)); + bounds.add(Double.valueOf(maxX)); + bounds.add(Double.valueOf(maxY)); + + page.put("bbox", bounds); + + page.put("mapTitle", "FLYS Karte"); + page.put("comment", "Eine mit FLYS gedruckte Karte."); + page.put("rotation", Integer.valueOf(0)); + + pages.add(page); + + return JSON.toJSONString(spec); + } @Override @@ -200,11 +180,17 @@ throw new ServletException(ce); } - MapConfig mapConfig = MapHelper.parseConfig(result); + String spec = generateSpec( + result, + minX, minY, + maxX, maxY); - System.err.println(XMLUtils.toString(result)); + if (log.isDebugEnabled()) { + log.debug(spec); + //System.err.println(spec); + } - producePDF(DEMO_JSON, resp); + producePDF(spec, resp); } protected String getURL() throws ServletException { @@ -237,7 +223,7 @@ protected void producePDF(String json, HttpServletResponse resp) throws ServletException, IOException { - PJsonObject jsonSpec = MapPrinter.parseSpec(DEMO_JSON); + PJsonObject jsonSpec = MapPrinter.parseSpec(json); MapPrinter printer = getMapPrinter();
--- a/flys-client/src/main/webapp/WEB-INF/config.yaml Fri May 04 17:03:04 2012 +0000 +++ b/flys-client/src/main/webapp/WEB-INF/config.yaml Fri May 04 18:31:03 2012 +0000 @@ -21,7 +21,7 @@ - 2000000 - 4000000 -outputFilename: 'pigma-${date}.pdf' +outputFilename: 'flys-${date}.pdf' #=========================================================================== # the list of allowed hosts @@ -29,28 +29,13 @@ hosts: - !localMatch dummy: true - - !ipMatch - ip: www.camptocamp.org - !dnsMatch - host: labs.metacarta.com - port: 80 - - !dnsMatch - host: terraservice.net + host: wsvmapserv.wsv.bund.de port: 80 - !dnsMatch host: tile.openstreetmap.org port: 80 -security: - - !basicAuth - matcher: !dnsMatch - host: c2cpc61.camptocamp.com - port:80 - username: xyz - password: yxz - preemptive: true - - layouts: #=========================================================================== A4 portrait: @@ -62,58 +47,58 @@ keywords: 'map,print' creator: 'MapFish' - titlePage: - pageSize: A4 - items: - - !text - spacingAfter: 150 - - !text - font: Helvetica - fontSize: 40 - spacingAfter: 100 - align: center - text: '${title}' - - !image - maxWidth: 160 - maxHeight: 160 - spacingAfter: 100 - align: center - url: http://trac.mapfish.org/trac/mapfish/attachment/ticket/3/logo_v8_sphere.svg?format=raw - - !image - maxWidth: 160 - maxHeight: 160 - spacingAfter: 100 - align: center - url: 'file://${configDir}/../images/flys_logo.gif' - - !text - font: Helvetica - fontSize: 14 - align: left - text: | - Two layers are asked by the client: - - a base layer from Metacarta - - a transparent layer from Camptocamp.org (routes) - . - Some text is added over the map, just to demonstrate the absolute positionning. - footer: &commonFooter - height: 30 - items: - - !columns - config: - cells: - - paddingBottom: 5 - items: - - !image - maxWidth: 40 - backgroundColor: #FF0000 - align: left - url: '${configDir}/../images/properties.gif' - - !text - backgroundColor: #FF0000 - text: ©Camptocamp SA - - !text - align: right - text: 'Page ${pageNum}' +# titlePage: +# pageSize: A4 +# items: +# - !text +# spacingAfter: 150 +# - !text +# font: Helvetica +# fontSize: 40 +# spacingAfter: 100 +# align: center +# text: '${title}' +# - !image +# maxWidth: 160 +# maxHeight: 160 +# spacingAfter: 100 +# align: center +# url: http://trac.mapfish.org/trac/mapfish/attachment/ticket/3/logo_v8_sphere.svg?format=raw +# - !image +# maxWidth: 160 +# maxHeight: 160 +# spacingAfter: 100 +# align: center +# url: 'file://${configDir}/../images/flys_logo.gif' +# - !text +# font: Helvetica +# fontSize: 14 +# align: left +# text: | +# Two layers are asked by the client: +# - a base layer from Metacarta +# - a transparent layer from Camptocamp.org (routes) +# . +# Some text is added over the map, just to demonstrate the absolute positionning. +# footer: &commonFooter +# height: 30 +# items: +# - !columns +# config: +# cells: +# - paddingBottom: 5 +# items: +# - !image +# maxWidth: 40 +# backgroundColor: #FF0000 +# align: left +# url: '${configDir}/../images/properties.gif' +# - !text +# backgroundColor: #FF0000 +# text: ©Camptocamp SA +# - !text +# align: right +# text: 'Page ${pageNum}' #------------------------------------------------------------------------- mainPage: