view flys-artifacts/src/main/java/de/intevation/flys/utils/FLYSUtils.java @ 1110:563e015f0f22

Export floodplain to shapefile and add its file path to WSPLGEN job. flys-artifacts/trunk@2616 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 31 Aug 2011 08:53:30 +0000
parents adb52a2005e7
children 7398280b11a0
line wrap: on
line source
package de.intevation.flys.utils;

import java.util.HashMap;
import java.util.Map;

import javax.xml.xpath.XPathConstants;

import org.w3c.dom.Document;

import gnu.trove.TDoubleArrayList;

import de.intevation.artifacts.common.utils.Config;
import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.RiverFactory;
import de.intevation.flys.model.River;

public class FLYSUtils {

    public static enum KM_MODE { RANGE, LOCATIONS, NONE };

    public static final String XPATH_RIVER_PROJECTION =
        "/artifact-database/floodmap/river[@name=$name]/srid/@value";


    private FLYSUtils() {
    }


    public static KM_MODE getKmRangeMode(FLYSArtifact flys) {
        String mode = flys.getDataAsString("ld_mode");

        if (mode == null || mode.length() == 0) {
            return KM_MODE.NONE;
        }
        else if (mode.equals("distance"))  {
            return KM_MODE.RANGE;
        }
        else if (mode.equals("locations")) {
            return KM_MODE.LOCATIONS;
        }
        else {
            return KM_MODE.NONE;
        }
    }


    public static double[] getKmRange(FLYSArtifact flys) {
        switch (getKmRangeMode(flys)) {
            case RANGE: {
                return getKmFromTo(flys);
            }

            case LOCATIONS: {
                double[] locs = getLocations(flys);
                return new double[] { locs[0], locs[locs.length-1] };
            }

            case NONE: {
                double[] locs = getLocations(flys);
                if (locs != null) {
                    return new double[] { locs[0], locs[locs.length-1] };
                }
                else {
                    return getKmFromTo(flys);
                }
            }
        }

        return new double[] { Double.NaN, Double.NaN };
    }


    public static double[] getKmFromTo(FLYSArtifact flys) {
        String strFrom = flys.getDataAsString("ld_from");
        String strTo   = flys.getDataAsString("ld_to");

        if (strFrom == null || strTo == null) {
            return null;
        }

        try {
            return new double[] {
                Double.parseDouble(strFrom),
                Double.parseDouble(strTo) };
        }
        catch (NumberFormatException nfe) {
            return null;
        }
    }


    public static double[] getLocations(FLYSArtifact flys) {
        String locationStr = flys.getDataAsString("ld_locations");

        if (locationStr == null || locationStr.length() == 0) {
            return null;
        }

        String[] tmp               = locationStr.split(" ");
        TDoubleArrayList locations = new TDoubleArrayList();

        for (String l: tmp) {
            try {
                locations.add(Double.parseDouble(l));
            }
            catch (NumberFormatException nfe) {
            }
        }

        locations.sort();

        return locations.toNativeArray();
    }


    /**
     * Returns the selected River object based on the 'river' data that might
     * have been inserted by the user.
     *
     * @return the selected River or null if no river has been chosen yet.
     */
    public static River getRiver(FLYSArtifact flys) {
        String sRiver = flys.getDataAsString("river");

        return (sRiver != null)
            ? RiverFactory.getRiver(sRiver)
            : null;
    }


    /**
     * Extracts the SRID defined in the global configuration for the river
     * specified in <i>artifact</i>.
     *
     * @param artifact The FLYSArtifact that stores the name of the river.
     *
     * @return the SRID as string (e.g. "31466").
     */
    public static String getRiverSrid(FLYSArtifact artifact) {
        String river = artifact.getDataAsString("river");

        if (river == null || river.length() == 0) {
            return null;
        }

        Map<String, String> variables = new HashMap<String, String>(1);
        variables.put("name", river);

        Document cfg = Config.getConfig();

        return (String) XMLUtils.xpath(
            cfg,
            XPATH_RIVER_PROJECTION,
            XPathConstants.STRING,
            null,
            variables);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org