-
Notifications
You must be signed in to change notification settings - Fork 165
/
simple.js
executable file
·91 lines (74 loc) · 2.74 KB
/
simple.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
var fs = require('fs');
var mapnik = require('mapnik');
var path = require('path');
var merc = require('../utils/sphericalmercator').proj4;
// map with just a style
// eventually the api will support adding styles in javascript
var s = '<Map srs="' + merc + '">';
s += '<Style name="points">';
s += ' <Rule>';
s += ' <Filter>[POP2005]>200000000</Filter>';
s += ' <MarkersSymbolizer marker-type="ellipse" fill="red" width="10" allow-overlap="true" placement="point"/>';
s += ' </Rule>';
s += ' <Rule>';
s += ' <Filter>[POP2005]>20000000</Filter>';
s += ' <MarkersSymbolizer marker-type="ellipse" fill="green" width="7" allow-overlap="true" placement="point"/>';
s += ' </Rule>';
s += ' <Rule>';
s += ' <ElseFilter />';
s += ' <MarkersSymbolizer marker-type="ellipse" fill="blue" width="5" allow-overlap="true" placement="point"/>';
s += ' </Rule>';
s += '</Style>';
s += '</Map>';
// create map object
var map = new mapnik.Map(256, 256);
map.fromStringSync(s);
// go get some arbitrary data that we can stream
var shp = path.join(__dirname, '../data/world_merc');
var ds = new mapnik.Datasource({
type: 'shape',
file: shp
});
// get the featureset that exposes lazy next() iterator
var featureset = ds.featureset();
var mem_datasource = new mapnik.MemoryDatasource(
{'extent': '-20037508.342789,-8283343.693883,20037508.342789,18365151.363070'}
);
// build up memory datasource
while ((feat = featureset.next(true))) {
var e = feat.extent();
// center longitude of polygon bbox
var x = (e[0] + e[2]) / 2;
// center latitude of polygon bbox
var y = (e[1] + e[3]) / 2;
var attr = feat.attributes();
mem_datasource.add({ 'x' : x,
'y' : y,
'properties' : { 'feat_id': feat.id(), 'NAME': attr.NAME, 'POP2005': attr.POP2005 }
});
}
var options = {
extent: '-20037508.342789,-8283343.693883,20037508.342789,18365151.363070'
};
// contruct a mapnik layer dynamically
var l = new mapnik.Layer('test');
l.srs = map.srs;
l.styles = ['points'];
// add our custom datasource
l.datasource = mem_datasource;
// add this layer to the map
map.add_layer(l);
// zoom to the extent of the new layer (pulled from options since otherwise we cannot know)
map.zoomAll();
// render it! You should see a bunch of red and blue points reprenting
map.renderFileSync('memory_points.png');
var options = {
layer: 0,
fields: ['POP2005', 'NAME', 'feat_id']
};
var grid = new mapnik.Grid(map.width, map.height, {key: 'feat_id'});
map.render(grid, options, function(err, grid) {
if (err) throw err;
fs.writeFileSync('memory_points.json', JSON.stringify(grid.encodeSync('utf', {resolution: 4})));
});
console.log('rendered to memory_points.png and memory_points.json');