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);
}
}
Aggregations