Search code examples
javacoordinate-systemsgeotools

GeoTools: Converting from LAT/LNG to Albers


I need to convert GPS coordinates (latitude and longitude) to a Albers equal area projection. Someone can help me to do this convertion using the GeoTools Java Library?

I can't find any example or documentation to help me do this. I'm trying to do something like:

    // set a global hint to force GeoTools to use x/y order (longitute first)
    System.setProperty("org.geotools.referencing.forceXY", "true");

    double lat = 43.96499314;
    double lng = 2.33656753;

    // FACTORIES
    Hints hints = new Hints(Hints.CRS, DefaultGeographicCRS.WGS84);
    PositionFactory positionFactory = GeometryFactoryFinder.getPositionFactory(hints);
    GeometryFactory geometryFactory = GeometryFactoryFinder.getGeometryFactory(hints);
    PrimitiveFactory primitiveFactory = GeometryFactoryFinder.getPrimitiveFactory(hints);
    AggregateFactory aggregateFactory = GeometryFactoryFinder.getAggregateFactory(hints);

    // POINTS
    WKTParser parser = new WKTParser(geometryFactory, primitiveFactory, positionFactory, aggregateFactory);
    Point point = (Point) parser.parse("POINT(" + lng + " " + lat + ")");

    CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);
    CoordinateReferenceSystem latLngCRS = DefaultGeographicCRS.WGS84;
    CoordinateReferenceSystem albersCRS = factory.createCoordinateReferenceSystem("EPSG:3005");

    MathTransform transform = CRS.findMathTransform(latLngCRS, albersCRS, false);
    DirectPosition latLngDirectPosition = point.getDirectPosition();
    DirectPosition albersDirectPosition = transform.transform(latLngDirectPosition, null);

    System.out.println("latLngDirectPosition: " + Arrays.toString(latLngDirectPosition.getCoordinate()));
    System.out.println("albersDirectPosition: " + Arrays.toString(albersDirectPosition.getCoordinate()));

I got the transformation, but don't know it is correct.

The obtained values are: Lng/Lat [-124.453125, 54.140625] => Albers [1100811.6154403698, 1016407.2018336256]

Can anyone help me? Thanks!


Solution

  • Have a look at : http://docs.geotools.org/latest/userguide/library/referencing/crs.html

    In section "Finding a Math Transform", there is an example with Albers_Conic_Equal_Area projection involved.

    You can use another output from an external tool to check if your calculation is correct. Here is one you can use online by moving the map: http://spatialreference.org/ref/epsg/nad83-bc-albers/