如何检查多边形实体是否实际上是多面?我试过了:
if len(polygon) > 1:
Run Code Online (Sandbox Code Playgroud)
但后来得到错误:
TypeError: object of type 'Polygon' has no len()
Run Code Online (Sandbox Code Playgroud)
我试过了Nill,None其他人没有用.
我试图将一个点插入到LineStringShapely 中,然后相应地分割线串。但是,由于精度误差,Shapely 认为插值点不在 上linestring,因此该split操作不起作用。
这是一个例子:
from shapely.ops import split
from shapely.geometry import LineString, Point
### Initialize point and line
line = LineString([(0.123,0.456),(5.678,7.890),(12.135,6.789)])
point = Point(4.785,8.382)
### Interpolate point onto line
new_point = line.interpolate(line.project(point))
print new_point
>> POINT (5.593949278213755 7.777518800043393)
### BUT: line does not intersect the interpolated point
line.intersects(new_point)
>> False
### EVEN THOUGH: distance between them is essentially (not exactly) zero
line.distance(new_point)
>> 0.0
### THEREFORE: line cannot be split using the new …Run Code Online (Sandbox Code Playgroud) 我对python很新,所以关于这个问题的答案可能很简单,但我到处寻找并尝试了很多,但找不到答案.
使用Shapely简化多边形可能会导致空多边形.如果多边形为空,我想用点替换多边形.有点像:
if mypoly is empty:
mypoly = [(0,0)]
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) 我想在pyspark中使用matplotlib.bblpath或shapely.geometry库.
当我尝试导入其中任何一个时,我得到以下错误:
>>> from shapely.geometry import polygon
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named shapely.geometry
Run Code Online (Sandbox Code Playgroud)
我知道模块不存在,但是如何将这些包带到我的pyspark库?
我已经安装shapely并将pip install shapely其导入为from shapely.geometry import Point. 我收到此错误:
from shapely.geometry import Point
ModuleNotFoundError: No module named 'shapely'
Run Code Online (Sandbox Code Playgroud)
我是 MacOS High Sierra。所有其他已安装的 python 库都可以正常工作。我是 Python 新手,所以请提前原谅我。我读过几个类似的问题(比如这个),但似乎没有一个能解决我的问题。
我已经看到这个问题被问到,但还没有真正找到完整的答复。我有一个简单的形状多边形,称为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) 我有一个由七个重叠圆和椭圆组成的簇,我正在尝试将它组合成一个形状,但是当我运行cascaded_union()时,我得到错误:ValueError:No可以从null值创建Shapely几何
这是我到目前为止所写的内容:
ValueError: No Shapely geometry can be created from null value
Run Code Online (Sandbox Code Playgroud)
我的目标是获得右图所示的内容, http://toblerity.org/shapely/code/cascaded_union.png ,其余的代码将确定随机分布中有多少点落在不规则的边界内形状.当它返回"空值"注释时,我对错误引用的内容感到困惑.我是不是以正确的方式考虑了各个形状的重叠?从我搜索过的内容来看,已经cascaded_union接受了一系列形状的输入,但由于某些原因,在这种情况下不起作用.
我希望使用Geopandas/Shapely 做相当于ArcPy Generate Near Table的工作.我是Geopandas和Shapely的新手,并开发了一种有效的方法,但我想知道是否有更有效的方法.
我有两个点文件数据集 - Census Block Centroids和餐馆.我希望找到每个人口普查区块中心距离它最近的餐厅的距离.对于同一个餐厅而言,对于多个街区来说,最近的餐厅没有任何限制.
这对我来说有点复杂的原因是因为Geopandas距离函数根据索引计算元素,匹配.因此,我的一般方法是将Restaurants文件转换为多点文件,然后将blocks文件的索引设置为全部相同的值.然后,所有块质心和餐馆都具有相同的索引值.
import pandas as pd
import geopandas as gpd
from shapely.geometry import Polygon, Point, MultiPoint
Run Code Online (Sandbox Code Playgroud)
现在阅读Block Centroid和Restaurant Shapefiles:
Blocks=gpd.read_file(BlockShp)
Restaurants=gpd.read_file(RestaurantShp)
Run Code Online (Sandbox Code Playgroud)
由于Geopandas距离函数按元素计算距离,我将Restaurant GeoSeries转换为MultiPoint GeoSeries:
RestMulti=gpd.GeoSeries(Restaurants.unary_union)
RestMulti.crs=Restaurants.crs
RestMulti.reset_index(drop=True)
Run Code Online (Sandbox Code Playgroud)
然后我将Blocks的索引设置为等于0(与Restaurants多点相同的值)作为元素计算的解决方法.
Blocks.index=[0]*len(Blocks)
Run Code Online (Sandbox Code Playgroud)
最后,我使用Geopandas距离函数计算每个Block质心到最近餐馆的距离.
Blocks['Distance']=Blocks.distance(RestMulti)
Run Code Online (Sandbox Code Playgroud)
请提供有关如何改进这方面的任何建议.我并不喜欢使用Geopandas或Shapely,但我希望学习ArcPy的替代方案.
谢谢您的帮助!
我想将Shapely用于我的计算几何项目。为此,我需要能够可视化和显示多边形,直线和其他几何对象。我试图为此使用matplotlib,但遇到了麻烦。
from shapely.geometry import Polygon
import matplotlib.pyplot as plt
polygon1 = Polygon([(0,5),
(1,1),
(3,0),
])
plt.plot(polygon1)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我希望能够在绘图中显示此多边形。我将如何更改我的代码来做到这一点?
python ×10
shapely ×10
geopandas ×3
mask ×1
matplotlib ×1
pandas ×1
polygon ×1
pyspark ×1
python-2.7 ×1