4

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.
emax
  • 6,965
  • 19
  • 74
  • 141
  • Can you be more specific? What is not working? Is it raising an error if you try to change the crs with `to_crs` ? – joris May 23 '19 at 12:20
  • @joris I modified the text. I don't receive an error but the values of the coordinates don't change – emax May 23 '19 at 12:40
  • If your data has no CRS information, you first need to *set* (how you did it before) the CRS, but with the CRS your data is in, not the CRS you want to convert them to. – joris May 24 '19 at 07:43
  • @joris now it seems to work. Thanks – emax May 24 '19 at 08:22

2 Answers2

5

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

joris
  • 133,120
  • 36
  • 247
  • 202
  • Then please show that in your question (the code you tried, with the output). I don't see any `to_crs` call in your example code. – joris May 23 '19 at 14:49
  • yes it is there. `tmp = gpd.GeoDataFrame(df, geometry='geometry') tmp.crs = {'init' :'epsg:32618'}` – emax May 23 '19 at 14:50
  • 1
    Please read my answer and comment. You are using `tmp.crs = ...`, I am saying to use `tmp.to_crs(...)`. – joris May 23 '19 at 22:01
  • please see the modified version of my question – emax May 24 '19 at 07:16
0

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.

MaPhewJ
  • 13
  • 3
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – PeterS Dec 02 '22 at 17:41