This is a follow up to How to find the middle point of a line in QGIS - Where to place input file name
Errors coming up when I executed the code below (taken from Finding middle point (midpoint) of line in QGIS?) included an "IndentationError: unexpected indent" which I resolved by deleting a leading space on the 8th line of the code right before
for i in range(1, len(list)):.
A second set of errors came up "Traceback (most recent call last): File "", line 1, in NameError: name 'mylayer' is not defined". I think I resolved the 'mylayer' error part by providing the name of the active layer ('col19_lines') on line 21 of the code
for elem in 'mylayer'.selectedFeatures(): I want midpoints computed from.
The correction I made still did not give me the midpoints as another error came up "Traceback (most recent call last): File "", line 1, in AttributeError: 'str' object has no attribute 'selectedFeatures'".
How can I make Gene's code run without error?
QGIS 1.8 is being used.
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() for elem in 'col19_lines'.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])
I need to properly process a layer named 'col19_lines' I do not have a background in python scripting. What do I need to do for the routine to process specifically a layer named 'col19_lines' so the midpoint of segments will be computed and no "Traceback (most recent call last): File ", line 1, in AttributeError: 'str' object has no attribute 'selectedFeatures'" error shows up.