QGIS Python Programming Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Filtering a layer by attributes

In addition to the spatial queries outlined in the previous recipe, we can also subset a layer by its attributes. This type of query resembles a more traditional relational database query and, in fact, uses SQL statements. In this recipe, we will filter a point shapefile-based layer by an attribute.

Getting ready

We'll use the same New York City Museums layer used in the previous recipes in this chapter. You can download the layer from https://github.com/GeospatialPython/Learn/raw/master/NYC_MUSEUMS_GEO.zip.

Unzip that file and place the shapefile's contents in a directory named nyc within your qgis_data directory, within your root or home directory.

How to do it...

In this recipe, we'll filter the layer by an attribute, select the filtered features, and zoom to them, as follows:

  1. First, we load the point layer:
            lyrPts = QgsVectorLayer("/qgis_data/nyc/NYC_MUSEUMS_GEO.shp",
                                    "Museums", "ogr") 
    
  2. Next, we add the layer to the map in order to visualize the points:
            QgsMapLayerRegistry.instance().addMapLayers([lyrPts]) 
    
  3. Now, we filter the point layer to points with attributes that match a specific zip code:
            selection = lyrPts.getFeatures(QgsFeatureRequest()
                                  .setFilterExpression(u'"ZIP" = 10002')) 
    
  4. Then, we use a list comprehension to create a list of feature IDs that are fed to the feature selection method:
            lyrPts.setSelectedFeatures([s.id() for s in selection]) 
    
  5. Finally, we zoom to the selection:
            iface.mapCanvas().zoomToSelected() 
    
  6. Verify that the points layer has three selected features, shown in yellow similar to the following screenshot:

    How to do it...

How it works...

This recipe takes advantage of the QGIS filter expressions, highlighted in Step 3. These filter expressions are a subset of SQL. The QgsFeatureRequest object handles the query expression as an optional argument to return an iterator with just the features you want. These queries also allow some basic geometrical manipulations. This recipe also introduces the mapCanvas().zoomToSelected() method, which is a convenient way to set the map's extent to the features of your interest.