-
Notifications
You must be signed in to change notification settings - Fork 7
/
couchapp.js
84 lines (74 loc) · 2.04 KB
/
couchapp.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
var couchapp = require('couchapp')
var path = require('path')
ddoc =
{ "_id":'_design/polymapper',
"description": "Simple GeoJSON visualizer using Polymaps",
"name": "Polymapper",
"rewrites": [
{
"to": "index.html",
"from": "/"
},
{
"to": "/_spatiallist/geojson/full",
"from": "/data"
},
{
"to": "/../../",
"from": "api"
},
{
"to": "/../../*",
"from": "api/*"
},
{
"to": "/_spatiallist/geojson/full",
"from": "/all",
"query": {
"bbox": "-180,-90,180,90"
}
},
{"from":"/*", "to":'*'}
]
};
ddoc.spatial = {
/**
* A simple spatial view that emits the GeoJSON plus the complete documents.
*/
full: function(doc){
if(doc.geometry){
emit(doc.geometry, doc);
}
}
}
ddoc.lists = {
/**
* This function outputs a GeoJSON FeatureCollection (compatible with
* OpenLayers). The geometry is stored in the geometry property, all other
* properties in the properties property.
*
* @author Volker Mische
*/
geojson: function(head, req) {
var row, out, sep = '\n';
// Send the same Content-Type as CouchDB would
if (typeof(req.headers.Accept) != "undefined" && req.headers.Accept.indexOf('application/json')!=-1)
start({"headers":{"Content-Type" : "application/json"}});
else
start({"headers":{"Content-Type" : "text/plain"}});
if ('callback' in req.query) send(req.query['callback'] + "(");
send('{"type": "FeatureCollection", "features":[');
while (row = getRow()) {
out = '{"type": "Feature", "id": ' + JSON.stringify(row.id);
out += ', "geometry": ' + JSON.stringify(row.value.geometry);
delete row.value.geometry;
out += ', "properties": ' + JSON.stringify(row.value) + '}';
send(sep + out);
sep = ',\n';
}
send("]}");
if ('callback' in req.query) send(")");
}
}
couchapp.loadAttachments(ddoc, path.join(__dirname, 'attachments'));
module.exports = ddoc;