2

I am working on Qgis python console. I want to draw a line in the center of two parallel lines..For this purpose firstly i have to store the coordinates of those two lines in a variable so i can apply distance formula to draw line in center of two lines..

Can anybody tell me how to get coordinates of a selected line through python console?

1 Answer 1

5

If you have only one line selected you can use this:

select = iface.activeLayer().selectedFeatures()
for feature in select:
    geom = feature.geometry()
    if geom.type() == QGis.Line:        
        feat_geom = geom.asPolyline()
        x=feat_geom[0][0]
        y=feat_geom[0][1]
        print x, y

        #you can also export it as wkt, if you need it for further calculations
        wkt = geom.exportToWkt()
        print wkt

if you have more selected features you can create a list and append the values there.

feat_geom = []
select = iface.activeLayer().selectedFeatures()
for feature in select:
    geom = feature.geometry()
    if geom.type() == QGis.Line:        
        f_geom = geom.asPolyline()

        #create a list of geometries
        feat_geom.append(f_geom)

print feat_geom 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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