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