7

Hey I am in the middle of a project and I suddenly realized that my layer is projected somewhere else on the globe. After some experimentation I've realized that this is because the program translates lng for lat. I really don't understand why is that, but at the same time I need to solve the issue ASAP, as time is running out.

Case: [37.994, 24.75906] is being read as [24.75906, 37.994]

I have used the e.latlng method to acquire the data so it really puzzles me that the GeoJSON reads coordinates in reverse.

Edit: OK it turns out that this is a known issue of conflicting standards. Now the issue remains: How can I quickly resolve this since I already have hundreds of JSON [y,x] couples? Last resort for me will be regex but I would like to avoid it.

Do you have any ideas on how to solve quickly the issue? I have hundreds of data like that.

Note: if you know this to be a duplicate please leave a comment and I'll delete the question.

nmtoken
  • 13,355
  • 5
  • 38
  • 87
alexandros84
  • 181
  • 1
  • 1
  • 4
  • What was/is the source of the GeoJSON, do the coordinates match the order specified in the GeoJSON standard ~ https://tools.ietf.org/html/rfc7946 ? – nmtoken Jul 09 '17 at 08:20
  • It turned out it was a known issue: https://gis.stackexchange.com/questions/54065/leaflet-geojson-coordinate-problem. Ok I see the corrections were trivial. Ty. – alexandros84 Jul 09 '17 at 11:05

2 Answers2

13

For the record, in case you end up in such situation where you have a lot of GeoJSON data in hand but the lat-lng coordinates order is reversed, a very easy workaround within Leaflet is to use the coordsToLatLng option of the L.geoJSON factory:

L.geoJSON(geoJsonData, {
    coordsToLatLng: function (coords) {
        //                    latitude , longitude, altitude
        //return new L.LatLng(coords[1], coords[0], coords[2]); //Normal behavior
        return new L.LatLng(coords[0], coords[1], coords[2]);
    }
});

Of course the definitive solution is to find a way to correct the original GeoJSON data.

See also this post on SO: Leaflet: how to swap coordinates received from an ajax call

ghybs
  • 7,193
  • 20
  • 25
  • I ve tried to upvote but I dont have the necessary reputation yet! This is the kind of answer I was looking for last week, ty. – alexandros84 Jul 10 '17 at 04:28
0

Can't wait any longer:

var a = "[LatLng(37.43943, 25.30563)],[LatLng(37.4367, 25.30495)],[LatLng(37.43071, 25.29945)],[LatLng(37.42362, 25.30426)],[LatLng(37.42089, 25.31113)],[LatLng(37.41489, 25.31113)],[LatLng(37.41053, 25.30769)],[LatLng(37.40235, 25.30563)],[LatLng(37.40562, 25.31525)],[LatLng(37.41816, 25.31937)],[LatLng(37.42307, 25.3228)],[LatLng(37.40889, 25.33104)],[LatLng(37.4078, 25.33722)],[LatLng(37.41598, 25.33791)],[LatLng(37.40344, 25.35027)],[LatLng(37.40726, 25.3688)],[LatLng(37.42253, 25.39009)],[LatLng(37.4138, 25.39902)],[LatLng(37.42907, 25.4052)],[LatLng(37.42961, 25.41962)],[LatLng(37.44215, 25.42442)],[LatLng(37.44543, 25.45807)],[LatLng(37.46287, 25.4615)],[LatLng(37.47595, 25.46013)],[LatLng(37.47104, 25.42786)],[LatLng(37.48249, 25.42923)],[LatLng(37.48358, 25.41)],[LatLng(37.49502, 25.40588)],[LatLng(37.49447, 25.38597)],[LatLng(37.49066, 25.3791)],[LatLng(37.46941, 25.38254)],[LatLng(37.46178, 25.37773)],[LatLng(37.4754, 25.35988)],[LatLng(37.4972, 25.35782)],[LatLng(37.50047, 25.34203)],[LatLng(37.49774, 25.32761)],[LatLng(37.49229, 25.3228)],[LatLng(37.4912, 25.3125)],[LatLng(37.4814, 25.30907)],[LatLng(37.47268, 25.31113)],[LatLng(37.46341, 25.32692)],[LatLng(37.44597, 25.32692)],[LatLng(37.42798, 25.3228)],[LatLng(37.43943, 25.30563)]".split("],[");
var b = a.map(function(x) { 
    var couple= (/\d+.\d+, \d+.\d+/g).exec(x).toString();
    var splitted = couple.split(", ");
    return "["+splitted[1]+","+splitted[0]+"]";
                            } );
b.join();
nmtoken
  • 13,355
  • 5
  • 38
  • 87
alexandros84
  • 181
  • 1
  • 1
  • 4