overallStatistics = {
for (let i = 0; i < bayAreaGeoJSON.features.length; i++) {
const feature = bayAreaGeoJSON.features[i];
const zip = feature.properties.zip;
const incomeInfo = taxableIncomeByZipcode.find((info) => info.Zip === zip);
if (incomeInfo) {
feature.properties.medianHouseholdIncome =
incomeInfo.MedianHouseholdIncome;
feature.properties.population = incomeInfo.Population;
} else {
feature.properties.medianHouseholdIncome = 0;
feature.properties.population = 0;
}
}
for (let i = 0; i < bayAreaGeoJSON.features.length; i++) {
const feature = bayAreaGeoJSON.features[i];
const zip = feature.properties.zip;
const accessibilityInfo = bartstationaccessibility.find(
(info) => info.zipCode === zip
);
if (accessibilityInfo) {
feature.properties.routesCount = accessibilityInfo.routesCount;
} else {
feature.properties.routesCount = 0;
}
}
for (let i = 0; i < sellingPriceByZip.length; i++) {
const zip = sellingPriceByZip[i].Zip;
const averagePrice = sellingPriceByZip[i].averagePrice;
for (let j = 0; j < bayAreaGeoJSON.features.length; j++) {
const feature = bayAreaGeoJSON.features[j];
if (feature.properties.zip === zip) {
feature.properties.averageSellingPrice = parseInt(averagePrice);
break;
}
}
}
// Merge rentByZip
for (let i = 0; i < rentByZip.length; i++) {
const zip = rentByZip[i].Zip;
const averagePrice = rentByZip[i].averagePrice;
for (let j = 0; j < bayAreaGeoJSON.features.length; j++) {
const feature = bayAreaGeoJSON.features[j];
if (feature.properties.zip === zip) {
feature.properties.averageRent = parseInt(averagePrice);
break;
}
}
}
// Set averageSellingPrice & averageRent 0 if corresponding zip does not exist
for (let i = 0; i < bayAreaGeoJSON.features.length; i++) {
const feature = bayAreaGeoJSON.features[i];
if (!feature.properties.hasOwnProperty("averageSellingPrice")) {
feature.properties.averageSellingPrice = 0;
}
if (!feature.properties.hasOwnProperty("averageRent")) {
feature.properties.averageRent = 0;
}
}
for (let i = 0; i < bayAreaGeoJSON.features.length; i++) {
const feature = bayAreaGeoJSON.features[i];
const { averageRent, averageSellingPrice } = feature.properties;
if (averageRent && averageSellingPrice) {
feature.properties.costEffectiveness = Math.round(
averageSellingPrice / averageRent
);
} else {
feature.properties.costEffectiveness = 0;
}
}
function calculateRankings(data, property) {
// from great to less
const sortedData = data.sort(
(a, b) => b.properties[property] - a.properties[property]
);
sortedData.forEach((zipData, index) => {
const rankingKey = `${property}Ranking`;
zipData.properties[rankingKey] = index + 1;
});
}
function calculateReverseRankings(data, property) {
// from less to great
const sortedData = data.sort(
(a, b) => a.properties[property] - b.properties[property]
);
sortedData.forEach((zipData, index) => {
const rankingKey = `${property}ReverseRanking`;
zipData.properties[rankingKey] = index + 1;
});
}
calculateRankings(bayAreaGeoJSON.features, "population");
calculateRankings(bayAreaGeoJSON.features, "routesCount");
calculateRankings(bayAreaGeoJSON.features, "medianHouseholdIncome");
calculateRankings(bayAreaGeoJSON.features, "averageSellingPrice");
calculateReverseRankings(bayAreaGeoJSON.features, "averageSellingPrice");
calculateRankings(bayAreaGeoJSON.features, "averageRent");
calculateReverseRankings(bayAreaGeoJSON.features, "averageRent");
calculateRankings(bayAreaGeoJSON.features, "costEffectiveness");
calculateReverseRankings(bayAreaGeoJSON.features, "costEffectiveness");
return bayAreaGeoJSON;
}