我构建了一个python程序,该程序将通过我的电子邮件进行虹吸,并检索与未来分析相关的lat/long对工作站点。目前,我已返回以下数据帧。
lat1 long1 lat2 long2
0 29.886283 -97.932083 29.892553 -97.921784
1 29.890503 -97.940304 29.891903 -97.938405
2 30.56325 -97.661213 30.570474 -97.651814
3 29.890692 -97.954414 29.891938 -97.952977
4 29.890564 -97.938196 29.892173 -97.936506
.. ... ... ... ...
63 29.8900381016903 -97.9450610026556 29.8906241085088 -97.9442241534448
64 29.8847283631397 -97.9325702241829 29.8873980640358 -97.9291477254781
65 30.556555 -97.659824 30.569138 -97.650855
66 30.556555 -97.659824 30.569138 -97.650855
67 29.890564 -97.938196 29.892173 -97.936506
[68 rows x 4 columns]
我的问题是,我不知道如何使用GeoSeries.envelope函数将这些点转化为多条线,并最终转化为多边形。使用文档,我能够用一组lat/long对创建GeoDataFrame点,就像这样…
print(df)
gdf = geopandas.GeoDataFrame(
df, geometry=geopandas.points_from_xy(df.long1, df.lat1)) #df.lat2, df.long2))
print(gdf.head())
world = geopandas.read_file(geopandas.datasets.get_path(('naturalearth_lowres')))
ax = world[world.continent == 'North America'].plot(
color = 'white', edgecolor = 'black')
gdf.plot(ax = ax, color='green')
plt.show()
这会产生以下输出:
0 29.886283 -97.932083 29.892553 -97.921784 POINT (-97.93208 29.88628)
1 29.890503 -97.940304 29.891903 -97.938405 POINT (-97.94030 29.89050)
2 30.56325 -97.661213 30.570474 -97.651814 POINT (-97.66121 30.56325)
3 29.890692 -97.954414 29.891938 -97.952977 POINT (-97.95441 29.89069)
4 29.890564 -97.938196 29.892173 -97.936506 POINT (-97.93820 29.89056)
但我似乎不知道如何使用lat/long对将这些值作为行返回。
我希望在文档中看到一个与"points_from_xy"
类似的函数,它将生成多行GeoDataFrame,但我不认为存在任何这样的函数。
任何明智的言辞和(或(与文件的链接都将得到高度评价。
没有预先构建的精确变换方法,因此您必须自己创建几何体对象。我想您的意思是在DataFrame中每行有一个LineString
对象。只需少量键入,就可以使用强大的apply
方法创建这样一个列。
from shapely.geomtry import LineString
series = df.apply(
lambda r: LineString([
(r['long1'], r['lat1']),
(r['long2'], r['lat2'])
]),
axis=1
)
然后将其转换为GeoSeries:
In [28]: geopandas.GeoSeries(series)
Out[28]:
0 LINESTRING (29.886 -97.932, 29.893 -97.922)
1 LINESTRING (29.891 -97.940, 29.892 -97.938)
2 LINESTRING (30.563 -97.661, 30.570 -97.652)
3 LINESTRING (29.891 -97.954, 29.892 -97.953)
4 LINESTRING (29.891 -97.938, 29.892 -97.937)
dtype: geometry
如果我最初将坐标作为一个普通的Python数据结构(例如元组列表(,我可能会首先准备一个简单的LineString对象列表,只有当你特别需要它的处理/绘图能力时,才会将其放入(geo(panda机器中。