# Use map projections to find closest point over dateline

I want to identify closest points to a polygon over the dateline in an OGR/Python script. I found this workaround for PostGIS but I'm not 100% certain about the applicability in my case. This may be because I'm not very good at reading SQL but I'm also not convinced about the fixed longitude values to detect dateline intersections (-178.75 lon) in the script. I experimented with map projections because I want to process data automatically, regardless of the input data and the distance to the dateline.

Problems don't occur when the longitutes are outside the -180 / +180 degree range (as long as coordinates are adjacent), but when, for instance, the polygon is at -179° longitude and a close point is at +179° longitude. The first image shows a case, where the closest points are correctly identified. Imagine the orange line to be the dateline and the green lines the connectors between points and closest point on the polygon outline. The second case is what I would like to consider.

This is OK:

This is not OK:

I experimented with map projections. I defined a central meridian for the longitude of the polygon's centroid. However, this didn't solve the issue as in the 'not OK' case, the point which is causing problems simply has a longitude of +350° while the polygon has a central longitude of 0°. For calculations, points and polygon seem far off even though they are very close on a spheroid. Can you help me find a workaround for this? Are map projections suitable for the consideration of dateline intersections?

I experimented further with map projections and found a solution for the "Not OK" case. The trick was to first project the data to a projected coordinate system using a central meridian which corresponds to the centroid of the investigated region. Then, the data is projected back to a geographic coordinate system unsing the same central meridian which corresponds to the centroid of the investigated region. If I directly project the geographic coordinates to a geographic coordinate system with a new central meridian (as I did initially), the problem with the dateline intersection still persists.

Solution: Geographic input -> Projected input (central meridian = centroid of investigated region) -> Geographic input (central meridian = centroid of investigated region)

To give an example, I chose a polygon which directly intersects the dateline. The Polygon WKT and the coordinate system are given in the screenshots. The center however was not correctly identified (GDAL assumed that the polygon spans the whole sphere and not just a small section over the dateline), but that didn't affect the results.

Original coordinate system and polygon intersecting the dateline:

Projected coordinate system with reference meridian at 59.2° longitue (not exactly the centroid of the polygon but that's OK in my case):

Reprojected geographic coordinate system. The reference meridian is set to 59.2°. The polygon doesn't intersect any dateline in this case and can be used for further processing:

So, map projections are indeed suitable for the consideration of dateline intersections.