Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/MapfileGenerator.java @ 1792:49ad801076e4
Enabled the MapGenerator to create WMS layers with database datastore - riveraxis is the first layer that is no longer fetched from static WMS service but from user specific one.
flys-artifacts/trunk@3115 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 28 Oct 2011 13:48:02 +0000 |
parents | ef2300b450bf |
children | 1636686070f7 |
comparison
equal
deleted
inserted
replaced
1791:e0e5a5b51a40 | 1792:49ad801076e4 |
---|---|
3 import java.io.File; | 3 import java.io.File; |
4 import java.io.FilenameFilter; | 4 import java.io.FilenameFilter; |
5 import java.io.FileNotFoundException; | 5 import java.io.FileNotFoundException; |
6 import java.io.FileWriter; | 6 import java.io.FileWriter; |
7 import java.io.IOException; | 7 import java.io.IOException; |
8 import java.io.StringWriter; | |
9 import java.io.Writer; | 8 import java.io.Writer; |
10 | 9 |
11 import java.util.ArrayList; | 10 import java.util.ArrayList; |
12 import java.util.Date; | 11 import java.util.Date; |
13 import java.util.List; | 12 import java.util.List; |
19 import org.apache.velocity.app.VelocityEngine; | 18 import org.apache.velocity.app.VelocityEngine; |
20 | 19 |
21 import de.intevation.artifacts.common.utils.Config; | 20 import de.intevation.artifacts.common.utils.Config; |
22 | 21 |
23 import de.intevation.flys.artifacts.FLYSArtifact; | 22 import de.intevation.flys.artifacts.FLYSArtifact; |
23 import de.intevation.flys.artifacts.model.DBLayerInfo; | |
24 import de.intevation.flys.artifacts.model.LayerInfo; | 24 import de.intevation.flys.artifacts.model.LayerInfo; |
25 import de.intevation.flys.artifacts.model.WMSLayerFacet; | 25 import de.intevation.flys.artifacts.model.WMSLayerFacet; |
26 import de.intevation.flys.artifacts.model.WMSDBLayerFacet; | |
26 | 27 |
27 /** | 28 /** |
28 * This class iterates over a bunch of directories, searches for meta | 29 * This class iterates over a bunch of directories, searches for meta |
29 * information coresponding to shapefiles and creates a mapfile which is used by | 30 * information coresponding to shapefiles and creates a mapfile which is used by |
30 * a <i>MapServer</i>. | 31 * a <i>MapServer</i>. |
37 public static final String WSPLGEN_RESULT_SHAPE = "wsplgen.shp"; | 38 public static final String WSPLGEN_RESULT_SHAPE = "wsplgen.shp"; |
38 public static final String WSPLGEN_LINES_SHAPE = "barrier_lines.shp"; | 39 public static final String WSPLGEN_LINES_SHAPE = "barrier_lines.shp"; |
39 public static final String WSPLGEN_POLYGONS_SHAPE = "barrier_polygons.shp"; | 40 public static final String WSPLGEN_POLYGONS_SHAPE = "barrier_polygons.shp"; |
40 | 41 |
41 public static final String SHP_LAYER_TEMPLATE = "shapefile_layer.vm"; | 42 public static final String SHP_LAYER_TEMPLATE = "shapefile_layer.vm"; |
43 public static final String DB_LAYER_TEMPLATE = "db_layer.vm"; | |
42 | 44 |
43 public static final String MS_WSPLGEN_POSTFIX = "-wsplgen"; | 45 public static final String MS_WSPLGEN_POSTFIX = "-wsplgen"; |
44 public static final String MS_BARRIERS_POSTFIX = "-barriers"; | 46 public static final String MS_BARRIERS_POSTFIX = "-barriers"; |
45 public static final String MS_LINE_POSTFIX = "-lines"; | 47 public static final String MS_LINE_POSTFIX = "-lines"; |
46 public static final String MS_POLYGONS_POSTFIX = "-polygons"; | 48 public static final String MS_POLYGONS_POSTFIX = "-polygons"; |
218 | 220 |
219 engine.init(); | 221 engine.init(); |
220 } | 222 } |
221 | 223 |
222 | 224 |
225 protected VelocityContext getVelocityContext() { | |
226 VelocityContext context = new VelocityContext(); | |
227 | |
228 try { | |
229 context.put("MAPSERVERURL", | |
230 FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL)); | |
231 context.put("SHAPEFILEPATH", | |
232 getShapefileBaseDir().getCanonicalPath()); | |
233 context.put("CONFIGDIR", | |
234 Config.getConfigDirectory().getCanonicalPath()); | |
235 } | |
236 catch (FileNotFoundException fnfe) { | |
237 // this is bad | |
238 } | |
239 catch (IOException ioe) { | |
240 // this is also bad | |
241 } | |
242 | |
243 return context; | |
244 } | |
245 | |
246 | |
223 /** | 247 /** |
224 * Returns a template specified by <i>model</i>. | 248 * Returns a template specified by <i>model</i>. |
225 * | 249 * |
226 * @param model The name of the template. | 250 * @param model The name of the template. |
227 * @return a template. | 251 * @return a template. |
270 * @return the shapefile base directory. | 294 * @return the shapefile base directory. |
271 * | 295 * |
272 * @throws FileNotFoundException if no shapefile path is found or | 296 * @throws FileNotFoundException if no shapefile path is found or |
273 * configured. | 297 * configured. |
274 */ | 298 */ |
275 protected File getShapefileBaseDir() | 299 public File getShapefileBaseDir() |
276 throws FileNotFoundException, IOException | 300 throws FileNotFoundException, IOException |
277 { | 301 { |
278 if (shapefileDirectory == null) { | 302 if (shapefileDirectory == null) { |
279 String path = FLYSUtils.getXPathString( | 303 String path = FLYSUtils.getXPathString( |
280 FLYSUtils.XPATH_SHAPEFILE_DIR); | 304 FLYSUtils.XPATH_SHAPEFILE_DIR); |
434 } | 458 } |
435 } | 459 } |
436 | 460 |
437 | 461 |
438 /** | 462 /** |
463 * Creates a layer file used for Mapserver's mapfile which represents | |
464 * geometries from database. | |
465 * | |
466 * @param flys The FLYSArtifact that owns <i>wms</i>. | |
467 * @param wms The WMSLayerFacet that contains information for the layer. | |
468 */ | |
469 public void createDatabaseLayer(FLYSArtifact flys, WMSDBLayerFacet wms) | |
470 throws FileNotFoundException, IOException | |
471 { | |
472 logger.debug("createDatabaseLayer"); | |
473 | |
474 LayerInfo layerinfo = new DBLayerInfo( | |
475 flys.identifier() + "-" + wms.getName(), | |
476 "LINE", | |
477 wms.getFilter(), | |
478 wms.getData(), | |
479 wms.getDescription()); | |
480 | |
481 String name = MS_LAYER_PREFIX + wms.getName(); | |
482 | |
483 Template template = getTemplateByName(DB_LAYER_TEMPLATE); | |
484 if (template == null) { | |
485 logger.warn("Template '" + DB_LAYER_TEMPLATE + "' found."); | |
486 return; | |
487 } | |
488 | |
489 try { | |
490 File dir = new File(getShapefileBaseDir(), flys.identifier()); | |
491 writeLayer(layerinfo, dir, name, template); | |
492 } | |
493 catch (FileNotFoundException fnfe) { | |
494 logger.error(fnfe, fnfe); | |
495 logger.warn("Unable to write layer: " + name); | |
496 } | |
497 } | |
498 | |
499 | |
500 /** | |
439 * Creates a layer snippet which might be included in the mapfile. | 501 * Creates a layer snippet which might be included in the mapfile. |
440 * | 502 * |
441 * @param layerinfo A LayerInfo object that contains all necessary | 503 * @param layerinfo A LayerInfo object that contains all necessary |
442 * information to build a Mapserver LAYER section. | 504 * information to build a Mapserver LAYER section. |
443 * @param dir The base dir for the LAYER snippet. | 505 * @param dir The base dir for the LAYER snippet. |
463 Writer writer = null; | 525 Writer writer = null; |
464 | 526 |
465 try { | 527 try { |
466 writer = new FileWriter(layer); | 528 writer = new FileWriter(layer); |
467 | 529 |
468 VelocityContext context = new VelocityContext(); | 530 VelocityContext context = getVelocityContext(); |
469 context.put("LAYER", layerinfo); | 531 context.put("LAYER", layerinfo); |
470 | 532 |
471 tpl.merge(context, writer); | 533 tpl.merge(context, writer); |
472 } | 534 } |
473 catch (FileNotFoundException fnfe) { | 535 catch (FileNotFoundException fnfe) { |
510 tmp = new File(mapfile.getParent(), tmpMapName); | 572 tmp = new File(mapfile.getParent(), tmpMapName); |
511 tmp.createNewFile(); | 573 tmp.createNewFile(); |
512 | 574 |
513 writer = new FileWriter(tmp); | 575 writer = new FileWriter(tmp); |
514 | 576 |
515 VelocityContext context = new VelocityContext(); | 577 VelocityContext context = getVelocityContext(); |
516 context.put("MAPSERVERURL", | |
517 FLYSUtils.getXPathString(FLYSUtils.XPATH_MAPSERVER_URL)); | |
518 context.put("SHAPEFILEPATH", | |
519 getShapefileBaseDir().getCanonicalPath()); | |
520 context.put("CONFIGDIR", | |
521 Config.getConfigDirectory().getCanonicalPath()); | |
522 context.put("LAYERS", layers); | 578 context.put("LAYERS", layers); |
523 | 579 |
524 Template mapTemplate = getMapfileTemplate(); | 580 Template mapTemplate = getMapfileTemplate(); |
525 if (mapTemplate == null) { | 581 if (mapTemplate == null) { |
526 logger.warn("No mapfile template found."); | 582 logger.warn("No mapfile template found."); |