|
| 1 | +use geo::{BoundingRect, Contains, MultiPolygon, Point}; |
| 2 | +use geojson::GeoJson; |
| 3 | +use nanorand::{Rng, WyRand}; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | +use utils::Mercator; |
| 6 | + |
| 7 | +use crate::{IntersectionID, MapModel}; |
| 8 | + |
| 9 | +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] |
| 10 | +pub struct ZoneID(pub usize); |
| 11 | + |
| 12 | +/// Origin/destination demand data, representing driving trips between two zones. |
| 13 | +#[derive(Serialize, Deserialize)] |
| 14 | +pub struct DemandModel { |
| 15 | + pub zones: Vec<Zone>, |
| 16 | + // (zone1, zone2, count), with count being the number of trips from zone1 to zone2 |
| 17 | + pub desire_lines: Vec<(ZoneID, ZoneID, usize)>, |
| 18 | +} |
| 19 | + |
| 20 | +impl DemandModel { |
| 21 | + /// Turn all of the zones into Mercator. Don't do this when originally building and serializing |
| 22 | + /// them, because that process might not use exactly the same Mercator object. |
| 23 | + pub fn finish_loading(&mut self, mercator: &Mercator) { |
| 24 | + for zone in &mut self.zones { |
| 25 | + mercator.to_mercator_in_place(&mut zone.geometry); |
| 26 | + let bbox = zone.geometry.bounding_rect().unwrap(); |
| 27 | + zone.x1 = (bbox.min().x * 100.0) as i64; |
| 28 | + zone.y1 = (bbox.min().y * 100.0) as i64; |
| 29 | + zone.x2 = (bbox.max().x * 100.0) as i64; |
| 30 | + zone.y2 = (bbox.max().y * 100.0) as i64; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + pub fn make_requests(&self, map: &MapModel) -> Vec<(IntersectionID, IntersectionID, usize)> { |
| 35 | + info!( |
| 36 | + "Making requests from {} zones and {} desire lines", |
| 37 | + self.zones.len(), |
| 38 | + self.desire_lines.len() |
| 39 | + ); |
| 40 | + |
| 41 | + // TODO Plumb through UI |
| 42 | + // To speed up the impact calculation, how many specific requests per (zone1, zone2)? If |
| 43 | + // true, just do one, but weight it by count. |
| 44 | + let fast_sample = true; |
| 45 | + |
| 46 | + let mut rng = WyRand::new_seed(42); |
| 47 | + let mut requests = Vec::new(); |
| 48 | + |
| 49 | + for (zone1, zone2, raw_count) in &self.desire_lines { |
| 50 | + let (iterations, trip_count) = if fast_sample { |
| 51 | + (1, *raw_count) |
| 52 | + } else { |
| 53 | + (*raw_count, 1) |
| 54 | + }; |
| 55 | + |
| 56 | + for _ in 0..iterations { |
| 57 | + let pt1 = self.zones[zone1.0].random_point(&mut rng); |
| 58 | + let pt2 = self.zones[zone2.0].random_point(&mut rng); |
| 59 | + if let (Some(i1), Some(i2)) = ( |
| 60 | + map.closest_intersection |
| 61 | + .nearest_neighbor(&pt1) |
| 62 | + .map(|obj| obj.data), |
| 63 | + map.closest_intersection |
| 64 | + .nearest_neighbor(&pt2) |
| 65 | + .map(|obj| obj.data), |
| 66 | + ) { |
| 67 | + if i1 != i2 { |
| 68 | + requests.push((i1, i2, trip_count)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + requests |
| 74 | + } |
| 75 | + |
| 76 | + pub fn to_gj(&self, map: &MapModel) -> GeoJson { |
| 77 | + // Per (from, to) pair, how many trips? |
| 78 | + let mut from: Vec<Vec<usize>> = |
| 79 | + std::iter::repeat_with(|| std::iter::repeat(0).take(self.zones.len()).collect()) |
| 80 | + .take(self.zones.len()) |
| 81 | + .collect(); |
| 82 | + // Per (to, from) pair, how many trips? |
| 83 | + let mut to: Vec<Vec<usize>> = |
| 84 | + std::iter::repeat_with(|| std::iter::repeat(0).take(self.zones.len()).collect()) |
| 85 | + .take(self.zones.len()) |
| 86 | + .collect(); |
| 87 | + |
| 88 | + for (zone1, zone2, count) in &self.desire_lines { |
| 89 | + from[zone1.0][zone2.0] += count; |
| 90 | + to[zone2.0][zone1.0] += count; |
| 91 | + } |
| 92 | + |
| 93 | + let mut features = Vec::new(); |
| 94 | + for (idx, zone) in self.zones.iter().enumerate() { |
| 95 | + let mut f = map.mercator.to_wgs84_gj(&zone.geometry); |
| 96 | + f.set_property("name", zone.name.clone()); |
| 97 | + f.set_property("counts_from", std::mem::take(&mut from[idx])); |
| 98 | + f.set_property("counts_to", std::mem::take(&mut to[idx])); |
| 99 | + features.push(f); |
| 100 | + } |
| 101 | + GeoJson::from(features) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[derive(Serialize, Deserialize)] |
| 106 | +pub struct Zone { |
| 107 | + /// An original opaque string ID, from different data sources. |
| 108 | + pub name: String, |
| 109 | + /// WGS84 when built originally and serialized, then Mercator right before being used |
| 110 | + pub geometry: MultiPolygon, |
| 111 | + /// The bbox, rounded to centimeters, for generate_range to work. Only calculated right before |
| 112 | + /// use. |
| 113 | + #[serde(skip_deserializing, skip_serializing)] |
| 114 | + pub x1: i64, |
| 115 | + #[serde(skip_deserializing, skip_serializing)] |
| 116 | + pub y1: i64, |
| 117 | + #[serde(skip_deserializing, skip_serializing)] |
| 118 | + pub x2: i64, |
| 119 | + #[serde(skip_deserializing, skip_serializing)] |
| 120 | + pub y2: i64, |
| 121 | +} |
| 122 | + |
| 123 | +impl Zone { |
| 124 | + fn random_point(&self, rng: &mut WyRand) -> Point { |
| 125 | + loop { |
| 126 | + let x = (rng.generate_range(self.x1..=self.x2) as f64) / 100.0; |
| 127 | + let y = (rng.generate_range(self.y1..=self.y2) as f64) / 100.0; |
| 128 | + let pt = Point::new(x, y); |
| 129 | + if self.geometry.contains(&pt) { |
| 130 | + return pt; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// Deterministically produce a bunch of OD pairs, just as a fallback when there's no real data |
| 137 | +pub fn synthetic_od_requests(map: &MapModel) -> Vec<(IntersectionID, IntersectionID, usize)> { |
| 138 | + let num_requests = 1_000; |
| 139 | + |
| 140 | + let mut rng = WyRand::new_seed(42); |
| 141 | + let mut requests = Vec::new(); |
| 142 | + for _ in 0..num_requests { |
| 143 | + let i1 = IntersectionID(rng.generate_range(0..map.intersections.len())); |
| 144 | + let i2 = IntersectionID(rng.generate_range(0..map.intersections.len())); |
| 145 | + requests.push((i1, i2, 1)); |
| 146 | + } |
| 147 | + requests |
| 148 | +} |
0 commit comments