Remove the dependency on abstutil from geom, so other repos can pull in less dependencies

This commit is contained in:
Dustin Carlino 2022-11-16 21:54:33 +00:00
parent c997e28d51
commit 1cc491e4ca
14 changed files with 53 additions and 30 deletions

2
Cargo.lock generated
View File

@ -1577,8 +1577,8 @@ name = "geom"
version = "0.1.0"
dependencies = [
"aabb-quadtree",
"abstutil",
"anyhow",
"bincode",
"earcutr",
"fs-err",
"geo",

View File

@ -37,6 +37,7 @@ opt-level = 3
# repeating in a bunch of crates
[workspace.dependencies]
anyhow = "1.0.38"
bincode = "1.3.1"
contour = "0.7.0"
fs-err = "2.6.0"
geo = "0.23.0"

View File

@ -8,7 +8,7 @@ edition = "2021"
abstutil = { path = "../abstutil" }
anyhow = { workspace = true }
base64 = "0.13.0"
bincode = "1.3.1"
bincode = { workspace = true }
fs-err = { workspace = true }
instant = { workspace = true }
lazy_static = "1.4.0"

View File

@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
anyhow = { workspace = true }
bincode = "1.3.1"
bincode = { workspace = true }
env_logger = { version = "0.8.2" }
fs-err = { workspace = true }
instant = { workspace = true }

View File

@ -6,7 +6,7 @@ use maplit::btreemap;
use structopt::StructOpt;
use abstio::MapName;
use abstutil::Timer;
use abstutil::{Tags, Timer};
use geom::{Bounds, Circle, Distance, Duration, FindClosest, Polygon, Pt2D, Time};
use map_gui::colors::ColorScheme;
use map_gui::options::Options;
@ -890,7 +890,7 @@ fn add_study_area(map: &mut Map, name: &str) -> Result<()> {
map.get_gps_bounds(),
require_in_bounds,
)? {
map.hack_add_area(AreaType::StudyArea, polygon, tags);
map.hack_add_area(AreaType::StudyArea, polygon, Tags::new(tags));
}
Ok(())
}

View File

@ -6,7 +6,6 @@ edition = "2021"
[dependencies]
aabb-quadtree = "0.1.0"
abstutil = { path = "../abstutil" }
anyhow = { workspace = true }
earcutr = "0.1.1"
fs-err = { workspace = true }
@ -20,5 +19,6 @@ serde = { workspace = true }
serde_json = { workspace = true }
[dev-dependencies]
bincode = { workspace = true }
rand = { workspace = true }
rand_xorshift = { workspace = true }

View File

@ -4,8 +4,6 @@ use anyhow::Result;
use instant::Instant;
use serde::{Deserialize, Serialize};
use abstutil::elapsed_seconds;
use crate::{deserialize_f64, serialize_f64, trim_f64, Distance, Speed, UnitFmt};
/// A duration, in seconds. Can be negative.
@ -147,7 +145,8 @@ impl Duration {
/// Returns the duration elapsed from this moment in real time.
pub fn realtime_elapsed(since: Instant) -> Duration {
Duration::seconds(elapsed_seconds(since))
let dt = since.elapsed();
Duration::seconds((dt.as_secs() as f64) + (f64::from(dt.subsec_nanos()) * 1e-9))
}
/// Rounds a duration up to the nearest whole number multiple.

View File

@ -41,6 +41,7 @@ mod speed;
mod stats;
mod tessellation;
mod time;
mod utils;
// About 0.4 inches... which is quite tiny on the scale of things. :)
pub const EPSILON_DIST: Distance = Distance::const_meters(0.01);
@ -177,9 +178,10 @@ mod tests {
let input = rng.gen_range(-214_000.00..214_000.0);
let trimmed = trim_f64(input);
let json_roundtrip: f64 =
abstutil::from_json(abstutil::to_json(&trimmed).as_bytes()).unwrap();
serde_json::from_slice(serde_json::to_string(&trimmed).unwrap().as_bytes())
.unwrap();
let bincode_roundtrip: f64 =
abstutil::from_binary(&abstutil::to_binary(&trimmed)).unwrap();
bincode::deserialize(&bincode::serialize(&trimmed).unwrap()).unwrap();
assert_eq!(json_roundtrip, trimmed);
assert_eq!(bincode_roundtrip, trimmed);
}
@ -188,8 +190,9 @@ mod tests {
let input = 1.2345678;
let trimmed = trim_f64(input);
let json_roundtrip: f64 =
abstutil::from_json(abstutil::to_json(&trimmed).as_bytes()).unwrap();
let bincode_roundtrip: f64 = abstutil::from_binary(&abstutil::to_binary(&trimmed)).unwrap();
serde_json::from_slice(serde_json::to_string(&trimmed).unwrap().as_bytes()).unwrap();
let bincode_roundtrip: f64 =
bincode::deserialize(&bincode::serialize(&trimmed).unwrap()).unwrap();
assert_eq!(json_roundtrip, 1.2346);
assert_eq!(bincode_roundtrip, 1.2346);
}

View File

@ -1,11 +1,10 @@
use std::collections::BTreeMap;
use std::fmt;
use anyhow::Result;
use geo::{Area, BooleanOps, Contains, ConvexHull, Intersects, SimplifyVWPreserve};
use serde::{Deserialize, Serialize};
use abstutil::Tags;
use crate::{
Angle, Bounds, CornerRadii, Distance, GPSBounds, LonLat, PolyLine, Pt2D, Ring, Tessellation,
Triangle,
@ -435,7 +434,7 @@ impl Polygon {
raw_bytes: &[u8],
gps_bounds: &GPSBounds,
require_in_bounds: bool,
) -> Result<Vec<(Self, Tags)>> {
) -> Result<Vec<(Self, BTreeMap<String, String>)>> {
let raw_string = std::str::from_utf8(raw_bytes)?;
let geojson = raw_string.parse::<geojson::GeoJson>()?;
let features = match geojson {
@ -468,10 +467,10 @@ impl Polygon {
continue;
};
if let Ok(ring) = Ring::new(pts) {
let mut tags = Tags::empty();
let mut tags = BTreeMap::new();
for (key, value) in feature.properties_iter() {
if let Some(value) = value.as_str() {
tags.insert(key, value);
tags.insert(key.to_string(), value.to_string());
}
}
results.push((ring.into_polygon(), tags));

View File

@ -1,12 +1,10 @@
use std::collections::HashSet;
use std::collections::{BTreeMap, HashSet};
use std::fmt;
use anyhow::{Context, Result};
use geo::prelude::ClosestPoint;
use serde::{Deserialize, Serialize};
use abstutil::Tags;
use crate::{
Angle, Bounds, Circle, Distance, GPSBounds, HashablePt2D, InfiniteLine, Line, LonLat, Polygon,
Pt2D, Ring, Tessellation, EPSILON_DIST,
@ -1093,7 +1091,7 @@ impl PolyLine {
raw_bytes: &[u8],
gps_bounds: &GPSBounds,
require_in_bounds: bool,
) -> Result<Vec<(Self, Tags)>> {
) -> Result<Vec<(Self, BTreeMap<String, String>)>> {
let raw_string = std::str::from_utf8(raw_bytes)?;
let geojson = raw_string.parse::<geojson::GeoJson>()?;
let features = match geojson {
@ -1122,10 +1120,10 @@ impl PolyLine {
} else {
continue;
};
let mut tags = Tags::empty();
let mut tags = BTreeMap::new();
for (key, value) in feature.properties_iter() {
if let Some(value) = value.as_str() {
tags.insert(key, value);
tags.insert(key.to_string(), value.to_string());
}
}
results.push((Self::unchecked_new(pts), tags));

View File

@ -150,7 +150,7 @@ impl<T: HgramValue<T>> Histogram<T> {
format!(
"{} count, 50%ile {}, 90%ile {}, 99%ile {}, min {}, mean {}, max {}",
abstutil::prettyprint_usize(self.count),
crate::utils::prettyprint_usize(self.count),
self.select(Statistic::P50).unwrap(),
self.select(Statistic::P90).unwrap(),
self.select(Statistic::P99).unwrap(),

16
geom/src/utils.rs Normal file
View File

@ -0,0 +1,16 @@
// These're copied from abstutil! geom should be split into its own repository at some point, and
// abstutil brings in too many dependencies.
pub fn prettyprint_usize(x: usize) -> String {
let num = format!("{}", x);
let mut result = String::new();
let mut i = num.len();
for c in num.chars() {
result.push(c);
i -= 1;
if i > 0 && i % 3 == 0 {
result.push(',');
}
}
result
}

View File

@ -195,7 +195,11 @@ fn parse_zones(gps_bounds: &GPSBounds, path: String) -> Result<HashMap<String, P
for (polygon, tags) in
Polygon::from_geojson_bytes(&abstio::slurp_file(path)?, gps_bounds, require_in_bounds)?
{
zones.insert(tags.get_result("geo_code")?.to_string(), polygon);
if let Some(code) = tags.get("geo_code") {
zones.insert(code.to_string(), polygon);
} else {
bail!("Input is missing geo_code: {:?}", tags);
}
}
Ok(zones)
}

View File

@ -204,18 +204,21 @@ impl ExtraShapes {
let bytes = abstio::slurp_file(path)?;
let mut shapes = Vec::new();
for (polygon, tags) in Polygon::from_geojson_bytes(&bytes, gps_bounds, require_in_bounds)? {
for (polygon, attributes) in
Polygon::from_geojson_bytes(&bytes, gps_bounds, require_in_bounds)?
{
shapes.push(ExtraShape {
// Awkward, but we have to convert back
points: gps_bounds.convert_back(polygon.get_outer_ring().points()),
attributes: tags.into_inner(),
attributes,
});
}
for (pl, tags) in PolyLine::from_geojson_bytes(&bytes, gps_bounds, require_in_bounds)? {
for (pl, attributes) in PolyLine::from_geojson_bytes(&bytes, gps_bounds, require_in_bounds)?
{
shapes.push(ExtraShape {
// Awkward, but we have to convert back
points: gps_bounds.convert_back(pl.points()),
attributes: tags.into_inner(),
attributes,
});
}