Published
Edited
May 20, 2021
Importers
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function rasterizeMap(M, N, sampleArea, projection){
var top = sampleArea[0];
var bot = sampleArea[1];
var lft = sampleArea[2];
var rgt = sampleArea[3];

var cellWidth = (rgt-lft)/N;
var cellHeight = (bot-top)/M;

var Matrix = [];
for(var rr of d3.range(0,M)){
var Row = [];
for(var cc of d3.range(0,N)){
var x = lft+cellWidth/2 + cellWidth*(cc);
var y = top+cellHeight/2+ cellHeight*(rr);
var lonlat = projection.invert([x, y]);
if((lonlat == null)|(lonlat == undefined)){
Row.push({x:x, y:y, lon:null, lat:null});
}else{
if((lonlat[0]==null)|(lonlat[1]==null)){
Row.push({x:x, y:y, lon:null, lat:null});
}else{
// we get numbers back for lonlat
// let's make sure it's on the map
var x2 = projection(lonlat); // reproject the point
if(x2 != undefined){
if((Math.abs(x2[0]-x)>cellWidth/1000)|(Math.abs(x2[1]-y)>cellHeight/1000)){
Row.push({x:x, y:y, lon:null, lat:null}); // point is off the map
}else{
Row.push({x:x, y:y, lon:lonlat[0], lat:lonlat[1]});
}
}else{
Row.push({x:x, y:y, lon:null, lat:null}); // point is off the map
}
}
}
}
Matrix.push(Row);
}

return Matrix;
}
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more