我有一个 .shp 文件,我将其读入 geopandas 数据帧中。我将坐标参考系更改为 2163,因为我正在制作一些矩形地图并希望它们看起来有些正常。
geo_df = geo.GeoDataFrame.from_file('path to shp files here')
geo_df = geo_df.to_crs(epsg=2163)
Run Code Online (Sandbox Code Playgroud)
数据框看起来像这样:
In [10]: geo_df.geometry
Out[10]:
0 POLYGON ((1189879.121395004 -1019103.847184072...
1 POLYGON ((1220434.289428635 -1303875.122589418...
2 POLYGON ((1210969.088787247 -1295221.772496042...
3 POLYGON ((1217371.162725744 -1300978.843646188...
Run Code Online (Sandbox Code Playgroud)
现在我正在计算一个成对距离矩阵,但由于这些坐标以米为单位给出,因此数字非常大,并且遇到了溢出问题。所以我尝试这个:
geo_df.geometry = geo_df.geometry.scale(xfact=1/10000, yfact=1/10000, zfact=1.0)
Run Code Online (Sandbox Code Playgroud)
但我的多边形的坐标并没有变小(只是移动了一点,这很好)。打印据称重新缩放的数据框给出了以下结果:
In [12]: geo_df.geometry.scale(xfact=1/10000, yfact=1/10000, zfact=1.0)
Out[12]:
0 POLYGON ((1197230.99656014 -1026296.564074459,...
1 POLYGON ((1221475.564597901 -1304490.752661498...
2 POLYGON ((1215982.083961291 -1290526.930856638...
3 POLYGON ((1223839.012413891 -1292585.80012823,...
Run Code Online (Sandbox Code Playgroud)
我希望能得到这样的东西。
Out[12]:
0 POLYGON ((119.723099656014 -102.6296564074459,...
1 POLYGON ((122.1475564597901 -130.4490752661498...
2 POLYGON ((121.5982083961291 -129.0526930856638... …Run Code Online (Sandbox Code Playgroud) 我有一个 geopandas 数据框,其中一个GeoSeries.
此列只有一个条目,即shapely.geometry.linestring.LineString.
LineString (first_lon first_lat, second_lon second_lat, ...)
Run Code Online (Sandbox Code Playgroud)
我找不到一种简单的方法来获取LineString像这样的 DataFrame的坐标
LON LAT
first_lon first_LAT
second_lon second_LAT
...
Run Code Online (Sandbox Code Playgroud)
是否有为此构建的功能?
谢谢
我有一个带有“几何”列的地理数据框,其中有点几何图形,例如 POINT (-0.01334 51.52883)。我需要提取纬度和经度并将其添加为数据框中的新列。
我尝试使用
df['lon'] = df['geometry'].x
df['lat'] = df['geometry'].y
Run Code Online (Sandbox Code Playgroud)
但它给了我一个SettingWithCopyWarning,我不知道为什么。有人可以帮忙吗?谢谢你!
我正在尝试将使用 OSMNX 绘制的路线保存到磁盘。生成的路线遵循道路路径,但是,当我尝试将路径转换为可以轻松保存到磁盘的 LineString 时,路线会以不遵循可以在两个图像之间进行比较的道路的方式进行更改。
ORIGIN_point = (13.013206, 77.670987)
DESTINATION_point= (12.821339, 77.678500)
G = ox.graph_from_point(ORIGIN_point, distance=10000, distance_type='network', network_type='drive')
ORIGIN_node = ox.get_nearest_node(G, ORIGIN_point)
DESTINATION_node = ox.get_nearest_node(G, DESTINATION_point)
# find the route between these nodes then plot it
route = nx.shortest_path(G, ORIGIN_node, DESTINATION_node, weight='length')
Run Code Online (Sandbox Code Playgroud)
该路线是使用 osmnx 绘制的最短路径IMAGE-1
from shapely.geometry import LineString, Point
graph_proj = ox.project_graph(G)
nodes_proj, edges_proj = ox.graph_to_gdfs(graph_proj, nodes=True, edges=True)
route_nodes = nodes_proj.loc[route]
# Create a geometry for the shortest path
route_line = LineString(list(route_nodes.geometry.values))
Run Code Online (Sandbox Code Playgroud)
路线现在转换为 lineString IMAGE-2
我想将路径保存到磁盘的形状文件中,请帮助我。
我有两个地理数据框。一个包含点,另一个包含多边形。
我需要从 GeoDataFrame 获取任何多边形内的所有点。
我尝试迭代所有多边形并检查点是否位于该多边形内。该解决方案有效,但速度非常慢。
我想知道是否有其他方法来解决这个任务。
编辑:我的解决方案如下所示:
for i in range(len(Poly_gdf.index)):
inter = Points_gdf[Points_gdf.intersects(Poly_gdf.loc[i,'geometry'])]
if not inter.empty:
for i in inter['geometry'].values:
points.append(i)
Run Code Online (Sandbox Code Playgroud) 以下是具有相同点集但起点/舍入误差不同但方向仍然相同的多边形。
poly1 = Polygon([(0,0),(0,1),(1,1),(1,0)])
poly2 = Polygon([(0,1),(1,1),(1,0),(0,0)])
poly3 = Polygon([(0,1),(1,1.00000001),(1,0),(0,0)])
poly4 = Polygon([(0,0),(0,1),(1,1.00000001),(1,0)])
Run Code Online (Sandbox Code Playgroud)
问题1:poly1.almost_equals(poly2)返回False但poly1.equals(poly2)返回True。所以equals可以处理不同的起点但almost_equals不能。
问题2:poly1.almost_equals(poly3)返回False但poly1.almost_equals(poly4)返回True。因此almost_equals可以处理舍入误差,但起点仍然不同。
这是almost_equals函数应该表现的方式吗?我认为具有不同起点的多边形仍然是相同的多边形,应该如此对待。有方便的方法来解决这个问题吗?我有一个复杂的自定义解决方案,但我想知道这样的操作是否已在 Shapely 中实现。
大家好,有没有办法绘制线串列表和点列表
例如我有
line_string = [LINESTRING (-1.15.12 9.9, -1.15.13 9.93), LINESTRING (-2.15.12 8.9, -2.15.13 8.93)]
point = [POINT (5.41 3.9), POINT (6.41 2.9)]
Run Code Online (Sandbox Code Playgroud)
我的目标是拥有一张地图或图表,它可以向我显示点与线串的连接位置。
先感谢您
编辑
当我绘制它的样子时,谢谢大家的悲伤回答。我认为问题在于,有些 LINESTRINGS 有 4 个点(LINESTRING (-1.15.12 9.9, -1.15.13 9.93, -5.15.13 5.53, -3.15.13 2.23)),有些有 3 个点。有没有办法更好地绘制这些?
我shapely.ops.unary_union在许多 6 边上使用shapely.geometry.Polygon,并获得以下形状A:
注意上部有两条“裂缝”。这些不是有意的,并且可能是由一些浮点边缘情况引起的。
如果您构建B位于 内部的另一个形状A,并且如果A碰巧与这些“裂缝”之一相交,那么A.covers(B)将会是False!
在我的特定情况下,这会导致测试套件失败,因为A.covers(B)它应该是一个不变量。因此我需要以某种方式处理这个问题。我可以使用某种算法来“密封”这些裂缝吗?
在实践中,这些“裂缝”不会影响应用程序的功能,因为我们只关心A覆盖的外边界B。因此,我愿意接受调整各个六边形的解决方案,以便通过引入重叠来消除裂缝。
但是,我无法接受这种形状变化的外边框,因为这实际上将不再测试应用程序的用途。
总而言之,我希望结果看起来像这样(我的手工编辑版本):
我曾经使用 shapely 制作一个圆圈并将其绘制在之前填充的图上。这曾经工作得很好。最近,我收到索引错误。我将代码分解为最简单的操作,但它甚至无法执行最简单的循环。
import descartes
import shapely.geometry as sg
import matplotlib.pyplot as plt
circle = sg.Point((0,0)).buffer(1)
# Plot the cricle
fig = plt.figure()
ax = fig.add_subplot(111)
patch = descartes.PolygonPatch(circle)
ax.add_patch(patch)
plt.show()
Run Code Online (Sandbox Code Playgroud)
以下是我现在遇到的错误。我觉得这可能是新版本与可能发生的事情不匹配。我尝试卸载并重新安装最后一个已知的稳定版本,但这也没有帮助
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[20], line 6
4 fig = plt.figure()
5 ax = fig.add_subplot(111)
----> 6 patch = descartes.PolygonPatch(circle)
7 ax.add_patch(patch)
8 plt.show()
File ~/env/lib/python3.8/site-packages/descartes/patch.py:87, in PolygonPatch(polygon, **kwargs)
73 def PolygonPatch(polygon, **kwargs):
74 """Constructs a matplotlib patch from a geometric object
75
76 …Run Code Online (Sandbox Code Playgroud) 尝试计算两个几何对象之间的交集时收到此警告。
>>> shapely.intersection(LineString([(0, 0), (1, 1)], LineString([(2.5, 2.5), (3, 3)]))
.../lib/python3.9/site-packages/shapely/set_operations.py:133: RuntimeWarning: invalid value encountered in intersection
return lib.intersection(a, b, **kwargs)
Run Code Online (Sandbox Code Playgroud)