changes neighbors property in unit features from array of obs to array of num ids, now convenient to export units into a js file

This commit is contained in:
noncomputable 2018-09-01 18:45:13 -04:00
parent 6b22692e70
commit b473034054
2 changed files with 52 additions and 29 deletions

View File

@ -249,6 +249,28 @@ Agentmap.prototype.getNearestIntersection = function(lat_lng, place) {
return closest_intersection_point;
}
/*
* Since units may take a noticeably long time to generate while typically staying the same over simulations,
* downloadUnits makes it easy to get a JS file containing the units object, so it can be included with an
* AgentMaps app and imported into Agentmap.buildingify so that they do not need to be regenerated.
*/
Agentmap.prototype.downloadUnits = function() {
let file_content = "let units_data = ",
units_json = this.units.toGeoJSON();
file_content += JSON.stringify(units_json),
file = new Blob([file_content]);
var element = document.createElement("a");
element.setAttribute("href", URL.createObjectURL(file)),
element.setAttribute("download", "units_data.js"),
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
/**
* Generates an agentmap for the given map.
*

View File

@ -159,10 +159,39 @@ function setupUnitFeatures(OSM_data, bounding_box, unit_options = {}) {
unit.street_id = unit.feature.properties.street_id,
unit.street_anchors = unit.feature.properties.street_anchors,
//Change the IDs of each unit in this unit's neighbours array into the appropriate Leaflet IDs.
unit.neighbors = getUnitNeighborLayerIDs.call(this, unit.feature.properties.neighbors);
unit.neighbors = getUnitNeighborLayerIDs.call(this, unit.feature.properties.neighbors),
unit.feature.properties.neighbors = unit.neighbors;
}, this);
}
/**
* Given an array of pre-layer IDs, check if any of them correspond to the pre-layer IDs of unit layers, and if so
* return an array of the corresponding layer IDs.
* @private
*
* @param {Array<?number>} - An array of pre-layer feature IDs for a unit's neighbors.
* @returns {Array<?number>} - An array of Leaflet layer IDs corresponding to the unit's neighbors.
*/
function getUnitNeighborLayerIDs(neighbors) {
let neighbor_layer_ids = neighbors.map(function(neighbor) {
if (neighbor !== null) {
let neighbor_layer_id = null;
this.units.eachLayer(function(possible_neighbor_layer) {
if (possible_neighbor_layer.feature.properties.id === neighbor.properties.id) {
neighbor_layer_id = this.units.getLayerId(possible_neighbor_layer);
}
}, this);
return neighbor_layer_id;
}
else {
return null;
}
}, this);
return neighbor_layer_ids;
}
/**
* Get all appropriate units within the desired bounding box.
* @private
@ -365,32 +394,4 @@ function noOverlaps(reference_polygon_feature, polygon_feature_array) {
return true;
}
/**
* Given an array of pre-layer IDs, check if any of them correspond to the pre-layer IDs of unit layers, and if so
* return an array of the corresponding layer IDs.
* @private
*
* @param {Array<?number>} - An array of pre-layer feature IDs for a unit's neighbors.
* @returns {Array<?number>} - An array of Leaflet layer IDs corresponding to the unit's neighbors.
*/
function getUnitNeighborLayerIDs(neighbors) {
let neighbor_layer_ids = neighbors.map(function(neighbor) {
if (neighbor !== null) {
let neighbor_layer_id = null;
this.units.eachLayer(function(possible_neighbor_layer) {
if (possible_neighbor_layer.feature.properties.id === neighbor.properties.id) {
neighbor_layer_id = this.units.getLayerId(possible_neighbor_layer);
}
}, this);
return neighbor_layer_id;
}
else {
return null;
}
}, this);
return neighbor_layer_ids;
}
Agentmap.prototype.buildingify = buildingify;