0

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.

code:

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.

  • What is col lines? In the console, you cannot use the name of the layer in this way – gene Sep 28 '14 at 17:58
3

before line 19, insert

  mylayer = qgis.utils.iface.activeLayer()

Then make sure that the layer on which you want to perform the query (col19_lines) is selected in the layer tree and that all its features are selected (if you want to run the script on all the features, that is).

What is does is that it points to your layer, so that the variable "mylayer" is now referencing your active layer (= the one that is grey in the layer tree).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.