Small refactoring of RenderingMultiPlexer

This commit is contained in:
Pieter Vander Vennet 2022-06-21 22:56:14 +02:00
parent c38bc5decf
commit 9ebdb81531

View file

@ -6,9 +6,76 @@ import {GeoOperations} from "../../GeoOperations";
import FeatureSource from "../FeatureSource"; import FeatureSource from "../FeatureSource";
import PointRenderingConfig from "../../../Models/ThemeConfig/PointRenderingConfig"; import PointRenderingConfig from "../../../Models/ThemeConfig/PointRenderingConfig";
import LayerConfig from "../../../Models/ThemeConfig/LayerConfig"; import LayerConfig from "../../../Models/ThemeConfig/LayerConfig";
import LineRenderingConfig from "../../../Models/ThemeConfig/LineRenderingConfig";
import {within} from "@turf/turf";
export default class RenderingMultiPlexerFeatureSource { export default class RenderingMultiPlexerFeatureSource {
public readonly features: UIEventSource<(any & { pointRenderingIndex: number | undefined, lineRenderingIndex: number | undefined })[]>; public readonly features: UIEventSource<(any & { pointRenderingIndex: number | undefined, lineRenderingIndex: number | undefined })[]>;
private readonly pointRenderings: { rendering: PointRenderingConfig; index: number }[];
private centroidRenderings: { rendering: PointRenderingConfig; index: number }[];
private projectedCentroidRenderings: { rendering: PointRenderingConfig; index: number }[];
private startRenderings: { rendering: PointRenderingConfig; index: number }[];
private endRenderings: { rendering: PointRenderingConfig; index: number }[];
private hasCentroid: boolean;
private lineRenderObjects: LineRenderingConfig[];
private inspectFeature(feat, addAsPoint: (feat, rendering, centerpoint: [number, number]) => void, withIndex: any[]){
if (feat.geometry.type === "Point") {
for (const rendering of this.pointRenderings) {
withIndex.push({
...feat,
pointRenderingIndex: rendering.index
})
}
} else {
// This is a a line: add the centroids
let centerpoint: [number, number] = undefined;
let projectedCenterPoint : [number, number] = undefined
if(this.hasCentroid){
centerpoint = GeoOperations.centerpointCoordinates(feat)
if(this.projectedCentroidRenderings.length > 0){
projectedCenterPoint = <[number,number]> GeoOperations.nearestPoint(feat, centerpoint).geometry.coordinates
}
}
for (const rendering of this.centroidRenderings) {
addAsPoint(feat, rendering, centerpoint)
}
if (feat.geometry.type === "LineString") {
for (const rendering of this.projectedCentroidRenderings) {
addAsPoint(feat, rendering, projectedCenterPoint)
}
// Add start- and endpoints
const coordinates = feat.geometry.coordinates
for (const rendering of this.startRenderings) {
addAsPoint(feat, rendering, coordinates[0])
}
for (const rendering of this.endRenderings) {
const coordinate = coordinates[coordinates.length - 1]
addAsPoint(feat, rendering, coordinate)
}
}else{
for (const rendering of this.projectedCentroidRenderings) {
addAsPoint(feat, rendering, centerpoint)
}
}
// AT last, add it 'as is' to what we should render
for (let i = 0; i < this.lineRenderObjects.length; i++) {
withIndex.push({
...feat,
lineRenderingIndex: i
})
}
}
}
constructor(upstream: FeatureSource, layer: LayerConfig) { constructor(upstream: FeatureSource, layer: LayerConfig) {
@ -16,13 +83,13 @@ export default class RenderingMultiPlexerFeatureSource {
rendering: r, rendering: r,
index: i index: i
})) }))
const pointRenderings = pointRenderObjects.filter(r => r.rendering.location.has("point")) this.pointRenderings = pointRenderObjects.filter(r => r.rendering.location.has("point"))
const centroidRenderings = pointRenderObjects.filter(r => r.rendering.location.has("centroid")) this.centroidRenderings = pointRenderObjects.filter(r => r.rendering.location.has("centroid"))
const projectedCentroidRenderings = pointRenderObjects.filter(r => r.rendering.location.has("projected_centerpoint")) this.projectedCentroidRenderings = pointRenderObjects.filter(r => r.rendering.location.has("projected_centerpoint"))
const startRenderings = pointRenderObjects.filter(r => r.rendering.location.has("start")) this.startRenderings = pointRenderObjects.filter(r => r.rendering.location.has("start"))
const endRenderings = pointRenderObjects.filter(r => r.rendering.location.has("end")) this.endRenderings = pointRenderObjects.filter(r => r.rendering.location.has("end"))
const hasCentroid = centroidRenderings.length > 0 || projectedCentroidRenderings.length > 0 this.hasCentroid = this.centroidRenderings.length > 0 || this.projectedCentroidRenderings.length > 0
const lineRenderObjects = layer.lineRendering this.lineRenderObjects = layer.lineRendering
this.features = upstream.features.map( this.features = upstream.features.map(
features => { features => {
@ -31,8 +98,7 @@ export default class RenderingMultiPlexerFeatureSource {
} }
const withIndex: (any & { pointRenderingIndex: number | undefined, lineRenderingIndex: number | undefined, multiLineStringIndex: number | undefined })[] = []; const withIndex: any[] = [];
function addAsPoint(feat, rendering, coordinate) { function addAsPoint(feat, rendering, coordinate) {
const patched = { const patched = {
@ -46,62 +112,9 @@ export default class RenderingMultiPlexerFeatureSource {
withIndex.push(patched) withIndex.push(patched)
} }
for (const f of features) { for (const f of features) {
const feat = f.feature; this.inspectFeature(f.feature, addAsPoint, withIndex)
if (feat.geometry.type === "Point") {
for (const rendering of pointRenderings) {
withIndex.push({
...feat,
pointRenderingIndex: rendering.index
})
}
} else {
// This is a a line: add the centroids
let centerpoint: [number, number] = undefined;
let projectedCenterPoint : [number, number] = undefined
if(hasCentroid){
centerpoint = GeoOperations.centerpointCoordinates(feat)
if(projectedCentroidRenderings.length > 0){
projectedCenterPoint = <[number,number]> GeoOperations.nearestPoint(feat, centerpoint).geometry.coordinates
}
}
for (const rendering of centroidRenderings) {
addAsPoint(feat, rendering, centerpoint)
}
if (feat.geometry.type === "LineString") {
for (const rendering of projectedCentroidRenderings) {
addAsPoint(feat, rendering, projectedCenterPoint)
}
// Add start- and endpoints
const coordinates = feat.geometry.coordinates
for (const rendering of startRenderings) {
addAsPoint(feat, rendering, coordinates[0])
}
for (const rendering of endRenderings) {
const coordinate = coordinates[coordinates.length - 1]
addAsPoint(feat, rendering, coordinate)
}
}else{
for (const rendering of projectedCentroidRenderings) {
addAsPoint(feat, rendering, centerpoint)
}
}
// AT last, add it 'as is' to what we should render
for (let i = 0; i < lineRenderObjects.length; i++) {
withIndex.push({
...feat,
lineRenderingIndex: i
})
}
}
} }