This repository has been archived by the owner on Aug 20, 2019. It is now read-only.
/
tinyows.js
171 lines (141 loc) · 5.14 KB
/
tinyows.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
var request = require('request'),
spawn = require('child_process').spawn,
libxmljs = require('libxmljs'),
GeoJS = require('geojs'),
reXmlDoc = /<\?(.|\r|\n)*/m,
reContentType = /^Content-Type.*/i,
reTrailingComma = /\,\]\}$/;
exports.init = function(params) {
var requiredParams = ['geomField'],
configOK = true,
wfs = require('./wfs'),
defaultParams = {
kvp: false,
eastingFirst: true,
version: '1.1.0',
request: 'GetFeature',
maxFeatures: 200,
outputFormat: 'JSON'
};
/* internals */
function execRequest(stack, callback, requestBody) {
var tinyProc,
log = stack.initLog(),
results,
response = '';
console.log(requestBody);
tinyProc = spawn('/usr/local/bin/tinyows', [], {
env: {
REQUEST_METHOD: 'POST',
CONTENT_LENGTH: requestBody.length,
CONTENT_TYPE: 'text/xml'
}
});
tinyProc.stdout.setEncoding('UTF-8');
tinyProc.stdout.on('data', function(data) {
response += data;
});
tinyProc.stderr.setEncoding('UTF-8');
tinyProc.stderr.on('data', function(data) {
console.log(data);
});
tinyProc.on('exit', function(code) {
var respTicks = new Date().getTime();
// strip the content type
response = response.replace(reContentType, '');
response = response.replace(reTrailingComma, ']}');
log.checkpoint('recevied data from tinyows');
try {
callback({
log: log.getData(),
results: wfs.standardize(response, params)
});
}
catch (e) {
callback({
log: log.getData(),
error: e.message,
raw: response
});
} // try..catch
/*
if (matches) {
try {
// parse the xml document
xmldoc = libxmljs.parseXmlString(matches[0]);
log.checkpoint('parsed xml');
// results =
}
catch (e) {
stack.reportError(callback, e);
} // try..catch
} // if
*/
console.log('child process exited with code ' + code);
});
tinyProc.stdin.write(requestBody);
} // execFilter
/* exports */
/**
### bbox(queryParams, req, res)
*/
function bbox(stack, callback, queryParams, req, res) {
var min = new GeoJS.Pos(queryParams.min),
max = new GeoJS.Pos(queryParams.max);
/*
execFilter(stack, callback, [
'<ogc:BBOX>' +
'<ogc:PropertyName>' + params.geomField + '</ogc:PropertyName>' +
'<gml:Envelope srsName="EPSG::4326">' +
'<gml:lowerCorner>' + min.lat + ' ' + min.lon + '</gml:lowerCorner>' +
'<gml:upperCorner>' + max.lat + ' ' + max.lon + '</gml:upperCorner>' +
'</gml:Envelope>' +
'</ogc:BBOX>'
]);
*/
/*
'<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">' +
'</ogc:Filter>'
execFilter(stack, callback, [
'<BBOX>' +
'<PropertyName>' + params.geomField + '</PropertyName>' +
'<Box srsName="EPSG::4326">' +
'<coordinates>' + min.lon + ',' + min.lat + ' ' + max.lon + ',' + max.lat + '</coordinates>' +
'</Box>' +
'</BBOX>'
]);
*/
} // bbox
function filter(stack, callback, queryParams, req, res) {
var request;
if (queryParams.filter) {
request = wfs.makeRequest(params, queryParams.filter);
}
else if (queryParams.conditions) {
request = wfs.makeRequest(params, wfs.parseConditions(queryParams.conditions, params));
} // if..else
if (request) {
execRequest(stack, callback, request);
}
else {
stack.reportError(callback, 'Unable to locate required filter or conditions parameter');
} // if..else
} // filter
/* initialization */
// iterate through and check the required params have been specified
for (var ii = 0; ii < requiredParams.length; ii++) {
configOK = configOK && typeof params[requiredParams[ii]] != 'undefined';
} // for
if (! configOK) {
throw new Error('Dataset not configured correctly, please contact administrator');
} // if
for (var key in defaultParams) {
if (! params[key]) {
params[key] = defaultParams[key];
} // if
} // for
return {
bbox: bbox,
filter: filter
};
};