-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGeoWorld.js
78 lines (66 loc) · 2.21 KB
/
GeoWorld.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import * as gis from './gis.js'
import World from './World.js'
import { bbox as toBBox } from '../vendor/turfImports.js'
// class World defines the coordinate system for the model.
// It has been upgraded with methods converting from other
// transforms like GIS and DataSets/Canvas transforms.
// This is an example World object for geojson worlds.
class GeoWorld extends World {
static defaultOptions(bbox = gis.newMexicoBBox, patchesWidth = 100) {
return {
bbox,
patchesWidth,
}
}
// Use geo bbox & patchesWidth to create a World object
// BBox can be a geojson obj, which is converted to geojson's bbox
constructor(options = GeoWorld.defaultOptions()) {
let { bbox, patchesWidth } = options
let json
if (!Array.isArray(bbox)) {
json = bbox
bbox = toBBox(json)
}
console.log('GeoWorld bbox', bbox)
const aspect = gis.bboxMetricAspect(bbox)
const maxZ = Math.round(patchesWidth / 2)
super({
minX: 0,
maxX: patchesWidth - 1,
minY: 0,
maxY: Math.round(patchesWidth / aspect),
minZ: -maxZ,
maxZ: maxZ,
})
// these have to be after super call above, assignments to "this":
this.bbox = bbox
this.xfm = this.bboxTransform(...bbox)
if (json) this.geojson = json
}
// Convert from world patch coords to geo coords.
toGeo(x, y) {
return this.xfm.toBBox([x, y])
}
// Convert from geo lon/lat coords to patch coords
toWorld(geoX, geoY) {
return this.xfm.toWorld([geoX, geoY])
}
// return bbox in geo coords. use model.world.bbox variable rather than a method
// bbox() { // would be name conflict with this.bbox
// // return this.xfm.bbox
// return this.bbox
// }
// return bbox lonlat center
bboxCenter() {
return gis.bboxCenter(this.bbox)
}
// return bbox as 4 lonlat coords/points
bboxCoords() {
return gis.bboxCoords(this.bbox)
}
// return bbox geojson feature
bboxFeature(options = {}) {
return gis.bboxFeature(this.bbox, options)
}
}
export default GeoWorld