Commit be6bf750 authored by Felix Delattre's avatar Felix Delattre
Browse files

Refactored to asyncio code

parent a4eaff3d
Pipeline #28148 passed with stage
in 1 minute and 25 seconds
......@@ -15,3 +15,9 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
from .osm_analyzer import OSMAnalyzer
__all__ = ["OSMAnalyzer"]
#!/usr/bin/env python3
# Copyright (C) 2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
import logging
import osmdiff
logger = logging.getLogger(__name__)
class OSMAnalyzer:
"""OSM analyzer.
This class contains the logic for obtaining and interpreting the OpenStreetMap's
"augmented diffs".
"""
async def get_augmented_diff(self, provider: osmdiff.AugmentedDiff):
"""Obtain an augmented diff from Overpass API
Args:
provider: connection to database
Returns:
provider (AugmentedDiff):
An object containing all information of an augmented diff.
"""
provider.get_state()
provider.retrieve()
return provider
async def get_buildings(self, augmented_diff):
"""Collect the building IDs of building objects that have changed in a given
augmented diff.
"""
building_count = 0
# Check on newly created objects
for n in augmented_diff.create:
if n.tags.get("building"):
if self._is_way_or_relation(n):
building_count += 1
# Check on objects that have been modified
for n in augmented_diff.modify:
if n["new"].tags.get("building"):
if self._is_way_or_relation(n["new"]):
building_count += 1
elif n["old"].tags.get("building"):
if self._is_way_or_relation(n["old"]):
building_count += 1
# Check on deleted objects
for n in augmented_diff.delete:
if self._is_way_or_relation(n):
building_count += 1
print("building_count: " + str(building_count))
def _is_way_or_relation(self, element):
"""Check if element is of type `osm.Way` or `osm.Relation`"""
return isinstance(element, osmdiff.osm.osm.Way) or isinstance(
element, osmdiff.osm.osm.Relation
)
......@@ -16,61 +16,40 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
import asyncio
import logging
import sys
import osmdiff
from spearhead import OSMAnalyzer
logger = logging.getLogger()
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
def main():
asyncio.run(async_main())
logger.info("spearhead started")
a = osmdiff.AugmentedDiff()
a.base_url = "https://overpass.openbuildingmap.org/api"
a.get_state()
logger.info(a.sequence_number)
a.retrieve()
building_count = 0
# Check on newly created objects
for n in a.create:
if n.tags.get("building"):
if _is_way_or_relation(n):
building_count += 1
async def async_main():
# Check on objects that have been modified
for n in a.modify:
if n["new"].tags.get("building"):
if _is_way_or_relation(n["new"]):
building_count += 1
elif n["old"].tags.get("building"):
if _is_way_or_relation(n["old"]):
building_count += 1
# Check on deleted objects
for n in a.delete:
if _is_way_or_relation(n):
building_count += 1
logger.info("spearhead started")
logger.info("building_count: " + str(building_count))
await get_changed_buildings()
# Leave the program
sys.exit()
def _is_way_or_relation(element):
"""Check if element is of type `osm.Way` or `osm.Relation`"""
return isinstance(element, osmdiff.osm.osm.Way) or isinstance(
element, osmdiff.osm.osm.Relation
)
async def get_changed_buildings():
analyzer = OSMAnalyzer()
diff_provider = osmdiff.AugmentedDiff()
diff_provider.base_url = "https://overpass.openbuildingmap.org/api"
if __name__ == "__main__":
main()
while True:
diff = await analyzer.get_augmented_diff(diff_provider)
await analyzer.get_buildings(diff)
await asyncio.sleep(60)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment