下面的代码是根据我在这里找到的代码进行修改的,该代码将形状优美的线串在沿线定义的点处分成两段。我还检查了其他问题,但他们没有直接解决我的问题。然而,我想扩展它以将线分成多个段(在多个点),到目前为止我所有的尝试都失败了。如何修改它以将字符串拆分为任意给定数量的段或在多个点((4,5),(9,18)和(6,5))。
input:
line = LineString([(1,2),(8,7),(4,5),(2,4),(4,7),(8,5),(9,18),(1,2),(12,7),(4,5),(6,5),(4,9)])
breakPoint = Point(2,4)
from shapely.geometry import Point,LineString
def make_line_segment(line_string, breakPoint):
geoLoc = line_string.coords
j = None
for i in range(len(geoLoc) - 1):
if LineString(geoLoc[i:i + 2]).intersects(breakPoint):
j = i
break
assert j is not None
# Make sure to always include the point in the first group
if Point(geoLoc[j + 1:j + 2]).equals(breakPoint):
return geoLoc[:j + 2], geoLoc[j + 1:]
else:
return geoLoc[:j + 1], geoLoc[j:]
line1,line2 = make_line_segment(line,breakPoint)
line1 = LineString(line1)
line2 …Run Code Online (Sandbox Code Playgroud) 给定美国的地理坐标,如何确定它是在城市还是农村地区?
我有大约10000个地理坐标,全部在美国,我想使用Python +底图来找出一个点是城市还是乡村。
我不确定要使用哪个库或形状文件。
我需要一个这样的函数:
def is_urban(coordinate):
# use the shapefile
urban = False
return urban
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 shapely 来识别扇形和矩形之间相交的区域。所以,我的问题分为两部分:
如何将扇区定义(创建、表示)为形状对象(三角形也足够),我的输入是坐标 x,y 、起始角度、结束角度、半径。
如何计算扇区列表和多边形(矩形)之间相交的面积
谢谢
我正在使用克林特·哈里斯的解决方案
import fiona
import shapely
with fiona.open("./areas_shp.shp") as fiona_collection:
shapefile_record = next(iter(fiona_collection))
shape = shapely.geometry.asShape( shapefile_record['geometry'] )
point = shapely.geometry.Point(coords[0])
for point in coords:
if (shape.contains(point)):
print("yay")
Run Code Online (Sandbox Code Playgroud)
在这里,我只是用 shapefile 测试一个坐标,但看起来代码可能已经过时了。我已经更改shapefile_record = fiona_collection.next()为shapefile_record = next(iter(fiona_collection)),但这个错误我似乎无法解决:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-18ca8979d01f> in <module>
38 with fiona.open("./areas_shp.shp") as fiona_collection:
39 shapefile_record = next(iter(fiona_collection))
---> 40 shape = shapely.geometry.asShape( shapefile_record['geometry'] )
41 point = shapely.geometry.Point(coords[0])
42 for point in coords:
AttributeError: module 'shapely' has no attribute …Run Code Online (Sandbox Code Playgroud) 我正在寻找在 Cartopy 地图上绘制一个形状优美的多边形。我查看了 Cartopy 和 shapely 文档,并查看了 StackOverflow 上的各种解决方案,但似乎没有任何效果,我不确定为什么。
这是我所拥有的:
from shapely.geometry.polygon import Polygon
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
# make the map
bounds = [(-122., -72., 25., 50.)]
plt.figure(figsize=(5, 3))
ax = plt.axes(projection=ccrs.LambertConformal())
ax.set_extent(*bounds, crs=ccrs.PlateCarree())
ax.add_feature(cfeature.COASTLINE.with_scale('50m'), linewidth=0.75)
ax.add_feature(cfeature.STATES, linewidth=0.5)
# add in the polygon
ax.add_geometries([polygon], crs=ccrs.PlateCarree(), facecolor = 'b', edgecolor='black', alpha=0.5)
Run Code Online (Sandbox Code Playgroud)
使用当前的代码,我得到了一张只有美国的空白图像。我认为:1)投影是错误的,而是将多边形放置在其他地方,或者2)多边形已ax.add_geometry正确放置在地图上,但由于属性设置不正确而未显示。
对于上述内容,polygon如下:
polygon = Polygon[(-6719446.159777582, 8747162.244957967),
(-6714522.73594268, 8747162.244957967),
(-6714522.73594268, 8742238.821123065),
(-6704675.888272878, 8742238.821123065),
(-6704675.888272878, 8737315.397288164),
(-6689905.616768174, 8737315.397288164), …Run Code Online (Sandbox Code Playgroud) 我有一个大(O(10^6)行)数据集(带值的点),我需要对所有点执行以下操作:
“非矢量化”方法是简单地循环所有点......对于所有点,然后应用逻辑。然而,这扩展性很差。
我已经包含了一个可以完成我想要的玩具示例。我已经考虑过的想法是:
这是我要实现的逻辑的一个玩具示例:
import pandas as pd
import numpy as np
from shapely.wkt import loads
import geopandas as gp
points=[
'POINT (1 1.1)', 'POINT (1 1.9)', 'POINT (1 3.1)',
'POINT (2 1)', 'POINT (2 2.1)', 'POINT (2 2.9)',
'POINT (3 0.8)', 'POINT (3 2)', 'POINT (3 3)'
]
values=[9,8,7,6,5,4,3,2,1]
df=pd.DataFrame({'points':points,'values':values})
gdf=gp.GeoDataFrame(df,geometry=[loads(x) for x in df.points], crs={'init': 'epsg:' + str(25832)})
for index,row in gdf.iterrows(): # Looping over all points
gdf['dist'] …Run Code Online (Sandbox Code Playgroud) 我有一大堆多边形(> 10 ^ 6),其中大部分是不相交的,但其中一些多边形是另一个多边形的洞(~10 ^ 3个案例)。这里有一张图片来解释这个问题,较小的多边形是较大多边形中的一个洞,但两者都是多边形列表中的独立多边形。
现在我想有效地确定哪些多边形是孔并减去孔,即减去完全位于另一个多边形内部的较小多边形并返回“清理”多边形列表。一对孔和父多边形应该像这样转换(所以基本上是从父多边形中减去孔):

Stackoverflow 和 gis.stackexchange.com 上有很多类似的问题,但我还没有找到真正解决这个问题的问题。以下是一些相关问题: 1. https://gis.stackexchange.com/questions/5405/using-shapely-translating-between-polygons-and-multipolygons 2. https://gis.stackexchange.com/questions/319546 /converting-list-of-polygons-to-multipolygon-using-shapely
这是一个示例代码。
from shapely.geometry import Point
from shapely.geometry import MultiPolygon
from shapely.ops import unary_union
import numpy as np
#Generate a list of polygons, where some are holes in others;
def generateRandomPolygons(polygonCount = 100, areaDimension = 1000, holeProbability = 0.5):
pl = []
radiusLarge = 2 #In the real dataset the size of polygons can vary
radiusSmall = 1 #Size of holes can also vary
for …Run Code Online (Sandbox Code Playgroud) 我不太确定如何解释这一点,但我有 2 个多边形,Polygon1 和 Polygon2。这些多边形相互重叠。如何在没有来自 Polygon1的P 的情况下使用 Shapely 获取Polygon2。

我试图(大致)将一条线的点等距到一个预定义的距离。
距离之间有一些容差是可以的,但尽可能接近是可取的。
我知道我可以手动遍历行中的每个点并检查 p1 距离与 p2 并在需要时添加更多点。
但我想知道是否有人知道是否有一种方法可以通过匀称的方式实现这一点,因为我已经在 LineString 中拥有了坐标。
我有一个点列表(经度和纬度),以及它们在地理数据框中的关联点几何。所有的点都应该能够细分为单独的多边形,因为这些点通常聚集在几个区域中。我想做的是有某种算法来遍历点并检查前一个点和当前点之间的距离。如果距离足够小,它会将这些点组合在一起。这个过程会一直持续到当前点离得太远。它会从这些接近的点中创建一个多边形,然后用下一组点继续这个过程。
gdf
longitude latitude geometry
0 -76.575249 21.157229 POINT (-76.57525 21.15723)
1 -76.575035 21.157453 POINT (-76.57503 21.15745)
2 -76.575255 21.157678 POINT (-76.57526 21.15768)
3 -76.575470 21.157454 POINT (-76.57547 21.15745)
5 -112.973177 31.317333 POINT (-112.97318 31.31733)
... ... ... ...
2222 -113.492501 47.645914 POINT (-113.49250 47.64591)
2223 -113.492996 47.643609 POINT (-113.49300 47.64361)
2225 -113.492379 47.643557 POINT (-113.49238 47.64356)
2227 -113.487443 47.643142 POINT (-113.48744 47.64314)
2230 -105.022627 48.585669 POINT (-105.02263 48.58567)
Run Code Online (Sandbox Code Playgroud)
所以在上面的数据中,前 4 个点将组合在一起并变成一个多边形。然后,它将移动到下一组,依此类推。每组点不是均匀分布的,即下一组可能是 7 对点,接下来可能是 3 对。理想情况下,最终输出将是另一个地理数据框,它只是一堆多边形。