Add an API for the Xi'an group to get agent positions

This commit is contained in:
Dustin Carlino 2020-08-26 09:22:33 -07:00
parent 75933f686d
commit 83a5df7886
3 changed files with 39 additions and 7 deletions

View File

@ -43,6 +43,8 @@ are missing, etc. A summary of the commands available so far:
mode, duration of trip in seconds). The mode is either a string like "Walk"
or "Drive", or null if the trip was aborted (due to a simulation bug or
disconnected map).
- **GET /data/get-agent-positions**: Returns a JSON list of all active agents.
Vehicle type (or pedestrian), person ID, and position is included.
## Related tools

View File

@ -25,6 +25,13 @@ def main():
print('Baseline: {} finished trips, total of {} seconds'.format(len(trips1), sum(trips1.values())))
print()
# Find the average position of all active pedestrians
agents = [x['pos'] for x in requests.get(api + '/data/get-agent-positions').json()['agents'] if x['vehicle_type'] is None]
avg_lon = sum([x['longitude'] for x in agents]) / len(agents)
avg_lat = sum([x['latitude'] for x in agents]) / len(agents)
print('Average position of all active pedestrians: {}, {}'.format(avg_lon, avg_lat))
print()
# Modify one traffic signal, doubling the duration of its second phase
print('Modify a traffic signal')
ts = requests.get(api + '/traffic-signals/get', params={'id': 67}).json()
@ -91,11 +98,7 @@ def stringify_direction(direxn):
def stringify_road(directed_road):
if directed_road['forwards']:
direxn = 'fwd'
else:
direxn = 'back'
return 'Road #{} ({})'.format(directed_road['id'], direxn)
return 'Road #{} ({})'.format(directed_road['id'], directed_road['dir'])
if __name__ == '__main__':

View File

@ -10,11 +10,13 @@
// ... huge JSON blob
use abstutil::{serialize_btreemap, CmdArgs, Timer};
use geom::{Duration, Time};
use geom::{Duration, LonLat, Time};
use hyper::{Body, Request, Response, Server};
use map_model::{CompressedTurnGroupID, ControlTrafficSignal, IntersectionID, Map, TurnGroupID};
use serde::Serialize;
use sim::{AlertHandler, Sim, SimFlags, SimOptions, TripID, TripMode};
use sim::{
AlertHandler, GetDrawAgents, PersonID, Sim, SimFlags, SimOptions, TripID, TripMode, VehicleType,
};
use std::collections::{BTreeMap, HashMap};
use std::convert::TryFrom;
use std::error::Error;
@ -176,6 +178,17 @@ fn handle_command(
"/data/get-finished-trips" => Ok(abstutil::to_json(&FinishedTrips {
trips: sim.get_analytics().finished_trips.clone(),
})),
"/data/get-agent-positions" => Ok(abstutil::to_json(&AgentPositions {
agents: sim
.get_unzoomed_agents(map)
.into_iter()
.map(|a| AgentPosition {
vehicle_type: a.vehicle_type,
pos: a.pos.to_gps(map.get_gps_bounds()),
person: a.person,
})
.collect(),
})),
_ => Err("Unknown command".into()),
}
}
@ -200,3 +213,17 @@ struct Throughput {
#[serde(serialize_with = "serialize_btreemap")]
per_direction: BTreeMap<TurnGroupID, usize>,
}
#[derive(Serialize)]
struct AgentPositions {
agents: Vec<AgentPosition>,
}
#[derive(Serialize)]
struct AgentPosition {
// None for pedestrians
vehicle_type: Option<VehicleType>,
pos: LonLat,
// None for buses
person: Option<PersonID>,
}