多边形 shapefile 中的垂直线
我正在处理一个GIS(地理信息系统)的问题,使用的是一个多边形的形状文件作为输入。
想象一下一个不规则的多边形。我想在这个多边形的范围内,按照相等的间距画出垂直的线。
我打算这样进行:
首先,确定这个多边形的边界框 (已经用PyShp完成)
然后,在边界框的左边缘画出垂直的线,间距要相等 (怎么做呢?)
最后,把这些线裁剪到多边形的范围内 (怎么做,不用ArcPy?)
注意: 这些线必须是垂直的,而不是网格线。而且,我不打算使用ArcPy,想用Python(2.7)来完成这段代码,因为这段代码需要放到一个由PyQt生成的工具里。
2 个回答
0
首先,找到描述左边缘的点/顶点,也就是 (x1, y1) 和 (x2, y2) 这两个坐标。
接着,在这两个点的 x 值上加上一个常数 k,变成 (x1+k, y1) 和 (x2+k, y2)。
然后,在新的 x 值上找到多边形的 y 值,变成 (x1+k, y3) 和 (x2+k, y4)。
最后,连接这两个新点,画出一条线。
0
我终于找到了我问题的代码!所以我来回答这个问题……谢谢大家的帮助。
Ipath = raw_input("Enter the input file :- ")
Opath = raw_input("Enter the output directory :- ")
Ipath = Ipath.replace("\\", "/") # Python requirement for paths
Opath = Opath.replace("\\", "/")
copyfile(str(Ipath) + ".prj", str(Opath) + "/" + "Out_Lines" + ".prj") # Copying projection file
sf = shapefile.Reader(str('Input Path'))
shapes = sf.shapes()
Box = shapes[0].bbox
Spc = input("Enter the grid spacing :- ") # Grid Spacing read
x_min = Box[0] # Save the coordinates of the right-bottom, left-top bounding box
y_min = Box[1]
x_max = Box[2]
y_max = Box[3]
A_bbox = [x_min, y_min] # First assignment of coordinates
B_bbox = [x_max, y_max]
C_bbox = [x_min, y_max]
D_bbox = [x_max, y_min]
w = shapefile.Writer(shapefile.POLYLINE) # Shapefile writer
w.line(parts = [[A_bbox, C_bbox]])
w.field('Path number', 'C', '50')
w.record(str(1)) # Writes the first line, that is the left 'side' of the bounding box
# Increasing the X coordinate to generate a line at a specified spacing
i = 2
while (A_bbox[0] <= x_max):
A_bbox = [A_bbox[0] + Spc, A_bbox[1]]
C_bbox = [C_bbox[0] + Spc, C_bbox[1]]
w.line(parts = [[A_bbox, C_bbox]])
w.record(str(i))
i = i+1
w.save(str(Opath) + "/" + "Out_Lines")
这个代码会把结果保存到一个形状文件里。
接着上面提到的问题,解决方案可以在这个链接找到:在多边形范围内裁剪线形状文件。我觉得这个问题现在可以算是解决了,可以结束讨论了。
感谢大家的支持!