ingo@1095: package de.intevation.flys.utils;
ingo@1095:
ingo@1105: import java.util.HashMap;
ingo@1105: import java.util.Map;
ingo@1105:
ingo@1105: import javax.xml.xpath.XPathConstants;
ingo@1105:
ingo@1105: import org.w3c.dom.Document;
ingo@1105:
ingo@1095: import gnu.trove.TDoubleArrayList;
ingo@1095:
ingo@1118: import de.intevation.artifacts.CallContext;
ingo@1118:
ingo@1105: import de.intevation.artifacts.common.utils.Config;
ingo@1105: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@1105:
ingo@1095: import de.intevation.flys.artifacts.FLYSArtifact;
felix@1099: import de.intevation.flys.artifacts.model.RiverFactory;
felix@1099: import de.intevation.flys.model.River;
ingo@1095:
ingo@1095: public class FLYSUtils {
ingo@1095:
ingo@1095: public static enum KM_MODE { RANGE, LOCATIONS, NONE };
ingo@1095:
ingo@1105: public static final String XPATH_RIVER_PROJECTION =
ingo@1105: "/artifact-database/floodmap/river[@name=$name]/srid/@value";
ingo@1105:
ingo@1129: public static final String XPATH_SHAPEFILE_DIR =
ingo@1129: "/artifact-database/floodmap/shapefile-path/@value";
ingo@1129:
ingo@1129: public static final String XPATH_VELOCITY_LOGFILE =
ingo@1129: "/artifact-database/floodmap/velocity/logfile/@path";
ingo@1129:
ingo@1129: public static final String XPATH_MAPSERVER_URL =
ingo@1129: "/artifact-database/floodmap/mapserver/server/@path";
ingo@1129:
ingo@1129: public static final String XPATH_MAPFILE_PATH =
ingo@1129: "/artifact-database/floodmap/mapserver/mapfile/@path";
ingo@1129:
ingo@1129: public static final String XPATH_MAPFILE_TEMPLATE =
ingo@1129: "/artifact-database/floodmap/mapserver/map-template/@path";
ingo@1129:
ingo@1129: public static final String XPATH_MAPSERVER_TEMPLATE_PATH =
ingo@1129: "/artifact-database/floodmap/mapserver/templates/@path";
ingo@1129:
ingo@1095:
ingo@1095: private FLYSUtils() {
ingo@1095: }
ingo@1095:
ingo@1095:
ingo@1118: public static FLYSArtifact getArtifact(String uuid, CallContext context) {
ingo@1118: // TODO IMPLEMENT ME
ingo@1118: // Fetch artifact based on its UUID from ArtifactDatabase
ingo@1118:
ingo@1118: return null;
ingo@1118: }
ingo@1118:
ingo@1118:
ingo@1129: /**
ingo@1129: * Convinience function to retrieve an XPath as string with replaced config
ingo@1129: * directory.
ingo@1129: *
ingo@1129: * @param xpath The XPath expression.
ingo@1129: *
ingo@1129: * @return a string with replaced config directory.
ingo@1129: */
ingo@1129: public static String getXPathString(String xpath) {
ingo@1129: String tmp = Config.getStringXPath(xpath);
ingo@1129: tmp = Config.replaceConfigDir(tmp);
ingo@1129:
ingo@1129: return tmp;
ingo@1129: }
ingo@1129:
ingo@1129:
ingo@1095: public static KM_MODE getKmRangeMode(FLYSArtifact flys) {
felix@1099: String mode = flys.getDataAsString("ld_mode");
ingo@1095:
ingo@1095: if (mode == null || mode.length() == 0) {
ingo@1095: return KM_MODE.NONE;
ingo@1095: }
ingo@1095: else if (mode.equals("distance")) {
ingo@1095: return KM_MODE.RANGE;
ingo@1095: }
ingo@1095: else if (mode.equals("locations")) {
ingo@1095: return KM_MODE.LOCATIONS;
ingo@1095: }
ingo@1095: else {
ingo@1095: return KM_MODE.NONE;
ingo@1095: }
ingo@1095: }
ingo@1095:
ingo@1095:
ingo@1095: public static double[] getKmRange(FLYSArtifact flys) {
ingo@1095: switch (getKmRangeMode(flys)) {
ingo@1095: case RANGE: {
ingo@1095: return getKmFromTo(flys);
ingo@1095: }
ingo@1095:
ingo@1095: case LOCATIONS: {
ingo@1095: double[] locs = getLocations(flys);
ingo@1095: return new double[] { locs[0], locs[locs.length-1] };
ingo@1095: }
ingo@1095:
ingo@1095: case NONE: {
ingo@1095: double[] locs = getLocations(flys);
ingo@1095: if (locs != null) {
ingo@1095: return new double[] { locs[0], locs[locs.length-1] };
ingo@1095: }
ingo@1095: else {
ingo@1095: return getKmFromTo(flys);
ingo@1095: }
ingo@1095: }
ingo@1095: }
ingo@1095:
ingo@1095: return new double[] { Double.NaN, Double.NaN };
ingo@1095: }
ingo@1095:
ingo@1095:
ingo@1095: public static double[] getKmFromTo(FLYSArtifact flys) {
ingo@1095: String strFrom = flys.getDataAsString("ld_from");
ingo@1095: String strTo = flys.getDataAsString("ld_to");
ingo@1095:
ingo@1095: if (strFrom == null || strTo == null) {
ingo@1095: return null;
ingo@1095: }
ingo@1095:
ingo@1095: try {
ingo@1095: return new double[] {
ingo@1095: Double.parseDouble(strFrom),
ingo@1095: Double.parseDouble(strTo) };
ingo@1095: }
ingo@1095: catch (NumberFormatException nfe) {
ingo@1095: return null;
ingo@1095: }
ingo@1095: }
ingo@1095:
ingo@1095:
ingo@1095: public static double[] getLocations(FLYSArtifact flys) {
ingo@1095: String locationStr = flys.getDataAsString("ld_locations");
ingo@1095:
ingo@1095: if (locationStr == null || locationStr.length() == 0) {
ingo@1095: return null;
ingo@1095: }
ingo@1095:
ingo@1095: String[] tmp = locationStr.split(" ");
ingo@1095: TDoubleArrayList locations = new TDoubleArrayList();
ingo@1095:
ingo@1095: for (String l: tmp) {
ingo@1095: try {
ingo@1095: locations.add(Double.parseDouble(l));
ingo@1095: }
ingo@1095: catch (NumberFormatException nfe) {
ingo@1095: }
ingo@1095: }
ingo@1095:
ingo@1095: locations.sort();
ingo@1095:
ingo@1095: return locations.toNativeArray();
ingo@1095: }
felix@1099:
felix@1099:
felix@1099: /**
felix@1099: * Returns the selected River object based on the 'river' data that might
felix@1099: * have been inserted by the user.
felix@1099: *
felix@1099: * @return the selected River or null if no river has been chosen yet.
felix@1099: */
felix@1099: public static River getRiver(FLYSArtifact flys) {
felix@1099: String sRiver = flys.getDataAsString("river");
felix@1099:
felix@1099: return (sRiver != null)
felix@1099: ? RiverFactory.getRiver(sRiver)
felix@1099: : null;
felix@1099: }
ingo@1105:
ingo@1105:
ingo@1105: /**
ingo@1105: * Extracts the SRID defined in the global configuration for the river
ingo@1105: * specified in artifact.
ingo@1105: *
ingo@1105: * @param artifact The FLYSArtifact that stores the name of the river.
ingo@1105: *
ingo@1105: * @return the SRID as string (e.g. "31466").
ingo@1105: */
ingo@1105: public static String getRiverSrid(FLYSArtifact artifact) {
ingo@1105: String river = artifact.getDataAsString("river");
ingo@1105:
ingo@1105: if (river == null || river.length() == 0) {
ingo@1105: return null;
ingo@1105: }
ingo@1105:
ingo@1105: Map variables = new HashMap(1);
ingo@1105: variables.put("name", river);
ingo@1105:
ingo@1105: Document cfg = Config.getConfig();
ingo@1105:
ingo@1105: return (String) XMLUtils.xpath(
ingo@1105: cfg,
ingo@1105: XPATH_RIVER_PROJECTION,
ingo@1105: XPathConstants.STRING,
ingo@1105: null,
ingo@1105: variables);
ingo@1105: }
ingo@1095: }
ingo@1105: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :