-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (77 loc) · 2.13 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var proj4 = require('proj4');
// register 3310 projection
proj4.defs('EPSG:3310','+proj=aea +lat_1=34 +lat_2=40.5 +lat_0=0 +lon_0=-120 +x_0=0 +y_0=-4000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');
var proj_gmaps = 'EPSG:4326';
var proj_cimis = 'EPSG:3310';
class CimisGrid {
constructor() {
// match layers for easier checking
this.ncols = 510;
this.nrows = 560;
this.xllcorner = -410000;
this.yllcorner = -660000;
this.cellsize = 2000;
}
bounds() {
var bottomLeft = proj4(
proj_cimis,
proj_gmaps,
[this.xllcorner, this.yllcorner]
);
var topRight = proj4(
proj_cimis,
proj_gmaps,
[
this.xllcorner + this.ncols*this.cellsize,
this.yllcorner + this.nrows*this.cellsize
]);
return [bottomLeft, topRight];
}
gridToBounds(row, col) {
var bottomLeft = proj4(
proj_cimis,
proj_gmaps,
[
this.xllcorner + (col* this.cellsize),
this.yllcorner + ((this.nrows - row)*this.cellsize)
]
);
var topRight = proj4(
proj_cimis,
proj_gmaps,
[
this.xllcorner + ((col+1) * this.cellsize),
this.yllcorner + ((this.nrows -(row+1)) * this.cellsize)
]
);
return [bottomLeft, topRight];
}
llToGrid(lng, lat) {
if( typeof lng === 'object' ) {
lat = lng.lat();
lng = lng.lng();
}
var result = proj4(proj_gmaps, proj_cimis, [lng, lat]);
// Assuming this is the input to the grid....
// Cols are X. Rows are Y and counted from the top down
result = {
row : this.nrows - Math.floor((result[1] - this.yllcorner) / this.cellsize),
col : Math.floor((result[0] - this.xllcorner) / this.cellsize),
};
var y = this.yllcorner + ((this.nrows-result.row) * this.cellsize);
var x = this.xllcorner + (result.col * this.cellsize) ;
result.topRight = proj4(
proj_cimis,
proj_gmaps,
[x+this.cellsize, y+this.cellsize]
);
result.bottomLeft = proj4(
proj_cimis,
proj_gmaps,
[x, y]
);
return result;
}
}
module.exports = CimisGrid;