Skip to content
Snippets Groups Projects
Commit d0c5c08b authored by esikkala's avatar esikkala
Browse files

Use ckmeans for choropleth map

parent ca6b414e
No related branches found
No related tags found
No related merge requests found
......@@ -26836,6 +26836,11 @@
"use-debounce": "^5.1.0"
}
},
"simple-statistics": {
"version": "7.7.0",
"resolved": "https://registry.npmjs.org/simple-statistics/-/simple-statistics-7.7.0.tgz",
"integrity": "sha512-TAsZRUJ7FD/yCnm5UBgyWU7bP1gOPsw9n/dVrE8hQ+BF1zJPgDJ5X/MOnxG+HE/7nejSpJLJLdmTh7bkfsFkRw=="
},
"sisteransi": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz",
......@@ -77,6 +77,7 @@
"reselect": "^4.0.0",
"rxjs": "^7.2.0",
"simple-react-lightbox": "^3.6.4",
"simple-statistics": "^7.7.0",
"swagger-ui-express": "^4.1.6",
"victory": "^0.26.1"
},
......
......@@ -199,7 +199,7 @@ class Deck extends React.Component {
filled: true,
lineWidthMinPixels: 1,
getPolygon: d => d.polygon,
getFillColor: d => [255, 0, 0, d.instanceCountScaled],
getFillColor: d => d.choroplethColor,
getLineColor: [80, 80, 80],
getLineWidth: 1
})
......
import { has, cloneDeep } from 'lodash'
import { getTreeFromFlatData } from '@nosferatu500/react-sortable-tree'
import { ckmeans } from 'simple-statistics'
export const mapPlaces = sparqlBindings => {
const results = sparqlBindings.map(b => {
......@@ -307,8 +308,10 @@ const mergeDataItems = (itemA, itemB) => {
}
export const toPolygonLayerFormat = ({ data, config }) => {
const scaledData = linearScale({ data, config })
scaledData.forEach(item => {
// const scaledData = linearScale({ data, config })
const valuesArray = []
data.forEach(item => {
valuesArray.push(item.instanceCount)
const pointArray = item.polygon.split(' ')
const deckGlArray = pointArray.map(point => {
const latLng = point.split(',')
......@@ -319,8 +322,31 @@ export const toPolygonLayerFormat = ({ data, config }) => {
})
item.polygon = deckGlArray
})
// const example = scaledData[0].polygon[0][0]
// console.log(typeof example)
// console.log(example)
return scaledData
// Ckmeans algorithm: https://journal.r-project.org/archive/2011-2/RJournal_2011-2_Wang+Song.pdf
const clusters = ckmeans(valuesArray, 8)
data.forEach(item => {
item.choroplethColor = getChoroplethMapColor({ value: Number(item.instanceCount), clusters })
})
return data
}
const getChoroplethMapColor = ({ value, clusters }) => {
// https://colorbrewer2.org/#type=sequential&scheme=YlOrRd&n=8
const colors = [
[255, 255, 204],
[255, 237, 160],
[254, 217, 118],
[254, 178, 76],
[253, 141, 60],
[252, 78, 42],
[227, 26, 28],
[177, 0, 38]
]
let heatmapColor
colors.forEach((color, index) => {
if (value >= Number(clusters[index][0]) && value <= Number(clusters[index][clusters[index].length - 1])) {
heatmapColor = color
}
})
return heatmapColor
}
......@@ -177,9 +177,7 @@ export const backendSearchConfig = {
postprocess: {
func: toPolygonLayerFormat,
config: {
variable: 'instanceCount',
minAllowed: 0,
maxAllowed: 255
variable: 'death'
}
}
},
......
......@@ -607,4 +607,5 @@ export const choroplethQuery = `
}
GROUP BY ?id ?prefLabel ?polygon
ORDER BY desc(?instanceCount)
# LIMIT 40
`
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment