LayerServer: first version which can use a local MVT-server

This commit is contained in:
Pieter Vander Vennet 2024-01-22 01:42:05 +01:00
parent 35228daa8f
commit ef2f1487c6
17 changed files with 1009 additions and 82 deletions

View file

@ -7,6 +7,7 @@ import { AllSharedLayers } from "../../src/Customizations/AllSharedLayers"
import fs from "fs"
import { Or } from "../../src/Logic/Tags/Or"
import { RegexTag } from "../../src/Logic/Tags/RegexTag"
import { Utils } from "../../src/Utils"
class LuaSnippets{
/**
@ -40,18 +41,24 @@ class GenerateLayerLua {
}
public functionName(){
const l = this._layer
if(!l.source?.osmTags){
return undefined
}
return `process_poi_${l.id}`
}
public generateFunction(): string {
const l = this._layer
if(!l.source?.osmTags){
return undefined
}
return [
`local pois_${l.id} = osm2pgsql.define_table({`,
` name = '${l.id}',`,
" ids = { type = 'any', type_column = 'osm_type', id_column = 'osm_id' },",
" columns = {",
" { column = 'tags', type = 'jsonb' },",
" { column = 'geom', type = 'point', not_null = true },",
" { column = 'geom', type = 'point', projection = 4326, not_null = true },",
" }" +
"})",
"",
@ -117,14 +124,13 @@ class GenerateLayerFile extends Script {
}
async main(args: string[]) {
let dw = AllSharedLayers.sharedLayers.get("drinking_water")
let t = AllSharedLayers.sharedLayers.get("toilet")
const layerNames = Array.from(AllSharedLayers.sharedLayers.values())
const generators = [dw, t].map(l => new GenerateLayerLua(l))
const generators = layerNames.map(l => new GenerateLayerLua(l))
const script = [
...generators.map(g => g.generateFunction()),
LuaSnippets.combine(generators.map(g => g.functionName())),
LuaSnippets.combine(Utils.NoNull(generators.map(g => g.functionName()))),
LuaSnippets.tail
].join("\n\n\n")
const path = "build_db.lua"

View file

@ -0,0 +1,44 @@
import { BBox } from "../../src/Logic/BBox"
import { Client } from "pg"
/**
* Connects with a Postgis database, gives back how much items there are within the given BBOX
*/
export default class TilecountServer {
private readonly _client: Client
private isConnected = false
constructor(connectionString: string) {
this._client = new Client(connectionString)
}
async getCount(layer: string, bbox: BBox = undefined): Promise<number> {
if (!this.isConnected) {
await this._client.connect()
this.isConnected = true
}
let query = "SELECT COUNT(*) FROM " + layer
if(bbox){
query += ` WHERE ST_MakeEnvelope (${bbox.minLon}, ${bbox.minLat}, ${bbox.maxLon}, ${bbox.maxLat}, 4326) ~ geom`
}
console.log(query)
const result = await this._client.query(query)
return result.rows[0].count
}
disconnect() {
this._client.end()
}
}
const tcs = new TilecountServer("postgresql://user:none@localhost:5444/osm-poi")
console.log(">>>", await tcs.getCount("drinking_water", new BBox([
[1.5052013991654007,
42.57480750272123,
], [
1.6663677350703097,
42.499856652770745,
]])))
tcs.disconnect()