如何为某个点附近最近的 LINESTRING 提供资金?
首先,我有一个 LINESTRING 和点值列表。我如何获得距离点(5.41 3.9)最近的线串以及距离?
from shapely.geometry import Point, LineString
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)
#distance
line_string [0].distance(point)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我认为我通过对第一个 LINESTRING 执行 line_string [0].distance(point) 来获得距离值,但我只是想确保我以正确的方式进行操作。
我正在使用matplotlib和shapely测试多边形点函数.
这是一张包含百慕大三角形多边形的地图.
Google地图的多边形点函数清楚地表明,testsPoint和testingPoint2位于多边形内部,这是正确的结果.
如果我在matplotlib中测试两个点并且形状合理,则只有point2通过测试.
In [1]: from matplotlib.path import Path
In [2]: p = Path([[25.774252, -80.190262], [18.466465, -66.118292], [32.321384, -64.75737]])
In [3]: p1=[27.254629577800088, -76.728515625]
In [4]: p2=[27.254629577800088, -74.928515625]
In [5]: p.contains_point(p1)
Out[5]: 0
In [6]: p.contains_point(p2)
Out[6]: 1
Run Code Online (Sandbox Code Playgroud)
形状上显示与matplotlib相同的结果.
In [1]: from shapely.geometry import Polygon, Point
In [2]: poly = Polygon(([25.774252, -80.190262], [18.466465, -66.118292], [32.321384, -64.75737]))
In [3]: p1=Point(27.254629577800088, -76.728515625)
In [4]: p2=Point(27.254629577800088, -74.928515625)
In [5]: poly.contains(p1)
Out[5]: False
In …Run Code Online (Sandbox Code Playgroud) python matplotlib point-in-polygon google-maps-api-3 shapely
我试图在六个多边形内找到数百万个点.这是我的代码:
def find_shape(longitude,latitude):
if longitude != 0 and latitude != 0:
point = shapely.geometry.Point(longitude,latitude)
else:
return "Unknown"
for current_shape in all_shapes:
if current_shape['bounding_box'].contains(point):
if current_shape['shape'].contains(point):
return current_shape['properties']['ShapeName']
break
return "Unknown"
Run Code Online (Sandbox Code Playgroud)
我已经阅读了其他一些问题,这些问题涉及改善多边形点查询的性能.他们建议Rtrees.然而,这似乎是当有很多的多边形这是非常有用的情况下(36 000个问题,10万另一个),这是不可取的遍历所有.
正如您所见,我已经设置了一个边界框.这是我的形状设置代码:
with fiona.open(SHAPEFILE) as f_col:
all_shapes = []
for shapefile_record in f_col:
current_shape = {}
current_shape['shape'] = shapely.geometry.asShape(shapefile_record['geometry'])
minx, miny, maxx, maxy = current_shape['shape'].bounds
current_shape['bounding_box'] = shapely.geometry.box(minx, miny, maxx, maxy)
current_shape['properties'] = shapefile_record['properties']
all_shapes.append(current_shape)
Run Code Online (Sandbox Code Playgroud)
还要检查另一个非常简化的形状版本,即由最大的内切矩形(或三角形)制成的形状是否有用?
检查匀称的文档,它似乎没有这个功能.也许一些设置simplify()?当然,我总是希望确保新的简化形状不会超出原始形状的范围,因此我不必调用contains() …
Docstring说:
Polygon.contains 如果几何包含另一个,则返回True,否则返回False
Polygon.within 如果几何在另一个内,则返回True,否则返回False
他们有什么不同?
假设我有两个不相交的群体/多边形"群岛"(想想两个非相邻县的人口普查区).我的数据看起来像这样:
>>> p1=Polygon([(0,0),(10,0),(10,10),(0,10)])
>>> p2=Polygon([(10,10),(20,10),(20,20),(10,20)])
>>> p3=Polygon([(10,10),(10,20),(0,10)])
>>>
>>> p4=Polygon([(40,40),(50,40),(50,30),(40,30)])
>>> p5=Polygon([(40,40),(50,40),(50,50),(40,50)])
>>> p6=Polygon([(40,40),(40,50),(30,50)])
>>>
>>> df=gpd.GeoDataFrame(geometry=[p1,p2,p3,p4,p5,p6])
>>> df
geometry
0 POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))
1 POLYGON ((10 10, 20 10, 20 20, 10 20, 10 10))
2 POLYGON ((10 10, 10 20, 0 10, 10 10))
3 POLYGON ((40 40, 50 40, 50 30, 40 30, 40 40))
4 POLYGON ((40 40, 50 40, 50 50, 40 50, 40 40))
5 POLYGON …Run Code Online (Sandbox Code Playgroud) 我试图从多边形中提取多边形的形状.我可以使用MultiPolygon从形状上将多边形列表转换为多边形.
>>> Multi = MultiPolygon([shape(pol['geometry']) for pol in fiona.open('data.shp')])
Run Code Online (Sandbox Code Playgroud)
和,
>>> Multi.wkt
'MULTIPOLYGON (((249744.2315302934148349 142798.1643468967231456, 250113.7910872535139788 142132.9571443685272243, 250062.6213024436729029 141973.7622582934272941, 249607.7787708004761953 141757.7120557629095856, 249367.7742475979903247 142304.6840291862317827, 249367.7742475979903247 142304.6840291862317827, 249744.2315302934148349 142798.1643468967231456)),
((249175.7899173096520826 142292.5352640640921891, 249367.7742475979903247 142304.6840291862317827, 249607.7787708004761953 141757.7120557629095856, 249014.4539607730694115 141876.1348429077770561, 249175.7899173096520826 142292.5352640640921891)))'
Run Code Online (Sandbox Code Playgroud)
有谁知道如何反转过程,即给定多面,我如何将其转换为单独的多边形?
我在一个显示2D图像的示例ASCII文件中有一组点.
我想估计这些点填充的总面积.在这个平面内有一些地方没有被任何点填满,因为这些区域已经被掩盖了.我认为估计该区域可能是实用的将是应用凹形船体 或alpha形状.我尝试了这种方法来找到合适的alpha值,并因此估计面积.
from shapely.ops import cascaded_union, polygonize
import shapely.geometry as geometry
from scipy.spatial import Delaunay
import numpy as np
import pylab as pl
from descartes import PolygonPatch
from matplotlib.collections import LineCollection
def plot_polygon(polygon):
fig = pl.figure(figsize=(10,10))
ax = fig.add_subplot(111)
margin = .3
x_min, y_min, x_max, y_max = polygon.bounds
ax.set_xlim([x_min-margin, x_max+margin])
ax.set_ylim([y_min-margin, y_max+margin])
patch = PolygonPatch(polygon, fc='#999999',
ec='#000000', fill=True,
zorder=-1)
ax.add_patch(patch)
return fig
def alpha_shape(points, alpha):
if len(points) < 4:
# …Run Code Online (Sandbox Code Playgroud) 有没有办法使用任何 GDAL/OGR API 或命令行工具来溶解(合并)重叠的多边形,同时保持生成的非重叠区域不同?我已经搜索了很多,但找不到任何类似于需要的东西。不过,我认为这个问题不太可能还没有解决。
这是我需要的更详细的描述:
这是造成麻烦的最后一点。除了最后一点之外,我基本上得到了我需要的东西。如果我运行溶解形状文件的典型解决方案
$ ogr2ogr -f "ESRI Shapefile" dissolved.shp input.shp -dialect sqlite -sql "select ST_union(Geometry) from input"
Run Code Online (Sandbox Code Playgroud)
我最终得到一个包含所有内容的多边形,即使这些区域没有连接。
更新: 我通过完全放弃 GDAL 解决了这个问题。正如许多消息来源指出的那样,使用 fiona 和 shapely 来处理 shapefile 通常是更好的方法。我在下面发布了我的解决方案。
是否可以使用Shapely将 aMultipolygon转换为Polygon填充所有孔或缺失内部区域的 a ?我已经尝试了一段时间,但在文档中找不到它。下图显示了一个多边形示例,其中包含我要填充的孔和要删除的正方形。
我已经保存了一些Shapely Polygons的字符串表示形式:
'POLYGON ((51.0 3.0, 51.3 3.61, 51.3 3.0, 51.0 3.0))'
Run Code Online (Sandbox Code Playgroud)
有直接将其转换回多边形类型的快速方法吗?还是我需要手动分析字符串以创建Polygon对象?