Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 84 lines (73 sloc) 3.157 kB
0329fcc @mapmeld from 500 to 10,000 points
authored
1 var path = require('path');
2 var url = require('url');
3 var fs = require('fs');
4 var mapnik = require('mapnik');
5 var Step = require('step');
6 var millstone = require('millstone');
7
8 models.Datasource.prototype.sync = function(method, model, success, error) {
9 if (method !== 'read') return error('Method not supported.');
10
11 var options = model.options;
12 var config = Bones.plugin.config;
13
14 if (!options) return error(new Error('options are required.'));
15 if (!options.id) return error(new Error('id is required.'));
16 if (!options.project) return error(new Error('project is required.'));
17
18 millstone.resolve({
19 mml: {
20 Stylesheet: [{ id: 'layer', data: '' }],
21 Layer: [{
22 name: options.id,
23 srs: options.srs || '',
24 Datasource: options
25 }]
26 },
27 base: path.join(config.files, 'project', options.project),
28 cache: path.join(config.files, 'cache')
29 }, function(err, mml) {
30 if (err) return error(err);
31
32 try {
33 mml.Layer[0].Datasource = _(mml.Layer[0].Datasource).defaults(options);
34
35 // Some mapnik datasource accept 'row_limit` (like postgis, shape)
36 // those that do not will be restricted during the featureset loop below
37 var row_limit = 10000;
38 //mml.Layer[0].Datasource = _(mml.Layer[0].Datasource).defaults({row_limit:row_limit});
39 //console.log(mml.Layer[0].Datasource);
40 var source = new mapnik.Datasource(mml.Layer[0].Datasource);
41
42 var features = [];
43 if (options.features || options.info) {
44 var featureset = source.featureset();
45 for (var i = 0, feat;
46 i < row_limit && (feat = featureset.next(true));
47 i++) {
48 features.push(feat.attributes());
49 }
50 }
51
52 var desc = source.describe();
53 var datasource = {
54 id: options.id,
55 project: options.project,
56 url: options.file,
57 fields: desc.fields,
58 features: options.features ? features : [],
59 type: desc.type,
60 geometry_type: desc.type === 'raster' ? 'raster' : desc.geometry_type
61 };
62
63 // Process fields and calculate min/max values.
64 for (var f in datasource.fields) {
65 var values = _(features).pluck(f);
66 var type = datasource.fields[f];
67 datasource.fields[f] = { type: type };
68 if (options.features || options.info) {
69 datasource.fields[f].max = type === 'String'
70 ? _(values).max(function(v) { return (v||'').length })
71 : _(values).max();
72 datasource.fields[f].min = type === 'String'
73 ? _(values).min(function(v) { return (v||'').length })
74 : _(values).min();
75 }
76 }
77 } catch(err) {
78 return error(err);
79 }
80
81 success(datasource);
82 });
83 };
Something went wrong with that request. Please try again.