![](/img/trans.png)
[英]GeoPandas tranforming CRS: Cannot transform naive geometries. Please set a crs on the object first
[英]Geopandas - ValueError: Cannot transform naive geometries. Please set a crs on the object first
有人可以解释一下这是如何不起作用的吗?
有问题的 shapefile 在这里,在代码中它读作
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
我只是不明白发生了什么。 我有Python 3.8.5
, geopandas 0.8.1
, pyproj 2.6.1.post1
。 不知道哪些其他软件包对了解版本很重要。
谢谢!
编辑:
1.- 修复了我出错的 shapefile 链接。
2.- 这与作为重复发布的问题不同,因为正如您在图像上看到的, shp.crs
的打印语句返回正确的 crs 信息,而不是 None。 我定义了一个 crs,但to_crs
不起作用。
在您的代码中:
shp = gpd.read_file("Comunas/Comunas.shp")
让您将shp
作为GeoDataFrame
。
接下来,线
shp.crs = "epsg:4326"
仅更改shp
的属性,但不对地理数据框执行坐标变换。
然后
shp = shp.to_crs(epsg=3857)
导致错误。
从错误消息中可以明显看出,导致错误的命令需要正确的object
作为其输入值。 根据方法的签名, 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) 的 CRS 转换为 epsg:4326 并返回原始数据,请执行以下步骤:
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}
编辑
使用新 shapefile 的附加图(由 OP 更新)。
epsg:3857
epsg:4326
在我的情况下,我试图投影 LineStrings,但它没有用。 我必须首先投影 Points 然后创建 LineStrings。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.