comparison flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java @ 4656:442fbb290fa8

Introduce river mapfile generation (WARNING: Changes in config xml files required!). On startup the RiverMapfileGeneratorStarter is called by the server app. The starter invokes a call to RiverMapfileGenerator.generate() which reads all available rivers from the database and generates layers for each river (in theory...uncompleted) in a river.map file. The old MapfileGenerator which generates layers that have artifact dependencies is now in ArtifactMapfileGenerator.
author Christian Lins <christian.lins@intevation.de>
date Tue, 11 Dec 2012 15:24:24 +0100
parents 861c47e0a8a0
children 38ec171544de
comparison
equal deleted inserted replaced
4655:cd44d28d0fbc 4656:442fbb290fa8
15 15
16 import org.apache.log4j.Logger; 16 import org.apache.log4j.Logger;
17 import org.apache.velocity.Template; 17 import org.apache.velocity.Template;
18 import org.apache.velocity.VelocityContext; 18 import org.apache.velocity.VelocityContext;
19 import org.apache.velocity.app.VelocityEngine; 19 import org.apache.velocity.app.VelocityEngine;
20 import org.apache.velocity.runtime.RuntimeConstants;
20 21
21 /** 22 /**
22 * This class iterates over a bunch of directories, searches for meta 23 * This class iterates over a bunch of directories, searches for meta
23 * information coresponding to shapefiles and creates a mapfile which is used by 24 * information coresponding to shapefiles and creates a mapfile which is used by
24 * a <i>MapServer</i>. 25 * a <i>MapServer</i>.
65 Template template = getTemplateByName(templateID); 66 Template template = getTemplateByName(templateID);
66 return template != null; 67 return template != null;
67 } 68 }
68 69
69 70
70 protected abstract void generate() throws Exception; 71 public abstract void generate() throws Exception;
71 72
72 73
73 /** 74 /**
74 * Returns the VelocityEngine used for the template mechanism. 75 * Returns the VelocityEngine used for the template mechanism.
75 * 76 *
102 engine.setProperty( 103 engine.setProperty(
103 "input.encoding", 104 "input.encoding",
104 "UTF-8"); 105 "UTF-8");
105 106
106 engine.setProperty( 107 engine.setProperty(
107 VelocityEngine.RUNTIME_LOG, 108 RuntimeConstants.RUNTIME_LOG,
108 FLYSUtils.getXPathString(FLYSUtils.XPATH_VELOCITY_LOGFILE)); 109 getVelocityLogfile());
109 110
110 engine.setProperty( 111 engine.setProperty(
111 "resource.loader", 112 "resource.loader",
112 "file"); 113 "file");
113 114
114 engine.setProperty( 115 engine.setProperty(
115 "file.resource.loader.path", 116 "file.resource.loader.path",
116 FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_TEMPLATE_PATH)); 117 getMapserverTemplatePath());
117 118
118 engine.init(); 119 engine.init();
119 } 120 }
120 121
122 protected abstract String getVelocityLogfile();
123
124 protected abstract String getMapserverTemplatePath();
125
126 protected abstract String getMapserverUrl();
121 127
122 protected VelocityContext getVelocityContext() { 128 protected VelocityContext getVelocityContext() {
123 VelocityContext context = new VelocityContext(); 129 VelocityContext context = new VelocityContext();
124 130
125 try { 131 try {
126 context.put("MAPSERVERURL", 132 context.put("MAPSERVERURL",
127 FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL)); 133 getMapserverUrl());
128 context.put("SHAPEFILEPATH", 134 context.put("SHAPEFILEPATH",
129 getShapefileBaseDir().getCanonicalPath()); 135 getShapefileBaseDir().getCanonicalPath());
130 context.put("CONFIGDIR", 136 context.put("CONFIGDIR",
131 Config.getConfigDirectory().getCanonicalPath()); 137 Config.getConfigDirectory().getCanonicalPath());
132 } 138 }
175 * Returns the mapfile template. 181 * Returns the mapfile template.
176 * 182 *
177 * @return the mapfile template. 183 * @return the mapfile template.
178 * @throws Exception if an error occured while reading the configuration. 184 * @throws Exception if an error occured while reading the configuration.
179 */ 185 */
180 protected Template getMapfileTemplate() 186 protected Template getMapfileTemplateObj()
181 throws Exception 187 throws Exception
182 { 188 {
183 String mapfileName = FLYSUtils.getXPathString( 189 String mapfileName = FLYSUtils.getXPathString(
184 FLYSUtils.XPATH_MAPFILE_TEMPLATE); 190 FLYSUtils.XPATH_FLOODMAP_MAPFILE_TEMPLATE);
185 191
186 return getTemplateByName(mapfileName); 192 return getTemplateByName(mapfileName);
187 } 193 }
194
195 protected abstract String getMapfilePath();
196
197 protected abstract String getMapfileTemplate();
188 198
189 199
190 /** 200 /**
191 * Returns the base directory storing the shapefiles. 201 * Returns the base directory storing the shapefiles.
192 * 202 *
198 public File getShapefileBaseDir() 208 public File getShapefileBaseDir()
199 throws FileNotFoundException, IOException 209 throws FileNotFoundException, IOException
200 { 210 {
201 if (shapefileDirectory == null) { 211 if (shapefileDirectory == null) {
202 String path = FLYSUtils.getXPathString( 212 String path = FLYSUtils.getXPathString(
203 FLYSUtils.XPATH_SHAPEFILE_DIR); 213 FLYSUtils.XPATH_FLOODMAP_SHAPEFILE_DIR);
204 214
205 if (path != null) { 215 if (path != null) {
206 shapefileDirectory = new File(path); 216 shapefileDirectory = new File(path);
207 } 217 }
208 218
268 * @param filename The name of the file that is written. 278 * @param filename The name of the file that is written.
269 * @param tpl The Velocity template which is used to create the LAYER 279 * @param tpl The Velocity template which is used to create the LAYER
270 * section. 280 * section.
271 */ 281 */
272 protected void writeLayer( 282 protected void writeLayer(
273 LayerInfo layerinfo, 283 LayerInfo layerInfo,
274 File dir, 284 File layerFile,
275 String filename,
276 Template tpl 285 Template tpl
277 ) 286 )
278 throws FileNotFoundException 287 throws FileNotFoundException
279 { 288 {
280 if (logger.isDebugEnabled()) { 289 if (logger.isDebugEnabled()) {
281 logger.debug("Write layer for:"); 290 logger.debug("Write layer for:");
282 logger.debug(" directory: " + dir.getName()); 291 logger.debug(" directory/file: " + layerFile.getName());
283 logger.debug(" name: " + filename); 292 }
284 } 293
285
286 File layer = new File(dir, filename);
287 Writer writer = null; 294 Writer writer = null;
288 295
289 try { 296 try {
290 writer = new FileWriter(layer); 297 writer = new FileWriter(layerFile);
291 298
292 VelocityContext context = getVelocityContext(); 299 VelocityContext context = getVelocityContext();
293 context.put("LAYER", layerinfo); 300 context.put("LAYER", layerInfo);
294 301
295 tpl.merge(context, writer); 302 tpl.merge(context, writer);
296 } 303 }
297 catch (FileNotFoundException fnfe) { 304 catch (FileNotFoundException fnfe) {
298 logger.error(fnfe, fnfe); 305 logger.error(fnfe, fnfe);
322 * @param layers Layer information. 329 * @param layers Layer information.
323 */ 330 */
324 protected void writeMapfile(List<String> layers) { 331 protected void writeMapfile(List<String> layers) {
325 String tmpMapName = "mapfile" + new Date().getTime(); 332 String tmpMapName = "mapfile" + new Date().getTime();
326 333
327 File mapfile = new File( 334 File mapfile = new File(getMapfilePath());
328 FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPFILE_PATH));
329 335
330 File tmp = null; 336 File tmp = null;
331 Writer writer = null; 337 Writer writer = null;
332 338
333 try { 339 try {
337 writer = new FileWriter(tmp); 343 writer = new FileWriter(tmp);
338 344
339 VelocityContext context = getVelocityContext(); 345 VelocityContext context = getVelocityContext();
340 context.put("LAYERS", layers); 346 context.put("LAYERS", layers);
341 347
342 Template mapTemplate = getMapfileTemplate(); 348 Template mapTemplate = getMapfileTemplateObj();
343 if (mapTemplate == null) { 349 if (mapTemplate == null) {
344 logger.warn("No mapfile template found."); 350 logger.warn("No mapfile template found.");
345 return; 351 return;
346 } 352 }
347 353

http://dive4elements.wald.intevation.org