forked from nodetiles/nodetiles-core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMap.js
212 lines (182 loc) · 6.08 KB
/
Map.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var async = require("async");
var __ = require("lodash");
var renderer = require("./renderer");
var projector = require("./projector");
var cartoRenderer = require("./cartoRenderer");
var BUFFER_RATIO = 0.25;
/**
* var map = new Map();
* map.addData(function(minX, minY, maxX, maxY, projection) { ... });
* map.setStyle(...);
* map.render(0, 0, 180, 90, 500, 250);
*/
// default to EPSG:3857 (web mercator)
// http://spatialreference.org/ref/sr-org/7483/
var DEFAULT_PROJECTION = "EPSG:900913";//"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs";
var Map = function(options) {
options = options || {};
this.datasources = [];
this.styles = [];
this.projection = DEFAULT_PROJECTION;
this.assetsPath = ".";
if (options.projection){
this.projection = projector.util.cleanProjString(options.projection);
console.log(this.projection);
}
this.boundsBuffer =
("boundsBuffer" in options) ? options.boundsBuffer : BUFFER_RATIO;
this._renderer = cartoRenderer;
};
Map.prototype = {
constructor: Map,
render: function(options) {
this._getData(options.bounds, options.boundsBuffer, function(error, shapes) {
if (error) {
options.callback(error);
}
else {
this._renderer.renderImage(__.extend({}, options, {
layers: shapes,
styles: this.processedStyles,
callback: function(error, canvas) {
options.callback && options.callback(error, canvas);
}
}));
}
}.bind(this));
},
// Should this really be here, or should it exist on a different object entirely?
renderGrid: function(options) {//minX, minY, maxX, maxY, width, height, asImage, callback) {
this._getData(options.bounds, options.boundsBuffer, function(error, shapes) {
if (error) {
options.callback(error);
console.error("ERROR! "+error);
}
else {
// this._renderer.renderGrid(minX, minY, maxX, maxY, width, height, shapes, this.processedStyles, asImage, callback);
this._renderer.renderGrid(__.extend({}, options, {
layers: shapes,
styles: this.processedStyles,
callback: function(error, canvas) {
options.callback && options.callback(error, canvas);
}
}));
}
}.bind(this));
},
_getData: function(bounds, buffer, callback) {
var dataBounds = this._bufferedBounds(bounds, buffer);
// this is a bit quick and dirty - we could possibly use style data
// to figure out more detailed queries than just geographic bounds
var projection = this.projection;
var self = this;
async.map(
this.datasources,
function(datasource, dataCallback) {
var sourceName = datasource.sourceName;
var preCallback = function(error, data) {
if (!error) {
data.source = sourceName;
}
dataCallback(error, data);
};
if (typeof datasource !== "function") {
datasource = datasource.getShapes.bind(datasource);
}
// allow simple sources to just return results immediately
var syncData = datasource(dataBounds.minX, dataBounds.minY, dataBounds.maxX, dataBounds.maxY, projection, preCallback);
if (syncData) {
preCallback(null, syncData);
}
},
callback
// HACK: this is temporary until all the style machinery is done
// should really be the above line
// function(error, data) {
// if (!error) {
// data.forEach(function(collection, index) {
// collection.styles = [self.styles[index].properties];
// });
// }
// callback(error, data);
// }
);
},
_bufferedBounds: function(bounds, buffer) {
if (buffer == null) {
buffer = this.boundsBuffer;
}
if (typeof buffer === "function") {
return buffer.call(this, bounds);
}
if (typeof buffer !== "number") {
buffer = BUFFER_RATIO;
}
var amount = (bounds.maxX - bounds.minX) * buffer;
return {
minX: bounds.minX - amount,
minY: bounds.minY - amount,
maxX: bounds.maxX + amount,
maxY: bounds.maxY + amount
};
},
addData: function(datasource) {
// validate datasource
if (!(typeof datasource === "function" || typeof datasource.getShapes === "function")) {
console.warn("Datasource is not a function or an object with a 'getShapes()' function.");
return false;
}
var index = this.datasources.indexOf(datasource);
if (index === -1) {
this.datasources.push(datasource);
return true;
}
return false;
},
removeData: function(datasource) {
var index = this.datasources.indexOf(datasource);
if (index > -1) {
this.datasources.splice(index, 1);
return true;
}
return false;
},
setProjection: function(projection) {
// TODO: validate this somehow?
this.projection = projection;
},
addStyle: function(style) {
// may need to do better flattening, etc.
if (Object.prototype.toString.call(style) === "[object Array]") {
this.styles = this.styles.concat(style);
}
else {
this.styles.push(style);
}
this._processStyles();
},
setRenderer: function(renderer) {
if (renderer.renderImage && renderer.renderGrid && renderer.processStyles) {
this._renderer = renderer;
this._processStyles();
}
},
/**
* Triggers all the map's datasources to prepare themselves. Usually this
* connecting to a database, loading and processing a file, etc.
* Calling this method is completely optional, but allows you to speed up
* rendering of the first tile.
*/
prepare: function() {
var projection = this.projection;
this.datasources.forEach(function(datasource) {
datasource.load && datasource.load(function(error) {
datasource.project && datasource.project(projection);
});
});
},
_processStyles: function() {
this.processedStyles = this._renderer.processStyles(this.styles, this.assetsPath);
}
};
module.exports = Map;