[GIS] GeoTools and PostGIS with ESPG:3857

coordinate systemgeotoolsjts-topology-suitepostgis

I'm trying to use both PostGIS and GeoTools with the same ESPG:3857 projection, however, it looks like the projections in PostGIS and GeoTools differ.

This is what I have in PostGIS:

PROJCS["WGS 84 / Pseudo-Mercator",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.0174532925199433,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Mercator_1SP"],
    PARAMETER["central_meridian",0],
    PARAMETER["scale_factor",1],
    PARAMETER["false_easting",0],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]],
    AXIS["X",EAST],
    AXIS["Y",NORTH],
    EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs"],
    AUTHORITY["EPSG","3857"]]

Here's GeoTools:

PROJCS["WGS 84 / Pseudo-Mercator", 
  GEOGCS["WGS 84", 
    DATUM["World Geodetic System 1984", 
      SPHEROID["WGS 84", 6378137.0, 298.257223563, AUTHORITY["EPSG","7030"]], 
      AUTHORITY["EPSG","6326"]], 
    PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], 
    UNIT["degree", 0.017453292519943295], 
    AXIS["Geodetic latitude", NORTH], 
    AXIS["Geodetic longitude", EAST], 
    AUTHORITY["EPSG","4326"]], 
  PROJECTION["Popular Visualisation Pseudo Mercator", AUTHORITY["EPSG","1024"]], 
  PARAMETER["semi_minor", 6378137.0], 
  PARAMETER["latitude_of_origin", 0.0], 
  PARAMETER["central_meridian", 0.0], 
  PARAMETER["scale_factor", 1.0], 
  PARAMETER["false_easting", 0.0], 
  PARAMETER["false_northing", 0.0], 
  UNIT["m", 1.0], 
  AXIS["Easting", EAST], 
  AXIS["Northing", NORTH], 
  AUTHORITY["EPSG","3857"]]

I've tried using the PostGIS one with GeoTools, but it cannot parse it due to the EXTENSION element. Of course, I could update PostGIS with the GeoTools one, but can anyone tell why they are different?

I'm using GeoTools 10.0, Postgres 9.3 with PostGIS 2.1. I'm just trying to convert a EPSG:4326 point into EPSG:3857, and vice versa. The code works:

    CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326");
    CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857");
    MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, false);
    GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
    Point point = geometryFactory.createPoint(new Coordinate(lon, lat));
    Point targetPoint = (Point) JTS.transform(point, transform);

but the targetPoint that I get is different from ST_Transform(point, 3857) that I get from PostGIS.

So for example, for

(41.937832, -87.642970)

I get from GeoTools with code above:

POINT (4668498.103213854 -24771604.62292875)

and from PostGIS

select ST_AsText(ST_Transform(ST_SetSRID(ST_Point(-87.642970, 41.937832), 4326), 3857))

POINT(-9756370.79201015 5151671.52336743)

Any solution on how to make these two work together? I'd really like to convert between projections in both SQL and Java.

Thanks!

Best Answer

Always be aware of the axis order. PostGIS always uses the axis order: longitude (x), latitude (y).

-- Somewhere in Chicago, USA
SELECT ST_AsText(ST_Transform(ST_SetSRID(ST_Point(-87.642970, 41.937832), 4326), 3857));
-- POINT(-9756370.79201015 5151671.52336743)

-- Somewhere in Antarctica
SELECT ST_AsText(ST_Transform(ST_SetSRID(ST_Point(41.937832, -87.642970), 4326), 3857));
-- POINT(4668498.10321386 -24771604.6229288)
Related Question