首页 > 解决方案 > 使用 read_file() 将 shapefile 导入 geopandas;无法打印 crs 或重新投影

问题描述

我有一个带有有效 .prj 文件的历史县界的 shapefile。我可以在ArcGIS中打开,发现投影是USA Contiguous Albers Equal Area Conic。我也可以在将 shapefile 读入 geopandas 后绘制它,并且投影看起来正确。

但是,我无法在 Python IDE 中打印坐标系的名称。

如果您从 geopandas 读取内置数据

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

然后运行

print(world.crs)

你得到

{'init': 'epsg:4326'}

但如果我跑

counties1910 = gpd.read_file('counties1910.shp')

print(counties1910.crs)

我得到的只是

{}

此外,我发现虽然我可以手动运行

counties1910.crs = {'init' :'epsg:102003'}

没有错误,如果我尝试重新投影counties1910,我确实会收到错误:

counties1910 = counties1910.to_crs("EPSG:4326")

--------------------------------------------------------------------------
CRSError                                  Traceback (most recent call last)
<ipython-input-148-4aa2873c2f22> in <module>
----> 1 counties1910 = counties1910.to_crs("EPSG:4326")

~\AppData\Local\Continuum\anaconda3\lib\site-packages\geopandas\geodataframe.py in to_crs(self, crs, epsg, inplace)
    532         else:
    533             df = self.copy()
--> 534         geom = df.geometry.to_crs(crs=crs, epsg=epsg)
    535         df.geometry = geom
    536         df.crs = geom.crs

~\AppData\Local\Continuum\anaconda3\lib\site-packages\geopandas\geoseries.py in to_crs(self, crs, epsg)
    408         # skip transformation if the input CRS and output CRS are the exact same
    409         if _PYPROJ_VERSION >= LooseVersion("2.1.2") and pyproj.CRS.from_user_input(
--> 410             self.crs
    411         ).is_exact_same(pyproj.CRS.from_user_input(crs)):
    412             return self

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pyproj\crs\crs.py in from_user_input(value, **kwargs)
    438         if isinstance(value, CRS):
    439             return value
--> 440         return CRS(value, **kwargs)
    441 
    442     def get_geod(self) -> Optional[Geod]:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pyproj\crs\crs.py in __init__(self, projparams, **kwargs)
    294             projstring = _prepare_from_string(" ".join((projstring, projkwargs)))
    295 
--> 296         super().__init__(projstring)
    297 
    298     @staticmethod

pyproj/_crs.pyx in pyproj._crs._CRS.__init__()

CRSError: Invalid projection: +init=epsg:102003 +type=crs: (Internal Proj Error: proj_create: crs not found)

我错过了什么?

谢谢!

标签: projectionshapefilegeopandas

解决方案


美国连续阿尔伯斯等面积圆锥曲线为 ESRI:102003。因为您使用的是旧版本的 GeoPandas 和 pyproj,所以它不会自动选择它。

因为您将其作为 EPSG:102003 而不是 ESRI 传递,所以它会引发该错误。

这应该在 GeoPandas 0.7.0 和 0.8.0 中按预期工作,它们使用pyproj.CRS类来存储投影信息。您最好通过更新到最新版本 (0.8.0) 来修复它。

或者,将 CRS 作为 ESRI 传递(但如果可能,建议进行更新)。

counties1910.crs = {'esri:102003'}

推荐阅读