forked from MapComplete/MapComplete
refactoring: more state splitting, basic layoutFeatureSource
This commit is contained in:
parent
8e2f04c0d0
commit
b94a8f5745
54 changed files with 1067 additions and 1969 deletions
|
@ -1,59 +1,53 @@
|
|||
/**
|
||||
* Fetches a geojson file somewhere and passes it along
|
||||
*/
|
||||
import { UIEventSource } from "../../UIEventSource"
|
||||
import FilteredLayer from "../../../Models/FilteredLayer"
|
||||
import { Store, UIEventSource } from "../../UIEventSource"
|
||||
import { Utils } from "../../../Utils"
|
||||
import { FeatureSourceForLayer, Tiled } from "../FeatureSource"
|
||||
import { Tiles } from "../../../Models/TileRange"
|
||||
import FeatureSource from "../FeatureSource"
|
||||
import { BBox } from "../../BBox"
|
||||
import { GeoOperations } from "../../GeoOperations"
|
||||
import { Feature } from "geojson"
|
||||
import LayerConfig from "../../../Models/ThemeConfig/LayerConfig"
|
||||
import { Tiles } from "../../../Models/TileRange"
|
||||
|
||||
export default class GeoJsonSource implements FeatureSourceForLayer, Tiled {
|
||||
public readonly features: UIEventSource<Feature[]>
|
||||
public readonly state = new UIEventSource<undefined | { error: string } | "loaded">(undefined)
|
||||
public readonly name
|
||||
public readonly isOsmCache: boolean
|
||||
public readonly layer: FilteredLayer
|
||||
public readonly tileIndex
|
||||
public readonly bbox
|
||||
export default class GeoJsonSource implements FeatureSource {
|
||||
public readonly features: Store<Feature[]>
|
||||
private readonly seenids: Set<string>
|
||||
private readonly idKey?: string
|
||||
|
||||
public constructor(
|
||||
flayer: FilteredLayer,
|
||||
zxy?: [number, number, number] | BBox,
|
||||
layer: LayerConfig,
|
||||
options?: {
|
||||
zxy?: number | [number, number, number] | BBox
|
||||
featureIdBlacklist?: Set<string>
|
||||
isActive?: Store<boolean>
|
||||
}
|
||||
) {
|
||||
if (flayer.layerDef.source.geojsonZoomLevel !== undefined && zxy === undefined) {
|
||||
if (layer.source.geojsonZoomLevel !== undefined && options?.zxy === undefined) {
|
||||
throw "Dynamic layers are not supported. Use 'DynamicGeoJsonTileSource instead"
|
||||
}
|
||||
|
||||
this.layer = flayer
|
||||
this.idKey = flayer.layerDef.source.idKey
|
||||
this.idKey = layer.source.idKey
|
||||
this.seenids = options?.featureIdBlacklist ?? new Set<string>()
|
||||
let url = flayer.layerDef.source.geojsonSource.replace("{layer}", flayer.layerDef.id)
|
||||
let url = layer.source.geojsonSource.replace("{layer}", layer.id)
|
||||
let zxy = options?.zxy
|
||||
if (zxy !== undefined) {
|
||||
let tile_bbox: BBox
|
||||
if (typeof zxy === "number") {
|
||||
zxy = Tiles.tile_from_index(zxy)
|
||||
}
|
||||
if (zxy instanceof BBox) {
|
||||
tile_bbox = zxy
|
||||
} else {
|
||||
const [z, x, y] = zxy
|
||||
tile_bbox = BBox.fromTile(z, x, y)
|
||||
|
||||
this.tileIndex = Tiles.tile_index(z, x, y)
|
||||
this.bbox = BBox.fromTile(z, x, y)
|
||||
url = url
|
||||
.replace("{z}", "" + z)
|
||||
.replace("{x}", "" + x)
|
||||
.replace("{y}", "" + y)
|
||||
}
|
||||
let bounds: { minLat: number; maxLat: number; minLon: number; maxLon: number } =
|
||||
tile_bbox
|
||||
if (this.layer.layerDef.source.mercatorCrs) {
|
||||
let bounds: Record<"minLat" | "maxLat" | "minLon" | "maxLon", number> = tile_bbox
|
||||
if (layer.source.mercatorCrs) {
|
||||
bounds = tile_bbox.toMercator()
|
||||
}
|
||||
|
||||
|
@ -62,103 +56,83 @@ export default class GeoJsonSource implements FeatureSourceForLayer, Tiled {
|
|||
.replace("{y_max}", "" + bounds.maxLat)
|
||||
.replace("{x_min}", "" + bounds.minLon)
|
||||
.replace("{x_max}", "" + bounds.maxLon)
|
||||
} else {
|
||||
this.tileIndex = Tiles.tile_index(0, 0, 0)
|
||||
this.bbox = BBox.global
|
||||
}
|
||||
|
||||
this.name = "GeoJsonSource of " + url
|
||||
|
||||
this.isOsmCache = flayer.layerDef.source.isOsmCacheLayer
|
||||
this.features = new UIEventSource<Feature[]>([])
|
||||
this.LoadJSONFrom(url)
|
||||
const eventsource = new UIEventSource<Feature[]>(undefined)
|
||||
if (options?.isActive !== undefined) {
|
||||
options.isActive.addCallbackAndRunD(async (active) => {
|
||||
if (!active) {
|
||||
return
|
||||
}
|
||||
this.LoadJSONFrom(url, eventsource, layer)
|
||||
.then((_) => console.log("Loaded geojson " + url))
|
||||
.catch((err) => console.error("Could not load ", url, "due to", err))
|
||||
return true
|
||||
})
|
||||
} else {
|
||||
this.LoadJSONFrom(url, eventsource, layer)
|
||||
.then((_) => console.log("Loaded geojson " + url))
|
||||
.catch((err) => console.error("Could not load ", url, "due to", err))
|
||||
}
|
||||
this.features = eventsource
|
||||
}
|
||||
|
||||
private LoadJSONFrom(url: string) {
|
||||
const eventSource = this.features
|
||||
private async LoadJSONFrom(
|
||||
url: string,
|
||||
eventSource: UIEventSource<Feature[]>,
|
||||
layer: LayerConfig
|
||||
): Promise<void> {
|
||||
const self = this
|
||||
Utils.downloadJsonCached(url, 60 * 60)
|
||||
.then((json) => {
|
||||
self.state.setData("loaded")
|
||||
// TODO: move somewhere else, just for testing
|
||||
// Check for maproulette data
|
||||
if (url.startsWith("https://maproulette.org/api/v2/tasks/box/")) {
|
||||
console.log("MapRoulette data detected")
|
||||
const data = json
|
||||
let maprouletteFeatures: any[] = []
|
||||
data.forEach((element) => {
|
||||
maprouletteFeatures.push({
|
||||
type: "Feature",
|
||||
geometry: {
|
||||
type: "Point",
|
||||
coordinates: [element.point.lng, element.point.lat],
|
||||
},
|
||||
properties: {
|
||||
// Map all properties to the feature
|
||||
...element,
|
||||
},
|
||||
})
|
||||
})
|
||||
json.features = maprouletteFeatures
|
||||
let json = await Utils.downloadJsonCached(url, 60 * 60)
|
||||
|
||||
if (json.features === undefined || json.features === null) {
|
||||
json.features = []
|
||||
}
|
||||
|
||||
if (layer.source.mercatorCrs) {
|
||||
json = GeoOperations.GeoJsonToWGS84(json)
|
||||
}
|
||||
|
||||
const time = new Date()
|
||||
const newFeatures: Feature[] = []
|
||||
let i = 0
|
||||
let skipped = 0
|
||||
for (const feature of json.features) {
|
||||
const props = feature.properties
|
||||
for (const key in props) {
|
||||
if (props[key] === null) {
|
||||
delete props[key]
|
||||
}
|
||||
|
||||
if (json.features === undefined || json.features === null) {
|
||||
return
|
||||
if (typeof props[key] !== "string") {
|
||||
// Make sure all the values are string, it crashes stuff otherwise
|
||||
props[key] = JSON.stringify(props[key])
|
||||
}
|
||||
}
|
||||
|
||||
if (self.layer.layerDef.source.mercatorCrs) {
|
||||
json = GeoOperations.GeoJsonToWGS84(json)
|
||||
}
|
||||
if (self.idKey !== undefined) {
|
||||
props.id = props[self.idKey]
|
||||
}
|
||||
|
||||
const time = new Date()
|
||||
const newFeatures: Feature[] = []
|
||||
let i = 0
|
||||
let skipped = 0
|
||||
for (const feature of json.features) {
|
||||
const props = feature.properties
|
||||
for (const key in props) {
|
||||
if (props[key] === null) {
|
||||
delete props[key]
|
||||
}
|
||||
if (props.id === undefined) {
|
||||
props.id = url + "/" + i
|
||||
feature.id = url + "/" + i
|
||||
i++
|
||||
}
|
||||
if (self.seenids.has(props.id)) {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
self.seenids.add(props.id)
|
||||
|
||||
if (typeof props[key] !== "string") {
|
||||
// Make sure all the values are string, it crashes stuff otherwise
|
||||
props[key] = JSON.stringify(props[key])
|
||||
}
|
||||
}
|
||||
let freshness: Date = time
|
||||
if (feature.properties["_last_edit:timestamp"] !== undefined) {
|
||||
freshness = new Date(props["_last_edit:timestamp"])
|
||||
}
|
||||
|
||||
if (self.idKey !== undefined) {
|
||||
props.id = props[self.idKey]
|
||||
}
|
||||
newFeatures.push(feature)
|
||||
}
|
||||
|
||||
if (props.id === undefined) {
|
||||
props.id = url + "/" + i
|
||||
feature.id = url + "/" + i
|
||||
i++
|
||||
}
|
||||
if (self.seenids.has(props.id)) {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
self.seenids.add(props.id)
|
||||
|
||||
let freshness: Date = time
|
||||
if (feature.properties["_last_edit:timestamp"] !== undefined) {
|
||||
freshness = new Date(props["_last_edit:timestamp"])
|
||||
}
|
||||
|
||||
newFeatures.push(feature)
|
||||
}
|
||||
|
||||
if (newFeatures.length == 0) {
|
||||
return
|
||||
}
|
||||
|
||||
eventSource.setData(eventSource.data.concat(newFeatures))
|
||||
})
|
||||
.catch((msg) => {
|
||||
console.debug("Could not load geojson layer", url, "due to", msg)
|
||||
self.state.setData({ error: msg })
|
||||
})
|
||||
eventSource.setData(newFeatures)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue