geoserver
Version:
Geoserver written in Node.JS
176 lines (160 loc) • 4.47 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var DOMParser, apiv1, domparser, express, geojsonFromGpx, geoutil, mongo, readFileSync, upload;
express = require('express');
geoutil = require('geoutil');
mongo = require('../mongo');
apiv1 = express.Router();
apiv1.get('/', function(req, res) {
return res.json({
boundary_intersect_post: {
method: 'POST',
endpoint: req.originalUrl + "/boundary/intersect/",
example_body: '{"geojson": { "type": "LineString", "coordinates": […] }}'
},
line_analyze_post: {
method: 'POST',
endpoint: req.originalUrl + "/boundary/intersect/",
example_body: '{"geojson": { "type": "LineString", "coordinates": […] }}'
},
gpx_parse_post: {
method: 'POST',
endpoint: req.originalUrl + "/gpx/parse/",
example_body: 'multipart/form-data upload'
}
});
});
apiv1.post('/boundary/intersect', function(req, res, next) {
var query, ref;
if (!req.body.geojson) {
return res.status(400).json({
message: 'Body should be a JSON object'
});
}
if ((ref = req.body.geojson.type) !== 'Point' && ref !== 'LineString' && ref !== 'Polygon') {
return res.status(422).json({
message: 'Geometry type must be Point, Linestring, or Polygon'
});
}
if (!(req.body.geojson.coordinates instanceof Array)) {
return res.status(422).json({
message: 'Geometry coordinates must be an Array'
});
}
query = {
geojson: {
$geoIntersects: {
$geometry: req.body.geojson
}
}
};
return mongo.grenser.find(query, {
fields: {
geojson: 0
}
}).toArray(function(err, docs) {
var doc, i, len, ret, typer;
if (err) {
return next(err);
}
typer = {
Område: 'områder',
Fylke: 'fylker',
Kommune: 'kommuner'
};
ret = {
områder: [],
fylker: [],
kommuner: []
};
for (i = 0, len = docs.length; i < len; i++) {
doc = docs[i];
if (typer[doc.type] !== void 0) {
if (doc.type === 'Område') {
ret[typer[doc.type]].push({
_id: doc._id,
navn: doc.navn
});
} else {
ret[typer[doc.type]].push(doc.navn);
}
}
}
return res.json(ret);
});
});
apiv1.post('/line/analyze', function(req, res, next) {
var geojson, ref;
if (!req.body.geojson) {
return res.status(400).json({
message: 'Body should be a JSON object'
});
}
if ((ref = req.body.geojson.type) !== 'Point' && ref !== 'LineString' && ref !== 'Polygon') {
return res.status(422).json({
message: 'Geometry type must be Point, Linestring, or Polygon'
});
}
if (!(req.body.geojson.coordinates instanceof Array)) {
return res.status(422).json({
message: 'Geometry coordinates must be an Array'
});
}
geojson = req.body.geojson;
if (geojson.properties == null) {
geojson.properties = {};
}
geojson.properties.start = {
type: 'Point',
coordinates: geojson.coordinates[0]
};
geojson.properties.stop = {
type: 'Point',
coordinates: geojson.coordinates[geojson.coordinates.length - 1]
};
return res.json({
length: Math.floor(geoutil.lineDistance(req.body.geojson.coordinates, true)),
geojson: geojson
});
});
upload = require('multer')({
dest: require('os').tmpdir(),
fileFilter: function(req, file, cb) {
file.extension = file.originalname.split('.').reverse()[0];
return cb(null, true);
}
});
apiv1.use('/gpx/parse', upload.any());
readFileSync = require('fs').readFileSync;
DOMParser = require('xmldom').DOMParser;
domparser = new DOMParser();
geojsonFromGpx = require('togeojson').gpx;
apiv1.post('/gpx/parse', function(req, res, next) {
var dom, f, file, files, i, len, ref;
if (!req.files) {
res.end();
}
files = {};
ref = req.files;
for (i = 0, len = ref.length; i < len; i++) {
f = ref[i];
if (!files[f.fieldname]) {
files[f.fieldname] = [];
}
file = {
fieldname: f.fieldname,
filename: f.originalname,
extension: f.extension,
mimetype: f.mimetype,
geojson: null
};
if (file.extension === 'gpx') {
dom = domparser.parseFromString(readFileSync(f.path, 'utf8'));
file.geojson = geojsonFromGpx(dom);
} else {
file.error = "Invalid extension '" + file.extension + "'";
}
files[f.fieldname].push(file);
}
return res.json(files);
});
module.exports = apiv1;