Skip to content
Snippets Groups Projects

Revised ResourcePrice class and methods. Added tests for problem...

Files

@@ -518,25 +518,19 @@ list_labels_bbr = [
# *****************************************************************************
# *****************************************************************************
def get_bbr_building_data_geodataframe(
building_entrance_ids: list,
selected_bbr_bdg_entrance_labels: list = SELECT_BBR_BDG_ENTR_LABELS,
selected_bbr_building_labels: list = SELECT_BBR_BDG_LABELS,
selected_bbr_building_point_labels: list = SELECT_BBR_BDG_POINT_LABELS,
) -> Tuple[GeoDataFrame, list]:
# *************************************************************************
# *************************************************************************
# get data about building entrances
dict_building_entrances, list_failures = fetch_building_entrance_data(
building_entrance_ids
)
if selected_bbr_bdg_entrance_labels == None:
# includes all labels
selected_bbr_bdg_entrance_labels = BBR_BDG_ENTR_LABELS
list_entries = [
@@ -547,7 +541,6 @@ def get_bbr_building_data_geodataframe(
]
for key, value in dict_building_entrances.items()
]
df_building_entrances = DataFrame(
data=list_entries,
columns=selected_bbr_bdg_entrance_labels,
@@ -560,19 +553,15 @@ def get_bbr_building_data_geodataframe(
# get data about buildings
dict_buildings = fetch_building_data(df_building_entrances.index)
if selected_bbr_building_labels == None:
# includes all labels
selected_bbr_building_labels = BBR_BDG_LABELS
# create dataframe with building data
list_entries = [
[value[bbr_key] for bbr_key in value if bbr_key in selected_bbr_building_labels]
for key, value in dict_buildings.items()
]
df_buildings = DataFrame(
data=list_entries,
columns=selected_bbr_building_labels,
@@ -583,12 +572,10 @@ def get_bbr_building_data_geodataframe(
# *************************************************************************
# get building point data
if selected_bbr_building_point_labels == None:
# includes all labels
selected_bbr_building_point_labels = BBR_BDG_POINT_LABELS
dict_buildings_points = {
building_entrance_id: dict_buildings[building_entrance_id][ # (
label_bbr_bygningpunkt
@@ -599,7 +586,6 @@ def get_bbr_building_data_geodataframe(
}
# create dataframe with building point data
list_entries = [
[
value[bbr_key]
@@ -608,7 +594,6 @@ def get_bbr_building_data_geodataframe(
]
for key, value in dict_buildings_points.items()
]
df_building_points = DataFrame(
data=list_entries,
columns=selected_bbr_building_point_labels,
@@ -637,15 +622,11 @@ def get_bbr_building_data_geodataframe(
# *************************************************************************
# create a geodataframe whose geometry is that of building points
# specify the coordinate system
coordinate_system = "EPSG:4326" # latitude, longitude
key_bbr_coordinate_system = 5 # WGS 84 = EPSG:4326
# raise an error if different coordinates systems are being used
for building_entrance_id in dict_building_entrances:
if (
dict_buildings[building_entrance_id][label_bbr_bygningpunkt][
@@ -656,7 +637,6 @@ def get_bbr_building_data_geodataframe(
raise NotImplementedError("Only WGS 84 coordinates can be used.")
# create a dictionary with the building point geometries (i.e. points)
dict_building_point_geometry = {
building_entrance_id: Point(
dict_buildings[building_entrance_id][label_bbr_bygningpunkt][
@@ -671,121 +651,70 @@ def get_bbr_building_data_geodataframe(
}
# create geodataframe
gdf_buildings = GeoDataFrame(
data=df_buildings,
geometry=GeoSeries(data=dict_building_point_geometry),
crs=coordinate_system,
)
return gdf_buildings, list_failures
# *************************************************************************
# *************************************************************************
# *****************************************************************************
# *****************************************************************************
def fetch_building_entrance_data(building_entrance_ids: list) -> Tuple[dict, list]:
# *************************************************************************
# *************************************************************************
# retrieve data about each node identified through OSM
dict_building_entrances = {}
list_failures = []
# # determine the number of osm entries
# number_building_entrance_ids = len(building_entrance_ids)
# for each building entrance id
for building_entrance_id in building_entrance_ids:
# compose the url from which to get bbr data associated with the id
_url = url_prefix_entrance + building_entrance_id
try:
# retrieve the building entrance data
with urllib.request.urlopen(_url) as response:
# parse the data
bbr_entrance_data_json = json.loads(response.read().decode("utf-8"))
# store the data
if len(bbr_entrance_data_json) != 0:
for bbr_entry in bbr_entrance_data_json:
dict_building_entrances[
bbr_entry[label_bbr_building_id]
] = bbr_entry
else:
list_failures.append(building_entrance_id)
response.close()
except Exception:
response.close()
# *************************************************************************
# *************************************************************************
return dict_building_entrances, list_failures
# *************************************************************************
# *************************************************************************
# *****************************************************************************
# *****************************************************************************
def fetch_building_data(building_codes: list):
# *************************************************************************
# *************************************************************************
# get data about each specific building
dict_buildings = {}
# for each building id
for building_id in building_codes:
# compose a url with it
_url = url_prefix_buildings + building_id
# try statement
try:
# retrieve that data
with urllib.request.urlopen(_url) as response:
# parse the data
bbr_data = json.loads(response.read().decode("utf-8"))
dict_buildings[building_id] = bbr_data
response.close()
except Exception:
response.close()
# *************************************************************************
# *************************************************************************
return dict_buildings
# *************************************************************************
# *************************************************************************
# *****************************************************************************
# *****************************************************************************
Loading