Using QGIS 2.18.3, I need to divide a trapezium (Math is Fun says that this is called a trapezoid in the UK - in any event, a 4-sided polygon with no sides parallel) into 4 polygons. Please refer to my crude MS Paint approximation to visualize what I need. I suspect that this would be accomplished by finding the midpoint along each input side and connecting it with the midpoint of the opposite side, thereby creating quarters.
Purpose: I have polygonal vector data of surveyed USA land at the section (square mile) level where each polygon is usually square, but often containing trapezium-shaped sections, that I need to divide into approximate quarter-sections. Since square polygons would exist in the data, the ability to subdivide a square would thus also be a requirement.
The ability to accomplish this subdivision with multiple sections at one time would be a bonus.
A search through the plugins didn't turn up anything, nor did a Google search. Sadly, I have no programming skills to accomplish this. Suggestions?
Answer
I will rely my answer on gene's creative answer, and I will proceed from there.
You need to do the following steps:
- Convert the polygon shapefile into polyline shapefile using
Vector -> Geometry tool -> polygon to polyline
- Make the polyline layer in the table of content the active layer
- Select the polylines (4 boundary lines)
Copy and paste the following code into python editor and run it. It will creates a mid point at each line:
def mid(pt1, pt2):
x = (pt1.x() + pt2.x())/2
y = (pt1.y() + pt2.y())/2
return QgsPoint(x,y)
def pair(list):
#Iterate over pairs in a list
for i in range(1, len(list)):
yield list[i-1], list[i]
def create_geometry(point,pr):
# create geometry record
seg = QgsFeature()
seg.setGeometry(QgsGeometry.fromPoint(point))
pr.addFeatures( [seg] )
# memory layer
pt_layer = QgsVectorLayer("Point", "midpoint", "memory")
pr = pt_layer.dataProvider()
mylayer = qgis.utils.iface.activeLayer() # This line was missing in the original code
for elem in mylayer.selectedFeatures():
line = elem.geometry()
for seg_start, seg_end in pair(line.asPolyline()):
line_start = QgsPoint(seg_start)
line_end = QgsPoint(seg_end)
# midpoint
midpt= mid(line_start, line_end)
# add midpoint point to layer
create_geometry(midpt,pr)
pt_layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayers([pt_layer])
- Go to
settings -> Snapping Options -> Snapping Option: select Advanced
Select the polygon layer in the table of content
Start editing the polygon shapefile
Cut the polygon using the mid point as snapping feature, and save the edits.
No comments:
Post a Comment