Monday 1 May 2017

polygon - Dividing a trapezium into quarters with QGIS?


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?


enter image description here



Answer



I will rely my answer on gene's creative answer, and I will proceed from there.


You need to do the following steps:



  1. Convert the polygon shapefile into polyline shapefile using Vector -> Geometry tool -> polygon to polyline


enter image description here



enter image description here



  1. Make the polyline layer in the table of content the active layer


enter image description here



  1. Select the polylines (4 boundary lines)


enter image description here





  1. 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])


enter image description here



  1. Go to settings -> Snapping Options -> Snapping Option: select Advanced



enter image description here




  1. Select the polygon layer in the table of content




  2. Start editing the polygon shapefile





  3. Select Split feature enter image description here




  4. Cut the polygon using the mid point as snapping feature, and save the edits.




enter image description here


enter image description here


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...