[英]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有问题的 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
I just dont get what is happening.我只是不明白发生了什么。 I have Python 3.8.5
, geopandas 0.8.1
, pyproj 2.6.1.post1
.我有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. 1.- 修复了我出错的 shapefile 链接。
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. 2.- 这与作为重复发布的问题不同,因为正如您在图像上看到的, shp.crs
的打印语句返回正确的 crs 信息,而不是 None。 I have a crs defined and yet to_crs
is not working.我定义了一个 crs,但to_crs
不起作用。
In your code:在您的代码中:
shp = gpd.read_file("Comunas/Comunas.shp")
let you get shp
as a GeoDataFrame
.让您将shp
作为GeoDataFrame
。
Next, the line接下来,线
shp.crs = "epsg:4326"
only changes the property of shp
, but does not perform coordinate transformation on the geodataframe.仅更改shp
的属性,但不对地理数据框执行坐标变换。
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.从错误消息中可以明显看出,导致错误的命令需要正确的object
作为其输入值。 The value in epsq=3857
is wrong according to the method's signature as follows.根据方法的签名, epsq=3857
的值是错误的,如下所示。
.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:对于您的特定数据集,要将原始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()
(image of comunas0
) ( comunas0
图像)
comunas4326 = comunas0.to_crs({'init': 'epsg:4326'})
print(comunas4326.crs) #{'init': 'epsg:4326'}
comunas4326.plot()
(image of comunas4326
) ( 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).使用新 shapefile 的附加图(由 OP 更新)。
epsg:3857 epsg:3857
epsg:4326 epsg:4326
In my case I was trying to project LineStrings, it didn't work.在我的情况下,我试图投影 LineStrings,但它没有用。 I had to first project Points then create LineStrings.我必须首先投影 Points 然后创建 LineStrings。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.