Geopandas: not able to change the crs of a geopandas object

Question:

I am trying to set the crs of a geopandas object as described here.

The example file can be downloaded from here

import geopandas as gdp
df = pd.read_pickle('myShp.pickle')

I upload the screenshot to show the values of the coordinates

enter image description here

then if I try to change the crs the values of the polygon don’t change

tmp = gpd.GeoDataFrame(df, geometry='geometry')
tmp.crs = {'init' :'epsg:32618'}

I show again the screenshot

enter image description here

If I try:

import geopandas as gdp
df = pd.read_pickle('myShp.pickle')
df = gpd.GeoDataFrame(df, geometry='geometry')
dfNew=df.to_crs(epsg=32618)

I get:

ValueError: Cannot transform naive geometries.  Please set a crs on the object first.
Asked By: emax

||

Answers:

Setting the crs like:

gdf.crs = {'init' :'epsg:32618'}

does not transform your data, it only sets the CRS (it basically says: “my data is represented in this CRS”). In most cases, the CRS is already set while reading the data with geopandas.read_file (if your file has CRS information). So you only need the above when your data has no CRS information yet.

If you actually want to convert the coordinates to a different CRS, you can use the to_crs method:

gdf_new = gdf.to_crs(epsg=32618)

See https://geopandas.readthedocs.io/en/latest/projections.html

Answered By: joris

super late answer, but it’s:

tmp.set_crs(...)

Use this to define whatever coordinate system the data is in, i.e. ‘telling the computer what coordinate system we started in’

Then;

tmp.to_crs(...)

Use this to change to your new preferred crs.

Answered By: MaPhewJ
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.