Qgis里的查询过滤

查询过虑实现方式

通过给getFeatures()传递 QgsFeatureRequest对象,实现数据的过虑,下边是一个查询的例子:

request = QgsFeatureRequest()
request.setFilterRect(areaOfInterest)
for feature in layer.getFeatures(request):
    # do whatever you need with the feature

基于空间范围的查询

向QgsFeatureRequest的构造函数里传递一个  QgsRectangle 对象实现空间范围内的过虑查询:

rect=QgsRectangle(121.11,31.11,121.12,31.12) 

request = QgsFeatureRequest(
rect)

基于字段过虑的查询

向QgsFeatureRequest的构造函数里传递一个 QgsExpression 对象实现字段的过虑查询:

# The expression will filter the features where the field "location_name" contains
# the word "Lake" (case insensitive)
exp = QgsExpression('location_name ILIKE \'%Lake%\'')
request = QgsFeatureRequest(exp)

QgsExpression的详细语法参考: Expressions, Filtering and Calculating Values 。

QgsFeatureRequest还可以用来指定返回的字段,即只返回所有要素中需要的字段。

# Only return selected fields
request.setSubsetOfAttributes([0,2])
# More user friendly version
request.setSubsetOfAttributes(['name','id'],layer.pendingFields())
# Don't return geometry objects
request.setFlags(QgsFeatureRequest.NoGeometry)

提示

可以设置QgsFeatureRequest.NoGeometry不返回几何信息来提升查询的速度。

posted @ 2017-07-25 17:42  八爻老骥  阅读(4352)  评论(0编辑  收藏  举报