Testing Alpha Shapes
The Alpha Shape endpoint calculates and returns the alpha shape polygons for a road network. An alpha shape is a generalization of the convex hull that provides a more detailed boundary representation by controlling the level of detail through an alpha parameter.
API Endpoint
Request
Send a POST
request to /api/v0/AlphaShape
with the following parameters:
-
Query Parameter:
roadNetId
- The road network identifier (e.g., 'DK' for Denmark) -
Request Body: JSON object containing the alpha value
-
Alpha Parameter:
- Controls the level of detail of the resulting shape
- A value of
0
uses the default alpha value defined in the DCC - Higher values produce simpler, more generalized boundaries
- Lower values typically result in more detailed shapes
Response
The endpoint returns an AlphaShapeReply
containing:
alphaShapePolygons
: Array of polygons, where each polygon is represented as an array of coordinatesbackendRoadNetVersion
: Version of the road network used by the backend serverapiRoadNetVersion
: Latest road network version known by the API server
Visualizing Alpha Shapes
For now, we are using an external tool (Python script) to visualize the alpha shape results. To test and visualize the alpha shapes:
-
Copy the result from the endpoint to a JSON file
Save the API response to a file, for example
alpha_shape_result.json
: -
Run the Python script to see the alpha shapes in the browser
import colorsys
import folium
import os, json
import webbrowser
def main():
file_name = "alpha_shape_result.json"
coordinates = load_coordinates(file_name)
# Create Map
map_obj = create_map(coordinates)
# Save the map
output_file = "interactive_map.html"
save_map(map_obj, output_file)
# Open the map in the default web browser
abs_path = os.path.abspath(output_file)
webbrowser.open(f"file://{abs_path}")
def load_coordinates(json_file_path):
with open(json_file_path, 'r') as json_file:
data = json.load(json_file)
return data['AlphaShapePolygons']
# Map Generation
def create_map(polygons):
"""Create an interactive map with multiple colored polygons"""
m = folium.Map()
# Generate distinct colors for all polygons
colors = generate_distinct_colors(len(polygons))
for i, polygon in enumerate(polygons):
# Convert coordinates to lat/lon pairs
lat_lon_pairs = [(point['latitude'], point['longitude']) for point in polygon]
# Create polygon with unique color and interactive features
folium.Polygon(
locations=lat_lon_pairs,
color=colors[i],
weight=2,
fill=True,
fillColor=colors[i],
fillOpacity=0.3,
popup=f'Alpha Shape {i+1}',
).add_to(m)
# Fit the map to show all polygons with some padding
bounds = calculate_bounds(polygons)
m.fit_bounds(bounds, padding=(20, 20))
return m
def generate_distinct_colors(num_colors):
"""Generate distinct colors"""
return [
"#{:02x}{:02x}{:02x}".format(
*[int(c * 255) for c in colorsys.hsv_to_rgb(
(i * 0.618033988749895) % 1.0,
0.7 + (i % 3) * 0.1,
0.8 + (i % 2) * 0.1
)]
) for i in range(num_colors)
]
def calculate_bounds(polygons):
"""Extract Bounding box of all points"""
all_lats = [point['latitude'] for polygon in polygons for point in polygon]
all_lons = [point['longitude'] for polygon in polygons for point in polygon]
return [[min(all_lats), min(all_lons)], [max(all_lats), max(all_lons)]]
def save_map(m, output_file):
m.save(output_file)
if __name__ == "__main__":
main()