为什么我的 geopandas 安装缺少 sjoin?AttributeError:“GeoDataFrame”对象没有属性“sjoin”

cla*_*ams 9 python arcgis geopandas

我需要在项目中使用 geopandas sjoin 。我已经成功创建了 GeoDataFrame 对象并使用了类方法 readfile、crs 和 to_crs。但是,当涉及到使用 sjoin 时,我收到错误: AttributeError: 'GeoDataFrame' object has no attribute 'sjoin'。

所以我想我会尝试让 sjoin 处理一个直接取自 geopandas 官方网站的简单示例: https: //geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.sjoin.html

import geopandas

countries = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
cities = geopandas.read_file(geopandas.datasets.get_path("naturalearth_cities"))
print(countries.head())
print(cities.head())

cities_w_country_data = cities.sjoin(countries)
print(cities_w_country_data.head())
Run Code Online (Sandbox Code Playgroud)

但我遇到了同样的问题。请问有人可以解释这个错误并提供解决方案吗?谢谢。

输出如下:

C:\Users\mmm\AppData\Local\ESRI\conda\envs\arcgispro-py3_clone\lib\site-packages\geopandas\array.py:85: ShapelyDeprecationWarning: __len__ for multi-part geometries is deprecated and will be removed in Shapely 2.0. Check the length of the `geoms` property instead to get the  number of parts of a multi-part geometry.
  aout[:] = out
C:\Users\mmm\AppData\Local\ESRI\conda\envs\arcgispro-py3_clone\lib\site-packages\geopandas\geodataframe.py:35: ShapelyDeprecationWarning: The array interface is deprecated and will no longer work in Shapely 2.0. Convert the '.coords' to a numpy array instead.
  out = from_shapely(data)
     pop_est      continent  ...  gdp_md_est                                           geometry
0     920938        Oceania  ...      8374.0  MULTIPOLYGON (((180.00000 -16.06713, 180.00000...
1   53950935         Africa  ...    150600.0  POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...
2     603253         Africa  ...       906.5  POLYGON ((-8.66559 27.65643, -8.66512 27.58948...
3   35623680  North America  ...   1674000.0  MULTIPOLYGON (((-122.84000 49.00000, -122.9742...
4  326625791  North America  ...  18560000.0  MULTIPOLYGON (((-122.84000 49.00000, -120.0000...

[5 rows x 6 columns]
           name                   geometry
0  Vatican City  POINT (12.45339 41.90328)
1    San Marino  POINT (12.44177 43.93610)
2         Vaduz   POINT (9.51667 47.13372)
3    Luxembourg   POINT (6.13000 49.61166)
4       Palikir  POINT (158.14997 6.91664)
Traceback (most recent call last):
  File "geopandas_sjoin_official_eg.py", line 8, in <module>
    cities_w_country_data = cities.sjoin(countries)
  File "C:\Users\mmm\AppData\Local\ESRI\conda\envs\arcgispro-py3_clone\lib\site-packages\pandas\core\generic.py", line 5465, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'GeoDataFrame' object has no attribute 'sjoin'

Run Code Online (Sandbox Code Playgroud)

注意:我使用的是 Windows 10。我在克隆的 ArcGIS Pro Conda 环境中运行代码。我最近使用命令“conda install geopandas -y”安装了 geopandas

Ser*_*nes 0

进口sjoingeopandas.tools。用你的数据:

import geopandas
from geopandas.tools import sjoin

countries = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
cities = geopandas.read_file(geopandas.datasets.get_path("naturalearth_cities"))
print(countries.head())
print(cities.head())

cities_w_country_data = cities.sjoin(countries)
print(cities_w_country_data.head())
Run Code Online (Sandbox Code Playgroud)

这是

     pop_est      continent                      name iso_a3  gdp_md_est  \
0     920938        Oceania                      Fiji    FJI      8374.0   
1   53950935         Africa                  Tanzania    TZA    150600.0   
2     603253         Africa                 W. Sahara    ESH       906.5   
3   35623680  North America                    Canada    CAN   1674000.0   
4  326625791  North America  United States of America    USA  18560000.0   

                                            geometry  
0  MULTIPOLYGON (((180.00000 -16.06713, 180.00000...  
1  POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...  
2  POLYGON ((-8.66559 27.65643, -8.66512 27.58948...  
3  MULTIPOLYGON (((-122.84000 49.00000, -122.9742...  
4  MULTIPOLYGON (((-122.84000 49.00000, -120.0000...  
           name                   geometry
0  Vatican City  POINT (12.45339 41.90328)
1    San Marino  POINT (12.44177 43.93610)
2         Vaduz   POINT (9.51667 47.13372)
3    Luxembourg   POINT (6.13000 49.61166)
4       Palikir  POINT (158.14997 6.91664)
        name_left                   geometry  index_right   pop_est continent  \
0    Vatican City  POINT (12.45339 41.90328)          141  62137802    Europe   
1      San Marino  POINT (12.44177 43.93610)          141  62137802    Europe   
192          Rome  POINT (12.48131 41.89790)          141  62137802    Europe   
2           Vaduz   POINT (9.51667 47.13372)          114   8754413    Europe   
184        Vienna  POINT (16.36469 48.20196)          114   8754413    Europe   

    name_right iso_a3  gdp_md_est  
0        Italy    ITA   2221000.0  
1        Italy    ITA   2221000.0  
192      Italy    ITA   2221000.0  
2      Austria    AUT    416600.0  
184    Austria    AUT    416600.0  
Run Code Online (Sandbox Code Playgroud)

如果您想要其他类型的加入

cities_w_country_data = sjoin(cities, countries, how="inner", op='intersects')

Run Code Online (Sandbox Code Playgroud)

这使

        name_left                     geometry  index_right    pop_est  \
0    Vatican City    POINT (12.45339 41.90328)          141   62137802   
1      San Marino    POINT (12.44177 43.93610)          141   62137802   
192          Rome    POINT (12.48131 41.89790)          141   62137802   
2           Vaduz     POINT (9.51667 47.13372)          114    8754413   
184        Vienna    POINT (16.36469 48.20196)          114    8754413   
..            ...                          ...          ...        ...   
195       Jakarta   POINT (106.82749 -6.17247)            8  260580739   
196        Bogota    POINT (-74.08529 4.59837)           32   47698524   
197         Cairo    POINT (31.24802 30.05191)          163   97041072   
198         Tokyo   POINT (139.74946 35.68696)          155  126451398   
200      Santiago  POINT (-70.66899 -33.44807)           10   17789267   

         continent name_right iso_a3  gdp_md_est  
0           Europe      Italy    ITA   2221000.0  
1           Europe      Italy    ITA   2221000.0  
192         Europe      Italy    ITA   2221000.0  
2           Europe    Austria    AUT    416600.0  
184         Europe    Austria    AUT    416600.0  
..             ...        ...    ...         ...  
195           Asia  Indonesia    IDN   3028000.0  
196  South America   Colombia    COL    688000.0  
197         Africa      Egypt    EGY   1105000.0  
198           Asia      Japan    JPN   4932000.0  
200  South America      Chile    CHL    436100.0  

[172 rows x 8 columns]
Run Code Online (Sandbox Code Playgroud)