Chore: improve types, add checks

This commit is contained in:
Pieter Vander Vennet 2025-04-03 02:12:59 +02:00
parent 6e4879fb0d
commit 4b3e1fad4f
3 changed files with 17 additions and 10 deletions

View file

@ -37,6 +37,7 @@ export class BBox {
this.minLon = Math.min(coors[0], coors[2]) this.minLon = Math.min(coors[0], coors[2])
this.maxLat = Math.max(coors[1], coors[3]) this.maxLat = Math.max(coors[1], coors[3])
this.minLat = Math.min(coors[1], coors[3]) this.minLat = Math.min(coors[1], coors[3])
this.check()
return return
} }
this.maxLat = -90 this.maxLat = -90
@ -325,6 +326,11 @@ export class BBox {
console.trace("BBox with NaN detected:", this) console.trace("BBox with NaN detected:", this)
throw "BBOX has NAN" throw "BBOX has NAN"
} }
if (this.minLat < -90 || this.maxLat > 90) {
const msg = "Invalid BBOX detected: latitude is out of range. Did you swap lat & lon somewhere? min:" + this.minLat + "; max:" + this.maxLat
console.trace(msg)
throw msg
}
} }
public overlapsWithFeature(f: Feature) { public overlapsWithFeature(f: Feature) {

View file

@ -10,15 +10,16 @@ import {
MultiPolygon, MultiPolygon,
Point, Point,
Polygon, Polygon,
Position, Position
} from "geojson" } from "geojson"
import { Tiles } from "../Models/TileRange" import { Tiles } from "../Models/TileRange"
import { Utils } from "../Utils" import { Utils } from "../Utils"
;("use strict")
("use strict")
export class GeoOperations { export class GeoOperations {
private static readonly _earthRadius = 6378137 private static readonly _earthRadius: number = 6378137
private static readonly _originShift = (2 * Math.PI * GeoOperations._earthRadius) / 2 private static readonly _originShift: number = (2 * Math.PI * GeoOperations._earthRadius) / 2
private static readonly directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"] as const private static readonly directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"] as const
private static readonly directionsRelative = [ private static readonly directionsRelative = [
"straight", "straight",
@ -480,7 +481,7 @@ export class GeoOperations {
const lon = lonLat[0] const lon = lonLat[0]
const lat = lonLat[1] const lat = lonLat[1]
const x = (180 * lon) / GeoOperations._originShift const x = (180 * lon) / GeoOperations._originShift
let y = (180 * lat) / GeoOperations._originShiftcons let y = (180 * lat) / GeoOperations._originShift
y = (180 / Math.PI) * (2 * Math.atan(Math.exp((y * Math.PI) / 180)) - Math.PI / 2) y = (180 / Math.PI) * (2 * Math.atan(Math.exp((y * Math.PI) / 180)) - Math.PI / 2)
return [x, y] return [x, y]
} }

View file

@ -86,7 +86,7 @@ export class Tiles {
static asGeojson(zIndex: number, x?: number, y?: number): Feature<Polygon> { static asGeojson(zIndex: number, x?: number, y?: number): Feature<Polygon> {
let z = zIndex let z = zIndex
if (x === undefined) { if (x === undefined) {
;[z, x, y] = Tiles.tile_from_index(zIndex) [z, x, y] = Tiles.tile_from_index(zIndex)
} }
const bounds = Tiles.tile_bounds_lon_lat(z, x, y) const bounds = Tiles.tile_bounds_lon_lat(z, x, y)
return new BBox(bounds).asGeoJson() return new BBox(bounds).asGeoJson()
@ -154,17 +154,17 @@ export class Tiles {
return (180 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))) return (180 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)))
} }
private static lon2tile(lon, zoom) { private static lon2tile(lon: number, zoom: number): number {
return Math.floor(((lon + 180) / 360) * Math.pow(2, zoom)) return Math.floor(((lon + 180) / 360) * Math.pow(2, zoom))
} }
private static lat2tile(lat, zoom) { private static lat2tile(lat: number, zoom: number): number {
return Math.floor( return Math.floor(
((1 - ((1 -
Math.log(Math.tan((lat * Math.PI) / 180) + 1 / Math.cos((lat * Math.PI) / 180)) / Math.log(Math.tan((lat * Math.PI) / 180) + 1 / Math.cos((lat * Math.PI) / 180)) /
Math.PI) / Math.PI) /
2) * 2) *
Math.pow(2, zoom) Math.pow(2, zoom)
) )
} }
} }