如何使用字符串的内容创建类似文件的对象(与文件类似的鸭子类型)?
我已经看到这个问题被问到,但还没有真正找到完整的答复。我有一个简单的形状多边形,称为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) 我有以下格式输入x,y坐标值:
[[1,1], [2,1], [2,2], [1,2], [0.5,1.5]]
Run Code Online (Sandbox Code Playgroud)
我想绘制多边形,但我不知道如何绘制它们!
谢谢
我正在尝试了解 Shapely 的工作原理。
我可以用下面的代码画一条简单的线:
import matplotlib.pyplot as plt
A = Point(0,0)
B = Point(1,1)
AB = LineString([A,B])
plt.plot(AB)
Run Code Online (Sandbox Code Playgroud)
但是当我改变坐标时:
A = Point(1,0)
B = Point(3,4)
AB = LineString([A,B])
plt.plot(AB)
Run Code Online (Sandbox Code Playgroud)
Shapely 决定绘制两条线,这是我不理解的行为。
使用Shapely 1.7.0
我使用 geopandas 创建了一张地图,但我无法在地图上添加“指北针”。
创建地图后,我尝试使用 matplotlib.image 模块添加“北箭头”并尝试了不同的方法(参见下面的示例),但没有一个提供好的结果。我正在寻找更好的代码,可以在地图上添加一个好的“指北针”
import matplotlib.image as img
from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage,
AnnotationBbox
im=img.imread(r'C:\Users\jnisengw\Dropbox\2019\Data
Science\QGIS\north_arrow1.png')
imagebox = OffsetImage(im,zoom=0.27)
ab = AnnotationBbox(imagebox, (598500,4699000))
ax.add_artist(ab)
Run Code Online (Sandbox Code Playgroud)