shady_amsterdam

Content

1. Shade Calculation Functions

2. Cool Spaces Functions

3. Network Functions


Shade Calculation Functions

General Functions (functions.py)

raster_center_coords(min_x, max_x, min_y, max_y, resolution)

Compute the center xy coordinates of all cells in a grid.

PARAMETERS

RETURNS

create_affine_transform(top_left_x, top_left_y, res)

Create Affine transform for the write_output function.

PARAMETERS

RETURNS

write_output(dataset, output, transform, name, change_nodata=False)

Write a grid (numpy array) to the given path (name, string) as a .TIF file.

PARAMETERS

RETURNS


Data Acquisition (collect_data.py)

download_las_tiles(tile_list_file, output_folder)

Download LAZ files for each GeoTiles subtile specified in a text file.
Note: the base link for downloading is hard coded as “https://geotiles.citg.tudelft.nl/AHN4_T”, this function will have to be updated if GeoTiles changes

PARAMETERS

RETURNS

download_and_extract(url, file_path, output_folder)

Download a ZIP file from a URL, extract its contents, and delete the ZIP file.

PARAMETERS

RETURNS

merge_tif_files(input_folder, output_file, file_prefix, nodata_value=-9999)

Merge TIF files in a folder with a specific prefix into a single raster file.

PARAMETERS

RETURNS

download_raster_tiles(tile_list_file, output_folder, name)

Download DSM and DTM files for each GeoTiles tile specified in a text file.
Note: the base links for downloading are hard coded as “https://ns_hwh.fundaments.nl/hwh-ahn/ahn4/02a_DTM_0.5m” and “https://ns_hwh.fundaments.nl/hwh-ahn/ahn4/03a_DSM_0.5m”, this function will have to be updated if GeoTiles changes

PARAMETERS

RETURNS

download_wfs_data(wfs_url, layer_name, bbox, gpkg_name, output_folder, tile_name)

Download data from a WFS server in batches and save it to a GeoPackage.

PARAMETERS

RETURNS

setup_WFS_download(gpkg_name, tile_bounds, output_folder)

Collecting the needed information for downloading the WFS data.

PARAMETERS

RETURNS


CHM Creation (create_first_chm_parallel.py)

median_filter_chm(chm_array, nodata_value=-9999, size=3)

Apply a median filter to a numpy array, ignoring the NoData values.

PARAMETERS

RETURNS

extract_vegetation_points(LasData, ndvi_threshold=0.1, pre_filter=False)

Extract vegetation points based on AHN classification and NDVI threshold.

PARAMETERS

RETURNS

chm_creation(LasData, vegetation_data, output_filename, resolution=0.5, smooth=False, nodata_value=-9999, filter_size=3)

Create a CHM from LiDAR vegetation data and save it as a raster.

PARAMETERS

RETURNS

interpolation_vegetation(LasData, veg_points, resolution, no_data_value=-9999)

Create a vegetation raster from LAS vegetation points using Laplace interpolation.

PARAMETERS

RETURNS

process_single_laz_file(file_path, output_folder, ndvi_threshold=0.0, resolution=0.5, remove=False, smooth_chm=False, filter_size=3, pre_filter=False)

Process a LAZ file to extract vegetation points and generate a CHM.

PARAMETERS

RETURNS

process_laz_files(input_folder, output_folder, ndvi_threshold=0.0, resolution=0.5, remove=False, smooth_chm=False, filter_size=3, pre_filter=False, max_workers=4)

Process a folder of LAZ files in parallel to extract vegetation points and generate CHMs.

PARAMETERS

RETURNS


DSM and Final CHM Creation (create_dsm_and_chm_parallel.py)

get_bbox(raster_paths)

Compute the overlapping bounding box of the CHM, DSM, and DTM file.

Input:

Output:

crop_raster(raster_path, bbox, no_data=-9999)

Crop the input rasters to the size of the overlapping bounding box.

Input:

Output:

extract_center_cells(cropped_data, no_data=-9999)

Extract the values of each cell in the input data and save these with the x and y (row and col) indices.

Input:

Output:

fill_raster(cropped_data, nodata_value, transform, speed_up=False)

Fill the NoData values of a given raster using Laplace interpolation.

Input:

Output:

chm_finish(chm_array, dtm_array, transform, min_height=2, max_height=40)

Finish the CHM file by first removing the ground height, then removing vegetation height below and above a certain range.

Input:

Output:

replace_buildings(filled_dtm, dsm_buildings, buildings_geometries, transform)

Replace the values of the filled DTM with the values of the filled DSM, if there is a building at the value location.

Input:

Output:

load_buildings(buildings_path, layer)

Load the building shapes from a geopackage file.

Input:

Output:

extract_tilename(filename)

Extract the name of the AHN tile from the file name.

Input:

Output:

process_single_file(chm_path, dtm_path, dsm_path, building_geometries, output_base_folder, nodata_value=-9999, speed_up=False, min_height=2, max_height=40)

Process from one CHM file to a final DSM and CHM.

Input:

Output:

process_files(chm_folder, dtm_path, dsm_path, buildings_path, output_base_folder, nodata_value=-9999, max_workers=4, speed_up=False, min_height=2, max_height=40)

Run the whole process of creating the final DSM and CHM.

Input:

Output:

process_folders(base_chm_folder, dtm_path, dsm_path, buildings_path, output_base_folder, max_workers=4, speed_up=False, min_height=2, max_height=40)

Process each folder containing CHM files concurrently.

Input:

Output:


Creating Shade Maps (shade_parallel.py)

process_chm_dsm(chm_filename, dsm_filename, folder_path, output_dir, date, start_time=9, end_time=20, interval=30, use_chm=True, trans=10, trunkheight=25)

Function to process a single DSM and CHM file pair and create shade maps from them for the given time frame and interval.

Input:

Output:

run_shade_calculation(file_pairs, output_base_folder, date, start_time, end_time, interval, use_chm=True, trans=10, trunkheight=25, max_workers=4)

Process CHM and DSM file pairs in parallel to create shade maps.

Input:

Output:

process_folders(base_folder, output_base_folder, date, start_time=9, end_time=20, interval=30, use_chm=True, trans=10, trunkheight=25, max_workers=4)

Function to process all subfolders in a base folder for shade calculations.

Input:

Output:


Merging Shade Maps (merge_shademaps.py)

merge_tif_files_by_time(main_folder, output_folder, merged_name, start_time=900, end_time=2000, delete_input_files=False, nodata_value=-9999)

Merge multiple TIF files from specified subfolders into a single file for each specified time step.

Input:

Output:


Running Code (main_shade.py)

flatten_dict(nested_dict, parent_key=’’, sep=’_’)

Flatten a nested dictionary, ignoring the first layer.

Input:

Output:

read_config(file_path)

Read the contents of a JSON configuration file and save them to a dictionary.

Input:

Output:


Cool Spaces Functions

Process Functions (coolspace_process.py)

read_config(filename)

Load configuration settings from a JSON file.

PARAMETERS

RETURNS

convert_to_datetime(time)

Convert an integer in HHMM format to a datetime object.

PARAMETERS

RETURNS

compute_search_range(search_start_time, search_end_time, start_time, end_time, time_interval)

Calculate the index range for shade maps based on a specified time interval within a given range.

PARAMETERS

RETURNS

identification(coolspace_file, road_file, building_file, shademaps_path, road_buffer_attri, building_buffer_num=4, mode=’single-day’, num_days=None, single_day_time_range=None, time_interval=None, search_range=None, morning_range=None, afternoon_range=None, late_afternoon_range=None, output_coolspace_type=’land-use’, useMultiProcessing=False)

Identify and evaluate cool spaces based on shade coverage across different time ranges, area ratios and output configurations.

PARAMETERS

RETURNS

evaluation(coolspace, building_population_file, bench_file, heatrisk_file, pet_file, gpkg_file, output_layer, search_buffer=700, single_day_time_range=None, time_interval=None, search_range=None, morning_range=None, afternoon_range=None, late_afternoon_range=None, useMultiProcessing=False)

Evaluate cool spaces by analyzing their accessibility, capacity, street furniture, heat risk, and PET scores based on spatial and temporal parameters.

PARAMETERS

RETURNS

list_to_string(gdf)

Convert list values in columns of a GeoDataFrame to strings.

PARAMETERS

RETURNS

drop_or_wkt(gdf, mode=’to_wkt’)

Convert or drop geometry columns in a GeoDataFrame.

PARAMETERS

RETURNS

output_all_shade_geoms(gdf, folder_path, output_gpkg)

Export shade geometries from a GeoDataFrame to a GeoPackage, filtering out non-public areas.

PARAMETERS

RETURNS


Cool Space Class for Identification (identification.py)

Initialization and Geometry Functions

__init__

Initializes a CoolSpace object.

clip

Clips the geometries in data using a specified clipping method.

Process Summary:

Shade Calculation Functions

calculate_shade

Calculates shade metrics (average shade, shade area, and geometry) for each raster.

Process Summary:

calculate_shade_for_raster

Processes a single raster and calculates shade metrics.

Process Summary:

calculate_shade_multi

Multi-processing version of calculate_shade.

Process Summary:

Geometry Retrieval Functions

get_shade_geometries

Retrieves specific shade geometries for a given raster.

Process Summary:

get_cool_spaces

Retrieves cool spaces that contain shade geometries within a specified range.

Process Summary:

Evaluation Functions

evaluate_shade_coverage

Evaluates shade coverage over a specific time range and the ratio of shaded area to total cool space area.

Process Summary:


Cool Space Class for Evaluation (evaluation.py)

Initialization Functions

__init__

Initializes a CoolEval object with data about cool places, nearby buildings, benches, heat risk, and PET (Physiological Equivalent Temperature) values.

Walking Shed Functions

calculate_walking_shed_origin

Calculates the walking shed by assigning each building to the nearest cool place within a specified buffer distance.

calculate_walking_shed

Similar to calculate_walking_shed_origin, but processes buildings and cool places in batches for efficiency.

calculate_walking_shed_multi

Multi-processing version of calculate_walking_shed, designed for parallel execution.

3. Resident and Capacity Evaluation Functions

evaluate_resident

Aggregates the number of residents, elderly residents, and children within a specified distance to each cool place.

evaluate_capacity

Evaluates the capacity of shaded areas based on area and nearby residents.

4. Shade Furniture and Heat Risk Evaluation Functions

evaluate_sfurniture

Checks for benches within each shaded area.

evaluate_heatrisk

Calculates and classifies the heat risk within shaded areas.

PET Evaluation Functions

eval_pet

Computes the average PET (Physiological Equivalent Temperature) values for shaded areas and assigns recommendations.

eval_pet_multi

Multi-processing version of eval_pet, leveraging parallel computation for large datasets.

Aggregation and Recommendation Functions

**aggregate_to_cool_places**

Aggregates evaluation results from shaded areas back to cool places.

final_recom

Calculates a recommendation score for each cool place based on capacity, benches, heat risk, PET, and shade metrics.

7. Export Functions

export_eval_gpkg

Exports the final evaluation of cool places to a GeoPackage.


Building Class (building.py)

__init__

Initializes a Building object with building geometry data.

create_buffer

Creates buffer geometries around each building.


Road Class (road_process.py)

Note: The assign_buffer_hardSurface, assign_buffer_roadtype, and create_attribute methods are designed for a specific dataset and are used primarily during development. In practical applications, the input road dataset must include an attribute specifying the buffer distance for different road types, allowing direct use of the create_buffer method.

__init__

Initializes a Road object with road geometry data.

assign_buffer_hardSurface

Assigns buffer size based on the width of hard surfaces.

assign_buffer_roadtype

Assigns buffer size based on road type.

create_attribute

Creates a new attribute for buffer size based on road surface type or road type.

create_buffer

Creates buffer geometries for roads based on a specified buffer attribute.

Network Functions

Shade Weight Calculation (shade_weight_calculation.py)

add_edge_identifiers_to_gdf(graph, edges_gdf)

Adds unique edge identifiers (u, v, key) to a GeoDataFrame representing edges in a graph.

PARAMETERS

RETURNS

compute_shade_weights(edges, raster, affine)

Computes shade weights for graph edges based on zonal statistics of a raster.

PARAMETERS

RETURNS

update_graph_with_shade_weight(graph, edges_gdf)

Updates a graph with pre-calculated shade weights for each edge.

PARAMETERS

RETURNS

store_graph_with_shade_weights(graph, edges_gdf, output_path)

Saves a graph with shade weights applied to each edge to a GraphML file.

PARAMETERS

RETURNS

precalculate_and_store_shade_weights(graph, raster_path, output_graph_path)

Pre-calculates and stores shade weights for a graph using a specified raster.

PARAMETERS

RETURNS

generate_graph_name_from_raster(raster_filename)

Generates a graph filename based on a raster file’s date and time embedded in its name.

PARAMETERS

RETURNS

process_multiple_shade_maps(graph_file, raster_dir, output_dir)

Processes multiple raster files, calculating and storing shade weights for each corresponding graph.

PARAMETERS

RETURNS


Cool Places Nodes Calculation (cool_places_nodes_calculation.py)

load_graph_from_file(graph_file_path)

Loads a graph from a GraphML file and ensures that shade weights are assigned and numeric for calculations.

PARAMETERS

RETURNS

load_cool_place_polygons(polygon_path)

Loads and validates polygons representing cool places from a shapefile or GeoJSON, fixing any invalid geometries.

PARAMETERS

RETURNS

calculate_and_save_cool_place_nodes(graph, cool_place_polygons, output_path, max_distance=50, known_crs=”EPSG:28992”)

Identifies nodes near cool place polygons within a specified maximum distance and saves them to a file.

PARAMETERS

RETURNS

export_layers_to_shapefiles(geopackage_path, output_directory, date_str)

Exports each layer from a GeoPackage as individual shapefiles in the specified directory, named by a date string.

PARAMETERS

RETURNS

process_all_shapefiles(polygon_directory, graph_directory, output_directory)

Processes all shapefiles in a directory, calculating and saving cool place nodes for each.

PARAMETERS

RETURNS


Routes Calculation (routes_calculation.py)

load_graph_from_file(graph_file_path)

Loads a graph from a GraphML file and ensures shade weights are set for shortest path calculations.

PARAMETERS

RETURNS

load_cool_place_polygons(polygon_path)

Loads polygons representing cool places from a shapefile or GeoJSON.

PARAMETERS

RETURNS

find_nearest_node(graph, lat, lon)

Finds the nearest graph node to a given latitude and longitude in EPSG:4326 format.

PARAMETERS

RETURNS

geocode_location(location_name)

Geocodes a location name to its latitude and longitude coordinates.

PARAMETERS

RETURNS

find_cool_place_nodes(graph, cool_place_polygons)

Identifies nodes in a graph that are within cool place polygons.

PARAMETERS

RETURNS

load_cool_place_nodes(pre_calculated_path)

Loads a pre-calculated list of cool place nodes from a file.

PARAMETERS

RETURNS

find_nearest_cool_place_dijkstra(graph, start_node, cool_place_nodes, max_distance)

Finds the nearest cool place node from a starting node using Dijkstra’s algorithm.

PARAMETERS

RETURNS

calculate_routes(graph, start_node, end_node=None, cool_place_nodes=None, max_distance=5000)

Calculates multiple routes (shortest, shadiest, and balanced) between two nodes.

PARAMETERS

RETURNS

calculate_balanced_route(graph, start_node, destination_node, shade_weight_ratio=70, length_weight_ratio=30)

Calculates a balanced route between two nodes based on a mix of shade weight and length.

PARAMETERS

RETURNS

plot_routes(graph, shortest_route, shadiest_route, balanced_route_1, balanced_route_2)

Plots the shortest, shadiest, and balanced routes on a map.

PARAMETERS

RETURNS

demo_shade_route_calculation(graph_file_path, pre_calculated_nodes_path, user_input, input_type, mode=”nearest_cool_place”)

Demonstrates route calculation based on user input type and routing mode.

PARAMETERS

RETURNS

find_nearest_timestamp_files(date_time, graph_dir, nodes_dir)

Finds the closest timestamped graph and nodes files based on a given date and time.

PARAMETERS

RETURNS

demo_shade_route_calculation_with_time(graph_dir, nodes_dir, user_input, input_type, mode=”nearest_cool_place”, date_time=None)

Demonstrates route calculation using the nearest timestamped files based on a given date and time.

PARAMETERS

RETURNS


Walking Shed Network (walking_shed_network.py)

load_building_polygons(building_shapefile_path)

Loads building polygons from a shapefile.

PARAMETERS

RETURNS

load_graph_from_file(graph_file_path)

Loads a graph from a GraphML file.

PARAMETERS

RETURNS

process_graph(graph)

Ensures the graph is projected to EPSG:28992.

PARAMETERS

RETURNS

load_cool_place_polygons(polygon_path)

Loads cool place polygons from a shapefile or GeoJSON.

PARAMETERS

RETURNS

find_cool_place_nodes(graph, cool_place_polygons, max_distance=50, known_crs=”EPSG:28992”)

Finds nodes within a specified distance of cool place polygons.

PARAMETERS

RETURNS

process_single_cool_place_node(graph, cool_place_node, nearby_nodes, distances, weight=”shade_weight”)

Processes distances for a single cool place node within nearby nodes.

PARAMETERS

RETURNS

find_nodes_within_distances(graph, cool_place_nodes, distances=[200, 300, 400, 500], weight=”shade_weight”)

Finds nodes within specified distances of each cool place node.

PARAMETERS

RETURNS

save_cool_place_nodes_shapefile(cool_place_nodes, nodes_gdf, output_shapefile_path)

Saves cool place nodes to a shapefile.

PARAMETERS

RETURNS

assign_buildings_to_distance_category_with_precomputed(buildings, graph, distance_nodes)

Assigns distance categories to buildings based on proximity to cool places.

PARAMETERS

RETURNS

calculate_walking_shed_with_distance_categories(buildings, cool_place_nodes, graph, distances=[200, 300, 400, 500], weight=”length”)

Calculates the walking shed for buildings, classifying them by distance categories.

PARAMETERS

RETURNS

assign_building_colors(buildings)

Assigns color codes to buildings based on their distance categories.

PARAMETERS

RETURNS

plot_colored_walking_shed(buildings)

Plots the walking shed, coloring buildings by their distance to cool places.

PARAMETERS

RETURNS

preprocess_graph_weights(graph, weight)

Preprocesses graph edge weights, ensuring they are numeric.

PARAMETERS

RETURNS

walking_shed_calculation(graph=None, polygon_path=None, building_shapefile_path=None, weight=”length”, output_building_shapefile=None, output_cool_place_shapefile=None)

Performs the full walking shed calculation, classifying buildings by distance to cool places and saving results.

PARAMETERS

RETURNS