Reputation: 496
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.
Upvotes: 7
Views: 13471
Reputation: 1
In my case I was trying to project LineStrings, it didn't work. I had to first project Points then create LineStrings.
Upvotes: 0
Reputation: 18762
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()
comunas4326 = comunas0.to_crs({'init': 'epsg:4326'})
print(comunas4326.crs) #{'init': 'epsg:4326'}
comunas4326.plot()
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
epsg:4326
Upvotes: 5