christian@4654: package de.intevation.flys.utils; christian@4654: christian@4809: import com.vividsolutions.jts.geom.Envelope; rrenkert@5140: import com.vividsolutions.jts.geom.MultiLineString; christian@4809: christian@4654: import de.intevation.flys.artifacts.model.LayerInfo; christian@4654: import de.intevation.flys.artifacts.model.RiverFactory; teichmann@4843: christian@4654: import de.intevation.flys.model.River; christian@4809: import de.intevation.flys.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_RIVER_PROJECTION = christian@4703: "/artifact-database/rivermap/river[@name=$name]/srid/@value"; christian@4703: 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: christian@4656: private static Logger logger = Logger.getLogger(RiverMapfileGenerator.class); christian@4656: christian@4654: /** christian@4654: * Generate river axis mapfile. christian@4654: */ christian@4654: @Override christian@4656: public void generate() { christian@4702: logger.debug("generate()"); christian@4702: christian@4702: List rivers = RiverFactory.getRivers(); christian@4703: List riverFiles = new ArrayList(); christian@4654: christian@4654: for (River river : rivers) { christian@4809: // We expect that every river has only one RiverAxis. christian@4809: // This is not correct but currently the case here, see christian@4809: // RiverAxis.java. rrenkert@5170: List riverAxis = null; rrenkert@5170: try { rrenkert@5170: riverAxis = RiverAxis.getRiverAxis(river.getName()); rrenkert@5170: } rrenkert@5181: catch (HibernateException iae) { rrenkert@5170: logger.error("No valid riveraxis found for " + river.getName()); rrenkert@5170: continue; rrenkert@5170: } rrenkert@5170: christian@4809: if (riverAxis == null) { christian@4809: logger.warn("River " + river.getName() + " has no river axis!"); christian@4809: continue; christian@4809: } rrenkert@5170: if (riverAxis.get(0).getGeom() == null) { rrenkert@5170: logger.warn("River " + river.getName() + rrenkert@5170: " has no riveraxis geometry!"); rrenkert@5170: continue; rrenkert@5170: } rrenkert@5140: MultiLineString geom = riverAxis.get(0).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: christian@4654: protected void createRiverAxisLayer(String riverName, int riverID, String srid, String extend) { 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 aheinecke@5304: if (FLYSUtils.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: christian@4703: File layerFile = new File("river-" + riverName + ".map"); christian@4738: Template template = getTemplateByName("riveraxis-layer.vm"); christian@4656: if (template == null) { christian@4738: logger.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) { christian@4656: logger.warn(e.getLocalizedMessage(), e); christian@4656: } christian@4654: } christian@4654: christian@4656: @Override christian@4656: protected String getVelocityLogfile() { christian@4703: return FLYSUtils.getXPathString(XPATH_RIVERMAP_VELOCITY_LOGFILE); christian@4656: } christian@4656: christian@4656: @Override christian@4656: protected String getMapserverTemplatePath() { christian@4703: return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPSERVER_TEMPLATE_PATH); christian@4656: } christian@4656: christian@4656: @Override rrenkert@5309: public String getMapserverUrl() { christian@4703: return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPSERVER_URL); christian@4656: } christian@4656: christian@4656: @Override christian@4656: protected String getMapfilePath() { christian@4703: return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPFILE_PATH); christian@4656: } christian@4656: christian@4656: @Override christian@4656: protected String getMapfileTemplate() { christian@4703: return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPFILE_TEMPLATE); christian@4656: } christian@4654: }