For Wikipedia, I'm writing a service that extracts polygons of a country by a given ID, simplifies them, and produces GeoJSON. The PostGIS query produces this GeoJSON:
{"type":"MultiPolygon","coordinates":[
[[[179.16675595506,51.9554040068616],[179.183028487274,52.0533832939166],[179.242259803848,52.1241672041611],[179.521550607089,52.2320045907578],[179.765980758593,52.2319078127239],[179.999999974944,52.1384488027094],[179.999999974944,51.7940888148064],[179.763841869902,51.6997152871656],[179.574436853316,51.689260400522],[179.353476805577,51.7455660937973],[179.217039781909,51.8395464029174],[179.16675595506,51.9554040068616]]],
[[[-179.999999885112,51.7940888148064],[-179.999999885112,52.1384889958134],[-179.914841033482,52.0657268624567],[-179.883697790055,51.9764893877679],[-179.930211657151,51.864238501218],[-179.999999885112,51.7940888148064]]]
]}
The above shows fine in Leaflet 0.7, but if I encode it as TopoJSON for transmission and decode it at the client, it produces this GeoJSON which shows horizontal lines in Leaflet. It seems TopoJSON is unable to preserve the 180
vs -180
, and even though the original objects were split accurately along the date line, the result is broken.
I understand that this is a hard and well known problem, but I haven't been able to find an algorithm for solving this. My only guess has been to run through the points, and force the longitude 180 to -180 if there are any longitudes that are -180
{"type":"MultiPolygon","coordinates":[
[[[-180,51.79407478509499],[179.74797479747974,51.6997364109946],[179.5679567956796,51.689260400522],[179.35193519351935,51.74554860186959],[179.20792079207922,51.83956129688793],[179.17191719171916,51.955394490403606],[179.17191719171916,52.05336961425355],[179.24392439243928,52.124150534752346],[179.53195319531955,52.2320045907578],[179.7839783978398,52.23189603106378],[-180,52.13842613451553],[-180,51.79407478509499]]],
[[[-180,51.79407478509499],[-180,52.13848041436253],[-179.92799279927993,52.06574541937144],[-179.8919891989199,51.97650935088983],[-179.92799279927993,51.8642586272767],[-180,51.79407478509499]]]
]}