Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. from qgis.utils import iface
  2. from PyQt4.QtCore import QVariant
  3. _ID_FIELD = 'RS'
  4. _NAME_FIELD = 'GEN'
  5. _PLZ_FIELD = 'plz'
  6. _NEW_ID_FIELD = 'PARENT_ID'
  7. _NEW_NAME_FIELD = 'PARENT_NAME'
  8. Land_layer = iface.mapCanvas().layer(1)
  9. PLZ_layer = iface.mapCanvas().layer(0)
  10. # Create 2 new fields in the layer that will hold the list of neighbors and sum
  11. # of the chosen field.
  12.  
  13. #PLZ_layer.startEditing()
  14. #PLZ_layer.dataProvider().addAttributes(
  15. # [QgsField(_NEW_ID_FIELD, QVariant.String),
  16. # QgsField(_NEW_NAME_FIELD, QVariant.Int)])
  17. #PLZ_layer.updateFields()
  18.  
  19. ## Create a dictionary of all features
  20. PLZ_feature_dict = {f.id(): f for f in PLZ_layer.getFeatures()}
  21. Land_feature_dict = {f.id(): f for f in Land_layer.getFeatures()}
  22. # Build a spatial index
  23. index = QgsSpatialIndex()
  24. print 'PLZ Layer: %s' % PLZ_layer.name()
  25. print 'Landkreis Layer: %s' % Land_layer.name()
  26.  
  27. for f in Land_feature_dict.values():
  28. index.insertFeature(f)
  29. print 'Inserting %s in Landkreis index' % f[_NAME_FIELD]
  30.  
  31. for f in PLZ_feature_dict.values():
  32. print 'Working on %s' % f[_PLZ_FIELD]
  33. geom = f.geometry()
  34. intersect_ids = index.intersects(geom.boundingBox())
  35. for id in intersect_ids:
  36. tempf = Land_feature_dict[id]
  37. print 'got parent Landkreis %s' % tempf[_NAME_FIELD]
  38.  
  39. intersect_ids = index.intersects(geom.boundingBox())
  40.  
  41.  
  42. Traceback (most recent call last):
  43. File "<input>", line 1, in <module>
  44. File "//VAIMUCO9/G114305$/Anwendungsdaten/Desktop/PLZ/ContainerFinder.py", line 34, in <module>
  45. intersect_ids = index.intersects(geom.boundingBox())
  46. AttributeError: 'NoneType' object has no attribute 'boundingBox'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement