-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
84 lines (78 loc) · 2.94 KB
/
app.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 express = require('express');
var topojson = require('topojson');
var fs = require('fs');
var child = require("child_process");
var port = process.env.PORT || 3000;
var app = express();
app.use(express.bodyParser());
app.set('json spaces',0);
app.use(express.compress());
app.use(express.methodOverride());
app.use('/', express.static(__dirname + '/static'));
app.listen(port);
app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});
app.get('/map', function(req, res){
res.sendfile(__dirname + '/map.html');
});
var topo_out = function( req, res, topology ){
if(req.query && (req.query.doubledouble == "true" || req.query.doubleDouble == "true" || req.query.DoubleDouble == "true")){
var endpt = topology.objects.collection.geometries.length;
for(var i=0;i<endpt;i++){
var newobj = {
id: endpt + i + 1,
type: topology.objects.collection.geometries[i].type + "",
arcs: topology.objects.collection.geometries[i].arcs.concat( )
};
topology.objects.collection.geometries.push( newobj );
}
}
if(req.query && (req.query.animalstyle == "true" || req.query.animalStyle == "true" || req.query.AnimalStyle == "true")){
var animals = [ "olinguillo", "armadillo", "zebra", "chimpanzee", "dragon", "tiger", "lion", "squirrel", "dog", "frog", "shark", "tortoise", "giraffe", "parrot", "ant", "aphid", "spider", "crab", "tuna" ];
for(var i=0;i<topology.objects.collection.geometries.length;i++){
topology.objects.collection.geometries[i].id = animals[ Math.floor(Math.random() * animals.length) ] + topology.objects.collection.geometries[i].id;
}
}
return res.json( topology );
};
var keepProps = function(properties, key, value) {
properties[key] = value;
return true;
};
app.post('/', function(req, res){
// check for POSTed files through web interface
var fileCount = 0;
for(var file in req.files){
fileCount++;
fs.readFile(req.files[file].path, function(error, file){
var topology = topojson.topology({ collection: JSON.parse( file ) }, { "property-transform": keepProps });
return topo_out( req, res, topology );
});
break;
}
if(fileCount == 0){
// check for POSTed GeoJSON directly in body
if(req.body && req.body.type && (req.body.geometry || req.body.coordinates || req.body.features)){
if(req.body.features){
var topology = topojson.topology({ collection: JSON.parse( req.body ) }, { "property-transform": keepProps });
return topo_out( req, res, topology );
}
else{
return res.json( req.body );
}
}
else{
// check each POSTed variable for GeoJSON
var varCount = 0;
for(var bodyvar in req.body){
varCount++;
var topology = topojson.topology({ collection: JSON.parse( req.body[bodyvar] ) }, { "property-transform": keepProps });
return topo_out( req, res, topology );
}
if(varCount == 0){
res.send('no GeoJSON?');
}
}
}
});