diff --git a/src/Logic/BBox.ts b/src/Logic/BBox.ts index 7faebcdff6..ecf4b319a8 100644 --- a/src/Logic/BBox.ts +++ b/src/Logic/BBox.ts @@ -37,6 +37,7 @@ export class BBox { this.minLon = Math.min(coors[0], coors[2]) this.maxLat = Math.max(coors[1], coors[3]) this.minLat = Math.min(coors[1], coors[3]) + this.check() return } this.maxLat = -90 @@ -325,6 +326,11 @@ export class BBox { console.trace("BBox with NaN detected:", this) 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) { diff --git a/src/Logic/GeoOperations.ts b/src/Logic/GeoOperations.ts index cb719760f2..15a2277c05 100644 --- a/src/Logic/GeoOperations.ts +++ b/src/Logic/GeoOperations.ts @@ -10,15 +10,16 @@ import { MultiPolygon, Point, Polygon, - Position, + Position } from "geojson" import { Tiles } from "../Models/TileRange" import { Utils } from "../Utils" -;("use strict") + +("use strict") export class GeoOperations { - private static readonly _earthRadius = 6378137 - private static readonly _originShift = (2 * Math.PI * GeoOperations._earthRadius) / 2 + private static readonly _earthRadius: number = 6378137 + 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 directionsRelative = [ "straight", @@ -480,7 +481,7 @@ export class GeoOperations { const lon = lonLat[0] const lat = lonLat[1] 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) return [x, y] } diff --git a/src/Models/TileRange.ts b/src/Models/TileRange.ts index 90c8059970..f1ec7e351f 100644 --- a/src/Models/TileRange.ts +++ b/src/Models/TileRange.ts @@ -86,7 +86,7 @@ export class Tiles { static asGeojson(zIndex: number, x?: number, y?: number): Feature { let z = zIndex 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) 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))) } - private static lon2tile(lon, zoom) { + private static lon2tile(lon: number, zoom: number): number { 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( ((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) / 2) * - Math.pow(2, zoom) + Math.pow(2, zoom) ) } }