6

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?

Jochen Schwarze
  • 14,605
  • 7
  • 49
  • 117
Toke
  • 379
  • 1
  • 5
  • 13

3 Answers3

6

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"
underdark
  • 84,148
  • 21
  • 231
  • 413
  • 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 06 '13 at 07:42
  • 1
    @Toke Great! Please upvote helpful answers. – underdark Mar 06 '13 at 09:11
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 ...

Arturo
  • 51
  • 3
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?

gene
  • 54,868
  • 3
  • 110
  • 187
  • You don't need to layer.setSelectedFeatures as it just adds overhead. You can just do this https://gist.github.com/NathanW2/5107896 – Nathan W Mar 07 '13 at 13:00