Search in sources :

Example 1 with GeneralDirectPosition

use of org.geotools.geometry.GeneralDirectPosition in project hale by halestudio.

the class GeotoolsConverter method convert.

/**
 * @see GeoConverter#convert(GeoPosition, int)
 */
@Override
public GeoPosition convert(GeoPosition pos, int targetEpsg) throws IllegalGeoPositionException {
    if (targetEpsg == pos.getEpsgCode())
        return new GeoPosition(pos.getX(), pos.getY(), targetEpsg);
    try {
        CoordinateReferenceSystem sourceCRS = getCRS(pos.getEpsgCode());
        CoordinateReferenceSystem targetCRS = getCRS(targetEpsg);
        MathTransform math = getTransform(pos.getEpsgCode(), targetEpsg, sourceCRS, targetCRS);
        int dimension = sourceCRS.getCoordinateSystem().getDimension();
        DirectPosition pt1;
        boolean flipSource = flipCRS(sourceCRS);
        switch(dimension) {
            case 2:
                pt1 = new GeneralDirectPosition((flipSource) ? (pos.getY()) : (pos.getX()), (flipSource) ? (pos.getX()) : (pos.getY()));
                break;
            case 3:
                pt1 = new GeneralDirectPosition((flipSource) ? (pos.getY()) : (pos.getX()), (flipSource) ? (pos.getX()) : (pos.getY()), 0);
                break;
            default:
                log.error("Unsupported dimension: " + dimension);
                throw new IllegalArgumentException("Unsupported dimension: " + dimension);
        }
        DirectPosition pt2 = math.transform(pt1, null);
        if (flipCRS(targetCRS))
            return new GeoPosition(pt2.getOrdinate(1), pt2.getOrdinate(0), targetEpsg);
        else
            return new GeoPosition(pt2.getOrdinate(0), pt2.getOrdinate(1), targetEpsg);
    } catch (Exception e) {
        throw new IllegalGeoPositionException(e);
    }
}
Also used : GeneralDirectPosition(org.geotools.geometry.GeneralDirectPosition) DirectPosition(org.opengis.geometry.DirectPosition) GeneralDirectPosition(org.geotools.geometry.GeneralDirectPosition) MathTransform(org.opengis.referencing.operation.MathTransform) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FactoryException(org.opengis.referencing.FactoryException) NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException)

Aggregations

GeneralDirectPosition (org.geotools.geometry.GeneralDirectPosition)1 DirectPosition (org.opengis.geometry.DirectPosition)1 FactoryException (org.opengis.referencing.FactoryException)1 NoSuchAuthorityCodeException (org.opengis.referencing.NoSuchAuthorityCodeException)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 MathTransform (org.opengis.referencing.operation.MathTransform)1