From b473034054facbb615a809cbc4feb784cd534ef6 Mon Sep 17 00:00:00 2001 From: noncomputable Date: Sat, 1 Sep 2018 18:45:13 -0400 Subject: [PATCH] changes neighbors property in unit features from array of obs to array of num ids, now convenient to export units into a js file --- src/agentmap.js | 22 ++++++++++++++++++ src/buildings.js | 59 ++++++++++++++++++++++++------------------------ 2 files changed, 52 insertions(+), 29 deletions(-) diff --git a/src/agentmap.js b/src/agentmap.js index dae8c6e..d6e9563 100644 --- a/src/agentmap.js +++ b/src/agentmap.js @@ -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. * diff --git a/src/buildings.js b/src/buildings.js index b911295..2017840 100644 --- a/src/buildings.js +++ b/src/buildings.js @@ -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} - An array of pre-layer feature IDs for a unit's neighbors. + * @returns {Array} - 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} - An array of pre-layer feature IDs for a unit's neighbors. - * @returns {Array} - 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;