Selection

Attribute selection using copy

Field values can be also selected using a filter. A filter is always associated to filter fields, which are the fields where the selection takes place. A feature (record) will be copied only if the filter function returns True. The parameter filter is followed by the layer number; the correspondly field parameter is ffields (filter fields) followed by the layer number. It contais a field name or a list of field names.

def filter1(value):
    return value == 'Bayern'

lrs1 = lrs0.copy('', ofields0=['NAME_4', 'TYPE_4', 'NAME_1'], filter0=filter1, ffields0='NAME_1')

Note also the order of the output fields:

    Bayern
         geom                    NAME_4                 TYPE_4  NAME_1
0     Polygon                    Affing               Gemeinde  Bayern
1     Polygon                   Aichach                  Stadt  Bayern
2     Polygon                  Aindling               Gemeinde  Bayern
...       ...                       ...                    ...     ...
2235  Polygon  Weißenstadter Forst-Nord  Gemeindefreies Gebiet  Bayern
2236  Polygon   Weißenstadter Forst-Süd  Gemeindefreies Gebiet  Bayern
2237  Polygon                 Wunsiedel                  Stadt  Bayern

Using lambda and two filter fields (‘NAME_1’, ‘NAME_4’):

lrs1 = lrs0.copy('', ofields0=['NAME_4', 'TYPE_4'], ffields0=['NAME_1', 'NAME_4'],
                 filter=lambda s, t: s == 'Nordrhein-Westfalen' and t.startswith('A'))
lrs1.show(max_rows=6)
           geom      NAME_4    TYPE_4
0   Polygon       Ahaus     Stadt
1   Polygon   Ascheberg  Gemeinde
2   Polygon  Aldenhoven  Gemeinde
..      ...         ...       ...
12  Polygon  Altenberge  Gemeinde
13  Polygon       Ahlen     Stadt
14  Polygon       Alpen  Gemeinde

Attribute selection using filters

Attribute analysis refeers to the selection of layer field values, excluding geometry fields.

print lrs.get_feature_count()  # returns 11302
lrs.set_attribute_filter("NAME_1 = 'Nordrhein-Westfalen'")
print lrs.get_feature_count()  # returns 396
lrs.set_attribute_filter("NAME_1 = 'Bayern'")
print lrs.get_feature_count()  # returns 2238
lrs.set_attribute_filter("NAME_1 = 'Nordrhein-Westfalen' OR NAME_1 = 'Bayern'")
print lrs.get_feature_count()  # returns 2634