7

ST_Shift_Longitude(geom) (renamed to ST_Shift_Longitude in PostGIS 2.2) adds 360 to longitudes below 0, thereby (for lon-lat coordinates like WGS84) moving the part of the world "left" (west) of the 0-meridian to the "right" (east) of the "right" (eastern) antimeridian. So this turns a flat -180°…+180° map "split" around the international date line into a flat 0°…+360° map "split" at Greenwich.

Is there any function that does the reverse (i.e. subtracting 360 from longitudes above 180) to get back from a flat 0°…+360° map "split" at Greenwich to a flat -180°…+180° map "split" at the antimeridian?

In PostGIS ≥ 2.3 I could use ST_WrapX(shifted_geom, 180, -360), I guess, but in PostGIS 2.1 and 2.2 ST_WrapX isn't available, yet.

I tried ST_MakeValid(shifted_geom) and ST_Transform(shifted_geom, 4326), but both return the shifted_geom unchanged.

1 Answer 1

3

Use shifted_geom::geography to cast the shifted_geometry to a geography:

SELECT ST_AsText(
    'POLYGON(
        (
            176.792 -12.2085,
            182.1313 -12.2085,
            182.1313 -20.8999,
            176.792 -20.8999,
            176.792 -12.2085
        )
    )'::geometry
);
--                                              st_astext                                             
-- ---------------------------------------------------------------------------------------------------
--  POLYGON((176.792 -12.2085,182.1313 -12.2085,182.1313 -20.8999,176.792 -20.8999,176.792 -12.2085))
-- (1 row)

(Mike T's answer to Best-practices for databases and APIs with geographic data spanning the antimeridian made me aware that

[c]asting a EPSG:4326 geometry to a geography type also normalizes the coordinates

which solves this nicely.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.