|
| 1 | +import folium |
| 2 | + |
| 3 | +# Coordinate Ordering in GeoJSON and Folium |
| 4 | + |
| 5 | +## Understanding the Problem |
| 6 | + |
| 7 | +Leaflet expects coordinates in [latitude, longitude] format, but the GeoJSON standard uses [longitude, latitude] format. When you work with both GeoJSON and Folium together, this difference can cause markers and features to appear in the wrong locations on your map. |
| 8 | + |
| 9 | +## Why the Difference Exists |
| 10 | + |
| 11 | +Leaflet.js was designed to use [lat, lon] order, which is common in geographic information systems. GeoJSON follows RFC 7946, which specifies [lon, lat] order to align with mathematical conventions (x before y). Understanding both conventions is important when combining data from different sources. |
| 12 | + |
| 13 | +## Example 1: Correct Coordinates |
| 14 | + |
| 15 | +When you have GeoJSON data with [lon, lat] coordinates and want to display it in Folium, the coordinates need to be reversed for the Folium.Map location parameter. |
| 16 | + |
| 17 | +```python |
| 18 | +import folium |
| 19 | + |
| 20 | +# GeoJSON with correct [lon, lat] order |
| 21 | +geojson_data = { |
| 22 | + "type": "Feature", |
| 23 | + "geometry": { |
| 24 | + "type": "Point", |
| 25 | + "coordinates": [-87.6298, 41.8781], # longitude, latitude (Chicago) |
| 26 | + }, |
| 27 | +} |
| 28 | + |
| 29 | +# Folium map with correct [lat, lon] order |
| 30 | +m = folium.Map(location=[41.8781, -87.6298], zoom_start=12) |
| 31 | +folium.GeoJson(geojson_data).add_to(m) |
| 32 | +m |
| 33 | +``` |
| 34 | + |
| 35 | +Both represent the same location (Chicago), but notice how the coordinates are reversed between the two formats. |
| 36 | + |
| 37 | +## Example 2: Wrong Coordinate Order Problem |
| 38 | + |
| 39 | +If you accidentally put coordinates in [lat, lon] order in your GeoJSON data, the marker will appear in the wrong location. This is one of the most common mistakes when combining GeoJSON with Folium. |
| 40 | + |
| 41 | +```python |
| 42 | +import folium |
| 43 | + |
| 44 | +# GeoJSON coordinates in wrong [lat, lon] order |
| 45 | +geojson_data = { |
| 46 | + "type": "Feature", |
| 47 | + "geometry": { |
| 48 | + "type": "Point", |
| 49 | + "coordinates": [41.8781, -87.6298], # WRONG: latitude, longitude |
| 50 | + }, |
| 51 | +} |
| 52 | + |
| 53 | +m = folium.Map(location=[41.8781, -87.6298], zoom_start=12) |
| 54 | +folium.GeoJson(geojson_data).add_to(m) |
| 55 | +m |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | +GeoJSON interprets the coordinates as [lon, lat], so when you provide [lat, lon] by mistake, the point ends up in the wrong location. In this case, the marker would appear far from Chicago. |
| 60 | + |
| 61 | +## Example 3: Converting Between Coordinate Orders |
| 62 | + |
| 63 | +If you have coordinates in [lat, lon] order but need them in [lon, lat] format for GeoJSON, you can flip them by swapping their positions. This is useful when working with data from sources that use different conventions. |
| 64 | + |
| 65 | +```python |
| 66 | +import folium |
| 67 | + |
| 68 | +# Start with coordinates in [lat, lon] order |
| 69 | +wrong_order = [41.8781, -87.6298] # latitude, longitude |
| 70 | + |
| 71 | +# Flip to [lon, lat] order for GeoJSON |
| 72 | +correct_order = [wrong_order[1], wrong_order[0]] |
| 73 | + |
| 74 | +geojson_data = { |
| 75 | + "type": "Feature", |
| 76 | + "geometry": {"type": "Point", "coordinates": correct_order}, # now [lon, lat] |
| 77 | +} |
| 78 | + |
| 79 | +m = folium.Map(location=[correct_order[1], correct_order[0]], zoom_start=12) |
| 80 | +folium.GeoJson(geojson_data).add_to(m) |
| 81 | +m |
| 82 | +``` |
| 83 | + |
| 84 | +By swapping the coordinate positions, you convert from [lat, lon] to [lon, lat]. This approach works for individual coordinates, though for large datasets you may prefer automated solutions. |
| 85 | + |
| 86 | +## Example 4: Using GeoPandas |
| 87 | + |
| 88 | +GeoPandas provides a convenient way to handle coordinate ordering automatically. GeoPandas uses [lon, lat] order by default, which matches the GeoJSON specification. |
| 89 | + |
| 90 | +```python |
| 91 | +import folium |
| 92 | +import geopandas as gpd |
| 93 | +from shapely.geometry import Point |
| 94 | + |
| 95 | +# Create a GeoDataFrame with points |
| 96 | +# Shapely/GeoPandas uses [lon, lat] order automatically |
| 97 | +points = [Point(-87.6298, 41.8781), Point(-118.2437, 34.0522)] |
| 98 | +gdf = gpd.GeoDataFrame(geometry=points, crs="EPSG:4326") |
| 99 | + |
| 100 | +# When you pass a GeoDataFrame to folium.GeoJson, |
| 101 | +# the coordinates are already in the correct [lon, lat] order |
| 102 | +m = folium.Map(location=[41.8781, -87.6298], zoom_start=4) |
| 103 | +folium.GeoJson(gdf).add_to(m) |
| 104 | +m |
| 105 | +``` |
| 106 | + |
| 107 | +GeoPandas handles the coordinate order for you, eliminating the need to manually track which format you're using. |
| 108 | + |
| 109 | +## How to Detect Swapped Coordinates |
| 110 | + |
| 111 | +If your markers are appearing in the wrong location, you likely have a coordinate ordering issue. Signs include: |
| 112 | + |
| 113 | +- Markers appear far from where you expected them |
| 114 | +- Markers appear completely off the map or in the ocean |
| 115 | +- The location makes sense geographically but is in the wrong hemisphere |
| 116 | + |
| 117 | +If you notice these issues, check whether you're mixing [lat, lon] and [lon, lat] formats. The fix is usually as simple as reversing your coordinate order. |
| 118 | + |
| 119 | +## Best Practices |
| 120 | + |
| 121 | +- Always verify the coordinate format of your data source before loading it into Folium |
| 122 | +- Remember the key difference: GeoJSON uses [lon, lat], Folium uses [lat, lon] |
| 123 | +- Use GeoPandas when possible to avoid manually managing coordinate order |
| 124 | +- Test your first few markers to confirm they appear in the correct location before processing large datasets |
| 125 | +- When combining data from multiple sources, document which coordinate order each source uses |
0 commit comments