我试图在GeoPandas中找到两个多边形的并集,并输出一个包含两个多边形点作为其顶点的几何体.该geopandas.overlay
函数为每个单独的联合提供了多边形,但我想要一个多边形.
对于上下文,我使用它将两个管理区域组合成一个区域(即包括一个国家内的城镇区域).
以下示例来自geopandas网站,并说明了我想要的内容:
from matplotlib import pyplot as plt
import geopandas as gpd
from shapely.geometry import Polygon
polys1 = gpd.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
Polygon([(2,2), (4,2), (4,4), (2,4)])])
polys2 = gpd.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
Polygon([(3,3), (5,3), (5,5), (3,5)])])
df1 = gpd.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})
df2 = gpd.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})
res_union = gpd.overlay(df1, df2, how='union')
res_union.plot()
Run Code Online (Sandbox Code Playgroud)
没有任何输出几何是我所期望的,具体如下:
poly_union = gpd.GeoSeries([Polygon([(0,0), (0,2), (1,2), (1,3), \
(2,3), (2,4), (3, 4), (3, 5), (5, 5), (5, 3), (4, 3), (4, 2), \
(3,2), (3,1), …
Run Code Online (Sandbox Code Playgroud) 我唯一的代码是
import geopandas
Run Code Online (Sandbox Code Playgroud)
它给了我错误
OSError: Could not find libspatialindex_c library file
Run Code Online (Sandbox Code Playgroud)
有没有人遇到过这个?我的脚本工作正常,直到出现此错误.
**请注意,未找到rtree模块是一个问题fisrt,安装后我收到了上述错误.
我试图通过运行!pip install geopandas来安装通过I python安装geopandas,但这失败,"python setup.py egg_info"失败,错误代码为1,然后是路径到长目录.我在网上看到piproj是geopandas所必需的,并试图安装它,但没有运气,类似的错误.有人能指出我正确的方向吗?谢谢.
顺便说一句,如果这有帮助,我可以使用这种方法安装匀称,fiona和Descartes.
我有一个存储为 csv 文件的数据框,其中一列是 Polygon 对象。但是,此列存储为字符串而不是 GeoPandas 几何对象。如何将此列转换为 Geopandas 几何对象,以便我可以执行地理分析?
这是我的数据的样子
my_df['geometry'].head()
Run Code Online (Sandbox Code Playgroud)
0 POLYGON ((-122.419942 37.809021, -122.419938 3...
1 POLYGON ((-122.419942 37.809021, -122.419938 3...
2 POLYGON ((-122.419942 37.809021, -122.419938 3...
3 POLYGON ((-122.419942 37.809021, -122.419938 3...
4 POLYGON ((-122.405659 37.806674, -122.405974 3...
Name: geometry, dtype: object
Run Code Online (Sandbox Code Playgroud)
我想将此 Pandas DataFrame 转换为 Geopandas GeoDataFrame,使用列“几何”作为 Geopandas 几何列。
my_geo_df = gpd.GeoDataFrame(my_df, geometry=my_df['geometry'])
Run Code Online (Sandbox Code Playgroud)
但是,由于该列存储为字符串,因此 Geopandas.DataFrame() 无法识别它,因此实际上无法创建 GeoDataFrame。
TypeError: Input geometry column must contain valid geometry objects.
Run Code Online (Sandbox Code Playgroud) 鉴于此处提供的形状文件:我想在地图中标记每个多边形(县).这可能与GeoPandas有关吗?
import geopandas as gpd
import matplotlib.pyplot as plt
%matplotlib inline
shpfile=<Path to unzipped .shp file referenced and linked above>
c=gpd.read_file(shpfile)
c=c.loc[c['GEOID'].isin(['26161','26093','26049','26091','26075','26125','26163','26099','26115','26065'])]
c.plot()
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我正在尝试在 python 中剪辑空间数据,但是当我运行我的代码时......
europe = gpd.clip(worldmap, europe_bound_gdf)
Run Code Online (Sandbox Code Playgroud)
...我收到错误:
(ImportError: Spatial indexes require either
rtree or
pygeos`。)
当我尝试使用以下命令安装 rtree 时:
pip3 install rtree
Run Code Online (Sandbox Code Playgroud)
有人告诉我:
Requirement already satisfied: rtree in /Users/joshuajones/.pyenv/versions/3.9.4/lib/python3.9/site-packages (0.9.7`)
Run Code Online (Sandbox Code Playgroud)
那么为什么我的代码不起作用呢?
I'am trying to run a simple geopandas code using ANACONDA spyder. However, I'am encountering an error.
I have included the code and the error as below:
--
here is the code:
import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.plot()
import matplotlib.pyplot as plt
plt.show()
Run Code Online (Sandbox Code Playgroud)
--
here is the error:
File "C:\Users\usr\Anaconda3\lib\site-packages\geopandas\plotting.py", line 90, in plot_polygon_collection "The descartes package is required for plotting polygons in geopandas."
ImportError: The descartes package is required for plotting polygons in geopandas.
--
I checked online …
我在 python 中使用 GeoPandas 并且有一个有效的多边形 GeoDataframe。
0 POLYGON Z ((68.70999999999999 623.1 0, 35.71 6...
1 POLYGON Z ((221.33 645.02 0, 185.7 640.33 0, 1...
2 POLYGON Z ((150.3 650 0, 160.9 650 0, 150.58 6...
Run Code Online (Sandbox Code Playgroud)
我想获得一个新的数据帧,它具有数据帧中每一行的边界框坐标。
现在我对 GeoPandas 有一些奇怪的行为。
假设我命名 GeoDataFrame gdf
,然后使用代码:
gdf.bounds
Run Code Online (Sandbox Code Playgroud)
我得到相应的错误。我不知道这个错误是什么意思,因为我没有将任何值传递给bounds
方法——它们是隐式传递的。
ValueError: Shape of passed values is (1, 110042), indices imply (4, 110042)
Run Code Online (Sandbox Code Playgroud)
当我尝试时:
gdf.geometry.bounds
我得到相同的结果ValueError...
但是,当我这样做时,我得到了一个有效的答案:
gdf.head(10).bounds
Run Code Online (Sandbox Code Playgroud)
我得到
minx miny maxx maxy
0 0.00 618.15 68.71 650.00
1 169.56 640.33 221.33 650.00 …
Run Code Online (Sandbox Code Playgroud) 这是我的第一个 geodatframe :
!pip install geopandas
import pandas as pd
import geopandas
city1 = [{'City':"Buenos Aires","Country":"Argentina","Latitude":-34.58,"Longitude":-58.66},
{'City':"Brasilia","Country":"Brazil","Latitude":-15.78 ,"Longitude":-70.66},
{'City':"Santiago","Country":"Chile ","Latitude":-33.45 ,"Longitude":-70.66 }]
city2 = [{'City':"Bogota","Country":"Colombia ","Latitude":4.60 ,"Longitude":-74.08},
{'City':"Caracas","Country":"Venezuela","Latitude":10.48 ,"Longitude":-66.86}]
city1df = pd.DataFrame(city1)
city2df = pd.DataFrame(city2)
gcity1df = geopandas.GeoDataFrame(
city1df, geometry=geopandas.points_from_xy(city1df.Longitude, city1df.Latitude))
gcity2df = geopandas.GeoDataFrame(
city2df, geometry=geopandas.points_from_xy(city2df.Longitude, city2df.Latitude))
Run Code Online (Sandbox Code Playgroud)
城市1
City Country Latitude Longitude geometry
0 Buenos Aires Argentina -34.58 -58.66 POINT (-58.66000 -34.58000)
1 Brasilia Brazil -15.78 -47.91 POINT (-47.91000 -15.78000)
2 Santiago Chile -33.45 -70.66 POINT (-70.66000 -33.45000)
Run Code Online (Sandbox Code Playgroud)
和我的第二个地理数据框: City2 …
我有一个具有多边形几何形状的地理数据框:
我想将它们转换为多边形,即填充多多边形的孔并使其成为单个多边形。
我已经尝试过这个类似问题的代码:
from shapely.geometry import MultiPolygon, Polygon
gdf['Polygon'] = gdf['SHAPE'].apply( lambda x: MultiPolygon(Polygon(p.exterior) for p in x))
Run Code Online (Sandbox Code Playgroud)
但我收到错误:
TypeError: 'Polygon' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
我尝试过堆栈溢出的其他解决方案,但没有成功。
有任何想法吗?
以下是数据类型:
FID int64
LHO object
Shape__Area float64
Shape__Length float64
SHAPE geometry
Run Code Online (Sandbox Code Playgroud)
这是获取 shapefile 的完整代码:
import pandas as pd
import geopandas as gpd
from arcgis import GIS
gis = GIS(verify_cert=False,api_key='your_api_key')
search_result = gis.content.search(query="title:National_LHO", item_type="Feature Layer")
# get layer
layer = search_result[0].layers[0]
# dataframe from layer
df= pd.DataFrame.spatial.from_layer(layer)
gdf = gpd.GeoDataFrame(df)
gdf = gdf.set_geometry('SHAPE') …
Run Code Online (Sandbox Code Playgroud) geopandas ×10
python ×8
shapely ×3
gis ×2
pandas ×2
dataframe ×1
geolocation ×1
geospatial ×1
install ×1
matplotlib ×1
polygon ×1
python-3.x ×1