In the example below, thepolygon table is called poly_coverage and the output table is fixed_polygons.
Select the polygons that cross the anti-meridian, cast to geom and shift to 0-360, then split them. Select the split polys with any longitude > 180, and shift back 360 deg
Create table beams_fixed_dump4 as Select ST_Translate(foo2.thegeom, -360, 0) as newgeom from
(Select (ST_DUMP(foo.geom)).geom as thegeom from
(Select ST_Split(shift_lon.geom, line2) as geom
from ST_SetSRID(ST_MakeLine(ST_MakePoint(180, -90),ST_MakePoint(180, 90)), 4326) As line2,
(Select ST_ShiftLongitude(geog::geometry) as geom from
(SELECT ST_Intersects(poly_coverage.geog, line) as inter, poly_coverage.* from poly_coverage, ST_GeogFromText('LINESTRING(180 -90, 180 0, 180 90)') As line) as tmp where inter = TRUE) as shift_lon) as foo) as foo2 where st_xmax(foo2.thegeom) > 180
Union all
Select the polygon that cross the anti-meridian, cast to geom and shift to 0-360, then split them. Select the split polys with all longitude <= 180.
Select newgeom from
(Select (ST_DUMP(foo.geom)).geom as newgeom from
(Select ST_Split(shift_lon.geom, line2) as geom from
ST_SetSRID(ST_MakeLine(ST_MakePoint(180, -90),ST_MakePoint(180, 90)), 4326) As line2,
(Select ST_ShiftLongitude(geog::geometry) as geom from
(SELECT ST_Intersects(poly_coverage.geog, line) as inter, poly_coverage.*
from poly_coverage, ST_GeogFromText('LINESTRING(180 -90, 180 0, 180 90)') As line) as tmp where inter = TRUE) as shift_lon) as foo)as foo2 where st_xmax(newgeom) <= 180
Union all
Select the polygons that don't cross the anti-meridian and cast to geom.
SELECT foo.geog::geometry from
(Select ST_Intersects(poly_coverage.geog, line) as inter, poly_coverage.* from
poly_coverage, ST_GeogFromText('LINESTRING(180 -90, 180 0, 180 90)') As line) as foo where inter = FALSE;
Best Answer
Why don't you just do: