/
lists.js
143 lines (120 loc) · 4.51 KB
/
lists.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
exports.geojson =
/**
* This function outputs a GeoJSON FeatureCollection (compatible with
* OpenLayers). JSONP requests are supported as well.
*
* @author Volker Mische
*/
function(head, req) {
var row, out, sep = '\n';
// Send the same Content-Type as CouchDB would
if (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 = JSON.stringify({type: "Feature", geometry: row.geometry,
properties: row.value});
send(sep + out);
sep = ',\n';
}
send("]}");
if ('callback' in req.query) {
send(")");
}
};
exports.kml =
/**
* A list function that transforms a spatial view result set into a KML feed.
*
* @author Benjamin Erb
*/
function(head, req) {
var row, out, sep = '\n';
start({"headers":{"Content-Type" : "application/vnd.google-earth.kml+xml"}});
send('<?xml version="1.0" encoding="UTF-8"?>\n');
send('<kml xmlns="http://www.opengis.net/kml/2.2">\n');
send('<Document>\n');
send('<name>GeoCouch Result - KML Feed</name>\n');
while (row = getRow()) {
if(row.geometry){
send('\t<Placemark>');
send('<name>'+row.id+'</name>');
send('<Point><coordinates>'+row.geometry.coordinates[0]+','+row.geometry.coordinates[1]+',0</coordinates></Point>');
send('</Placemark>\n');
}
}
send('</Document>\n');
send('</kml>\n');
};
exports['knn-clustering'] =
/**
* A clustering algorithm that clusters object based on their proximity, producing k distinct clusters.
*/
function(head, req) {
start({"code": 501,"headers":{"Content-Type" : "text/plain"}});
//TODO: implement kNN clustering list
}
exports['proximity-clustering'] =
/**
* A clustering algorithm that clusters object based on their proximity, using a threshold value.
*/
function(head, req) {
var g = require('vendor/clustering/ProximityCluster'),
row,
threshold =100;
start({"headers":{"Content-Type" : "application/json"}});
if ('callback' in req.query) send(req.query['callback'] + "(");
if('threshold' in req.query){ threshold = req.query.threshold;}
var pc = new g.PointCluster(parseInt(threshold));
while (row = getRow()) {
pc.addToClosestCluster(row.value);
}
send(JSON.stringify({"rows":pc.getClusters()}));
if ('callback' in req.query) send(")");
};
exports.radius =
/**
* This will take the centroid of the bbox parameter and a supplied radius
* parameter in meters and filter the rectangularly shaped bounding box
* result set by circular radius.
*
* @author Max Ogden
*/
function(head, req) {
var gju = require('vendor/geojson-js-utils/geojson-utils'),
row,
out,
radius = req.query.radius,
bbox = JSON.parse("[" + req.query.bbox + "]"),
center = gju.rectangleCentroid({
"type": "Polygon",
"coordinates": [[[bbox[0], bbox[1]], [bbox[2], bbox[3]]]]
}),
callback = req.query.callback,
circle = gju.drawCircle(radius, center),
startedOutput = false;
if (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()) {
if (gju.pointInPolygon(row.geometry, circle)) {
if (startedOutput) send(",\n");
out = '{"type": "Feature", "geometry": ' + JSON.stringify(row.geometry) +
', "properties": ' + JSON.stringify(row.value) + '}';
send(out);
startedOutput = true;
}
}
send("\n]};");
if ('callback' in req.query) send(")");
};