Search in sources :

Example 1 with GeotoolsConverter

use of org.jdesktop.swingx.mapviewer.GeotoolsConverter in project hale by halestudio.

the class AreaCalc method checkEPSG.

/**
 * Checks if GeoPosition are in a metric system and if not convert them if
 * necessary.
 *
 * @param pos List of {@link GeoPosition}
 *
 * @return List of {@link GeoPosition}
 */
public List<GeoPosition> checkEPSG(List<GeoPosition> pos) {
    // list is empty
    if (pos.size() == 0) {
        return pos;
    }
    // 
    int epsg = pos.get(0).getEpsgCode();
    // Worldmercator
    int FALLBACK_EPSG = 3395;
    // WGS84
    FALLBACK_EPSG = 4326;
    try {
        CoordinateSystem cs = CRS.decode("EPSG:" + epsg).getCoordinateSystem();
        for (int i = 0; epsg != FALLBACK_EPSG && i < cs.getDimension(); i++) {
            CoordinateSystemAxis axis = cs.getAxis(i);
            try {
                Unit<Length> unit = axis.getUnit().asType(Length.class);
                if (!unit.toString().equals("m")) {
                    // $NON-NLS-1$
                    // not metric
                    epsg = FALLBACK_EPSG;
                }
            } catch (ClassCastException e) {
                // no length unit
                epsg = FALLBACK_EPSG;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // convert all coordinates
    try {
        GeotoolsConverter g = (GeotoolsConverter) GeotoolsConverter.getInstance();
        pos = g.convertAll(pos, epsg);
    } catch (IllegalGeoPositionException e1) {
        e1.printStackTrace();
    }
    return pos;
}
Also used : GeotoolsConverter(org.jdesktop.swingx.mapviewer.GeotoolsConverter) Length(javax.measure.quantity.Length) CoordinateSystem(org.opengis.referencing.cs.CoordinateSystem) CoordinateSystemAxis(org.opengis.referencing.cs.CoordinateSystemAxis) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException) IllegalGeoPositionException(org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)

Aggregations

Length (javax.measure.quantity.Length)1 GeotoolsConverter (org.jdesktop.swingx.mapviewer.GeotoolsConverter)1 IllegalGeoPositionException (org.jdesktop.swingx.mapviewer.IllegalGeoPositionException)1 CoordinateSystem (org.opengis.referencing.cs.CoordinateSystem)1 CoordinateSystemAxis (org.opengis.referencing.cs.CoordinateSystemAxis)1