Reputation: 21
Basically the idea is to automate some workflows without using Qgis.
I'm failing to achieve similar results to the Qgis feature 'Add coordinates to points' in Geopandas, which allows you to grab the x,y coordinates of points in its current projection and create new attributes to the table.
So I have a set of points with which I played with. The original shapefile's CRS is epsg 2154 (Lambert 93). I need to get the latitude and longitude in a format compatible with Google Maps.
Google uses epsg 3857 for Google Maps.
points = pd.DataFrame({'Id': ['001', '002', '003'],'geometry': ['POINT (909149.3986619939 6881986.232659903)', 'POINT (909649.3986619939 6882486.232659903)', 'POINT (909149.3986619939 6882486.232659903)']})
The idea is to switch to epsg 3857 (wgs84) and from there, create lat/long columns which would be filled with wgs84 coordinates, such as 47.357955,1.7317783.
So what I did was obvisouly changing the CRS:
pointswgs84 = points.to_crs(espg=3857)
And then
pointswgs84['lon'] = pointswgs84.geometry.x
pointswgs84['lat'] = pointswgs84.geometry.y
But my lat/long columns get then filled with coordinates corresponding to the original points dataframe:
points = pd.DataFrame({'Id': ['001', '002', '003'],'geometry':['POINT (909149.3986619939 6881986.232659903)', 'POINT (909649.3986619939 6882486.232659903)', 'POINT (909149.3986619939 6882486.232659903)'],'long': ['6881986.232659903', '6882486.232659903', '6882486.232659903'], 'lat': ['909149.3986619939', '909649.3986619939', '909149.3986619939']})
Looks like I'm missing something here but since I'm rather new to Python & Geopandas in general I'm not sure about what...
Thank you for your help.
Upvotes: 0
Views: 918
Reputation: 21
Alright, so problem solved. It was a simple espg issue. Here's the solution if that can help any other newbie like me:
Even if Google uses espg 3857, if you need to convert the X,Y of your point to something which could be exploitable (such as a url parameter), you need to use espg 4326.
You'll then get your long lat the right way. Like 5.859162,49.003710.
So:
Step 1: convert your original shapefile to espg 4326
pointswgs84 = points.to_crs({'init': 'epsg:4326'})
Step 2: simply extract x and y and add them in new columns
pointswgs84['lon'] = pointswgs84.geometry.x
pointswgs84['lat'] = pointswgs84.geometry.y
Upvotes: 0
Reputation: 4827
You can try:
import geopandas as gpd
from shapely.geometry import Point
points = pd.DataFrame({'Id': ['001', '002', '003'],
'geometry': [Point(909149.3986619939, 6881986.232659903), Point(909649.3986619939, 6882486.232659903), Point(909149.3986619939, 6882486.232659903)]})
gdf = gpd.GeoDataFrame(points, crs={'init': 'epsg:2154'}, geometry=points.geometry) # https://epsg.io/2154
gdf.geometry = gdf.geometry.to_crs({'init': 'epsg:3857'}) # https://epsg.io/3857
print(gdf)
Result:
Id geometry
0 001 POINT (652238.9846394559 6275490.88115861)
1 002 POINT (653026.8173626668 6276225.689838496)
2 003 POINT (652266.5262486706 6276253.346635176)
Upvotes: 0