在您的代码中:
shp = gpd.read_file("Comunas/Comunas.shp")
让您将shp
作为GeoDataFrame
获得。
接下来,这行代码:
shp.crs = "epsg:4326"
仅更改了shp
的属性,但不对地理数据框执行坐标转换。
然后
shp = shp.to_crs(epsg=3857)
导致错误发生。
从错误消息可以明显看出,导致错误的命令需要一个正确的对象
作为输入值。根据以下方法签名,epsq=3857
中的值是错误的。
.to_crs(crs=None, epsg=None, inplace=False)
这种方法的适当使用方式可以是:
.to_crs({'init': 'epsg:4326'})
.to_crs(crs={'init': 'epsg:4326'})
.to_crs(epsg='4326')
对于您的特定数据集,要将原始GeoDataFrame
(epsg:3857)转换为epsg:4326并恢复回原始状态,请按照以下步骤进行:
shp_file = './data/comunas/comunas.shp'
comunas0 = gpd.read_file(shp_file)
print(comunas0.crs)
comunas0.plot()
(comunas0
的图片)
comunas4326 = comunas0.to_crs({'init': 'epsg:4326'})
print(comunas4326.crs) #{'init': 'epsg:4326'}
comunas4326.plot()
(comunas4326
的图片)
comunas3857 = comunas4326.to_crs(epsg='3857') #back to original CRS
print(comunas3857.crs) #{'init': 'epsg:3857', 'no_defs': True}
编辑
使用由OP更新的新形状文件绘制额外的图表。
epsg:3857
epsg:4326
shp = shp.set_crs(4326)
替换第二行代码吗? - martinfleisset_crs
而不是.crs=
可以解决这个问题!有什么原因吗? - M.O.