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.

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?

share|improve this question

2 Answers 2

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"
share|improve this answer
    
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
    
@Toke Great! Please upvote helpful answers. –  underdark Mar 6 '13 at 9:11

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?

share|improve this answer
    
You don't need to layer.setSelectedFeatures as it just adds overhead. You can just do this gist.github.com/NathanW2/5107896 –  Nathan W Mar 7 '13 at 13:00

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.