ethicnology / ophois

Creates street graph from OpenStreetMap
https://ethicnology.github.io/cartographe/
GNU General Public License v3.0
47 stars 2 forks source link
data graph network openstreetmap osm street

License: GPL v3codecovcoverage

Ophoïs, creates street graph from OpenStreetMap

installation

pre-built

Download the lastest linux release

If you trust/verified this code, make it executable and add it to your path

sudo chmod +x ophois
sudo mv ophois /usr/local/bin

or build from sources

cargo build --release # >= Rust 1.58
# output should be in /target/release/ophois

:one: download a map

CITY=Pantin # Save your city in an environment variable
ophois download --city $CITY

:two: extract

cat $CITY.osm | ophois format | ophois extract > $CITY-extracted.graph

same command with space separator

NOTE: Default separator is "" ASCII 31 (0x1F) Unit Separator but you can use any suitable separator as long you specify it with --separator

cat $CITY.osm | ophois format | ophois extract --separator ' ' > $CITY-extracted.graph

:three: simplify

The tool used to generate the following screenshots is cartographe
keep the largest component, remove degree two nodes, replace nodes with under delta links by links and replace links (and nodes) which distance is under delta by a midpoint node connected to neighbours

cat $CITY-extracted.graph | ophois simplify --delta 10.0 > $CITY-simplified.graph

extracted input

remove degree two nodes

replace nodes that only have distance links less than delta with links between their neighbours

NOTE: delta=6

replace links (including nodes) which are under delta distance by a midpoint node

NOTE: delta=6

:four: discretize

split links that have distance over 2*delta in equal parts

cat $CITY-simplified.graph | ophois discretize --delta 6.0 > $CITY-discretized.graph

NOTE: delta=6

one line simplify and discretize

ophois download --city $CITY; cat $CITY.osm | ophois format | ophois extract | ophois simplify --delta 10 | ophois discretize --delta 5 > $CITY.graph

same command with space separator

ophois download --city $CITY; cat $CITY.osm | ophois format | ophois extract -s ' ' | ophois simplify -s ' ' -d 10 | ophois discretize -s ' ' -d 5 > $CITY.graph

graph format

NOTE: Default separator is "" ASCII 31 (0x1F) Unit Separator

node_id␟latitude␟longitude #represents a node
node_id␟latitude␟longitude
node_id␟node_id #represents a link
node_id␟node_id

real life data

3758221295␟48.8275185␟2.3484976 #represents a node
3761637488␟48.8275416␟2.3486683
3761637488␟3758221295 #represents a link

Load ophois graph format into NetworkX

import networkx as nx

# Create an empty graph object
G = nx.Graph()

# Open the file for reading
with open("ophois-graph.txt", "r") as f:
    # Loop through each line in the file
    for line in f:
        # Remove any leading or trailing white space characters
        line = line.strip()
        # Split the line using the ␟ separator
        fields = line.split('␟')
        # Check the length of the line to determine whether it is a node or an edge
        if len(fields) == 3:
            # This is a node, add it to the graph
            node_id = fields[0]
            latitude = float(fields[1])
            longitude = float(fields[2])
            G.add_node(node_id, latitude=latitude, longitude=longitude)
        elif len(fields) == 2:
            # This is an edge, add it to the graph
            node1 = fields[0]
            node2 = fields[1]
            G.add_edge(node1, node2)

# Print the nodes and edges in the graph
print("Nodes:", G.nodes())
print("Edges:", G.edges())

# Write the graph to a GraphML file
nx.write_graphml(G, "output.graphml")

Authors