christian@4654: package de.intevation.flys.utils;
christian@4654: 
christian@4809: import com.vividsolutions.jts.geom.Envelope;
christian@4809: import com.vividsolutions.jts.geom.LineString;
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;
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<River>  rivers     = RiverFactory.getRivers();
christian@4703:         List<String> riverFiles = new ArrayList<String>();
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.
christian@4809:             List<RiverAxis> riverAxis = RiverAxis.getRiverAxis(river.getName());
christian@4809:             if (riverAxis == null) {
christian@4809:                 logger.warn("River " + river.getName() + " has no river axis!");
christian@4809:                 continue;
christian@4809:             }
christian@4809:             LineString 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");
christian@4809:         layerInfo.setData("geom FROM river_axes");      // FIXME: Use templates for that
christian@4738:         layerInfo.setFilter("river_id = " + riverID);
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
christian@4656:     protected 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: }