简体   繁体   中英

Geopandas - ValueError: Cannot transform naive geometries. Please set a crs on the object first

Can someone please explain how this is not working?

在此处输入图片说明

The shapefile in question is here and in the code its read as

shp=gpd.read_file("Microdatos_Censo_2017_Manzana/Microdatos_Censo_2017_Manzana.shp")
shp.crs="epsg:4326"
breakpoint()
shp=shp.to_crs(epsg=3857)## Error here

I just dont get what is happening. I have Python 3.8.5 , geopandas 0.8.1 , pyproj 2.6.1.post1 . Not sure what other package would be important to know versions of.

Thanks!


Edit:

1.- Fixing link to shapefile that I had gotten wrong.

2.- It's not the same as the question that is posted as a duplicate, because as you can see on the image the print statement of shp.crs returns the correct crs information, not None. I have a crs defined and yet to_crs is not working.

In your code:

shp = gpd.read_file("Comunas/Comunas.shp")

let you get shp as a GeoDataFrame .

Next, the line

shp.crs = "epsg:4326"

only changes the property of shp , but does not perform coordinate transformation on the geodataframe.

Then

shp = shp.to_crs(epsg=3857)

causes the error.

From the error messages it is obvious that the command that causes error requires proper object as its input value. The value in epsq=3857 is wrong according to the method's signature as follows.

.to_crs(crs=None, epsg=None, inplace=False)

The proper use of this method can be:

.to_crs({'init': 'epsg:4326'})
.to_crs(crs={'init': 'epsg:4326'})
.to_crs(epsg='4326')

For your particular dataset, to convert the CRS of the original GeoDataFrame (epsg:3857), to epsg:4326, and back to original, do these steps:

shp_file = './data/comunas/comunas.shp'  #(on my machine)
comunas0 = gpd.read_file(shp_file)
print(comunas0.crs)                      #{'init': 'epsg:3857'}
comunas0.plot()

epsg3857 (image of comunas0 )

comunas4326 = comunas0.to_crs({'init': 'epsg:4326'})
print(comunas4326.crs)                   #{'init': 'epsg:4326'}
comunas4326.plot()

epsg4326 (image of comunas4326 )

comunas3857 = comunas4326.to_crs(epsg='3857')  #back to original CRS
print(comunas3857.crs)                   #{'init': 'epsg:3857', 'no_defs': True}

EDIT

Additional plots using new shapefiles (as updated by OP).

epsg:3857

新3857

epsg:4326

新4326

In my case I was trying to project LineStrings, it didn't work. I had to first project Points then create LineStrings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM