forked from MapComplete/MapComplete
Reformat all files with prettier
This commit is contained in:
parent
e22d189376
commit
b541d3eab4
382 changed files with 50893 additions and 35566 deletions
172
Logic/BBox.ts
172
Logic/BBox.ts
|
@ -1,31 +1,32 @@
|
|||
import * as turf from "@turf/turf";
|
||||
import {TileRange, Tiles} from "../Models/TileRange";
|
||||
import {GeoOperations} from "./GeoOperations";
|
||||
import * as turf from "@turf/turf"
|
||||
import { TileRange, Tiles } from "../Models/TileRange"
|
||||
import { GeoOperations } from "./GeoOperations"
|
||||
|
||||
export class BBox {
|
||||
|
||||
static global: BBox = new BBox([[-180, -90], [180, 90]]);
|
||||
readonly maxLat: number;
|
||||
readonly maxLon: number;
|
||||
readonly minLat: number;
|
||||
readonly minLon: number;
|
||||
static global: BBox = new BBox([
|
||||
[-180, -90],
|
||||
[180, 90],
|
||||
])
|
||||
readonly maxLat: number
|
||||
readonly maxLon: number
|
||||
readonly minLat: number
|
||||
readonly minLon: number
|
||||
|
||||
/***
|
||||
* Coordinates should be [[lon, lat],[lon, lat]]
|
||||
* @param coordinates
|
||||
*/
|
||||
constructor(coordinates) {
|
||||
this.maxLat = -90;
|
||||
this.maxLon = -180;
|
||||
this.minLat = 90;
|
||||
this.minLon = 180;
|
||||
|
||||
this.maxLat = -90
|
||||
this.maxLon = -180
|
||||
this.minLat = 90
|
||||
this.minLon = 180
|
||||
|
||||
for (const coordinate of coordinates) {
|
||||
this.maxLon = Math.max(this.maxLon, coordinate[0]);
|
||||
this.maxLat = Math.max(this.maxLat, coordinate[1]);
|
||||
this.minLon = Math.min(this.minLon, coordinate[0]);
|
||||
this.minLat = Math.min(this.minLat, coordinate[1]);
|
||||
this.maxLon = Math.max(this.maxLon, coordinate[0])
|
||||
this.maxLat = Math.max(this.maxLat, coordinate[1])
|
||||
this.minLon = Math.min(this.minLon, coordinate[0])
|
||||
this.minLat = Math.min(this.minLat, coordinate[1])
|
||||
}
|
||||
|
||||
this.maxLon = Math.min(this.maxLon, 180)
|
||||
|
@ -33,27 +34,32 @@ export class BBox {
|
|||
this.minLon = Math.max(this.minLon, -180)
|
||||
this.minLat = Math.max(this.minLat, -90)
|
||||
|
||||
|
||||
this.check();
|
||||
this.check()
|
||||
}
|
||||
|
||||
static fromLeafletBounds(bounds) {
|
||||
return new BBox([[bounds.getWest(), bounds.getNorth()], [bounds.getEast(), bounds.getSouth()]])
|
||||
return new BBox([
|
||||
[bounds.getWest(), bounds.getNorth()],
|
||||
[bounds.getEast(), bounds.getSouth()],
|
||||
])
|
||||
}
|
||||
|
||||
static get(feature): BBox {
|
||||
if (feature.bbox?.overlapsWith === undefined) {
|
||||
const turfBbox: number[] = turf.bbox(feature)
|
||||
feature.bbox = new BBox([[turfBbox[0], turfBbox[1]], [turfBbox[2], turfBbox[3]]]);
|
||||
feature.bbox = new BBox([
|
||||
[turfBbox[0], turfBbox[1]],
|
||||
[turfBbox[2], turfBbox[3]],
|
||||
])
|
||||
}
|
||||
return feature.bbox;
|
||||
return feature.bbox
|
||||
}
|
||||
|
||||
static bboxAroundAll(bboxes: BBox[]): BBox {
|
||||
let maxLat: number = -90;
|
||||
let maxLon: number = -180;
|
||||
let minLat: number = 80;
|
||||
let minLon: number = 180;
|
||||
let maxLat: number = -90
|
||||
let maxLon: number = -180
|
||||
let minLat: number = 80
|
||||
let minLon: number = 180
|
||||
|
||||
for (const bbox of bboxes) {
|
||||
maxLat = Math.max(maxLat, bbox.maxLat)
|
||||
|
@ -61,17 +67,20 @@ export class BBox {
|
|||
minLat = Math.min(minLat, bbox.minLat)
|
||||
minLon = Math.min(minLon, bbox.minLon)
|
||||
}
|
||||
return new BBox([[maxLon, maxLat], [minLon, minLat]])
|
||||
return new BBox([
|
||||
[maxLon, maxLat],
|
||||
[minLon, minLat],
|
||||
])
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the BBox based on a slippy map tile number
|
||||
*
|
||||
*
|
||||
* const bbox = BBox.fromTile(16, 32754, 21785)
|
||||
* bbox.minLon // => -0.076904296875
|
||||
* bbox.maxLon // => -0.0714111328125
|
||||
* bbox.minLat // => 51.5292513551899
|
||||
* bbox.maxLat // => 51.53266860674158
|
||||
* bbox.minLon // => -0.076904296875
|
||||
* bbox.maxLon // => -0.0714111328125
|
||||
* bbox.minLat // => 51.5292513551899
|
||||
* bbox.maxLat // => 51.53266860674158
|
||||
*/
|
||||
static fromTile(z: number, x: number, y: number): BBox {
|
||||
return new BBox(Tiles.tile_bounds_lon_lat(z, x, y))
|
||||
|
@ -85,11 +94,10 @@ export class BBox {
|
|||
}
|
||||
|
||||
public unionWith(other: BBox) {
|
||||
return new BBox([[
|
||||
Math.max(this.maxLon, other.maxLon),
|
||||
Math.max(this.maxLat, other.maxLat)],
|
||||
[Math.min(this.minLon, other.minLon),
|
||||
Math.min(this.minLat, other.minLat)]])
|
||||
return new BBox([
|
||||
[Math.max(this.maxLon, other.maxLon), Math.max(this.maxLat, other.maxLat)],
|
||||
[Math.min(this.minLon, other.minLon), Math.min(this.minLat, other.minLat)],
|
||||
])
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,32 +110,31 @@ export class BBox {
|
|||
|
||||
public overlapsWith(other: BBox) {
|
||||
if (this.maxLon < other.minLon) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
if (this.maxLat < other.minLat) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
if (this.minLon > other.maxLon) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
return this.minLat <= other.maxLat;
|
||||
|
||||
return this.minLat <= other.maxLat
|
||||
}
|
||||
|
||||
public isContainedIn(other: BBox) {
|
||||
if (this.maxLon > other.maxLon) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
if (this.maxLat > other.maxLat) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
if (this.minLon < other.minLon) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
if (this.minLat < other.minLat) {
|
||||
return false
|
||||
}
|
||||
return true;
|
||||
return true
|
||||
}
|
||||
|
||||
getEast() {
|
||||
|
@ -147,32 +154,35 @@ export class BBox {
|
|||
}
|
||||
|
||||
contains(lonLat: [number, number]) {
|
||||
return this.minLat <= lonLat[1] && lonLat[1] <= this.maxLat
|
||||
&& this.minLon <= lonLat[0] && lonLat[0] <= this.maxLon
|
||||
return (
|
||||
this.minLat <= lonLat[1] &&
|
||||
lonLat[1] <= this.maxLat &&
|
||||
this.minLon <= lonLat[0] &&
|
||||
lonLat[0] <= this.maxLon
|
||||
)
|
||||
}
|
||||
|
||||
pad(factor: number, maxIncrease = 2): BBox {
|
||||
|
||||
const latDiff = Math.min(maxIncrease / 2, Math.abs(this.maxLat - this.minLat) * factor)
|
||||
const lonDiff = Math.min(maxIncrease / 2, Math.abs(this.maxLon - this.minLon) * factor)
|
||||
return new BBox([[
|
||||
this.minLon - lonDiff,
|
||||
this.minLat - latDiff
|
||||
], [this.maxLon + lonDiff,
|
||||
this.maxLat + latDiff]])
|
||||
return new BBox([
|
||||
[this.minLon - lonDiff, this.minLat - latDiff],
|
||||
[this.maxLon + lonDiff, this.maxLat + latDiff],
|
||||
])
|
||||
}
|
||||
|
||||
padAbsolute(degrees: number): BBox {
|
||||
|
||||
return new BBox([[
|
||||
this.minLon - degrees,
|
||||
this.minLat - degrees
|
||||
], [this.maxLon + degrees,
|
||||
this.maxLat + degrees]])
|
||||
return new BBox([
|
||||
[this.minLon - degrees, this.minLat - degrees],
|
||||
[this.maxLon + degrees, this.maxLat + degrees],
|
||||
])
|
||||
}
|
||||
|
||||
toLeaflet() {
|
||||
return [[this.minLat, this.minLon], [this.maxLat, this.maxLon]]
|
||||
return [
|
||||
[this.minLat, this.minLon],
|
||||
[this.maxLat, this.maxLon],
|
||||
]
|
||||
}
|
||||
|
||||
asGeoJson(properties: any): any {
|
||||
|
@ -181,16 +191,16 @@ export class BBox {
|
|||
properties: properties,
|
||||
geometry: {
|
||||
type: "Polygon",
|
||||
coordinates: [[
|
||||
|
||||
[this.minLon, this.minLat],
|
||||
[this.maxLon, this.minLat],
|
||||
[this.maxLon, this.maxLat],
|
||||
[this.minLon, this.maxLat],
|
||||
[this.minLon, this.minLat],
|
||||
|
||||
]]
|
||||
}
|
||||
coordinates: [
|
||||
[
|
||||
[this.minLon, this.minLat],
|
||||
[this.maxLon, this.minLat],
|
||||
[this.maxLon, this.maxLat],
|
||||
[this.minLon, this.maxLat],
|
||||
[this.minLon, this.minLat],
|
||||
],
|
||||
],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -206,22 +216,22 @@ export class BBox {
|
|||
return new BBox([].concat(boundsul, boundslr))
|
||||
}
|
||||
|
||||
toMercator(): { minLat: number, maxLat: number, minLon: number, maxLon: number } {
|
||||
toMercator(): { minLat: number; maxLat: number; minLon: number; maxLon: number } {
|
||||
const [minLon, minLat] = GeoOperations.ConvertWgs84To900913([this.minLon, this.minLat])
|
||||
const [maxLon, maxLat] = GeoOperations.ConvertWgs84To900913([this.maxLon, this.maxLat])
|
||||
|
||||
return {
|
||||
minLon, maxLon,
|
||||
minLat, maxLat
|
||||
minLon,
|
||||
maxLon,
|
||||
minLat,
|
||||
maxLat,
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private check() {
|
||||
private check() {
|
||||
if (isNaN(this.maxLon) || isNaN(this.maxLat) || isNaN(this.minLon) || isNaN(this.minLat)) {
|
||||
console.trace("BBox with NaN detected:", this);
|
||||
throw "BBOX has NAN";
|
||||
console.trace("BBox with NaN detected:", this)
|
||||
throw "BBOX has NAN"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue