Skip to content

Commit

Permalink
Merge pull request #66 from bacetiner/master
Browse files Browse the repository at this point in the history
Example notebook for OSM_PowerNetworkScraper
  • Loading branch information
bacetiner authored Dec 5, 2024
2 parents 98d9b14 + 6e1d5e9 commit 07ad1ba
Show file tree
Hide file tree
Showing 4 changed files with 251,999 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@
import requests
from shapely.geometry import Polygon

from brails.scrapers.footprint_scraper import FootprintScraper
from brails.types.region_boundary import RegionBoundary
from brails.types.asset_inventory import Asset, AssetInventory

OVERPASS_URL = "http://overpass-api.de/api/interpreter"


class OSM_PowerNetworkScraper(FootprintScraper):
class OSM_PowerNetworkScraper:
"""
A class for retrieving & processing power network data from OpenStreetMap.
Expand Down Expand Up @@ -137,8 +136,12 @@ def get_elements(self, region: RegionBoundary) -> AssetInventory:

datalist = self._fetch_data_from_api(query, queryarea_printname)
assets_data = self._process_power_data(datalist)
inventory = self._create_inventory(assets_data)

return self._create_inventory(assets_data)
print(f'\nFound a total of {len(inventory.inventory)} power network '
f'elements in {queryarea_printname}.')

return inventory

def _fetch_data_from_api(self,
query: str,
Expand Down Expand Up @@ -209,7 +212,7 @@ def _process_power_data(self,
# Assemble asset geometries and dictionary of attributes and save them
# in assets_data:
for data in datalist:
if data["type"] == "way":
if data["type"] == "way" and 'tags' in data:
nodes = data["nodes"]
geometry = []
for node in nodes:
Expand Down
18 changes: 12 additions & 6 deletions brails/types/asset_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# Frank McKenna
#
# Last updated:
# 11-06-2024
# 12-05-2024

"""
This module defines classes associated with asset inventories.
Expand Down Expand Up @@ -152,7 +152,8 @@ class AssetInventory:
get_random_sample(size, seed): Get subset of the inventory.
get_coordinates(): Return a list of footprints.
get_geojson(): Return inventory as a geojson dict.
write_to_geojson(): Wtite inventory to file in GeoJSON format. Also return inventory as a geojson dict!
write_to_geojson(): Write inventory to file in GeoJSON format. Also
return inventory as a geojson dict.
read_from_csv(file_path, keep_existing, str_type, id_column): Read
inventory dataset from a csv table
add_asset_features_from_csv(file_path, id_column): Add asset features
Expand Down Expand Up @@ -379,16 +380,21 @@ def get_geojson(self) -> dict:
for key, asset in self.inventory.items():
if len(asset.coordinates) == 1:
geometry = {"type": "Point",
"coordinates": [asset.coordinates[0][:]]
"coordinates": asset.coordinates[0]
}
elif len(asset.coordinates) == 2:
geometry = {"type": "Line",
"coordinates": asset.coordinates
}
else:
geometry = {'type': 'Polygon',
'coordinates': [asset.coordinates]
}
if asset.coordinates[0] == asset.coordinates[-1]:
geometry = {'type': 'Polygon',
'coordinates': [asset.coordinates]
}
else:
geometry = {'type': 'LineString',
'coordinates': asset.coordinates
}

feature = {'type': 'Feature',
'properties': asset.features,
Expand Down
Loading

0 comments on commit 07ad1ba

Please sign in to comment.