From 4789ce8ac421a6ed81b513377ca8f2af9bf020d6 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 26 Feb 2025 22:39:19 -0500 Subject: [PATCH] Finally finished adding language boundaries - next step: export them to a file so that they don't have to be recomputed each time --- index.js | 113 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 42 deletions(-) diff --git a/index.js b/index.js index e095633..fc34c30 100644 --- a/index.js +++ b/index.js @@ -142,13 +142,50 @@ const district2lang = { // Should override state colors "Hapur": languages["urdu"], } + +function reverseCoordArrays(coords) { + if (!Array.isArray(coords)) { + return coords; + } + if (coords.every(item => Array.isArray(item) && item.length == 2 && item.every(val => Number.isFinite(val)))) { + if (!turf.booleanClockwise(coords)) { + return coords.reverse(); + } else { + return coords; + } + } + + return coords.map(reverseCoordArrays); +} + + function getOuterBoundaryPolygon(features) { try { - // Combine all polygons using turf.union - let combined = features[0]; - for (let i = 1; i < features.length; i++) { - combined = turf.union(turf.featureCollection([combined, features[i]])); + + // Check if we have features to process + if (!features || features.length === 0) { + console.warn("No features to process for boundary"); + return null; + } + + // Handle single feature case + if (features.length === 1) { + return features[0]; } + + // Combine all polygons using turf.union + let combined = turf.union(turf.featureCollection(features)) + +// for (let i=0; i stateOrDistrictOrLanguage(d)) - .attr("fill", function(d) { - if (stateOrDistrictOrLanguage(d) == "state" || stateOrDistrictOrLanguage(d) == "district") { - return district2lang.hasOwnProperty(d.properties.district) ? - district2lang[d.properties.district].color : - state2lang[d.properties.st_nm].color - } - }) + .attr("class", d => stateOrDistrict(d)) + .attr("fill", d => district2lang.hasOwnProperty(d.properties.district) ? + district2lang[d.properties.district].color : + state2lang[d.properties.st_nm].color) .each(function(d) { - if (stateOrDistrictOrLanguage(d) == "district") { + if (stateOrDistrict(d) == "district") { districtLang = district2lang.hasOwnProperty(d.properties.district) ? district2lang[d.properties.district] : state2lang[d.properties.st_nm]; districtLang.districts.push(d) } @@ -213,29 +243,28 @@ function drawMap(world) { .attr("id", d => d.properties.district+"Text") .text(d => d.properties.district); -// let allLangs = [] -// -// for (const [langId,lang] of Object.entries(languages)) { -// let geojson = { -// "type": "FeatureCollection", -// "features": lang.districts -// }; -// -// let outerBound = getOuterBoundaryPolygon(geojson.features) -// outerBound["id"] = "lang" + lang.name -// outerBound.properties["lang_name"]= lang.name -// allLangs.push(outerBound); -// -// // Draw the boundary -// svg.append("path") -// .datum(outerBound) -// .attr("d", path) -// .attr("fill", "none") -// .attr("stroke", "red") -// .attr("stroke-width", 2); -// } -// -// console.log(JSON.stringify(allLangs,null,2)) + let allLangs = [] + + for (const [langId,lang] of Object.entries(languages)) { + let geojson = { + "type": "FeatureCollection", + "features": lang.districts + }; + + let outerBound = getOuterBoundaryPolygon(geojson.features) + outerBound["id"] = "lang" + lang.name + outerBound.properties["lang_name"]= lang.name + allLangs.push(outerBound); + console.log(JSON.stringify(outerBound, null, 2)) + + svg.append("path") + .datum(outerBound) + .attr("d", path) + .attr("fill", "none") + .attr("stroke", "red") + .attr("stroke-width", 2) + } + console.log(allLangs) }