comparison flys-client/src/main/java/de/intevation/flys/client/server/MapPrintServiceImpl.java @ 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 47d07709ba09
comparison
equal deleted inserted replaced
2503:e3bd1f412421 2504:425bc486a40f
7 import java.io.FileInputStream; 7 import java.io.FileInputStream;
8 import java.io.BufferedInputStream; 8 import java.io.BufferedInputStream;
9 import java.io.BufferedOutputStream; 9 import java.io.BufferedOutputStream;
10 import java.io.OutputStream; 10 import java.io.OutputStream;
11 11
12 import java.util.List;
13 import java.util.Map;
14 import java.util.ArrayList;
15 import java.util.LinkedHashMap;
16
12 import javax.servlet.ServletException; 17 import javax.servlet.ServletException;
13 18
14 import javax.servlet.http.HttpServlet; 19 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest; 20 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse; 21 import javax.servlet.http.HttpServletResponse;
17 22
23 import de.intevation.artifacts.common.ArtifactNamespaceContext;
24
25 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
26 import de.intevation.artifacts.common.utils.JSON;
27 import de.intevation.artifacts.common.utils.StringUtils;
28 import de.intevation.artifacts.common.utils.XMLUtils;
29
30 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
31
32 import de.intevation.artifacts.httpclient.http.HttpClient;
33 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
34
35 import de.intevation.flys.client.shared.model.MapConfig;
36
18 import org.apache.log4j.Logger; 37 import org.apache.log4j.Logger;
19 38
20 import org.mapfish.print.MapPrinter; 39 import org.mapfish.print.MapPrinter;
21 40
22 import org.mapfish.print.output.OutputFactory; 41 import org.mapfish.print.output.OutputFactory;
23 import org.mapfish.print.output.OutputFormat; 42 import org.mapfish.print.output.OutputFormat;
24 43
25 import org.mapfish.print.utils.PJsonObject; 44 import org.mapfish.print.utils.PJsonObject;
26 45
27 import de.intevation.artifacts.common.utils.StringUtils;
28 import de.intevation.artifacts.common.utils.XMLUtils;
29 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
30
31 import de.intevation.artifacts.httpclient.http.HttpClient;
32 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
33 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
34
35 import org.w3c.dom.Document; 46 import org.w3c.dom.Document;
36 47 import org.w3c.dom.Element;
37 import de.intevation.flys.client.shared.model.MapConfig; 48 import org.w3c.dom.NodeList;
38 49
39 public class MapPrintServiceImpl 50 public class MapPrintServiceImpl
40 extends HttpServlet 51 extends HttpServlet
41 { 52 {
42 private static final Logger log = 53 private static final Logger log =
43 Logger.getLogger(MapPrintServiceImpl.class); 54 Logger.getLogger(MapPrintServiceImpl.class);
44 55
45 private static final String DEMO_JSON = 56 protected static String generateSpec(
46 "{" + 57 Document document,
47 "\"layout\": \"A4 portrait\"," + 58 double minX, double minY,
48 "\"title\": \"A simple example\"," + 59 double maxX, double maxY
49 "\"srs\": \"EPSG:4326\"," + 60 ) {
50 "\"dpi\": 254," + 61 System.err.println(XMLUtils.toString(document));
51 "\"units\": \"degrees\"," + 62 MapConfig mapConfig = MapHelper.parseConfig(document);
52 "\"outputFormat\": \"pdf\"," + 63
53 "\"layers\": [{" + 64 Map<String, Object> spec = new LinkedHashMap<String, Object>();
54 " \"type\": \"WMS\"," + 65 spec.put("layout", "A4 portrait");
55 " \"layers\": [\"basic\"]," + 66 spec.put("title", "FLYS Druck");
56 " \"baseURL\": \"http://labs.metacarta.com/wms/vmap0\"," + 67 spec.put("srs", "EPSG:" + mapConfig.getSrid());
57 " \"format\": \"image/jpeg\"" + 68 spec.put("dpi", Integer.valueOf(254));
58 "}," + 69 spec.put("units", "degrees");
59 "{" + 70 spec.put("outputFormat", "pdf");
60 " \"type\": \"WMS\"," + 71
61 " \"layers\": [\"routes\"]," + 72 List<Object> layers = new ArrayList<Object>();
62 " \"baseURL\": \"http://www.camptocamp.org/cgi-bin/mapserv_c2corg\"," + 73
63 " \"format\": \"image/png\"" + 74 String ns = ArtifactNamespaceContext.NAMESPACE_URI;
64 "}, {" + 75
65 "\"opacity\": 0.5," + 76 NodeList facets = document.getElementsByTagNameNS(ns, "facet");
66 "\"customParams\": {}," + 77
67 "\"type\": \"Vector\"," + 78 for (int i = 0, N = facets.getLength(); i < N; ++i) {
68 "\"styles\": {" + 79 Element element = (Element)facets.item(i);
69 " \"1\": {" + 80 if (!element.getParentNode().getLocalName().equals("layers")) {
70 " \"fillColor\": \"red\"," + 81 continue;
71 " \"strokeColor\": \"red\"," + 82 }
72 " \"fillOpacity\": 0.4000000000000001," + 83 Map<String, Object> layer = new LinkedHashMap<String, Object>();
73 " \"hoverFillColor\": \"white\"," + 84
74 " \"hoverFillOpacity\": 0.8000000000000002," + 85 layer.put("type", "WMS");
75 " \"strokeOpacity\": 1," + 86 List<Object> subLayers = new ArrayList<Object>(1);
76 " \"strokeWidth\": 0.5," + 87 subLayers.add(element.getAttributeNS(ns, "layers"));
77 " \"strokeLinecap\": \"round\"," + 88 layer.put("layers", subLayers);
78 " \"strokeDashstyle\": \"solid\"," + 89 layer.put("baseURL", element.getAttributeNS(ns, "url"));
79 " \"hoverStrokeColor\": \"red\"," + 90 layer.put("format", "image/png");
80 " \"hoverStrokeOpacity\": 1.0," + 91
81 " \"hoverStrokeWidth\": 0.2," + 92 layers.add(layer);
82 " \"pointRadius\": 6," + 93 }
83 " \"hoverPointRadius\": 1," + 94
84 " \"hoverPointUnit\": \"%\"," + 95 spec.put("layers", layers);
85 " \"pointerEvents\": \"visiblePainted\"," + 96 spec.put("name", "Name");
86 " \"cursor\": \"inherit\"" + 97
87 " }" + 98 List<Object> pages = new ArrayList<Object>(1);
88 "}," + 99
89 "\"styleProperty\": \"_style\"," + 100 spec.put("pages", pages);
90 "\"geoJson\": {" + 101
91 " \"type\": \"FeatureCollection\"," + 102 Map<String, Object> page = new LinkedHashMap<String, Object>();
92 " \"features\": [{" + 103
93 " \"type\": \"Feature\"," + 104 List<Object> bounds = new ArrayList<Object>(4);
94 " \"id\": \"OpenLayers.Feature.Vector_243\"," + 105 bounds.add(Double.valueOf(minX));
95 " \"properties\": {" + 106 bounds.add(Double.valueOf(minY));
96 " \"_style\": 1," + 107 bounds.add(Double.valueOf(maxX));
97 " \"name\": \"\"," + 108 bounds.add(Double.valueOf(maxY));
98 " \"description\": \"\"," + 109
99 " \"ext-comp-1048\": \"default\"" + 110 page.put("bbox", bounds);
100 " }," + 111
101 " \"geometry\": {" + 112 page.put("mapTitle", "FLYS Karte");
102 " \"type\": \"Polygon\"," + 113 page.put("comment", "Eine mit FLYS gedruckte Karte.");
103 " \"coordinates\": [[[5, 45], [5, 47], [7, 47], [7, 45], [5, 45]]]" + 114 page.put("rotation", Integer.valueOf(0));
104 " }" + 115
105 " }]" + 116 pages.add(page);
106 "}," + 117
107 "\"name\": \"Cosmetic\"" + 118 return JSON.toJSONString(spec);
108 " }" + 119 }
109 "]," +
110 "\"pages\": [" +
111 " {" +
112 " \"center\": [6, 45.5]," +
113 " \"scale\": 4000000," +
114 " \"mapTitle\": \"First map\"," +
115 " \"comment\": \"This is the first page selected by the user.\"," +
116 " \"rotation\": 0," +
117 " \"data\": {" +
118 " \"data\": [" +
119 " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," +
120 " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" +
121 " ]," +
122 " \"columns\": [\"id\", \"name\", \"icon\"]" +
123 " }" +
124 " },{" +
125 " \"center\": [6, 45.5]," +
126 " \"scale\": 4000000," +
127 " \"mapTitle\": \"First map\"," +
128 " \"comment\": \"This is the first page selected by the user.\"," +
129 " \"rotation\": 0," +
130 " \"data\": {" +
131 " \"data\": [" +
132 " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," +
133 " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" +
134 " ]," +
135 " \"columns\": [\"id\", \"name\", \"icon\"]" +
136 " }" +
137 " }" +
138 "]" +
139 "}";
140 120
141 121
142 @Override 122 @Override
143 public void doGet(HttpServletRequest req, HttpServletResponse resp) 123 public void doGet(HttpServletRequest req, HttpServletResponse resp)
144 throws ServletException, IOException 124 throws ServletException, IOException
198 catch (ConnectionException ce) { 178 catch (ConnectionException ce) {
199 log.error(ce); 179 log.error(ce);
200 throw new ServletException(ce); 180 throw new ServletException(ce);
201 } 181 }
202 182
203 MapConfig mapConfig = MapHelper.parseConfig(result); 183 String spec = generateSpec(
204 184 result,
205 System.err.println(XMLUtils.toString(result)); 185 minX, minY,
206 186 maxX, maxY);
207 producePDF(DEMO_JSON, resp); 187
188 if (log.isDebugEnabled()) {
189 log.debug(spec);
190 //System.err.println(spec);
191 }
192
193 producePDF(spec, resp);
208 } 194 }
209 195
210 protected String getURL() throws ServletException { 196 protected String getURL() throws ServletException {
211 String url = getServletContext().getInitParameter("server-url"); 197 String url = getServletContext().getInitParameter("server-url");
212 if (url == null) { 198 if (url == null) {
235 221
236 222
237 protected void producePDF(String json, HttpServletResponse resp) 223 protected void producePDF(String json, HttpServletResponse resp)
238 throws ServletException, IOException 224 throws ServletException, IOException
239 { 225 {
240 PJsonObject jsonSpec = MapPrinter.parseSpec(DEMO_JSON); 226 PJsonObject jsonSpec = MapPrinter.parseSpec(json);
241 227
242 MapPrinter printer = getMapPrinter(); 228 MapPrinter printer = getMapPrinter();
243 229
244 OutputFormat outputFormat = OutputFactory.create( 230 OutputFormat outputFormat = OutputFactory.create(
245 printer.getConfig(), jsonSpec); 231 printer.getConfig(), jsonSpec);

http://dive4elements.wald.intevation.org