teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.utils; christian@4654: christian@4809: import com.vividsolutions.jts.geom.Envelope; rrenkert@5140: import com.vividsolutions.jts.geom.MultiLineString; christian@4809: tom@8802: import org.dive4elements.artifacts.common.utils.Config; tom@8802: teichmann@5831: import org.dive4elements.river.artifacts.model.LayerInfo; teichmann@5831: import org.dive4elements.river.artifacts.model.RiverFactory; teichmann@4843: teichmann@5831: import org.dive4elements.river.model.River; teichmann@5831: import org.dive4elements.river.model.RiverAxis; christian@4654: christian@4656: import java.io.File; christian@4656: import java.io.FileNotFoundException; teichmann@4843: christian@4702: import java.util.ArrayList; christian@4654: import java.util.List; teichmann@4843: rrenkert@4841: import java.util.regex.Pattern; christian@4654: christian@4656: import org.apache.log4j.Logger; teichmann@4843: christian@4656: import org.apache.velocity.Template; rrenkert@5181: import org.hibernate.HibernateException; christian@4656: christian@4654: public class RiverMapfileGenerator extends MapfileGenerator { christian@4654: christian@4703: public static final String XPATH_RIVERMAP_SHAPEFILE_DIR = christian@4703: "/artifact-database/rivermap/shapefile-path/@value"; christian@4703: christian@4703: public static final String XPATH_RIVERMAP_VELOCITY_LOGFILE = christian@4703: "/artifact-database/rivermap/velocity/logfile/@path"; christian@4703: christian@4703: public static final String XPATH_RIVERMAP_MAPSERVER_URL = christian@4703: "/artifact-database/rivermap/mapserver/server/@path"; christian@4703: christian@4703: public static final String XPATH_RIVERMAP_MAPFILE_PATH = christian@4703: "/artifact-database/rivermap/mapserver/mapfile/@path"; christian@4703: christian@4703: public static final String XPATH_RIVERMAP_MAPFILE_TEMPLATE = christian@4703: "/artifact-database/rivermap/mapserver/map-template/@path"; christian@4703: christian@4703: public static final String XPATH_RIVERMAP_MAPSERVER_TEMPLATE_PATH = christian@4703: "/artifact-database/rivermap/mapserver/templates/@path"; christian@4703: rrenkert@4841: public static final Pattern DB_URL_PATTERN = rrenkert@4841: Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)"); rrenkert@4841: rrenkert@4841: public static final Pattern DB_PSQL_URL_PATTERN = rrenkert@4841: Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9]+)"); rrenkert@4841: teichmann@8202: private static Logger log = Logger.getLogger(RiverMapfileGenerator.class); christian@4656: christian@4654: /** christian@4654: * Generate river axis mapfile. christian@4654: */ christian@4654: @Override christian@4656: public void generate() { teichmann@8202: log.debug("generate()"); christian@4702: christian@4702: List rivers = RiverFactory.getRivers(); christian@4703: List riverFiles = new ArrayList(); christian@4654: christian@4654: for (River river : rivers) { tom@8747: RiverAxis riverAxis = null; rrenkert@5170: try { rrenkert@5170: riverAxis = RiverAxis.getRiverAxis(river.getName()); rrenkert@5170: } rrenkert@5181: catch (HibernateException iae) { teichmann@8202: log.error("No valid riveraxis found for " + river.getName()); rrenkert@5170: continue; rrenkert@5170: } rrenkert@5170: christian@4809: if (riverAxis == null) { teichmann@8202: log.warn("River " + river.getName() + " has no river axis!"); christian@4809: continue; christian@4809: } tom@8747: if (riverAxis.getGeom() == null) { teichmann@8202: log.warn("River " + river.getName() + rrenkert@5170: " has no riveraxis geometry!"); rrenkert@5170: continue; rrenkert@5170: } tom@8747: MultiLineString geom = riverAxis.getGeom(); christian@4809: Envelope extent = geom.getEnvelopeInternal(); christian@4809: christian@4703: createRiverAxisLayer( christian@4654: river.getName(), christian@4654: river.getId(), christian@4809: Integer.toString(geom.getSRID()), christian@4809: extent.getMinX() + " " + christian@4809: extent.getMinY() + " " + christian@4809: extent.getMaxX() + " " + christian@4809: extent.getMaxY()); christian@4809: christian@4703: riverFiles.add("river-" + river.getName() + ".map"); christian@4654: } christian@4703: writeMapfile(riverFiles); christian@4654: } christian@4654: tom@8856: protected void createRiverAxisLayer( tom@8856: String riverName, tom@8856: int riverID, tom@8856: String srid, tom@8856: String extend tom@8856: ) { christian@4654: LayerInfo layerInfo = new LayerInfo(); christian@4654: layerInfo.setName(riverName); rrenkert@4841: layerInfo.setConnection(MapUtils.getConnection()); rrenkert@4841: layerInfo.setConnectionType(MapUtils.getConnectionType()); christian@4654: layerInfo.setSrid(srid); christian@4654: layerInfo.setExtent(extend); christian@4738: layerInfo.setType("line"); aheinecke@5304: // FIXME: Use templates for that teichmann@5865: if (RiverUtils.isUsingOracle()) { aheinecke@5304: layerInfo.setData("geom FROM river_axes USING SRID " + srid); aheinecke@5304: } else { aheinecke@5304: layerInfo.setData("geom FROM river_axes"); aheinecke@5304: } aheinecke@5477: layerInfo.setFilter("river_id = " + riverID + " and kind_id = 1"); christian@4738: layerInfo.setTitle(riverName + " RiverAxis"); christian@4656: tom@8802: File layerFile = new File( tom@8802: Config.getConfigDirectory().getParentFile(), tom@8802: "river-" + riverName + ".map"); christian@4738: Template template = getTemplateByName("riveraxis-layer.vm"); christian@4656: if (template == null) { teichmann@8202: log.warn("Template riveraxis-layer.vm not found."); christian@4656: return; christian@4656: } christian@4656: christian@4656: try { christian@4656: writeLayer(layerInfo, layerFile, template); christian@4656: } christian@4656: catch (FileNotFoundException e) { teichmann@8202: log.warn(e.getLocalizedMessage(), e); christian@4656: } christian@4654: } christian@4654: christian@4656: @Override christian@4656: protected String getVelocityLogfile() { teichmann@5865: return RiverUtils.getXPathString(XPATH_RIVERMAP_VELOCITY_LOGFILE); christian@4656: } christian@4656: christian@4656: @Override christian@4656: protected String getMapserverTemplatePath() { tom@8856: return RiverUtils.getXPathString( tom@8856: XPATH_RIVERMAP_MAPSERVER_TEMPLATE_PATH); christian@4656: } christian@4656: christian@4656: @Override rrenkert@5309: public String getMapserverUrl() { teichmann@5865: return RiverUtils.getXPathString(XPATH_RIVERMAP_MAPSERVER_URL); christian@4656: } christian@4656: christian@4656: @Override christian@4656: protected String getMapfilePath() { teichmann@5865: return RiverUtils.getXPathString(XPATH_RIVERMAP_MAPFILE_PATH); christian@4656: } christian@4656: christian@4656: @Override christian@4656: protected String getMapfileTemplate() { teichmann@5865: return RiverUtils.getXPathString(XPATH_RIVERMAP_MAPFILE_TEMPLATE); christian@4656: } christian@4654: }