Skip to content

Commit

Permalink
Merge pull request sharedstreets#103 from sharedstreets/4.3.0
Browse files Browse the repository at this point in the history
v4.3.0
  • Loading branch information
morganherlocker authored Jan 23, 2020
2 parents ca50a27 + b762e5c commit 2448d92
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 147 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ A `config.json` file is required to run Mobility Metrics. Enable providers and s
- maximum number of days without status change before vehicles are permanently lost
- `summary`
- enabled or disabled metrics in summary UI
- `vehicleFilter`
- optionally allow only one type of [MDS vehicle_type](https://github.com/openmobilityfoundation/mobility-data-specification/tree/dev/provider#vehicle-types)
- `geographicFilter`
- filter all data that falls outside the defined geographic filter, formatted as a valid GeoJSON Feature of type Polygon or MultiPolygon
- `providers`
- list of providers to query
- `type`
Expand Down
20 changes: 18 additions & 2 deletions example/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,30 @@
"zoom": 12.75,
"privacyMinimum": 3,
"lost": 2,
"geographicFilter": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-86.77564144134521, 36.163344537460084],
[-86.74620151519775, 36.163344537460084],
[-86.74620151519775, 36.17997342339555],
[-86.77564144134521, 36.17997342339555],
[-86.77564144134521, 36.163344537460084]
]
]
}
},
"summary": {
"Unique Vehicles": true,
"Active Vehicles": true,
"Total Trips": true,
"Total Trip Distance": true,
"Distance Per Vehicle": false,
"Distance Per Vehicle": true,
"Vehicle Utilization": true,
"Trips Per Active Vehicle": false,
"Trips Per Active Vehicle": true,
"Avg Trip Distance": true,
"Avg Trip Duration": true
},
Expand Down
6 changes: 3 additions & 3 deletions example/simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exec("../node_modules/osrm/lib/binding/osrm-contract graph/nash.osrm");

const providers = ["Flipr", "Scoob", "BikeMe", "Spuun"];

const days = 30;
const days = 2;

const start = 1563087600000; // Sunday, July 14, 2019 3:00:00 AM GMT-04:00

Expand All @@ -42,8 +42,8 @@ cmd += "--changes data/{provider}/changes.json ";
cmd += "--trips data/{provider}/trips.json ";
cmd += "--quiet ";

const minAgents = 100;
const maxAgents = 300;
const minAgents = 50;
const maxAgents = 200;

console.log("running simulations...");

Expand Down
50 changes: 43 additions & 7 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,53 @@ if (argv.help || argv.h || Object.keys(argv).length === 1) {

const config = require(path.resolve(argv.config));
// add spatial indices to zones
if (!config.zones) {
config.zones = turf.FeatureCollection([]);
}
const z = 19;
const zs = { min_zoom: z, max_zoom: z };
if (config.zones) {
for (let zone of config.zones.features) {
zone.properties.keys = {};
const keys = cover.indexes(zone.geometry, zs);
for (let key of keys) {
zone.properties.keys[key] = 1;
}
for (let zone of config.zones.features) {
zone.properties.keys = {};
const keys = cover.indexes(zone.geometry, zs);
for (let key of keys) {
zone.properties.keys[key] = 1;
}
}
// build geographicFilter lookup
if (config.geographicFilter) {
config.geographicFilterKeys = {};
cover.indexes(config.geographicFilter.geometry, zs).forEach(qk => {
config.geographicFilterKeys[qk] = 1;
});
}

// check for valid vehicleFilter
if (
config.vehicleFilter &&
(config.vehicleFilter !== "car" &&
config.vehicleFilter !== "bicycle" &&
config.vehicleFilter !== "scooter")
) {
throw new Error("detected invalid vehicle filter");
}

// defaults
if (!config.zoom) config.zoom = 12.5;
if (!config.lost) config.lost = 2;
if (!config.privacyMinimum || config.privacyMinimum < 3)
config.privacyMinimum = 3;
if (!config.summary)
config.summary = {
"Unique Vehicles": true,
"Active Vehicles": true,
"Total Trips": true,
"Total Trip Distance": true,
"Distance Per Vehicle": true,
"Vehicle Utilization": true,
"Trips Per Active Vehicle": true,
"Avg Trip Distance": true,
"Avg Trip Duration": true
};

const publicPath = path.resolve(argv.public);
const cachePath = path.resolve(argv.cache);
Expand Down
76 changes: 44 additions & 32 deletions src/matchers/change.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,62 @@ const z = 19;
const zs = { min_zoom: z, max_zoom: z };

module.exports = async function(change, config, graph) {
// STREETS
if (!config.vehicleFilter || config.vehicleFilter === change.vehicle_type) {
const keys = cover.indexes(
turf.point(change.event_location.geometry.coordinates).geometry,
zs
);

if (!change.matches) change.matches = {};
if (config.geographicFilter) {
var pass = false;
for (let key of keys) {
if (config.geographicFilterKeys[key]) {
pass = true;
}
}
if (!pass) return;
}

const matches = await graph.matchPoint(change.event_location, null, 1);
// STREETS
if (!change.matches) change.matches = {};
const matches = await graph.matchPoint(change.event_location, null, 1);

if (matches.length) {
change.matches.streets = matches;
}
if (matches.length) {
change.matches.streets = matches;
}

// BINS
// BINS

const bin = h3.geoToH3(
change.event_location.geometry.coordinates[1],
change.event_location.geometry.coordinates[0],
config.Z
);
const bin = h3.geoToH3(
change.event_location.geometry.coordinates[1],
change.event_location.geometry.coordinates[0],
config.Z
);

change.matches.bins = bin;
change.matches.bins = bin;

// ZONES
// ZONES

if (config.zones) {
var zoneMatches = [];
const keys = cover.indexes(
turf.point(change.event_location.geometry.coordinates).geometry,
zs
);
for (let zone of config.zones.features) {
let found = false;
for (let key of keys) {
if (zone.properties.keys[key]) found = true;
continue;
if (config.zones) {
var zoneMatches = [];

for (let zone of config.zones.features) {
let found = false;
for (let key of keys) {
if (zone.properties.keys[key]) found = true;
continue;
}

if (found) {
zoneMatches.push(zone.properties.id);
}
}

if (found) {
zoneMatches.push(zone.properties.id);
if (zoneMatches.length) {
change.matches.zones = zoneMatches;
}
}

if (zoneMatches.length) {
change.matches.zones = zoneMatches;
}
return change;
}

return change;
};
Loading

0 comments on commit 2448d92

Please sign in to comment.