Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Having loaded the .gpx file into a new layer from python console, I'm unable to access the gps points in the attributes table. Essentially, i'm unable to join these points to form a polygon (buildings) or a line segement (roads) depending on the number of gps points in a gpx file. It's possible to achieve the same in using the QGIS GUI, but I need to perform the same from the python console (to be implemented in a plugin).

Any help or suggestions would be much welcomed.

Thanks

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You will need to find some way to determine how to group and order the points. Depending on the organisation of your source data this may limit the amount of automation possible.

Once that issue is resolved the following two samples may help you get started.

This one will create a new layer with a single polygon connecting all points found in the active layer:

#derive points list from active layer
pointslayer = qgis.utils.iface.activeLayer()
pointsprovider = pointslayer.dataProvider()
pointsprovider.select([]) #no attributes
points=[]
pointsfeature = QgsFeature()
while pointsprovider.nextFeature(pointsfeature):
    coord = pointsfeature.geometry().asPoint()
    points.append(QgsPoint(coord[0],coord[1]))

points.append(points[0]) # repeat the first point to close the polygon

#create polygon from points list
polylayer = QgsVectorLayer("Polygon?crs=" + pointslayer.crs().authid(),\
                           pointslayer.name()+" as poly", "memory")
polyfeature = QgsFeature()
polyfeature.setGeometry(QgsGeometry.fromPolygon([points]))
polylayer.dataProvider().addFeatures([polyfeature])
QgsMapLayerRegistry.instance().addMapLayer(polylayer)

While this one creates a line in a similar manner:

#derive points list from active layer
pointslayer = qgis.utils.iface.activeLayer()
pointsprovider = pointslayer.dataProvider()
pointsprovider.select([]) #no attributes
points=[]
pointsfeature = QgsFeature()
while pointsprovider.nextFeature(pointsfeature):
    coord = pointsfeature.geometry().asPoint()
    points.append(QgsPoint(coord[0],coord[1]))

#create line from points list
linelayer = QgsVectorLayer("LineString?crs=" + pointslayer.crs().authid(),\
                           pointslayer.name()+" as line", "memory")
linefeature = QgsFeature()
linefeature.setGeometry(QgsGeometry.fromPolyline(points))
linelayer.dataProvider().addFeatures([linefeature])
QgsMapLayerRegistry.instance().addMapLayer(linelayer)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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