Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/ArtefactMapfileGenerator.java @ 4654:861c47e0a8a0
Refactor Mapserver mapfile generation.
Prepares the existing mapfile generation code for
a river axis mapfile generator.
Removes unused asynchronous mapfile generation to
reduce complexity.
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Sat, 08 Dec 2012 00:19:29 +0100 |
parents | |
children | 442fbb290fa8 |
comparison
equal
deleted
inserted
replaced
4653:a93699cb31eb | 4654:861c47e0a8a0 |
---|---|
1 package de.intevation.flys.utils; | |
2 | |
3 import de.intevation.artifacts.CallContext; | |
4 import de.intevation.flys.artifacts.FLYSArtifact; | |
5 import de.intevation.flys.artifacts.model.LayerInfo; | |
6 import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet; | |
7 import de.intevation.flys.artifacts.model.map.WMSLayerFacet; | |
8 import de.intevation.flys.artifacts.model.map.WSPLGENLayerFacet; | |
9 import de.intevation.flys.artifacts.resources.Resources; | |
10 | |
11 import java.io.File; | |
12 import java.io.FileNotFoundException; | |
13 import java.io.IOException; | |
14 import java.util.List; | |
15 | |
16 import org.apache.log4j.Logger; | |
17 import org.apache.velocity.Template; | |
18 import org.geotools.data.shapefile.ShpFiles; | |
19 import org.geotools.data.shapefile.shp.ShapefileHeader; | |
20 import org.geotools.data.shapefile.shp.ShapefileReader; | |
21 | |
22 public class ArtefactMapfileGenerator extends MapfileGenerator { | |
23 | |
24 private static Logger logger = Logger.getLogger(ArtefactMapfileGenerator.class); | |
25 | |
26 /** | |
27 * Method which starts searching for meta information file and mapfile | |
28 * generation. | |
29 */ | |
30 @Override | |
31 public void generate() throws IOException | |
32 { | |
33 File[] userDirs = getUserDirs(); | |
34 List<String> layers = parseLayers(userDirs); | |
35 logger.info("Found " + layers.size() + " layers for user mapfile."); | |
36 | |
37 writeMapfile(layers); | |
38 } | |
39 | |
40 /** | |
41 * Creates a layer file used for Mapserver's mapfile which represents the | |
42 * floodmap. | |
43 * | |
44 * @param flys The FLYSArtifact that owns <i>wms</i>. | |
45 * @param wms The WMSLayerFacet that contains information for the layer. | |
46 */ | |
47 public void createUeskLayer( | |
48 FLYSArtifact flys, | |
49 WSPLGENLayerFacet wms, | |
50 String style, | |
51 CallContext context | |
52 ) throws FileNotFoundException, IOException | |
53 { | |
54 logger.debug("createUeskLayer"); | |
55 | |
56 LayerInfo layerinfo = new LayerInfo(); | |
57 layerinfo.setName(MS_WSPLGEN_PREFIX + flys.identifier()); | |
58 layerinfo.setType("POLYGON"); | |
59 layerinfo.setDirectory(flys.identifier()); | |
60 layerinfo.setData(WSPLGEN_RESULT_SHAPE); | |
61 layerinfo.setTitle(Resources.getMsg(Resources.getLocale(context.getMeta()), | |
62 "floodmap.uesk", | |
63 "Floodmap")); | |
64 layerinfo.setStyle(style); | |
65 layerinfo.setSrid(wms.getSrid()); | |
66 | |
67 String name = MS_LAYER_PREFIX + wms.getName(); | |
68 | |
69 Template template = getTemplateByName(WSPLGEN_LAYER_TEMPLATE); | |
70 if (template == null) { | |
71 logger.warn("Template '" + WSPLGEN_LAYER_TEMPLATE + "' found."); | |
72 return; | |
73 } | |
74 | |
75 try { | |
76 File dir = new File(getShapefileBaseDir(), flys.identifier()); | |
77 writeLayer(layerinfo, dir, name, template); | |
78 } | |
79 catch (FileNotFoundException fnfe) { | |
80 logger.error(fnfe, fnfe); | |
81 logger.warn("Unable to write layer: " + name); | |
82 } | |
83 } | |
84 | |
85 | |
86 /** | |
87 * Creates a layer file used for Mapserver's mapfile which represents the | |
88 * user defined barriers. | |
89 * | |
90 * @param flys The FLYSArtifact that owns <i>wms</i>. | |
91 * @param wms The WMSLayerFacet that contains information for the layer. | |
92 */ | |
93 public void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms) | |
94 throws FileNotFoundException, IOException | |
95 { | |
96 logger.debug("createBarriersLayer"); | |
97 | |
98 //String uuid = flys.identifier(); | |
99 //File dir = new File(getShapefileBaseDir(), uuid); | |
100 | |
101 createBarriersLineLayer(flys, wms); | |
102 createBarriersPolygonLayer(flys, wms); | |
103 } | |
104 | |
105 | |
106 protected void createBarriersLineLayer( | |
107 FLYSArtifact flys, | |
108 WMSLayerFacet wms | |
109 ) | |
110 throws FileNotFoundException, IOException | |
111 { | |
112 String uuid = flys.identifier(); | |
113 String group = MS_BARRIERS_PREFIX + uuid; | |
114 String groupTitle = "I18N_BARRIERS_TITLE"; | |
115 | |
116 File dir = new File(getShapefileBaseDir(), uuid); | |
117 File test = new File(dir, WSPLGEN_LINES_SHAPE); | |
118 | |
119 if (!test.exists() || !test.canRead()) { | |
120 logger.debug("No barrier line layer existing."); | |
121 return; | |
122 } | |
123 | |
124 LayerInfo lineInfo = new LayerInfo(); | |
125 lineInfo.setName(MS_LINE_PREFIX + uuid); | |
126 lineInfo.setType("LINE"); | |
127 lineInfo.setDirectory(uuid); | |
128 lineInfo.setData(WSPLGEN_LINES_SHAPE); | |
129 lineInfo.setTitle("I18N_LINE_SHAPE"); | |
130 lineInfo.setGroup(group); | |
131 lineInfo.setGroupTitle(groupTitle); | |
132 lineInfo.setSrid(wms.getSrid()); | |
133 | |
134 String nameLines = MS_LAYER_PREFIX + wms.getName() + "-lines"; | |
135 | |
136 Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE); | |
137 if (tpl == null) { | |
138 logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found."); | |
139 return; | |
140 } | |
141 | |
142 try { | |
143 writeLayer(lineInfo, dir, nameLines, tpl); | |
144 } | |
145 catch (FileNotFoundException fnfe) { | |
146 logger.error(fnfe, fnfe); | |
147 logger.warn("Unable to write layer: " + nameLines); | |
148 } | |
149 } | |
150 | |
151 protected void createBarriersPolygonLayer( | |
152 FLYSArtifact flys, | |
153 WMSLayerFacet wms | |
154 ) | |
155 throws FileNotFoundException, IOException | |
156 { | |
157 String uuid = flys.identifier(); | |
158 String group = uuid + MS_BARRIERS_PREFIX; | |
159 String groupTitle = "I18N_BARRIERS_TITLE"; | |
160 | |
161 File dir = new File(getShapefileBaseDir(), uuid); | |
162 File test = new File(dir, WSPLGEN_POLYGONS_SHAPE); | |
163 | |
164 if (!test.exists() || !test.canRead()) { | |
165 logger.debug("No barrier line layer existing."); | |
166 return; | |
167 } | |
168 | |
169 LayerInfo polygonInfo = new LayerInfo(); | |
170 polygonInfo.setName(MS_POLYGONS_PREFIX + uuid); | |
171 polygonInfo.setType("POLYGON"); | |
172 polygonInfo.setDirectory(uuid); | |
173 polygonInfo.setData(WSPLGEN_POLYGONS_SHAPE); | |
174 polygonInfo.setTitle("I18N_POLYGON_SHAPE"); | |
175 polygonInfo.setGroup(group); | |
176 polygonInfo.setGroupTitle(groupTitle); | |
177 polygonInfo.setSrid(wms.getSrid()); | |
178 | |
179 String namePolygons = MS_LAYER_PREFIX + wms.getName() + "-polygons"; | |
180 | |
181 Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE); | |
182 if (tpl == null) { | |
183 logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found."); | |
184 return; | |
185 } | |
186 | |
187 try { | |
188 writeLayer(polygonInfo, dir, namePolygons, tpl); | |
189 } | |
190 catch (FileNotFoundException fnfe) { | |
191 logger.error(fnfe, fnfe); | |
192 logger.warn("Unable to write layer: " + namePolygons); | |
193 } | |
194 } | |
195 | |
196 | |
197 /** | |
198 * Creates a layer file used for Mapserver's mapfile which represents the | |
199 * shape files uploaded by the user. | |
200 * | |
201 * @param flys The FLYSArtifact that owns <i>wms</i>. | |
202 * @param wms The WMSLayerFacet that contains information for the layer. | |
203 */ | |
204 public void createUserShapeLayer(FLYSArtifact flys, WMSLayerFacet wms) | |
205 throws FileNotFoundException, IOException | |
206 { | |
207 logger.debug("createUserShapeLayer"); | |
208 | |
209 String uuid = flys.identifier(); | |
210 File dir = new File(getShapefileBaseDir(), uuid); | |
211 File test = new File(dir, WSPLGEN_USER_SHAPE); | |
212 | |
213 if (!test.exists() || !test.canRead()) { | |
214 logger.debug("No user layer existing."); | |
215 return; | |
216 } | |
217 | |
218 File userShape = new File(dir, WSPLGEN_USER_SHAPE); | |
219 ShpFiles sf = new ShpFiles(userShape); | |
220 ShapefileReader sfr = new ShapefileReader(sf, true, false, null); | |
221 ShapefileHeader sfh = sfr.getHeader(); | |
222 | |
223 String group = uuid + MS_USERSHAPE_PREFIX; | |
224 String groupTitle = "I18N_USER_SHAPE_TITLE"; | |
225 | |
226 LayerInfo info = new LayerInfo(); | |
227 info.setName(MS_USERSHAPE_PREFIX + uuid); | |
228 if (sfh.getShapeType().isLineType()) { | |
229 info.setType("LINE"); | |
230 } | |
231 else if (sfh.getShapeType().isPolygonType()) { | |
232 info.setType("POLYGON"); | |
233 } | |
234 else { | |
235 return; | |
236 } | |
237 info.setDirectory(uuid); | |
238 info.setData(WSPLGEN_USER_SHAPE); | |
239 info.setTitle("I18N_USER_SHAPE"); | |
240 info.setGroup(group); | |
241 info.setGroupTitle(groupTitle); | |
242 info.setSrid(wms.getSrid()); | |
243 | |
244 String nameUser = MS_LAYER_PREFIX + wms.getName(); | |
245 | |
246 Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE); | |
247 if (tpl == null) { | |
248 logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found."); | |
249 return; | |
250 } | |
251 | |
252 try { | |
253 writeLayer(info, dir, nameUser, tpl); | |
254 } | |
255 catch (FileNotFoundException fnfe) { | |
256 logger.error(fnfe, fnfe); | |
257 logger.warn("Unable to write layer: " + nameUser); | |
258 } | |
259 | |
260 } | |
261 | |
262 | |
263 /** | |
264 * Creates a layer file used for Mapserver's mapfile which represents | |
265 * geometries from database. | |
266 * | |
267 * @param flys The FLYSArtifact that owns <i>wms</i>. | |
268 * @param wms The WMSLayerFacet that contains information for the layer. | |
269 */ | |
270 public void createDatabaseLayer( | |
271 FLYSArtifact flys, | |
272 WMSDBLayerFacet wms, | |
273 String style | |
274 ) | |
275 throws FileNotFoundException, IOException | |
276 { | |
277 logger.debug("createDatabaseLayer"); | |
278 | |
279 LayerInfo layerinfo = new LayerInfo(); | |
280 layerinfo.setName(wms.getName() + "-" + flys.identifier()); | |
281 layerinfo.setType(wms.getGeometryType()); | |
282 layerinfo.setFilter(wms.getFilter()); | |
283 layerinfo.setData(wms.getData()); | |
284 layerinfo.setTitle(wms.getDescription()); | |
285 layerinfo.setStyle(style); | |
286 if(wms.getExtent() != null) { | |
287 layerinfo.setExtent(GeometryUtils.jtsBoundsToOLBounds(wms.getExtent())); | |
288 } | |
289 layerinfo.setConnection(wms.getConnection()); | |
290 layerinfo.setConnectionType(wms.getConnectionType()); | |
291 layerinfo.setLabelItem(wms.getLabelItem()); | |
292 layerinfo.setSrid(wms.getSrid()); | |
293 | |
294 String name = MS_LAYER_PREFIX + wms.getName(); | |
295 | |
296 Template template = getTemplateByName(DB_LAYER_TEMPLATE); | |
297 if (template == null) { | |
298 logger.warn("Template '" + DB_LAYER_TEMPLATE + "' found."); | |
299 return; | |
300 } | |
301 | |
302 try { | |
303 File dir = new File(getShapefileBaseDir(), flys.identifier()); | |
304 writeLayer(layerinfo, dir, name, template); | |
305 } | |
306 catch (FileNotFoundException fnfe) { | |
307 logger.error(fnfe, fnfe); | |
308 logger.warn("Unable to write layer: " + name); | |
309 } | |
310 } | |
311 | |
312 } |