Advertisement
3nids

circle select box

Jun 12th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. from math import cos, sin, pi
  2.  
  3. # params
  4. circle_x = 0
  5. circle_y = 0
  6. circle_r = 2
  7. tolerance = .1  # tolerance for Filter Rectangle around center of circle in the feature request
  8. arc_angle = 360  # 360=full circle, arc if lesser. Returns the feature if > 90
  9.  
  10. # create memory layer
  11. epsg = iface.mapCanvas().mapRenderer().destinationCrs().authid()
  12. layer = QgsVectorLayer("LineString?crs=%s&field=id:string&index=yes" % epsg, "Circle", "memory")
  13. QgsMapLayerRegistry.instance().addMapLayer(layer)
  14.  
  15. # uncomment to test on another layer
  16. #layer = QgsMapLayerRegistry.instance().mapLayer("pipe20130612142848065")
  17.  
  18. # add a circle as a line in the layer
  19. f = QgsFeature()
  20. fields = layer.dataProvider().fields()
  21. f.setFields(fields)
  22. f["id"] = "test"
  23.  
  24. f.setGeometry(QgsGeometry().fromPolyline([QgsPoint(circle_x + circle_r * cos(pi/180*a),
  25.                                                    circle_y + circle_r * sin(pi/180*a))
  26.               for a in range(0, arc_angle, 3)]))
  27. layer.dataProvider().addFeatures([f])
  28. layer.updateExtents()
  29. layer.setCacheImage(None)
  30. layer.triggerRepaint()
  31.  
  32. # select and print id of selected features
  33. features = []
  34. featReq = QgsFeatureRequest()
  35. box = QgsRectangle(circle_x-tolerance, circle_y-tolerance, circle_x+tolerance, circle_y+tolerance)
  36. featReq.setFilterRect(box)
  37. f = QgsFeature()
  38. vliter = layer.getFeatures(featReq)
  39. while vliter.nextFeature(f):
  40.     print f["id"]
  41.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement