Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Back propagation to find a suitable pFactor #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { download } from "./export";
import { render } from "./core/plot";
import { defaultPopulationFactor } from "./core/constants"

document.querySelector("#loader").classList.add("hide");

Expand Down Expand Up @@ -66,7 +67,7 @@ function load() {
shape: cellShape,
scale: cellScale,
};
render(topoData, popData, cellDetails, year);
render(topoData, popData, cellDetails, year, defaultPopulationFactor);
document.querySelector("#loader").classList.add("hide");
});
}
Expand Down
14 changes: 5 additions & 9 deletions core/catogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function () {
return 1;
};

function cartogram(topology, geometries, cellDetails, populationData, year) {
function cartogram(topology, geometries, cellDetails, populationData, year, populationFactor) {
topology = copyTopo(topology);
var tf = transformer(topology.transform),
x,
Expand Down Expand Up @@ -61,7 +61,7 @@ export default function () {
var values = objects.map(value),
totalValue = sum(values);

var pFactor = populationFactor(cellDetails.scale, populationData, year);
var pFactor = getPopulationFactor(cellDetails.scale, populationData, year, populationFactor);

if (iterations <= 0) {
return objects;
Expand Down Expand Up @@ -154,18 +154,14 @@ export default function () {
};
}

function populationFactor(selectedScale, populationData, year) {
function getPopulationFactor(selectedScale, populationData, year, populationFactor) {
switch (selectedScale) {
case cellScale.Fixed:
var factor =
getTotalPopulation(populationData, 2018) /
getTotalPopulation(populationData, year) /
1.6;
if (factor > 0.8) {
return factor;
} else {
return 1;
}
populationFactor;
return factor;
case cellScale.Fluid:
return 1;
}
Expand Down
2 changes: 2 additions & 0 deletions core/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ export const cellScale = {
Fixed: "Fixed",
Fluid: "Fluid",
};

export const defaultPopulationFactor = 1.0
37 changes: 29 additions & 8 deletions core/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ import {
dragged,
dragended,
} from "./events";
import { colors, margin, width, height, strokeWidth } from "./constants";
import { colors, margin, width, height, strokeWidth, cellScale } from "./constants";

export var exportJson = {
type: "FeatureCollection",
features: [],
};

export function render(topo, populationData, cellDetails, year) {
export function render(topo, populationData, cellDetails, year, populationFactor) {
let cellRadius = cellDetails.radius;
let cellShape = cellDetails.shape;
let cellScale = cellDetails.scale;

let shapeDistance = getRadius(cellRadius, cellShape);
let cols = width / shapeDistance;
Expand Down Expand Up @@ -56,7 +55,8 @@ export function render(topo, populationData, cellDetails, year) {
topo,
topo.objects.tiles.geometries,
cellDetails,
populationData, year
populationData, year,
populationFactor
).features;
exportFormat(topoFeatures);

Expand Down Expand Up @@ -137,7 +137,25 @@ export function render(topo, populationData, cellDetails, year) {
}
}

setCellSize(totalPopulation, cellCount);
var cellSize = getCellSize(totalPopulation, cellCount);

switch (cellDetails.scale) {
case cellScale.Fixed:
if (cellSize > 1.0) {
console.log(">", cellSize, populationFactor);
render(topo, populationData, cellDetails, year, populationFactor + 0.1);
} else if (cellSize < 1.0) {
console.log("<", cellSize, populationFactor);
render(topo, populationData, cellDetails, year, populationFactor - 0.1);
} else {
console.log("=", cellSize, populationFactor);
setCellSize(cellSize);
}
break;
case cellScale.Fluid:
setCellSize(cellSize);
}

}

function indexByCode(data) {
Expand All @@ -148,9 +166,12 @@ function indexByCode(data) {
return obj;
}

function setCellSize(totalPopulation, cellCount) {
document.getElementById("cell-size").value =
(totalPopulation / (cellCount * 1000000)).toFixed(1) + "M";
function setCellSize(cellSize) {
document.getElementById("cell-size").value = cellSize + "M";
}

function getCellSize(totalPopulation, cellCount) {
return (totalPopulation / (cellCount * 1000000)).toFixed(1);
}

function flattenFeatures(topoFeatures) {
Expand Down