Saturday 22 October 2016

Is there an inverse operation to ST_Shift_Longitude in PostGIS < 2.3?


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.



Answer



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.)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...