/// Loads a graph from a Postgres database.
Loads a graph from a Postgres database.
Loads a graph from a Postgres database .
pub fn load_from_db(uname: &String, pw: &String, db: &String,
                        ways_vert_table: &String, ways_table: &String,
                        weight: &String, weight_rev: &String) -> Graph {
        let conn_str = format!("postgres://{}:{}@localhost/{}", uname, pw, db);
        let conn = Connection::connect(conn_str, TlsMode::None).unwrap();
        let mut nodes = Vec::with_capacity(1000);
        let select_str_vert = format!("SELECT id, lon, lat FROM {} ORDER BY id", ways_vert_table);
        for row in &conn.query(&select_str_vert, &[]).unwrap() {
            let osm_id: i64 = row.get(0);
            let lon_raw: f64 = row.get(1);
            let lat_raw: f64 = row.get(2);
            let node = Node {
                id: osm_id,
                lon: lon_raw,
                lat: lat_raw,
            };
            nodes.push(node);
        }
        let mut edges = Vec::with_capacity(1000);
        let select_str = format!("SELECT source, target, {}, {} FROM {}", weight, weight_rev, ways_table);
        for row in &conn.query(&select_str, &[]).unwrap() {
            let source_id: i64 = row.get(0);
            let target_id: i64 = row.get(1);
            let weight_raw: f64 = row.get(2);
            let weight_raw_rev: f64 = row.get(3);
            // When inserting, we simply subtract 1, so that the IDs map to those of the nodes.
            // This comes from the fact that the Rust vector is 0-indexed, but in Postgres,
            // the IDs start with 1.
            let edge = Edge {
                source: source_id - 1,
                target: target_id - 1,
                weight: weight_raw as f32,
                highway_tag: "".to_string(),
            };
            edges.push(edge);
            // We also insert edges for every backward edge.
            let edge = Edge {
                source: target_id - 1,
                target: source_id - 1,
                weight: weight_raw_rev as f32,
                highway_tag: "".to_string(),
            };
            edges.push(edge);
        }
        // Finally, we build an R tree for quick access.
        let mut rtree = RTree::new();
        for n in nodes.iter() {
            let p = SpatialPoint::new(Point2::new(n.lon, n.lat), n.id);
            rtree.insert(p);
        }
        Graph { edges: edges, nodes: nodes, rtree: rtree }
    }
pub fn load_from_db ( uname : & String , pw : & String , db : & String , ways_vert_table : & String , ways_table : & String , weight : & String , weight_rev : & String ) -> Graph { let conn_str = format ! ( " " uname pw db ) ; let conn = Connection :: connect ( conn_str , TlsMode :: None ) . unwrap ( ) ; let mut nodes = Vec :: with_capacity ( 1000 ) ; let select_str_vert = format ! ( " " ways_vert_table ) ; for row in & conn . query ( & select_str_vert , & [ ] ) . unwrap ( ) { let osm_id : i64 = row . get ( 0 ) ; let lon_raw : f64 = row . get ( 1 ) ; let lat_raw : f64 = row . get ( 2 ) ; let node = Node { id : osm_id , lon : lon_raw , lat : lat_raw , } ; nodes . push ( node ) ; } let mut edges = Vec :: with_capacity ( 1000 ) ; let select_str = format ! ( " " weight weight_rev ways_table ) ; for row in & conn . query ( & select_str , & [ ] ) . unwrap ( ) { let source_id : i64 = row . get ( 0 ) ; let target_id : i64 = row . get ( 1 ) ; let weight_raw : f64 = row . get ( 2 ) ; let weight_raw_rev : f64 = row . get ( 3 ) ; // When inserting, we simply subtract 1, so that the IDs map to those of the nodes. // This comes from the fact that the Rust vector is 0-indexed, but in Postgres, // the IDs start with 1. let edge = Edge { source : source_id - 1 , target : target_id - 1 , weight : weight_raw as f32 , highway_tag : " " . to_string ( ) , } ; edges . push ( edge ) ; // We also insert edges for every backward edge. let edge = Edge { source : target_id - 1 , target : source_id - 1 , weight : weight_raw_rev as f32 , highway_tag : " " . to_string ( ) , } ; edges . push ( edge ) ; } // Finally, we build an R tree for quick access. let mut rtree = RTree :: new ( ) ; for n in nodes . iter ( ) { let p = SpatialPoint :: new ( Point2 :: new ( n . lon , n . lat ) , n . id ) ; rtree . insert ( p ) ; } Graph { edges : edges , nodes : nodes , rtree : rtree } }
Loads a graph from a Postgres database.
Loads a graph from a Postgres database .
uname
&String
pw
&String
db
&String
ways_vert_table
&String
ways_table
&String
weight
&String
weight_rev
&String