comparison artifacts/src/main/java/org/dive4elements/river/exports/MapGenerator.java @ 9619:63bbd5e45839

#21 WMS Legend
author dnt_bjoernsen <d.tironi@bjoernsen.de>
date Thu, 10 Oct 2019 16:08:47 +0200
parents 5e38e2924c07
children
comparison
equal deleted inserted replaced
9618:af671ee7835e 9619:63bbd5e45839
6 * documentation coming with Dive4Elements River for details. 6 * documentation coming with Dive4Elements River for details.
7 */ 7 */
8 8
9 package org.dive4elements.river.exports; 9 package org.dive4elements.river.exports;
10 10
11 import java.io.File;
12 import java.io.FileNotFoundException;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.apache.log4j.Logger;
11 import org.dive4elements.artifactdatabase.data.StateData; 19 import org.dive4elements.artifactdatabase.data.StateData;
12 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet; 20 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
13 import org.dive4elements.artifactdatabase.state.Facet; 21 import org.dive4elements.artifactdatabase.state.Facet;
14 import org.dive4elements.artifactdatabase.state.Settings; 22 import org.dive4elements.artifactdatabase.state.Settings;
15 import org.dive4elements.artifacts.Artifact; 23 import org.dive4elements.artifacts.Artifact;
25 import org.dive4elements.river.artifacts.states.WaterlevelGroundDifferences; 33 import org.dive4elements.river.artifacts.states.WaterlevelGroundDifferences;
26 import org.dive4elements.river.collections.D4EArtifactCollection; 34 import org.dive4elements.river.collections.D4EArtifactCollection;
27 import org.dive4elements.river.themes.ThemeDocument; 35 import org.dive4elements.river.themes.ThemeDocument;
28 import org.dive4elements.river.utils.ArtifactMapfileGenerator; 36 import org.dive4elements.river.utils.ArtifactMapfileGenerator;
29 import org.dive4elements.river.utils.GeometryUtils; 37 import org.dive4elements.river.utils.GeometryUtils;
30
31 import java.io.File;
32 import java.io.FileNotFoundException;
33 import java.io.IOException;
34 import java.io.OutputStream;
35 import java.util.ArrayList;
36 import java.util.List;
37
38 import org.apache.log4j.Logger;
39 import org.w3c.dom.Document; 38 import org.w3c.dom.Document;
40 import org.w3c.dom.Element; 39 import org.w3c.dom.Element;
41 40
42 import com.vividsolutions.jts.geom.Envelope; 41 import com.vividsolutions.jts.geom.Envelope;
43 42
44
45 public class MapGenerator implements OutGenerator, FacetTypes { 43 public class MapGenerator implements OutGenerator, FacetTypes {
46 44
47 private static Logger log = Logger.getLogger(MapGenerator.class); 45 private static Logger log = Logger.getLogger(MapGenerator.class);
48 46
49 protected D4EArtifactCollection collection; 47 protected D4EArtifactCollection collection;
65 63
66 protected String srid; 64 protected String srid;
67 65
68 protected String outName; 66 protected String outName;
69 67
70 @Override 68 private String legend;
71 public void setup(Object config) { 69
70 @Override
71 public void setup(final Object config) {
72 log.debug("MapGenerator.setup"); 72 log.debug("MapGenerator.setup");
73 } 73 }
74 74
75 @Override 75 @Override
76 public void init( 76 public void init(final String outName, final Document request, final OutputStream out, final CallContext context) {
77 String outName,
78 Document request,
79 OutputStream out,
80 CallContext context
81 ) {
82 log.debug("MapGenerator.init"); 77 log.debug("MapGenerator.init");
83 78
84 this.outName = outName; 79 this.outName = outName;
85 this.request = request; 80 this.request = request;
86 this.out = out; 81 this.out = out;
87 this.context = context; 82 this.context = context;
88 83
89 this.layers = new ArrayList<WMSLayerFacet>(); 84 this.layers = new ArrayList<>();
90 85
91 this.maxExtent = null; 86 this.maxExtent = null;
92 this.initialExtent = null; 87 this.initialExtent = null;
93 } 88 }
94 89
95 90 @Override
96 @Override 91 public void setMasterArtifact(final Artifact master) {
97 public void setMasterArtifact(Artifact master) {
98 log.debug("MapGenerator.setMasterArtifact"); 92 log.debug("MapGenerator.setMasterArtifact");
99 this.master = master; 93 this.master = master;
100 } 94 }
101 95
102 @Override 96 @Override
103 public void setCollection(D4EArtifactCollection collection) { 97 public void setCollection(final D4EArtifactCollection collection) {
104 this.collection = collection; 98 this.collection = collection;
105 } 99 }
106 100
107 @Override 101 @Override
108 public void doOut( 102 public void doOut(final ArtifactAndFacet artifactFacet, final ThemeDocument attr, final boolean visible) {
109 ArtifactAndFacet artifactFacet, 103 final String name = artifactFacet.getFacetName();
110 ThemeDocument attr, 104
111 boolean visible) 105 log.debug("MapGenerator.doOut: " + artifactFacet.getArtifact().identifier() + " | " + name);
112 { 106 final D4EArtifact flys = (D4EArtifact) artifactFacet.getArtifact();
113 String name = artifactFacet.getFacetName(); 107
114 108 final Facet nativeFacet = artifactFacet.getFacet();
115 log.debug("MapGenerator.doOut: " +
116 artifactFacet.getArtifact().identifier() + " | " + name);
117 D4EArtifact flys = (D4EArtifact) artifactFacet.getArtifact();
118
119 Facet nativeFacet = artifactFacet.getFacet();
120 109
121 if (nativeFacet instanceof WMSLayerFacet) { 110 if (nativeFacet instanceof WMSLayerFacet) {
122 WMSLayerFacet wms = (WMSLayerFacet) nativeFacet; 111 final WMSLayerFacet wms = (WMSLayerFacet) nativeFacet;
123 Envelope extent = wms.getOriginalExtent(); 112 final Envelope extent = wms.getOriginalExtent();
124 113
125 layers.add(wms); 114 this.layers.add(wms);
126 115
127 setMaxExtent(extent); 116 setMaxExtent(extent);
128 setSrid(wms.getSrid()); 117 setSrid(wms.getSrid());
129 118
130 if (FLOODMAP_WSPLGEN.equals(name)) { 119 if (FLOODMAP_WSPLGEN.equals(name)) {
131 setInitialExtent(extent); 120 setInitialExtent(extent);
132 createWSPLGENLayer(flys, wms, attr); 121 createWSPLGENLayer(flys, wms, attr);
133 } 122 }
134 // FIXME: Already generated by HWSBarrierState 123 // FIXME: Already generated by HWSBarrierState
135 // wms has a wrong SRID which would break that layer 124 // wms has a wrong SRID which would break that layer
136 //else if (FLOODMAP_USERSHAPE.equals(name)) { 125 // else if (FLOODMAP_USERSHAPE.equals(name)) {
137 // createUserShapeLayer(flys, wms); 126 // createUserShapeLayer(flys, wms);
138 //} 127 // }
139 else { 128 else {
140 log.debug("doOut: createDatabaseLayer for facet name: " + name); 129 log.debug("doOut: createDatabaseLayer for facet name: " + name);
141 createDatabaseLayer(flys, wms, attr); 130 createDatabaseLayer(flys, wms, attr);
142 } 131 }
143 } 132 } else {
144 else {
145 log.warn("Facet not supported: " + nativeFacet.getClass()); 133 log.warn("Facet not supported: " + nativeFacet.getClass());
146 } 134 }
147 } 135 }
148 136
149 137 protected void createWSPLGENLayer(final D4EArtifact flys, final WMSLayerFacet wms, final ThemeDocument attr) {
150 protected void createWSPLGENLayer(
151 D4EArtifact flys,
152 WMSLayerFacet wms,
153 ThemeDocument attr
154 ) {
155 try { 138 try {
156 if(wms instanceof WSPLGENLayerFacet) { 139 if (wms instanceof WSPLGENLayerFacet) {
157 // Retrieve waterlevel ground differences from artifact 140 // Retrieve waterlevel ground differences from artifact
158 StateData dFrom = 141 final StateData dFrom = flys.getData(WaterlevelGroundDifferences.LOWER_FIELD);
159 flys.getData(WaterlevelGroundDifferences.LOWER_FIELD); 142 final StateData dTo = flys.getData(WaterlevelGroundDifferences.UPPER_FIELD);
160 StateData dTo = 143 final StateData dStep = flys.getData(WaterlevelGroundDifferences.DIFF_FIELD);
161 flys.getData(WaterlevelGroundDifferences.UPPER_FIELD); 144
162 StateData dStep = 145 final String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
163 flys.getData(WaterlevelGroundDifferences.DIFF_FIELD); 146 final String toStr = dTo != null ? (String) dTo.getValue() : null;
164 147 final String stepStr = dStep != null ? (String) dStep.getValue() : null;
165 String fromStr = dFrom != null 148
166 ? (String) dFrom.getValue() 149 final float from = Float.parseFloat(fromStr);
167 : null; 150 final float to = Float.parseFloat(toStr);
168 String toStr = dTo != null 151 final float step = Float.parseFloat(stepStr);
169 ? (String) dTo.getValue() 152
170 : null; 153 final ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();
171 String stepStr = dStep != null 154 mfg.createUeskLayer(flys, (WSPLGENLayerFacet) wms, attr.createDynamicMapserverStyle(from, to, step, this.context.getMeta()), this.context);
172 ? (String) dStep.getValue() 155 } else {
173 : null; 156 log.warn("Cannot create WSPLGEN layer from: " + wms.getClass());
174 157 }
175 float from = Float.parseFloat(fromStr); 158 }
176 float to = Float.parseFloat(toStr); 159 catch (final IOException ioe) {
177 float step = Float.parseFloat(stepStr);
178
179 ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();
180 mfg.createUeskLayer(
181 flys,
182 (WSPLGENLayerFacet) wms,
183 attr.createDynamicMapserverStyle(
184 from, to, step, context.getMeta()),
185 context);
186 }
187 else {
188 log.warn("Cannot create WSPLGEN layer from: " +
189 wms.getClass());
190 }
191 }
192 catch (IOException ioe) {
193 log.error(ioe, ioe); 160 log.error(ioe, ioe);
194 } 161 }
195 } 162 }
196 163
197 164 protected void createUserShapeLayer(final D4EArtifact flys, final WMSLayerFacet wms) {
198 protected void createUserShapeLayer(D4EArtifact flys, WMSLayerFacet wms) { 165 final ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();
199 ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();
200 166
201 try { 167 try {
202 mfg.createUserShapeLayer(flys, wms); 168 mfg.createUserShapeLayer(flys, wms);
203 } 169 }
204 catch (FileNotFoundException fnfe) { 170 catch (final FileNotFoundException fnfe) {
205 log.error(fnfe, fnfe); 171 log.error(fnfe, fnfe);
206 } 172 }
207 catch (IOException ioe) { 173 catch (final IOException ioe) {
208 log.error(ioe, ioe); 174 log.error(ioe, ioe);
209 } 175 }
210 } 176 }
211 177
212 178 protected void createDatabaseLayer(final D4EArtifact flys, final WMSLayerFacet wms, final ThemeDocument attr) {
213 protected void createDatabaseLayer(
214 D4EArtifact flys,
215 WMSLayerFacet wms,
216 ThemeDocument attr
217 ) {
218 log.debug("createDatabaseLayer for facet: " + wms.getName()); 179 log.debug("createDatabaseLayer for facet: " + wms.getName());
219 180
220 ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); 181 final ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();
221 182
222 try { 183 try {
223 File baseDir = mfg.getShapefileBaseDir(); 184 final File baseDir = mfg.getShapefileBaseDir();
224 File artDir = new File(baseDir, flys.identifier()); 185 final File artDir = new File(baseDir, flys.identifier());
225 186
226 if (artDir != null && !artDir.exists()) { 187 if (artDir != null && !artDir.exists()) {
227 log.debug("Create new directory: " + artDir.getPath()); 188 log.debug("Create new directory: " + artDir.getPath());
228 artDir.mkdir(); 189 artDir.mkdir();
229 } 190 }
230 191
231 if (wms instanceof WMSDBLayerFacet) { 192 if (wms instanceof WMSDBLayerFacet) {
232 mfg.createDatabaseLayer( 193 mfg.createDatabaseLayer(flys, (WMSDBLayerFacet) wms, attr.createMapserverStyle());
233 flys, 194 } else {
234 (WMSDBLayerFacet) wms,
235 attr.createMapserverStyle());
236 }
237 else {
238 log.warn("Cannot create DB layer from: " + wms.getClass()); 195 log.warn("Cannot create DB layer from: " + wms.getClass());
239 } 196 }
240 } 197 }
241 catch (FileNotFoundException fnfe) { 198 catch (final FileNotFoundException fnfe) {
242 log.error(fnfe, fnfe); 199 log.error(fnfe, fnfe);
243 } 200 }
244 catch (IOException ioe) { 201 catch (final IOException ioe) {
245 log.error(ioe, ioe); 202 log.error(ioe, ioe);
246 } 203 }
247 } 204 }
248 205
249 206 @Override
250 @Override 207 public void generate() throws IOException {
251 public void generate()
252 throws IOException
253 {
254 log.debug("MapGenerator.generate"); 208 log.debug("MapGenerator.generate");
255 209
256 ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator(); 210 final ArtifactMapfileGenerator mfg = new ArtifactMapfileGenerator();
257 mfg.generate(); 211 mfg.generate();
258 212
259 Document response = XMLUtils.newDocument(); 213 final Document response = XMLUtils.newDocument();
260 ElementCreator c = new ElementCreator( 214 final ElementCreator c = new ElementCreator(response, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX);
261 response, 215
262 ArtifactNamespaceContext.NAMESPACE_URI, 216 final Element root = c.create("floodmap");
263 ArtifactNamespaceContext.NAMESPACE_PREFIX); 217 final Element layers = c.create("layers");
264
265 Element root = c.create("floodmap");
266 Element layers = c.create("layers");
267 218
268 response.appendChild(root); 219 response.appendChild(root);
269 root.appendChild(layers); 220 root.appendChild(layers);
270 221
271 appendLayers(layers); 222 appendLayers(layers);
272 appendMapInformation(root, c); 223 appendMapInformation(root, c);
273 224
274 XMLUtils.toStream(response, out); 225 XMLUtils.toStream(response, this.out);
275 } 226 }
276 227
277 228 protected void appendLayers(final Element parent) {
278 protected void appendLayers(Element parent) { 229 for (final WMSLayerFacet facet : this.layers) {
279 for (WMSLayerFacet facet: layers) {
280 parent.appendChild(facet.toXML(parent.getOwnerDocument())); 230 parent.appendChild(facet.toXML(parent.getOwnerDocument()));
281 } 231 }
282 } 232 }
283 233
284 234 protected void setMaxExtent(final Envelope maxExtent) {
285 protected void setMaxExtent(Envelope maxExtent) {
286 if (maxExtent == null) { 235 if (maxExtent == null) {
287 return; 236 return;
288 } 237 }
289 238
290 if (this.maxExtent == null) { 239 if (this.maxExtent == null) {
294 } 243 }
295 244
296 this.maxExtent.expandToInclude(maxExtent); 245 this.maxExtent.expandToInclude(maxExtent);
297 } 246 }
298 247
299 248 protected void setInitialExtent(final Envelope initialExtent) {
300 protected void setInitialExtent(Envelope initialExtent) {
301 if (this.initialExtent == null && initialExtent != null) { 249 if (this.initialExtent == null && initialExtent != null) {
302 log.debug("Set initial extent to: " + initialExtent); 250 log.debug("Set initial extent to: " + initialExtent);
303 this.initialExtent = new Envelope(initialExtent); 251 this.initialExtent = new Envelope(initialExtent);
304 } 252 }
305 } 253 }
306 254
307 255 protected void setSrid(final String srid) {
308 protected void setSrid(String srid) {
309 if (srid == null || srid.length() == 0) { 256 if (srid == null || srid.length() == 0) {
310 return; 257 return;
311 } 258 }
312 259
313 this.srid = srid; 260 this.srid = srid;
314 } 261 }
315 262
316 263 protected void appendMapInformation(final Element parent, final ElementCreator c) {
317 protected void appendMapInformation(Element parent, ElementCreator c) {
318 String mE; 264 String mE;
319 if (this.maxExtent != null) { 265 if (this.maxExtent != null) {
320 mE = GeometryUtils.jtsBoundsToOLBounds(this.maxExtent); 266 mE = GeometryUtils.jtsBoundsToOLBounds(this.maxExtent);
321 } else { 267 } else {
322 log.error("Layer without extent. Probably no geometry at all."); 268 log.error("Layer without extent. Probably no geometry at all.");
323 mE = "0 0 1 1"; 269 mE = "0 0 1 1";
324 } 270 }
325 271
326 Element maxExtent = c.create("maxExtent"); 272 final Element maxExtent = c.create("maxExtent");
327 maxExtent.setTextContent(mE); 273 maxExtent.setTextContent(mE);
328 274
329 if(this.initialExtent != null) { 275 if (this.initialExtent != null) {
330 String iE = GeometryUtils.jtsBoundsToOLBounds(this.initialExtent); 276 final String iE = GeometryUtils.jtsBoundsToOLBounds(this.initialExtent);
331 Element initExtent = c.create("initialExtent"); 277 final Element initExtent = c.create("initialExtent");
332 initExtent.setTextContent(iE); 278 initExtent.setTextContent(iE);
333 parent.appendChild(initExtent); 279 parent.appendChild(initExtent);
334 } 280 }
335 281
336 Element srid = c.create("srid"); 282 final Element srid = c.create("srid");
337 srid.setTextContent(this.srid); 283 srid.setTextContent(this.srid);
338 284
339 // TODO zoom levels 285 // TODO zoom levels
340 // TODO resolutation 286 // TODO resolutation
341 287
342 parent.appendChild(maxExtent); 288 parent.appendChild(maxExtent);
343 parent.appendChild(srid); 289 parent.appendChild(srid);
344 } 290 }
345
346 291
347 /** 292 /**
348 * Returns an instance of <i>EmptySettings</i> currently! 293 * Returns an instance of <i>EmptySettings</i> currently!
349 * 294 *
350 * @return an instance of <i>EmptySettings</i>. 295 * @return an instance of <i>EmptySettings</i>.
352 @Override 297 @Override
353 public Settings getSettings() { 298 public Settings getSettings() {
354 return new EmptySettings(); 299 return new EmptySettings();
355 } 300 }
356 301
357 302 @Override
358 @Override 303 public void setSettings(final Settings settings) {
359 public void setSettings(Settings settings) {
360 this.settings = settings; 304 this.settings = settings;
361 } 305 }
362 } 306 }
363 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 307 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org