use cbor for maps

This commit is contained in:
Dustin Carlino 2018-06-26 09:30:38 -07:00
parent 43536743a8
commit 82bac55275
6 changed files with 18 additions and 6 deletions

View File

@ -5,4 +5,5 @@ authors = ["Dustin Carlino <dabreegster@gmail.com>"]
[dependencies]
serde = "1.0"
serde_cbor = "0.8.2"
serde_json = "1.0"

View File

@ -1,10 +1,11 @@
extern crate serde;
extern crate serde_cbor;
extern crate serde_json;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::fs::File;
use std::io::{Error, Read, Write};
use std::io::{Error, ErrorKind, Read, Write};
pub fn write_json<T: Serialize>(path: &str, obj: &T) -> Result<(), Error> {
let mut file = File::create(path)?;
@ -19,3 +20,14 @@ pub fn read_json<T: DeserializeOwned>(path: &str) -> Result<T, Error> {
let obj: T = serde_json::from_str(&contents).unwrap();
Ok(obj)
}
pub fn write_binary<T: Serialize>(path: &str, obj: &T) -> Result<(), Error> {
let mut file = File::create(path)?;
serde_cbor::to_writer(&mut file, obj).map_err(|err| Error::new(ErrorKind::Other, err))
}
pub fn read_binary<T: DeserializeOwned>(path: &str) -> Result<T, Error> {
let file = File::open(path)?;
let obj: T = serde_cbor::from_reader(file).map_err(|err| Error::new(ErrorKind::Other, err))?;
Ok(obj)
}

View File

@ -50,7 +50,7 @@ fn main() {
let (map, bounds) = osm::osm_to_raw_roads(&flags.osm);
let mut map = osm::split_up_roads(&map, &elevation);
let parcels_map: raw_data::Map =
abstutil::read_json(&flags.parcels).expect("loading parcels failed");
abstutil::read_binary(&flags.parcels).expect("loading parcels failed");
println!(
"Finding matching parcels from {} candidates",
parcels_map.parcels.len()
@ -94,5 +94,5 @@ fn main() {
}
println!("writing to {}", flags.output);
abstutil::write_json(&flags.output, &map).expect("serializing map failed");
abstutil::write_binary(&flags.output, &map).expect("serializing map failed");
}

View File

@ -107,7 +107,6 @@ wait slow down even more -- before any of this change, lanes on adjacent roads s
- debug impossibly long derived roads, like the horizontal E McGraw. try to detect by comparing length of original points with length of derived center pts.
- big maps start centered over emptiness
- some bldg paths are quite long.
- more compact output
- draw sidewalk and parking markings
- make final Map serializable too
- useful to precompute sidewalk paths

View File

@ -31,5 +31,5 @@ fn main() {
let out_path = &args[2];
println!("writing to {}", out_path);
abstutil::write_json(out_path, &map).expect("serializing map failed");
abstutil::write_binary(out_path, &map).expect("serializing map failed");
}

View File

@ -29,7 +29,7 @@ pub struct Map {
impl Map {
pub fn new(path: &str) -> Result<Map, Error> {
let data: raw_data::Map = abstutil::read_json(path)?;
let data: raw_data::Map = abstutil::read_binary(path)?;
let bounds = data.get_gps_bounds();
let mut m = Map {