Working on defining a 'language' boundary, separate from state and district boundaries
This commit is contained in:
244
index.js
244
index.js
@@ -1,5 +1,4 @@
|
|||||||
const svg = d3.select("svg")
|
const svg = d3.select("svg")
|
||||||
const colorToDistrict = new Map()
|
|
||||||
|
|
||||||
const tamilColor = "#75d795" // Tamil
|
const tamilColor = "#75d795" // Tamil
|
||||||
const malayalamColor = "#ff7c7c" // Malayalam
|
const malayalamColor = "#ff7c7c" // Malayalam
|
||||||
@@ -14,7 +13,7 @@ const oriyaColor = "#9bcc9f" // Oriya
|
|||||||
const bengaliColor = "#bf9a77" // Bengali
|
const bengaliColor = "#bf9a77" // Bengali
|
||||||
const punjabiColor = "#e84a35" // Punjabi
|
const punjabiColor = "#e84a35" // Punjabi
|
||||||
const mizoColor = "#a6a4de" // Mizo
|
const mizoColor = "#a6a4de" // Mizo
|
||||||
const AssameseColor = "#c9535b" // Assamese
|
const assameseColor = "#c9535b" // Assamese
|
||||||
const bhojpuriColor = "#b3b876" // Bhojpuri
|
const bhojpuriColor = "#b3b876" // Bhojpuri
|
||||||
const manipuriColor = "#c9afad" // Manipuri
|
const manipuriColor = "#c9afad" // Manipuri
|
||||||
const dogriColor = "#9595e6" // Dogri (near Kashmir)
|
const dogriColor = "#9595e6" // Dogri (near Kashmir)
|
||||||
@@ -25,109 +24,122 @@ const maithaliColor = "#4472a6" // Maithali
|
|||||||
|
|
||||||
const unavailLangColor = "#555555"
|
const unavailLangColor = "#555555"
|
||||||
|
|
||||||
|
const languages = {
|
||||||
|
tamil: {name: "Tamil", color: tamilColor, districts: []},
|
||||||
|
malayalam: {name: "Malayalam", color: malayalamColor, districts: []},
|
||||||
|
kannada: {name: "Kannada", color: kannadaColor, districts: []},
|
||||||
|
telugu: {name: "Telugu", color: teluguColor, districts: []},
|
||||||
|
marathi: {name: "Marathi", color: marathiColor, districts: []},
|
||||||
|
konkani: {name: "Konkani", color: konkaniColor, districts: []},
|
||||||
|
hindi: {name: "Hindi", color: hindiColor, districts: []},
|
||||||
|
gujarati: {name: "Gujarati", color: gujaratiColor, districts: []},
|
||||||
|
marwari: {name: "Marwari", color: marwariColor, districts: []},
|
||||||
|
oriya: {name: "Oriya", color: oriyaColor, districts: []},
|
||||||
|
bengali: {name: "Bengali", color: bengaliColor, districts: []},
|
||||||
|
punjabi: {name: "Punjabi", color: punjabiColor, districts: []},
|
||||||
|
mizo: {name: "Mizo", color: mizoColor, districts: []},
|
||||||
|
assamese: {name: "Assamese", color: assameseColor, districts: []},
|
||||||
|
bhojpuri: {name: "Bhojpuri", color: bhojpuriColor, districts: []},
|
||||||
|
manipuri: {name: "Manipuri", color: manipuriColor, districts: []},
|
||||||
|
dogri: {name: "Dogri", color: dogriColor, districts: []},
|
||||||
|
nepali: {name: "Nepali", color: nepaliColor, districts: []},
|
||||||
|
urdu: {name: "Urdu", color: urduColor, districts: []},
|
||||||
|
tulu: {name: "Tulu", color: tuluColor, districts: []},
|
||||||
|
maithali: {name: "Maithali", color: maithaliColor, districts: []},
|
||||||
|
unavail: {name: "Unavailable", color: unavailLangColor, districts: []},
|
||||||
|
};
|
||||||
|
|
||||||
// Credit: https://www.artcraftblend.com/blogs/colors/shades-of-pastel
|
// Credit: https://www.artcraftblend.com/blogs/colors/shades-of-pastel
|
||||||
const colorMap = {
|
const state2lang = {
|
||||||
"Tamil Nadu": tamilColor,
|
"Tamil Nadu": languages["tamil"],
|
||||||
"Kerala": malayalamColor,
|
"Kerala": languages["malayalam"],
|
||||||
"Karnataka": kannadaColor,
|
"Karnataka": languages["kannada"],
|
||||||
"Andhra Pradesh": teluguColor,
|
"Andhra Pradesh": languages["telugu"],
|
||||||
"Telangana": teluguColor,
|
"Telangana": languages["telugu"],
|
||||||
"Maharashtra": marathiColor,
|
"Maharashtra": languages["marathi"],
|
||||||
"Goa": konkaniColor,
|
"Goa": languages["konkani"],
|
||||||
"Odisha": oriyaColor,
|
"Odisha": languages["oriya"],
|
||||||
"Gujarat": gujaratiColor,
|
"Gujarat": languages["gujarati"],
|
||||||
"Rajasthan": marwariColor,
|
"Rajasthan": languages["marwari"],
|
||||||
"Chhattisgarh": hindiColor,
|
"Chhattisgarh": languages["hindi"],
|
||||||
"Jharkhand": hindiColor, // DEFAULT
|
"Jharkhand": languages["hindi"], // DEFAULT
|
||||||
"West Bengal": bengaliColor,
|
"West Bengal": languages["bengali"],
|
||||||
"Assam": AssameseColor,
|
"Assam": languages["assamese"],
|
||||||
"Meghalaya": unavailLangColor, // DEFAULT
|
"Meghalaya": languages["unavail"], // DEFAULT
|
||||||
"Tripura": bengaliColor,
|
"Tripura": languages["bengali"],
|
||||||
"Mizoram": mizoColor,
|
"Mizoram": languages["mizo"],
|
||||||
"Manipur": manipuriColor,
|
"Manipur": languages["manipuri"],
|
||||||
"Nagaland": unavailLangColor, // DEFAULT
|
"Nagaland": languages["unavail"], // DEFAULT
|
||||||
"Arunachal Pradesh": unavailLangColor, // DEFAULT
|
"Arunachal Pradesh": languages["unavail"], // DEFAULT
|
||||||
"Sikkim": nepaliColor,
|
"Sikkim": languages["nepali"],
|
||||||
"Bihar": bhojpuriColor,
|
"Bihar": languages["bhojpuri"],
|
||||||
"Madhya Pradesh": hindiColor,
|
"Madhya Pradesh": languages["hindi"],
|
||||||
"Uttar Pradesh": hindiColor,
|
"Uttar Pradesh": languages["hindi"],
|
||||||
"Uttarakhand": hindiColor, // DEFAULT
|
"Uttarakhand": languages["hindi"], // DEFAULT
|
||||||
"Haryana": hindiColor, // DEFAULT
|
"Haryana": languages["hindi"], // DEFAULT
|
||||||
"Punjab": punjabiColor,
|
"Punjab": languages["punjabi"],
|
||||||
"Himachal Pradesh": hindiColor, // DEFAULT
|
"Himachal Pradesh": languages["hindi"], // DEFAULT
|
||||||
"Ladakh": dogriColor,
|
"Ladakh": languages["dogri"],
|
||||||
"Jammu and Kashmir": dogriColor,
|
"Jammu and Kashmir": languages["dogri"],
|
||||||
|
|
||||||
|
"Dadra and Nagar Haveli and Daman and Diu": languages["gujarati"],
|
||||||
|
"Puducherry": languages["tamil"],
|
||||||
|
"Lakshadweep": languages["malayalam"],
|
||||||
|
"Andaman and Nicobar Islands": languages["unavail"],
|
||||||
|
"Delhi": languages["hindi"],
|
||||||
|
"Chandigarh": languages["hindi"]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const districtColorMap = { // Should override state colors
|
const district2lang = { // Should override state colors
|
||||||
"Dakshina Kannada": tuluColor,
|
"Dakshina Kannada": languages["tulu"],
|
||||||
|
|
||||||
"Muzaffarpur": maithaliColor,
|
"Muzaffarpur": languages["maithali"],
|
||||||
"West Champaran": maithaliColor,
|
"West Champaran": languages["maithali"],
|
||||||
"East Champaran": maithaliColor,
|
"East Champaran": languages["maithali"],
|
||||||
"Vaishali": maithaliColor,
|
"Vaishali": languages["maithali"],
|
||||||
"Sitamarhi": maithaliColor,
|
"Sitamarhi": languages["maithali"],
|
||||||
"Sheohar": maithaliColor,
|
"Sheohar": languages["maithali"],
|
||||||
"Saharsa": maithaliColor,
|
"Saharsa": languages["maithali"],
|
||||||
"Madhepura": maithaliColor,
|
"Madhepura": languages["maithali"],
|
||||||
"Supaul": maithaliColor,
|
"Supaul": languages["maithali"],
|
||||||
"Araria": maithaliColor,
|
"Araria": languages["maithali"],
|
||||||
"Katihar": maithaliColor,
|
"Katihar": languages["maithali"],
|
||||||
"Kishanganj": maithaliColor,
|
"Kishanganj": languages["maithali"],
|
||||||
"Purnia": maithaliColor,
|
"Purnia": languages["maithali"],
|
||||||
"Banka": maithaliColor,
|
"Banka": languages["maithali"],
|
||||||
"Bhagalpur": maithaliColor,
|
"Bhagalpur": languages["maithali"],
|
||||||
"Munger": maithaliColor,
|
"Munger": languages["maithali"],
|
||||||
"Begusarai": maithaliColor,
|
"Begusarai": languages["maithali"],
|
||||||
"Jamui": maithaliColor,
|
"Jamui": languages["maithali"],
|
||||||
"Khagaria": maithaliColor,
|
"Khagaria": languages["maithali"],
|
||||||
"Sheikhpura": maithaliColor,
|
"Sheikhpura": languages["maithali"],
|
||||||
"Lakhisarai": maithaliColor,
|
"Lakhisarai": languages["maithali"],
|
||||||
"Godda": maithaliColor,
|
"Godda": languages["maithali"],
|
||||||
"Deoghar": maithaliColor,
|
"Deoghar": languages["maithali"],
|
||||||
"Dumka": maithaliColor,
|
"Dumka": languages["maithali"],
|
||||||
"Jamtara": maithaliColor,
|
"Jamtara": languages["maithali"],
|
||||||
"Sahibganj": maithaliColor,
|
"Sahibganj": languages["maithali"],
|
||||||
"Pakur": maithaliColor,
|
"Pakur": languages["maithali"],
|
||||||
"Darbhanga": maithaliColor,
|
"Darbhanga": languages["maithali"],
|
||||||
"Madhubani": maithaliColor,
|
"Madhubani": languages["maithali"],
|
||||||
"Samastipur:": maithaliColor,
|
"Samastipur": languages["maithali"],
|
||||||
|
|
||||||
"Moradabad": urduColor,
|
"Moradabad": languages["urdu"],
|
||||||
"Rampur": urduColor,
|
"Rampur": languages["urdu"],
|
||||||
"Bijnor": urduColor,
|
"Bijnor": languages["urdu"],
|
||||||
"Amroha": urduColor,
|
"Amroha": languages["urdu"],
|
||||||
"Meerut": urduColor,
|
"Meerut": languages["urdu"],
|
||||||
"Ghaziabad": urduColor,
|
"Ghaziabad": languages["urdu"],
|
||||||
"Bulandshahr": urduColor,
|
"Bulandshahr": languages["urdu"],
|
||||||
"Aligarh": urduColor,
|
"Aligarh": languages["urdu"],
|
||||||
"Budaun": urduColor,
|
"Budaun": languages["urdu"],
|
||||||
"Bareilly": urduColor,
|
"Bareilly": languages["urdu"],
|
||||||
"Sambhal": urduColor,
|
"Sambhal": languages["urdu"],
|
||||||
"Muzaffarnagar": urduColor,
|
"Muzaffarnagar": languages["urdu"],
|
||||||
"Saharanpur": urduColor,
|
"Saharanpur": languages["urdu"],
|
||||||
"Shamli": urduColor,
|
"Shamli": languages["urdu"],
|
||||||
"Hapur": urduColor,
|
"Hapur": languages["urdu"],
|
||||||
}
|
|
||||||
|
|
||||||
// Assuming 'features' is your array of GeoJSON polygon features
|
|
||||||
function getOuterBoundary(features) {
|
|
||||||
// Merge all polygons into a single multipolygon
|
|
||||||
const merged = turf.union(turf.featureCollection(features));
|
|
||||||
|
|
||||||
// If we have a multipolygon, extract its coordinates
|
|
||||||
const coordinates = merged.geometry.type === 'MultiPolygon'
|
|
||||||
? merged.geometry.coordinates
|
|
||||||
: [merged.geometry.coordinates];
|
|
||||||
|
|
||||||
// Extract just the outer ring of each polygon (first ring in each polygon)
|
|
||||||
const outerRings = coordinates.map(polygon => polygon[0]);
|
|
||||||
|
|
||||||
// Create a new Feature with a MultiLineString geometry containing the outer rings
|
|
||||||
const boundary = turf.multiLineString(outerRings);
|
|
||||||
|
|
||||||
console.log(boundary);
|
|
||||||
return boundary;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getOuterBoundaryPolygon(features) {
|
function getOuterBoundaryPolygon(features) {
|
||||||
@@ -171,16 +183,13 @@ function drawMap(world) {
|
|||||||
states.append("path")
|
states.append("path")
|
||||||
.attr("d", path)
|
.attr("d", path)
|
||||||
.attr("class", d => stateOrDistrict(d))
|
.attr("class", d => stateOrDistrict(d))
|
||||||
.attr("fill", d => districtColorMap.hasOwnProperty(d.properties.district) ?
|
.attr("fill", d => district2lang.hasOwnProperty(d.properties.district) ?
|
||||||
districtColorMap[d.properties.district] :
|
district2lang[d.properties.district].color :
|
||||||
colorMap[d.properties.st_nm])
|
state2lang[d.properties.st_nm].color)
|
||||||
.each(function(d) {
|
.each(function(d) {
|
||||||
if (stateOrDistrict(d) == "district") {
|
if (stateOrDistrict(d) == "district") {
|
||||||
if (!colorToDistrict.hasOwnProperty(this.getAttribute("fill"))) {
|
districtLang = district2lang.hasOwnProperty(d.properties.district) ? district2lang[d.properties.district] : state2lang[d.properties.st_nm];
|
||||||
colorToDistrict[this.getAttribute("fill")] = [d]
|
districtLang.districts.push(d)
|
||||||
} else {
|
|
||||||
colorToDistrict[this.getAttribute("fill")].push(d)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.append("title") // Tooltip
|
.append("title") // Tooltip
|
||||||
@@ -197,27 +206,30 @@ function drawMap(world) {
|
|||||||
.text(d => d.properties.district);
|
.text(d => d.properties.district);
|
||||||
|
|
||||||
|
|
||||||
|
// for (const lang of languages) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Example usage
|
|
||||||
let geojson = {
|
let geojson = {
|
||||||
"type": "FeatureCollection",
|
"type": "FeatureCollection",
|
||||||
"features": colorToDistrict[hindiColor]
|
"features": languages["kannada"].districts
|
||||||
};
|
};
|
||||||
console.log(geojson)
|
|
||||||
|
|
||||||
console.log(getOuterBoundaryPolygon(geojson.features));
|
let outerBound = getOuterBoundaryPolygon(geojson.features)
|
||||||
|
outerBound["id"] = "langKannada"
|
||||||
|
outerBound.properties["lang_name"]= "Kannada"
|
||||||
|
console.log(outerBound)
|
||||||
|
console.log(JSON.stringify(outerBound, null, 2));
|
||||||
|
|
||||||
// Draw the boundary
|
// Draw the boundary
|
||||||
svg.append("path")
|
svg.append("path")
|
||||||
.datum(getOuterBoundaryPolygon(geojson.features))
|
.datum(outerBound)
|
||||||
.attr("d", path)
|
.attr("d", path)
|
||||||
.attr("fill", "none")
|
.attr("fill", "none")
|
||||||
.attr("stroke", "red")
|
.attr("stroke", "red")
|
||||||
.attr("stroke-width", 2);
|
.attr("stroke-width", 2);
|
||||||
|
|
||||||
const bbox = turf.bbox(getOuterBoundaryPolygon(geojson.features));
|
//
|
||||||
console.log("Bounding box:", bbox);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user