5 releases (3 breaking)

new 0.4.0 Nov 18, 2024
0.3.1 Sep 21, 2024
0.3.0 Sep 7, 2024
0.2.0 Aug 24, 2024
0.1.1 Aug 18, 2024

#1186 in Data structures

Download history 121/week @ 2024-08-12 147/week @ 2024-08-19 19/week @ 2024-08-26 182/week @ 2024-09-02 18/week @ 2024-09-09 168/week @ 2024-09-16 35/week @ 2024-09-23 16/week @ 2024-09-30 1/week @ 2024-10-07

530 downloads per month

MIT license

37KB
520 lines

OSMGraph

⚠️ This is experimental software and pre-release (below v1.0). The API is subject to change. ⚠️

Example of A* path on Manhattan

Tests

osmgraph is a crate for quickly fetching data from OpenStreetMap (OSM) and formatting this data into a graph data structure that can be used for various computer science tasks and problems.

OpenStreetMap is a free wiki world map. All of the data is created and shared by users in a similar way to Wikipedia. The data is free to use under an open license which you can find here.

This crate queries the OverpassAPI which is a free to use API for getting OSM data. I caution users of this crate to not accidentally spam their server with very large requests. In general, try to query once and then save the result locally while you are developing. The query language that Overpass uses is known as the Overpass Query Language and you can learn more about how to use it here.

A graph in the context of computer science and mathematics is a structure consisting of a set of nodes and edges. Nodes can represent anything. Edges represent a connection between two nodes. In the context of maps, nodes represent some point on the globe, and an edge represents a way that you can move between these nodes (a highway, a road, a sidewalk). Some edges can be directed and the notion of a directed graph is useful for representing one-way streets; however, in this iteration of the crate, we do not support one-way roads. Coming soon!

The graph part of this library heavily relies on the petgraph crate. This gives us an efficient interface for dealing with graphs and some algorithms (such as A*) for free.

Example

use osmgraph::graph::{OSMGraph, create_graph};
use osmgraph::overpass_api::{OverpassResponse, QueryEngine};

use serde_json::Value;

let engine = QueryEngine::new();

// Query to fetch all data related to "Selinsgrove" town in Pennsylvania
let query = String::from(r#"
    [out:json];
    area[name="Selinsgrove"]->.searchArea;
    (
      way(area.searchArea);
      node(area.searchArea);
    );
    out body;
    >;
    out skel qt;
"#);

// Make the request and get the resulting data in a raw string
let response: String = engine.query_blocking(query)
    .expect("Was not able to make request!");

// Parse json
let json: OverpassResponse = serde_json::from_str(&response)
    .expect("Was not able to parse data from json!");
let elements: &Vec<Value> = json.elements().as_array()
    .expect("Was not able to retrieve elements from json!");

//Create graph
let g: OSMGraph = create_graph(elements)
    .expect("Was not able to create graph from json!");

Dependencies

~8–19MB
~252K SLoC