Skip to content

Commit

Permalink
Merge pull request #174 from wearepal/os-greenspaces
Browse files Browse the repository at this point in the history
caching and naming for green spaces and census comp
  • Loading branch information
paulthatjazz authored Oct 19, 2023
2 parents 34edeee + 292635c commit 292e950
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 82 deletions.
106 changes: 57 additions & 49 deletions app/javascript/projects/modelling/components/census_component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ async function fetchCensusShapefilesFromExtent(bbox: string){
}

export class CensusComponent extends BaseComponent {
DataCache: Map<any, any> // CACHING WIP
cachedData : NumericTileGrid | undefined

constructor() {
super("UK Census - Highest Education")
this.category = "Inputs"
this.cachedData = undefined
}

async builder(node: Node) {
Expand All @@ -101,60 +102,67 @@ export class CensusComponent extends BaseComponent {
const editorNode = this.editor?.nodes.find(n => n.id === node.id)
if (editorNode === undefined) { return }

const zoom = 20
if(this.cachedData){
editorNode.meta.output = outputs['out'] = this.cachedData
}else{
const zoom = 20

const shapeFiles = await fetchCensusShapefilesFromExtent(currentBbox)
const features = new GeoJSON().readFeatures(shapeFiles)
const shapeFiles = await fetchCensusShapefilesFromExtent(currentBbox)
const features = new GeoJSON().readFeatures(shapeFiles)

const tileGrid = createXYZ()
const outputTileRange = tileGrid.getTileRangeForExtentAndZ(currentExtent, zoom)
const tileGrid = createXYZ()
const outputTileRange = tileGrid.getTileRangeForExtentAndZ(currentExtent, zoom)

const result = editorNode.meta.output = outputs['out'] =new NumericTileGrid(
zoom,
outputTileRange.minX,
outputTileRange.minY,
outputTileRange.getWidth(),
outputTileRange.getHeight()
)

let index = node.data.censusOptionId as number
if (index === undefined) { index = 0 }

for (let feature of features) {

let n =
+feature.get("output_modified_HIGHEST_QUAL_0") +
+feature.get("output_modified_HIGHEST_QUAL_1") +
+feature.get("output_modified_HIGHEST_QUAL_2") +
+feature.get("output_modified_HIGHEST_QUAL_3") +
+feature.get("output_modified_HIGHEST_QUAL_4") +
+feature.get("output_modified_HIGHEST_QUAL_5") +
+feature.get("output_modified_HIGHEST_QUAL_6") +
+feature.get("output_modified_HIGHEST_QUAL_-8")

const geom = feature.getGeometry()
if (geom === undefined) { continue }

const featureTileRange = tileGrid.getTileRangeForExtentAndZ(
geom.getExtent(),
zoom
const result = editorNode.meta.output = outputs['out'] =new NumericTileGrid(
zoom,
outputTileRange.minX,
outputTileRange.minY,
outputTileRange.getWidth(),
outputTileRange.getHeight()
)
for (
let x = Math.max(featureTileRange.minX, outputTileRange.minX);
x <= Math.min(featureTileRange.maxX, outputTileRange.maxX);
++x
) {
for (
let y = Math.max(featureTileRange.minY, outputTileRange.minY);
y <= Math.min(featureTileRange.maxY, outputTileRange.maxY);
++y
) {
const center = tileGrid.getTileCoordCenter([zoom, x, y])
if (geom.intersectsCoordinate(center)) {
result.set(x, y, feature.get(censusOptions[index].code) / n * 100)

let index = node.data.censusOptionId as number
if (index === undefined) { index = 0 }

for (let feature of features) {

let n =
+feature.get("output_modified_HIGHEST_QUAL_0") +
+feature.get("output_modified_HIGHEST_QUAL_1") +
+feature.get("output_modified_HIGHEST_QUAL_2") +
+feature.get("output_modified_HIGHEST_QUAL_3") +
+feature.get("output_modified_HIGHEST_QUAL_4") +
+feature.get("output_modified_HIGHEST_QUAL_5") +
+feature.get("output_modified_HIGHEST_QUAL_6") +
+feature.get("output_modified_HIGHEST_QUAL_-8")

const geom = feature.getGeometry()
if (geom === undefined) { continue }

const featureTileRange = tileGrid.getTileRangeForExtentAndZ(
geom.getExtent(),
zoom
)
for (
let x = Math.max(featureTileRange.minX, outputTileRange.minX);
x <= Math.min(featureTileRange.maxX, outputTileRange.maxX);
++x
) {
for (
let y = Math.max(featureTileRange.minY, outputTileRange.minY);
y <= Math.min(featureTileRange.maxY, outputTileRange.maxY);
++y
) {
const center = tileGrid.getTileCoordCenter([zoom, x, y])
if (geom.intersectsCoordinate(center)) {
result.set(x, y, feature.get(censusOptions[index].code) / n * 100)
}
}
}
}
}

this.cachedData = result

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ async function fetchGreenspacesFromExtent(bbox: string){
}

export class OSGreenSpacesComponent extends BaseComponent {
cachedData: BooleanTileGrid | undefined

constructor(){
super('OS Green Spaces')
this.category = 'Inputs'
this.cachedData = undefined
}

async builder(node: Node) {
Expand All @@ -43,48 +45,61 @@ export class OSGreenSpacesComponent extends BaseComponent {
const editorNode = this.editor?.nodes.find(n => n.id === node.id)
if (editorNode === undefined) { return }

const res = await fetchGreenspacesFromExtent(currentBbox)
const features = new GeoJSON().readFeatures(res)
if(this.cachedData){
editorNode.meta.output = outputs['out'] = this.cachedData
}else{

const tileGrid = createXYZ()
const outputTileRange = tileGrid.getTileRangeForExtentAndZ(currentExtent, zoom)
const res = await fetchGreenspacesFromExtent(currentBbox)
const features = new GeoJSON().readFeatures(res)

const result = editorNode.meta.output = outputs['out'] =new BooleanTileGrid(
zoom,
outputTileRange.minX,
outputTileRange.minY,
outputTileRange.getWidth(),
outputTileRange.getHeight()
)

for (let feature of features) {
const tileGrid = createXYZ()
const outputTileRange = tileGrid.getTileRangeForExtentAndZ(currentExtent, zoom)

const geom = feature.getGeometry()
if (geom === undefined) { continue }

const featureTileRange = tileGrid.getTileRangeForExtentAndZ(
geom.getExtent(),
zoom
const result = editorNode.meta.output = outputs['out'] =new BooleanTileGrid(
zoom,
outputTileRange.minX,
outputTileRange.minY,
outputTileRange.getWidth(),
outputTileRange.getHeight()
)
for (
let x = Math.max(featureTileRange.minX, outputTileRange.minX);
x <= Math.min(featureTileRange.maxX, outputTileRange.maxX);
++x
) {
for (
let y = Math.max(featureTileRange.minY, outputTileRange.minY);
y <= Math.min(featureTileRange.maxY, outputTileRange.maxY);
++y
) {
const center = tileGrid.getTileCoordCenter([zoom, x, y])
if (geom.intersectsCoordinate(center)) {
result.set(x, y, true)

result.name = "OS Green Spaces"

for (let feature of features) {

const geom = feature.getGeometry()
if (geom === undefined) { continue }

const featureTileRange = tileGrid.getTileRangeForExtentAndZ(
geom.getExtent(),
zoom
)
for (
let x = Math.max(featureTileRange.minX, outputTileRange.minX);
x <= Math.min(featureTileRange.maxX, outputTileRange.maxX);
++x
) {
for (
let y = Math.max(featureTileRange.minY, outputTileRange.minY);
y <= Math.min(featureTileRange.maxY, outputTileRange.maxY);
++y
) {
const center = tileGrid.getTileCoordCenter([zoom, x, y])
if (geom.intersectsCoordinate(center)) {
result.set(x, y, true)
}
}
}
}
}

this.cachedData = result
}


editorNode.update()



}

}

0 comments on commit 292e950

Please sign in to comment.