4

In QGIS, I have made a polygon, and from that polygon I have made a vector grid via research tools-> vector grid. The grid covers the entire polygon.

Now I want to extract these grid points in the python console so I can work with them. But I can not figure out how to extract these coordinates.

I have done the following:

Layer = qgis.utils.iface.activeLayer()
provider = Layer.dataProvider()
feat = QgsFeature()
allAttrs = provider.attributeIndexes()
provider.select(allAttrs)
geom = feat.geometry()

but from here I do not know what to do. Any help?

5

The PyQGIS Cookbook is a great resource for such questions, especially this section: http://www.qgis.org/pyqgis-cookbook/vector.html#iterating-over-vector-layer

# retreive every feature with its geometry and attributes
while provider.nextFeature(feat):

  # fetch geometry
  geom = feat.geometry()
  print "Feature ID %d: " % feat.id() ,

  # show some information about the feature
  if geom.vectorType() == QGis.Point:
    x = geom.asPoint()
    print "Point: " + str(x)
  elif geom.vectorType() == QGis.Line:
    x = geom.asPolyline()
    print "Line: " + str(x)
  elif geom.vectorType() == QGis.Polygon:
    x = geom.asPolygon()
    print "Polygon: " + str(x)
  else:
    print "Unknown"
  • I have tried this, but the vectorType does not exist for some reason. But I found a better way to do it. Instead of making the vectorgrid, I can just take the 'x = geom.asPolygon()' from my polygon, which gives me the nodes at each intersection. This is all I need. Thanks for the help. – Toke Mar 6 '13 at 7:42
  • 1
    @Toke Great! Please upvote helpful answers. – underdark Mar 6 '13 at 9:11
1

Everything can be simplified in a more Pythonic way (see in How to add Direction and Distance to attribute table? ):

1) function to select all the elements (geometry and attributes) of a layer:

def select_all(layer):
   layer.select([])
   layer.setSelectedFeatures([obj.id() for obj in layer])

2) selection of the layer

mylayer = qgis.utils.iface.activeLayer()
select_all(mylayer)

3) processing the layer

for elem in mylayer.selectedFeatures():
     geom= elem.geometry()
     attrs = elem.attributeMap()

     # example with geometry
     wkt = geom.exportToWkt()
     print wkt

     # example with attributes
     for (k,atr) in attrs.iteritems():
            print "%d: %s" % (k, atr.toString())

example of results:

for geometry:

LINESTRING(110923.171250 113663.674220, 117364.375933 120736.374336, 117364.375933 120736.374336)

LINESTRING(112501.896619 119157.645479, 118248.464701 116189.640235)...

for attributes:

0, -100, test

1, -200, test2

...

If you want an iterator:

for i, elem in enumerate(mylayer.selectedFeatures()):
     geom= elem.geometry()
     wkt = geom.exportToWkt()
     print "element: ", i, "wkt: ", wkt

element: 0 wkt: LINESTRING(110923.171250 113663.674220, 117364.375933 120736.374336, 117364.375933 120736.374336)

element: 1 wkt: LINESTRING(112501.896619 119157.645479, 118248.464701 116189.640235)

and for the extraction of the xy coordinates, see How to add Direction and Distance to attribute table?

1

it can be this way for example:

def getCoordinatesLayer(layer):
    for item in layer.getFeatures():
    geometry = item.geometry()
    if geometry.type() == QgsWkbTypes.Point:
        coordinate = geometry.asPoint()
        string = "Id:{},x:{},y:{}".format(item.id(),coordinate[0],coordinate[1])
        print(string)
    else:
        print("other ...")
return True

layer = iface.activeLayer()
getCoordinatesLayer(layer)

to print the values or do what you want ...

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.