我刚刚创建了一个非常简单的geopandas示例(请参见下文)。它有效,但是我注意到能够拥有世界的自定义部分对我来说很重要。有时德国,有时只有柏林。(此外,我想按在geopandas文件中定义为多边形的区域汇总我拥有的数据,但我将其添加到另一个问题中。)
我如何获得与“
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
Run Code Online (Sandbox Code Playgroud)
可视化?
# 3rd party modules
import pandas as pd
import geopandas as gpd
import shapely
# needs 'descartes'
import matplotlib.pyplot as plt
df = pd.DataFrame({'city': ['Berlin', 'Paris', 'Munich'],
'latitude': [52.518611111111, 48.856666666667, 48.137222222222],
'longitude': [13.408333333333, 2.3516666666667, 11.575555555556]})
gdf = gpd.GeoDataFrame(df.drop(['latitude', 'longitude'], axis=1),
crs={'init': 'epsg:4326'},
geometry=[shapely.geometry.Point(xy)
for xy in zip(df.longitude, df.latitude)])
print(gdf)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
base = world.plot(color='white', edgecolor='black')
gdf.plot(ax=base, marker='o', color='red', markersize=5)
plt.show()
Run Code Online (Sandbox Code Playgroud) 我试图从一些.GDB文件(文件夹?)看这里:。
我使用 GeoPandas 并执行以下操作:
# file from local path
mallard = gpd.read_file('./bird-species/E00039600_mallard.gdb/')
# geopandas included map, filtered to just this hemisphere
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
westhem = world[(world['continent'] == 'North America') |
(world['continent'] == 'South America')]
# making sure the coordinates line up:
mallard = mallard.to_crs(world.crs)
#establishing figure axes
base = westhem.plot(color='white', edgecolor='black',figsize=(11,11))
# cmap because I'd LIKE the multiple layers to exist
bbw_duck.plot(ax=base, cmap = 'Reds');
Run Code Online (Sandbox Code Playgroud)
输出如下所示:
有没有办法在 GeoPandas 或 Python(Jupyter Notebook)中查看所有层?
我一直在尝试在地理数据框架上使用"相交"功能,以查看哪些点位于多边形内.但是,只有帧中的第一个特征才会返回true.我究竟做错了什么?
from geopandas.geoseries import *
p1 = Point(.5,.5)
p2 = Point(.5,1)
p3 = Point(1,1)
g1 = GeoSeries([p1,p2,p3])
g2 = GeoSeries([p2,p3])
g = GeoSeries([Polygon([(0,0), (0,2), (2,2), (2,0)])])
g1.intersects(g) # Flags the first point as inside, even though all are.
g2.intersects(g) # The second point gets picked up as inside (but not 3rd)
Run Code Online (Sandbox Code Playgroud) 假设我有以下字符串的GeoDataFrames,其中一个代表道路,其中一个代表等高线.
>>> import geopandas as gpd
>>> import geopandas.tools
>>> import shapely
>>> from shapely.geometry import *
>>>
>>> r1=LineString([(-1,2),(3,2.5)])
>>> r2=LineString([(-1,4),(3,0)])
>>> Roads=gpd.GeoDataFrame(['Main St','Spruce St'],geometry=[r1,r2], columns=['Name'])
>>> Roads
Name geometry
0 Main St LINESTRING (-1 2, 3 2.5)
1 Spruce St LINESTRING (-1 4, 3 0)
>>>
>>> c1=LineString(Point(1,2).buffer(.5).exterior)
>>> c2=LineString(Point(1,2).buffer(.75).exterior)
>>> c3=LineString(Point(1,2).buffer(.9).exterior)
>>> Contours=gpd.GeoDataFrame([100,90,80],geometry=[c1,c2,c3], columns=['Elevation'])
>>> Contours
Elevation geometry
0 100 LINESTRING (1.5 2, 1.497592363336099 1.9509914...
1 90 LINESTRING (1.75 2, 1.746388545004148 1.926487...
2 80 LINESTRING (1.9 2, …Run Code Online (Sandbox Code Playgroud) 我正在尝试对 2 个地理数据框进行空间连接。
\n\n两个索引都是这种类型:\nRangeIndex(start=0, stop=312, step=1)
\n\n我收到以下错误:
\n\n---------------------------------------------------------------------------\nValueError Traceback (most recent call last)\n<ipython-input-228-d0d0190e2940> in <module>()\n----> 1 Sales_new_data_concastinate_SR_coundaries_joines=gpd.sjoin(Sales_new_data_concastinated,SR_locations, op=\'within\',how=\'left\')\n\n~/anaconda3/lib/python3.7/site-packages/geopandas/tools/sjoin.py in sjoin(left_df, right_df, how, op, lsuffix, rsuffix)\n 51 or any(right_df.columns.isin([index_left, index_right]))):\n 52 raise ValueError("\'{0}\' and \'{1}\' cannot be names in the frames being"\n---> 53 " joined".format(index_left, index_right))\n 54 \n 55 # the rtree spatial index only allows limited (numeric) index types, but an\n\nValueError: \'index_left\' and \'index_right\' cannot be names in the frames being joined\nRun Code Online (Sandbox Code Playgroud)\n\n来源https://github.com/geopandas/geopandas/blob/master/geopandas/tools/sjoin.py
\n\n说: …
我将一个.csv文件作为数据框读取,如下所示:
import pandas as pd
df = pd.read_csv('myFile.csv')
df.head()
BoroName geometry
0 Brooklyn MULTIPOLYGON (((-73.97604935657381 40.63127590...
1 Queens MULTIPOLYGON (((-73.80379022888098 40.77561011...
2 Queens MULTIPOLYGON (((-73.8610972440186 40.763664477...
3 Queens MULTIPOLYGON (((-73.75725671509139 40.71813860...
4 Manhattan MULTIPOLYGON (((-73.94607828674226 40.82126321...
Run Code Online (Sandbox Code Playgroud)
我想将其转换为 geopandas 数据框。
import geopandas as gpd
crs = {'init': 'epsg:4326'}
gdf = gpd.GeoDataFrame(df, crs=crs).set_geometry('geometry')
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误
TypeError: Input must be valid geometry objects: MULTIPOLYGON (((-73.97604935657381 40.631275905646774, -73.97716511994669 40.63074665412933,....
Run Code Online (Sandbox Code Playgroud) 我从arcgis网站上获得了来自美国各州的多边形数据, 并且还有一个包含城市坐标的 Excel 文件。我已将坐标转换为几何数据(点)。现在我想测试积分是否在美国。两者都是 dtype: 几何。我认为这样我可以很容易地进行比较,但是当我使用我的代码时,我得到的每个点的答案都是错误的。即使有在美国的积分。
代码是:
import geopandas as gp
import pandas as pd
import xlsxwriter
import xlrd
from shapely.geometry import Point, Polygon
df1 = pd.read_excel('PATH')
gdf = gp.GeoDataFrame(df1, geometry= gp.points_from_xy(df1.longitude, df1.latitude))
US = gp.read_file('PATH')
print(gdf['geometry'].contains(US['geometry']))
Run Code Online (Sandbox Code Playgroud)
有人知道我做错了什么吗?
我已经看到这个问题被问到,但还没有真正找到完整的答复。我有一个简单的形状多边形,称为polygon。我想提取这个多边形作为二进制掩码(最好是 numpy 数组)。我该怎么做呢?
我还设法从 shapely 转换为 geopandas,如此处所示,因此从 geopandas 中提取掩模也可以工作,但不幸的是,我还没有真正找到这方面的线索。
编辑:要清楚,如果我要使用坐标网格,我的网格包含与构成形状轮廓的点相对应的 x 和 y 笛卡尔坐标(无序)。这些是浮点数,因此需要 int 输入的解决方案不太有效。理想情况下,我希望起点是一个匀称的多边形而不是一组点,但如果这是更可取的,我可以使用一组无序的点(或者以某种方式从匀称的多边形中提取顺时针顶点)
我已经尝试过 Yusuke 的方法,但我得到的面具不太有意义。
佑介的方法:
#%% create grid and plot
nx, ny = 100, 100
poly_verts = Plane1verts #this is a list of tuples containing cartesian coordinate pairs of the shape contour in x and y
# Create vertex coordinates for each grid cell...
# (<0,0> is at the top left of the grid in this system)
x, y …Run Code Online (Sandbox Code Playgroud) 我有困难装载包含GIS数据如下JSON(https://data.cityofnewyork.us/resource/5rqd-h5ci.json)成GeoDataFrame.
我尝试设置几何时,以下代码失败.
import requests
import geopandas as gpd
data = requests.get("https://data.cityofnewyork.us/resource/5rqd-h5ci.json")
gdf = gpd.GeoDataFrame(data.json())
gdf = gdf.set_geometry('the_geom')
gdf.head()
Run Code Online (Sandbox Code Playgroud) 我有一个GeoDataFrame的多边形(~30)和一个GeoDataFrame的点(~10k)
我想在我的GeoDataFrame中创建30个新的列(具有适当的多边形名称),如果该点存在于多边形中,则使用简单的布尔值True/False.
例如,多边形的GeoDataFrame是这样的:
id geometry
foo POLYGON ((-0.18353,51.51022, -0.18421,51.50767, -0.18253,51.50744, -0.1794,51.50914))
bar POLYGON ((-0.17003,51.50739, -0.16904,51.50604, -0.16488,51.50615, -0.1613,51.5091))
Run Code Online (Sandbox Code Playgroud)
Points的GeoDataFrame是这样的:
counter points
1 ((-0.17987,51.50974))
2 ((-0.16507,51.50925))
Run Code Online (Sandbox Code Playgroud)
预期产量:
counter points foo bar
1 ((-0.17987,51.50974)) False False
1 ((-0.16507,51.50925)) False False
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方式手动完成:
foo = df_poly.loc[df_poly.id=='foo']
df_points['foo'] = df_points['points'].map(lambda x: True if foo.contains(x).any()==True else False
Run Code Online (Sandbox Code Playgroud)
但鉴于我有30个多边形,我想知道是否有更好的方法.感谢任何帮助!