I am at my wit's end trying to figure out why my data is not being reprojected properly in a geodataframe.

Given this input data, saved in a variable called power_lines:

{'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'properties': {}, 'geometry': {'type': 'LineString', 'coordinates': [[34.059162, -18.676658], [34.193058, -18.677471]]}}]}

I create a geodataframe like this, specifying that the data is in ÈPSG:4326:

power_line_gdf = gpd.GeoDataFrame.from_features(power_lines["features"], crs="epsg:4326")

When I save this out to file and look at it, it looks like this:

"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 34.059162, -18.676658 ], [ 34.193058, -18.677471 ] ] } }

I then reproject the data like this:

power_line_gdf = power_line_gdf.to_crs(epsg=3036)

And then save it back out to file again, and it looks like this:

"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3036" } },
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ -4448151.722213513217866, 15265232.02820273861289 ], [ -4436899.918848425149918, 15281201.161440940573812 ] ] } }

It is somewhat obvious from the data itself, but this puts the data way off in another location, way off the west coast of Africa (it should be in Mozambique). Am I missing something blindingly obvious here?

  • 1
    Did you provide lat,lon when lon,lat was expected? – Vince Sep 9 '19 at 12:30
  • I don't think that should be possible, My input is actually the result of a layer being automatically "GeoJSONified" in Leaflet and forwarded to a server – wfgeo Sep 9 '19 at 13:12
  • GeoPandas is probably treating EPSG:4326 as if it is a long/lat projection (it's lat/lon) and not transforming it when converting to GeoJSON which is long/lat. (CRS:84) – nmtoken Sep 9 '19 at 16:00
  • I think it is caused by a known bug in GeoPandas (github.com/geopandas/geopandas/issues/1036). GeoPandas mismatch lat/lon with lon/lat in this case. You have to manually switch coordinates (until the next version will come). – martinfleis Sep 10 '19 at 10:58

I was able to "solve" it. Instead of initializing the geodataframe with the crs keyword argument I initialized it without it and then set it using a dictionary mapping "init" to the WGS84 EPSG code like this:

power_line_gdf = gpd.GeoDataFrame.from_features(power_lines["features"])
power_line_gdf.crs = {"init": "epsg:4326"}

After that, I transform the dataframe, again not using the crs keyword argument but using the same dictionary structure as before, but mapping it to my target CRS.

power_line_gdf = power_line_gdf.to_crs({'init': 'epsg:3036'})

I think that @martinfleis is correct, because both of these methods are considered valid by geopandas, but one does not apply the trransformation correctly, seemingly because it does not reverse the lat/lon to x/y

Since it is apparently going to be fixed in a later version, I will specify that the version of geopandas on which I was encountering this issue is 0.5.1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.