Skip to content

Commit

Permalink
update to processing mapreduce scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Mar 8, 2012
1 parent f6f2fc9 commit 50e8cb3
Showing 1 changed file with 119 additions and 11 deletions.
130 changes: 119 additions & 11 deletions nodehist.js
Expand Up @@ -31,9 +31,7 @@ db.runCommand( { mapreduce: "osm_ways",
reduce:reducef,
out:"nodes_count"
});
*/

/*
*//*
function mapf(){
emit("loc"+this.id,{id:this.id,loc:this.loc,count:0,ways:[]});
}
Expand All @@ -47,9 +45,6 @@ db.runCommand( { mapreduce: "osm_nodes",
map: mapf,
reduce: reducef,
out: {merge:"nodes_count"} } )
*/

/*
//merge loc and counts into one doc
function mapf(){
Expand All @@ -73,8 +68,9 @@ db.runCommand( {mapreduce: "nodes_count",
reduce: reducef,
out: "nodes_joined"} );
*/
/*
*/
/*
//convert node documents into way documents
function mapf(){
Expand Down Expand Up @@ -118,6 +114,7 @@ db.runCommand( {mapreduce:"nodes_joined",
*/

/*
//split up presplit ways
function mapf(){
var seg = {'nodes':[this.value.nodes[0].id],'loc':[this.value.nodes[0].loc],'keysvals':this.value.keysvals,'id':this._id};
Expand Down Expand Up @@ -147,18 +144,16 @@ db.runCommand( {mapreduce:"presliced_ways",
map:mapf,
reduce:reducef,
out:"sliced_ways"} );
*/



/*
//chunk split ways into tiles
function mapf(){
var gettilespec = function(loc,res){
var x = Math.round(Math.floor(loc[0]/res)*res*1000)/1000;
var y = Math.round(Math.floor(loc[1]/res)*res*1000)/1000;
return x.toString()+":"+y.toString();
return x.toFixed(2)+":"+y.toFixed(2);
}
if( !this.value.loc ){
Expand Down Expand Up @@ -198,3 +193,116 @@ db.runCommand({mapreduce:"sliced_ways",
map:mapf,
reduce:reducef,
out:"tiled_ways"})
/**/

//chunk split ways into tiles
function mapf(){
var gettilespec = function(loc,res){
var x = Math.round(Math.floor(loc[0]/res)*res*1000)/1000;
var y = Math.round(Math.floor(loc[1]/res)*res*1000)/1000;

return x.toFixed(2)+":"+y.toFixed(2);
}

var intify = function( ary, precision ){
ret = [];
for(var i in ary){
if(ary[i]){
ret.push( [Math.round(ary[i][0]*Math.pow(10,precision)),Math.round(ary[i][1]*Math.pow(10,precision))] );
} else {
ret[i]=ary[i];
}
}
return ret;
}

var zip = function(ary){
var lats=[];
var lons=[];
for(var i in ary){
if(ary[i]){
lons.push( ary[i][0] );
lats.push( ary[i][1] );
} else {
lons.push(null);
lats.push(null);
}
}
return [lons,lats];
}

var diffcompress = function(ary){
if(ary.length<2)
return ary;

var last=ary[0];
var ret = [last];

for(var i=1; i<ary.length; i++){
if( last===null ){
ret.push( ary[i] );
last = ary[i];
continue;
}
if(ary[i]===null){
ret.push( null );
continue;
}

ret.push( ary[i]-last );
last = ary[i];
}

return ret;
}

var diffcompress_locs = function(ary){
return [diffcompress(ary[0]),diffcompress(ary[1])];
}

if( !this.value.loc ){
//printjson( this );
return;
}

var tiles={};
//emit a tile for every tile that this way is in
for(var i=0; i<this.value.loc.length; i++){
if(!this.value.loc[i]){
//printjson(this.value.loc);
continue;
}

var tilespec = gettilespec(this.value.loc[i],0.02);
if(tiles[tilespec]!=true){
var wayinfo={};
wayinfo[this.value.id]=this.value.keysvals;
emit(tilespec, {ways:[{id:this._id,
wayid:this.value.id,
fromv:this.value.nodes[0],
tov:this.value.nodes[this.value.nodes.length-1],
loc:diffcompress_locs(zip(intify(this.value.loc,6)))}],
wayinfo:wayinfo});
tiles[tilespec]=true;
}
}
}

function reducef(key,values){
ret = []
wayinfo={};
for(var i=0; i<values.length; i++){
ret = ret.concat( values[i].ways );
for(var wayid in values[i].wayinfo){
wayinfo[wayid]=values[i].wayinfo[wayid];
}
}
return {ways:ret,wayinfo:wayinfo};
}

db.runCommand({mapreduce:"sliced_ways",
map:mapf,
reduce:reducef,
out:"simple_tiles"})

0 comments on commit 50e8cb3

Please sign in to comment.