diff --git a/CHANGES.rst b/CHANGES.rst index ac49ef5..82254f3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,6 +10,7 @@ in progress - Fix published messages getting lost when not starting the MQTT main loop after connecting to MQTT broker - Refactor station list filter +- Filter stations by country code 2019-01-22 0.8.2 diff --git a/doc/running.rst b/doc/running.rst index 5520925..e354763 100644 --- a/doc/running.rst +++ b/doc/running.rst @@ -15,6 +15,7 @@ luftdatenpumpe --help Options: --source= Data source, either "api" or "file://" [default: api]. + --country= Filter data by given country codes, comma-separated. --station= Filter data by given location ids, comma-separated. --sensor= Filter data by given sensor ids, comma-separated. --sensor-type= Filter data by given sensor types, comma-separated. @@ -28,8 +29,12 @@ luftdatenpumpe --help --debug Enable debug messages -h --help Show this screen + Station list examples: + # Display metadata for given countries in JSON format + luftdatenpumpe stations --country=BE,NL,LU + # Display metadata for given stations in JSON format, with reverse geocoding luftdatenpumpe stations --station=28,1071 --reverse-geocode diff --git a/luftdatenpumpe/commands.py b/luftdatenpumpe/commands.py index 8af1a15..39ea3c6 100644 --- a/luftdatenpumpe/commands.py +++ b/luftdatenpumpe/commands.py @@ -28,6 +28,7 @@ def run(): Options: --source= Data source, either "api" or "file://" [default: api]. + --country= Filter data by given country codes, comma-separated. --station= Filter data by given location ids, comma-separated. --sensor= Filter data by given sensor ids, comma-separated. --sensor-type= Filter data by given sensor types, comma-separated. @@ -44,6 +45,9 @@ def run(): Station list examples: + # Display metadata for given countries in JSON format + luftdatenpumpe stations --country=BE,NL,LU + # Display metadata for given stations in JSON format, with reverse geocoding luftdatenpumpe stations --station=28,1071 --reverse-geocode @@ -151,7 +155,7 @@ def run(): # B. Data processing targets - # Optionally, decode filters by station id and/or sensor id + # Optionally apply filters by country code, station id and/or sensor id filter = {} # Lists of Integers. @@ -164,6 +168,11 @@ def run(): if options[filter_name]: filter[filter_name] = list(map(str.lower, read_list(options[filter_name]))) + # Lists of upper-case Strings. + for filter_name in ['country']: + if options[filter_name]: + filter[filter_name] = list(map(str.upper, read_list(options[filter_name]))) + log.info('Applying filter: {}'.format(filter)) # Fake data source. Currently always LDI. diff --git a/luftdatenpumpe/core.py b/luftdatenpumpe/core.py index a4d91c4..850ede6 100644 --- a/luftdatenpumpe/core.py +++ b/luftdatenpumpe/core.py @@ -242,6 +242,7 @@ def apply_filter(self, data): #log.info('item: %s', item) # Decode JSON item + country_code = item['location']['country'].upper() station_id = item['location']['id'] sensor_id = item['sensor']['id'] @@ -250,6 +251,9 @@ def apply_filter(self, data): # TODO: Improve evaluating conditions. if self.filter: skip = False + if 'country' in self.filter: + if country_code not in self.filter['country']: + skip = True if 'station' in self.filter: if station_id not in self.filter['station']: skip = True