Skip to content

Instantly share code, notes, and snippets.

@sk-zk
Created April 5, 2023 00:01
Show Gist options
  • Save sk-zk/18cfb3762b1ca463f02cb840afe80bab to your computer and use it in GitHub Desktop.
Save sk-zk/18cfb3762b1ca463f02cb840afe80bab to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b492b051",
"metadata": {},
"outputs": [],
"source": [
"from OSMPythonTools.overpass import Overpass\n",
"from OSMPythonTools.api import ApiResult\n",
"from dataclasses import dataclass"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ab988935",
"metadata": {},
"outputs": [],
"source": [
"@dataclass\n",
"class Node:\n",
" node_id: int\n",
" coordinates: [float]\n",
" \n",
" def __repr__(self):\n",
" return f\"{self.node_id} {self.coordinates}]\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fb9e1ebb",
"metadata": {},
"outputs": [],
"source": [
"overpass = Overpass()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ce0accaf",
"metadata": {},
"outputs": [],
"source": [
"# Get all highways in bbox along with coordinates of their nodes\n",
"result = overpass.query(\"\"\"\n",
"way[\"highway\"~\"^(motorway|trunk|primary|secondary|tertiary|(motorway|trunk|primary|secondary)_link|unclassified|residential|living_street)$\"]\n",
" (54.45978962933613, 9.015727231296681, 54.50358681266358, 9.090656157324931); \n",
"out geom;\n",
"\"\"\")\n",
"ways = result.elements()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fec8d2fe",
"metadata": {},
"outputs": [],
"source": [
"# Count how many edges each node has\n",
"edge_counts = {}\n",
"for way in ways:\n",
" nodes = way.nodes()\n",
" coords = way.geometry().coordinates\n",
" \n",
" # flatten polygons, which OSMPythonTools will sometimes create for some reason\n",
" if way.geometry().type == \"Polygon\":\n",
" coords = [c for sublist in coords for c in sublist]\n",
" \n",
" for i in range(0, len(nodes) - 1):\n",
" node0 = nodes[i].id()\n",
" node1 = nodes[i+1].id()\n",
" if node0 in edge_counts:\n",
" edge_counts[node0][0] += 1\n",
" else:\n",
" edge_counts[node0] = [1, Node(nodes[i].id(), coords[i])]\n",
" if node1 in edge_counts:\n",
" edge_counts[node1][0] += 1\n",
" else:\n",
" edge_counts[node1] = [1, Node(nodes[i+1].id(), coords[i+1])]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "4f27f324",
"metadata": {},
"outputs": [],
"source": [
"# Get all nodes with more than two edges; these are our intersections\n",
"intersections = [node_data[1] for (node_id, node_data) in edge_counts.items() if node_data[0] > 2]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "491dc38b",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4df6703a2f4f4b5c9766e802141bb125",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(center=[54.48255936866238, 9.05765993376471], controls=(ZoomControl(options=['position', 'zoom_in_text', '…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Display locaitons with ipyleaflet\n",
"from ipyleaflet import Map, Marker\n",
"\n",
"center = (54.48255936866238, 9.05765993376471)\n",
"\n",
"m = Map(center=center, zoom=13)\n",
"\n",
"for intersection in intersections:\n",
" marker = Marker(location=(intersection.coordinates[1], intersection.coordinates[0]), draggable=False)\n",
" m.add_layer(marker);\n",
"\n",
"display(m)"
]
},
{
"cell_type": "code",
"execution_count": 127,
"id": "9bae7806",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa28237d",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2c31f49",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "c187e653",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9bb238c9",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment