-
Notifications
You must be signed in to change notification settings - Fork 6
/
addprofiles.js
122 lines (106 loc) · 3.41 KB
/
addprofiles.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
gf = require('./gridfloat.js');
var mongodb = require('mongodb');
function undiffcompress( ary ) {
var ret = [];
var last = null;
for(var i in ary){
// scan forward for first non-null item
if( last===null ){
last=ary[i];
ret.push( last );
continue;
}
// only get this far if 'last' is non-null
// if this item is null, push onto the stack and move forward
if( ary[i] === null ){
ret.push( null );
continue;
}
last += ary[i];
ret.push( last );
}
return ret;
}
function uncompressloc( loc ) {
var lons = undiffcompress( loc[0] );
var lats = undiffcompress( loc[1] );
for(var i in lons){
lons[i] = lons[i]!==null ? lons[i]/1000000 : null;
}
for(var i in lats){
lats[i] = lats[i]!==null ? lats[i]/1000000 : null;
}
var ret = [];
for(var i in lons){
ret.push( [lons[i],lats[i]] );
}
return ret;
}
function risefall( ary ){
// take ary of [x,y] and return sum rise and sum fall
// if any y===null, returns [null,null]
var rise=0;
var fall=0;
for(var i=0; i<ary.length-1; i++){
if(ary[i][1]===null || ary[i+1][1]===null){
return [null,null];
}
var diff = ary[i+1][1]-ary[i][1];
if(diff>0){
rise += diff;
} else {
fall -= diff; //subtract a negative number == add a positive number
}
}
return [rise,fall];
}
var tilewidth=0.02;
var ff = new gf.GridFloat( "/storage/maps/data/58735568/58735568" )
console.log( ff );
console.log( ff.cell(0,0) );
console.log( ff.elevation(-122.45345, 37.738332) );
var server = new mongodb.Db('test', new mongodb.Server("127.0.0.1", 27017, {}));
server.open(function(err, client) {
var compcoll = new mongodb.Collection(client,"simple_tiles");
var collection = new mongodb.Collection(client,"profiles");
// go through every tile that touches the DEM's rectange
var xstart = (Math.round(Math.floor(ff.left/tilewidth)*tilewidth*1000)/1000);
var ystart = (Math.round(Math.floor(ff.bottom/tilewidth)*tilewidth*1000)/1000);
var x = xstart;
var y = ystart;
while(x<ff.right){
while(y<ff.top){
var tilekey = x.toFixed(2)+":"+y.toFixed(2);
console.log( tilekey );
var cursor = compcoll.find({_id:tilekey}).limit(1);
cursor.nextObject( function(err,doc){
if(doc){
console.log( "doc for "+doc._id+" with "+doc.value.ways.length+" ways" );
var profiles = [];
for( var way in doc.value.ways ){
var loc = uncompressloc( doc.value.ways[way].loc );
//console.log( "way "+way+", "+loc.length+" points" );
//if( doc._id==="-122.52:37.76" && way==138 ) {
// debugger;
//}
var profile = {'id':doc.value.ways[way].id, 'profile':ff.profile( loc )};
if( profile===null ){
continue;
}
profiles.push( profile );
}
collection.insert(profiles, {safe:true}, function(err,obj){
console.log( doc._id+" profiles inserted" );
})
console.log( doc._id+" done" );
} else {
console.log( "null tile" );
}
});
y += tilewidth;
}
y = ystart;
x += tilewidth;
}
console.log("done");
});