comparison flys-artifacts/src/main/java/de/intevation/flys/mapserver/MapfileGenerator.java @ 5022:a9243df307b1 mapgenfix

Move all classes of mapfile generation to de.intevation.flys.mapserver package.
author Christian Lins <christian.lins@intevation.de>
date Tue, 19 Feb 2013 13:41:20 +0100
parents
children 1da61095040c
comparison
equal deleted inserted replaced
5009:7c8ce0a95a64 5022:a9243df307b1
1 package de.intevation.flys.mapserver;
2
3 import de.intevation.artifacts.common.utils.Config;
4 import de.intevation.flys.artifacts.model.LayerInfo;
5 import de.intevation.flys.utils.FLYSUtils;
6
7 import java.io.File;
8 import java.io.FileNotFoundException;
9 import java.io.FileWriter;
10 import java.io.FilenameFilter;
11 import java.io.IOException;
12 import java.io.Writer;
13 import java.util.ArrayList;
14 import java.util.Date;
15 import java.util.List;
16
17 import org.apache.log4j.Logger;
18 import org.apache.velocity.Template;
19 import org.apache.velocity.VelocityContext;
20 import org.apache.velocity.app.VelocityEngine;
21 import org.apache.velocity.runtime.RuntimeConstants;
22
23 /**
24 * This class iterates over a bunch of directories, searches for meta
25 * information coresponding to shapefiles and creates a mapfile which is used by
26 * a <i>MapServer</i>.
27 *
28 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
29 */
30 public abstract class MapfileGenerator
31 {
32 public static final String WSPLGEN_RESULT_SHAPE = "wsplgen.shp";
33 public static final String WSPLGEN_LINES_SHAPE = "barrier_lines.shp";
34 public static final String WSPLGEN_POLYGONS_SHAPE = "barrier_polygons.shp";
35 public static final String WSPLGEN_USER_SHAPE = "user-rgd.shp";
36
37 public static final String WSPLGEN_LAYER_TEMPLATE = "wsplgen_layer.vm";
38 public static final String SHP_LAYER_TEMPLATE = "shapefile_layer.vm";
39 public static final String DB_LAYER_TEMPLATE = "db_layer.vm";
40 public static final String RIVERAXIS_LAYER_TEMPLATE = "riveraxis-layer.vm";
41
42 public static final String MS_WSPLGEN_PREFIX = "wsplgen-";
43 public static final String MS_BARRIERS_PREFIX = "barriers-";
44 public static final String MS_LINE_PREFIX = "lines-";
45 public static final String MS_POLYGONS_PREFIX = "polygons-";
46 public static final String MS_LAYER_PREFIX = "ms_layer-";
47 public static final String MS_USERSHAPE_PREFIX = "user-";
48
49 private static Logger logger = Logger.getLogger(MapfileGenerator.class);
50
51 private File shapefileDirectory;
52
53 private VelocityEngine velocityEngine;
54
55
56 protected MapfileGenerator() {
57 }
58
59
60 /**
61 * Method to check the existance of a template file.
62 *
63 * @param templateID The name of a template.
64 * @return true, of the template exists - otherwise false.
65 */
66 public boolean templateExists(String templateID){
67 Template template = getTemplateByName(templateID);
68 return template != null;
69 }
70
71
72 public abstract void generate() throws Exception;
73
74
75 /**
76 * Returns the VelocityEngine used for the template mechanism.
77 *
78 * @return the velocity engine.
79 */
80 protected VelocityEngine getVelocityEngine() {
81 if (velocityEngine == null) {
82 velocityEngine = new VelocityEngine();
83 try {
84 setupVelocity(velocityEngine);
85 }
86 catch (Exception e) {
87 logger.error(e, e);
88 return null;
89 }
90 }
91 return velocityEngine;
92 }
93
94
95 /**
96 * Initialize velocity.
97 *
98 * @param engine Velocity engine.
99 * @throws Exception if an error occured while initializing velocity.
100 */
101 protected void setupVelocity(VelocityEngine engine)
102 throws Exception
103 {
104 engine.setProperty(
105 "input.encoding",
106 "UTF-8");
107
108 engine.setProperty(
109 RuntimeConstants.RUNTIME_LOG,
110 getVelocityLogfile());
111
112 engine.setProperty(
113 "resource.loader",
114 "file");
115
116 engine.setProperty(
117 "file.resource.loader.path",
118 getMapserverTemplatePath());
119
120 engine.init();
121 }
122
123 protected abstract String getVelocityLogfile();
124
125 protected abstract String getMapserverTemplatePath();
126
127 protected abstract String getMapserverUrl();
128
129 protected VelocityContext getVelocityContext() {
130 VelocityContext context = new VelocityContext();
131
132 try {
133 context.put("MAPSERVERURL",
134 getMapserverUrl());
135 context.put("SHAPEFILEPATH",
136 getShapefileBaseDir().getCanonicalPath());
137 context.put("CONFIGDIR",
138 Config.getConfigDirectory().getCanonicalPath());
139 }
140 catch (FileNotFoundException fnfe) {
141 // this is bad
142 logger.warn(fnfe, fnfe);
143 }
144 catch (IOException ioe) {
145 // this is also bad
146 logger.warn(ioe, ioe);
147 }
148
149 return context;
150 }
151
152
153 /**
154 * Returns a template specified by <i>model</i>.
155 *
156 * @param model The name of the template.
157 * @return a template.
158 */
159 protected Template getTemplateByName(String model) {
160 if (model.indexOf(".vm") < 0) {
161 model = model.concat(".vm");
162 }
163
164 try {
165 VelocityEngine engine = getVelocityEngine();
166 if (engine == null) {
167 logger.error("Error while fetching VelocityEngine.");
168 return null;
169 }
170
171 return engine.getTemplate(model);
172 }
173 catch (Exception e) {
174 logger.warn(e, e);
175 }
176
177 return null;
178 }
179
180
181 /**
182 * Returns the mapfile template.
183 *
184 * @return the mapfile template.
185 * @throws Exception if an error occured while reading the configuration.
186 */
187 protected Template getMapfileTemplateObj()
188 throws Exception
189 {
190 String mapfileName = getMapfileTemplate();
191 return getTemplateByName(mapfileName);
192 }
193
194 protected abstract String getMapfilePath();
195
196 protected abstract String getMapfileTemplate();
197
198
199 /**
200 * Returns the base directory storing the shapefiles.
201 *
202 * @return the shapefile base directory.
203 *
204 * @throws FileNotFoundException if no shapefile path is found or
205 * configured.
206 */
207 public File getShapefileBaseDir()
208 throws FileNotFoundException, IOException
209 {
210 if (shapefileDirectory == null) {
211 String path = FLYSUtils.getXPathString(
212 FLYSUtils.XPATH_FLOODMAP_SHAPEFILE_DIR);
213
214 if (path != null) {
215 shapefileDirectory = new File(path);
216 }
217
218 if (shapefileDirectory == null) {
219 throw new FileNotFoundException("No shapefile directory given");
220 }
221
222 if (!shapefileDirectory.exists()) {
223 shapefileDirectory.mkdirs();
224 }
225 }
226
227 return shapefileDirectory;
228 }
229
230
231 protected File[] getUserDirs()
232 throws FileNotFoundException, IOException
233 {
234 File baseDir = getShapefileBaseDir();
235 File[] artifactDirs = baseDir.listFiles();
236
237 // TODO ONLY RETURN DIRECTORIES OF THE SPECIFIED USER
238
239 return artifactDirs;
240 }
241
242
243 protected List<String> parseLayers(File[] dirs) {
244 List<String> layers = new ArrayList<String>();
245
246 for (File dir: dirs) {
247 File[] layerFiles = dir.listFiles(new FilenameFilter() {
248 @Override
249 public boolean accept(File directory, String name) {
250 return name.startsWith(MS_LAYER_PREFIX);
251 }
252 });
253
254 for (File layer: layerFiles) {
255 try {
256 layers.add(layer.getCanonicalPath());
257 }
258 catch (IOException ioe) {
259 logger.warn(ioe, ioe);
260 }
261 }
262 }
263
264 return layers;
265 }
266
267
268 /**
269 * Creates a layer snippet which might be included in the mapfile.
270 *
271 * @param layerinfo A LayerInfo object that contains all necessary
272 * information to build a Mapserver LAYER section.
273 * @param dir The base dir for the LAYER snippet.
274 * @param filename The name of the file that is written.
275 * @param tpl The Velocity template which is used to create the LAYER
276 * section.
277 */
278 protected void writeLayer(
279 LayerInfo layerInfo,
280 File layerFile,
281 Template tpl
282 )
283 throws FileNotFoundException
284 {
285 if (logger.isDebugEnabled()) {
286 logger.debug("Write layer for:");
287 logger.debug(" directory/file: " + layerFile.getName());
288 }
289
290 Writer writer = null;
291
292 try {
293 writer = new FileWriter(layerFile);
294
295 VelocityContext context = getVelocityContext();
296 context.put("LAYER", layerInfo);
297
298 tpl.merge(context, writer);
299 }
300 catch (FileNotFoundException fnfe) {
301 logger.error(fnfe, fnfe);
302 }
303 catch (IOException ioe) {
304 logger.error(ioe, ioe);
305 }
306 catch (Exception e) {
307 logger.error(e, e);
308 }
309 finally {
310 try {
311 if (writer != null) {
312 writer.close();
313 }
314 }
315 catch (IOException ioe) {
316 logger.debug(ioe, ioe);
317 }
318 }
319 }
320
321
322 /**
323 * Creates a mapfile with the layer information stored in <i>layers</i>.
324 *
325 * @param layers Layer information.
326 */
327 protected void writeMapfile(List<String> layers) {
328 String tmpMapName = "mapfile" + new Date().getTime();
329
330 File mapfile = new File(getMapfilePath());
331
332 File tmp = null;
333 Writer writer = null;
334
335 try {
336 tmp = new File(mapfile.getParent(), tmpMapName);
337 tmp.createNewFile();
338
339 writer = new FileWriter(tmp);
340
341 VelocityContext context = getVelocityContext();
342 context.put("LAYERS", layers);
343
344 Template mapTemplate = getMapfileTemplateObj();
345 if (mapTemplate == null) {
346 logger.warn("No mapfile template found.");
347 return;
348 }
349
350 mapTemplate.merge(context, writer);
351
352 // we need to create a temporary mapfile first und rename it into
353 // real mapfile because we don't run into race conditions on this
354 // way. (iw)
355 tmp.renameTo(mapfile);
356 }
357 catch (FileNotFoundException fnfe) {
358 logger.error(fnfe, fnfe);
359 }
360 catch (IOException ioe) {
361 logger.error(ioe, ioe);
362 }
363 catch (Exception e) {
364 logger.error(e, e);
365 }
366 finally {
367 try {
368 if (writer != null) {
369 writer.close();
370 }
371
372 if (tmp.exists()) {
373 tmp.delete();
374 }
375 }
376 catch (IOException ioe) {
377 logger.debug(ioe, ioe);
378 }
379 }
380 }
381 }
382 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org