-
Notifications
You must be signed in to change notification settings - Fork 2
/
Grid.js
147 lines (134 loc) · 3.54 KB
/
Grid.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";
/**
* An example Sample class with function definitions.
*/
export default class Grid {
constructor(rows, columns, center) {
this.rows = rows;
this.columns = columns;
this.center = center;
this.rowHeightMultiplier = 20;
this.columnWidthMultiplier = 20;
}
/**
* Return the GeoJSON for this Sample.
*
* @param {int} z The zoom level for which to fetch data. Some types
* of SampleData may return different data at different zooms.
* @return {object}
*/
getGeoJson(z) {
var features = [];
for (const i in this.rows) {
for (const j in this.columns) {
features.push(this.getFeature(i, j, z));
}
}
return {
"type": "FeatureCollection",
"features": features,
};
}
/**
* Answer the feature properties for the cell in our grid.
*
* @param {int} row The row index.
* @param {int} column The column index.
* @param {int} zoom The zoom level we're building for.
*
* @return {object} The GeoJSON properties.
*/
getFeatureProperties(row, column, zoom) {
return {
...this.rows[row],
...this.columns[column],
};
}
/**
* Answer a feature for cell in our grid.
*
* @param {int} row The row index.
* @param {int} column The column index.
* @param {int} zoom The zoom level we're building for.
*
* @return {object} The GeoJSON feature.
*/
getFeature(row, column, zoom) {
return {
"type": "Feature",
"geometry": this.getFeatureGeometry(row, column, zoom),
"properties": this.getFeatureProperties(row, column, zoom),
};
}
/**
* Answer the offset from center for column.
*
* @param {int} column
*
* @return {int} The offset, with 0 as the center.
*/
getColumnOffset(column) {
const center = this.columns.length / 2;
return column - center;
}
/**
* Answer the offset from center for row.
*
* @param {int} row
*
* @return {int} The offset, with 0 as the center.
*/
getRowOffset(row) {
const center = this.rows.length / 2;
return (row - center) * -1;
}
/**
* Answer the width of columns at a given zoom in degrees.
*
* @param {int} zoom
*
* @return {float} The column width
*/
getColumnWidth(zoom, column, numColumns) {
if (typeof this.columnWidthCallback == "function") {
return this.columnWidthCallback(zoom, column, numColumns);
}
return this.columnWidthMultiplier / numColumns / Math.pow(2, zoom - 4);
}
/**
* Answer the width of columns at a given zoom in degrees.
*
* @param {int} zoom
*
* @return {float} The column width
*/
getRowHeight(zoom, row, numRows) {
if (typeof this.rowHeightCallback == "function") {
return this.rowHeightCallback(zoom, row, numRows);
}
return this.rowHeightMultiplier / numRows / Math.pow(2, zoom - 4);
}
/**
* Set a callback function to customize the width of columns for this instance.
*
* @param {function} callback The callback will be called with zoom and number of columns.
*/
setColumnWidthCallback(callback) {
if (typeof callback !== "function") {
throw "Callback must be a function.";
}
this.columnWidthCallback = callback;
}
/**
* Set a callback function to customize the height of rows for this instance.
*
* @param {function} callback The callback will be called with:
* zoom and number of rows.
*/
setRowHeightCallback(callback) {
if (typeof callback !== "function") {
throw "Callback must be a function.";
}
this.rowHeightCallback = callback;
}
}