I converted a dataset with lat/lon to a geopandas dataframe (let's call it gpd). The gpd has no CRS, I was trying to project this gpd to "EPSG:3857" using:
gpd = gpd.to_crs("EPSG:3857")
I got an error saying that "Cannot transform naive geometries. Please set a CRS on the object first". Does this mean that for any gpd without CRS, I have to assign a CRS to it first, then reproject it to CRS I am interested in, for example, from 4326 to 3857:
gpd.crs = "EPSG:4326"
gpd = gpd.to_crs("EPSG:3857")
My confusions are the differences between CRS and projection, and why can not I use 3857 directly in this case. Also, the pyproj project has mentioned that there are some changes in reprojecting geodata and gave suggestions about how to project geodata, I do not clearly understand the details, can somebody give some advice on how to project/reproject geodata appropriately given the changes they made? Thanks.
You need to define the crs before you can project it. This often happens if your .shp file doesn't have an accompanying .prj file.
import geopandas as gpd
gdf = gpd.read_file('GSHHS_c_L1.shp')
print(gdf.crs)
None
#this is how we define the projection
gdf.crs = "EPSG:4326"
print(gdf.crs)
EPSG:4326
#In Jupyter if you don't use a print statement, but shift+enter, you'll get this for your crs
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
After you've defined your crs, then you can project it.
#this is how we project the data
gdf = gdf.to_crs("EPSG:3857")
# this will display crs information in jupyter
gdf.crs
<Projected CRS: EPSG:3857>
Name: WGS 84 / Pseudo-Mercator
Axis Info [cartesian]:
- X[east]: Easting (metre)
- Y[north]: Northing (metre)
Area of Use:
- name: World - 85°S to 85°N
- bounds: (-180.0, -85.06, 180.0, 85.06)
Coordinate Operation:
- name: Popular Visualisation Pseudo-Mercator
- method: Popular Visualisation Pseudo Mercator
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich