The principle
The solution consists of a few simple steps: reproject your polygon to a Pacific-centered CRS and create a line for the antimeridian in the same CRS. Create a small buffer around this line and substract it with the Difference
too from the polygon.
See below for screenshots.
How to do that in detail, step by step:
Re-project your layer (polygon) to a pacific-centered CRS. I used EPSG:3832
.
Create a line following the antimeridian (180th meridian). You can do this as follows: create a new, empty point layer in EPSG:4326
and add one feature, don't care where. Than run Menu processing / Toolbox / Geometry by expression
, set this layer as input and line as output style with this expression: make_line (make_point (180, 85), make_point (180,-85))
: this creates a line along the 180 degree meridian, from 85 degrees south to 85 degrees north.
The line we created consists only of two vertices. When we re-project it, only these two points get re-projected and the line will be drawn straight in between - thus it will pass through different locations, based on projection (see this for a similar case). Thus we must densify (add additional vertices) to keep the shape along the antimeridian even when re-projecting.
For this, use Menu processing / Toolbox / Densify by count
, set the line created in step 2 as input and define the number of additional vertices: I chose a number of 10.000. Very roughly, this corresponds to vertices with an interval of less than 2 km.
Now re-project this densified line to the same CRS used in step 1.
The densified antimeridian in EPSG_3832 from step 4 follows more or less the antimeridian, but not exactly: we put a vertex every 2 km or less, remember. Thus this line still crosses the actual antimeridian here and there. To be able to clearly separate what is to one side and what is to the other side, we apply a buffer( this is based on this solution by @Gabriel De Luca).
Thus run Menu Vector / Geoprocessing / Buffer
, set the line from step 4 as input and the buffersize to a small value (I was successfull with a size of 0.1 meters).
Run Menu Vector / Geoprocessing / Difference
, set the polygon layer from step 1 as Input layer
and the buffer around the antimeridian from step 5 as Overlay layer
. Again: be sure that both are in the same, Pacific-centered CRS. Run the tool.
You're done. Optional: run Menu Vector / Geometry Tools / Multipart to singleparts
to get separate features.
Screenshot 1: The polygon in a pacific-centered CRS (EPSG:3832
) on both sides of the antimeridian (black line). Red outlined (original polygon) as well as yellow polygon (solution) appear identical:
Screenshot 2: Same situation as above, but project CRS changed to EPSG:3857
(Web Mercator): the original polygon (red outline) streches over the whole canvas. The solution (yellow polygon) is split and appears in two parts:
Screenshot 3: again same situation, this time with project CRS changed to EPSG:4326
. original polygon (red outlined) reaches out of the underlying basemap extent, whereas the yellow polygon again is correctly split in two parts. The OpenStreetMap basemap does not fit - this has nothing to do with the soution, this is a known issue with QGIS and project CRS in 4326 when zooming out: