Thinking of rendering based on vector file mapnik

As a GISer, he is now in the transportation industry (the combination of transportation industry and GIS is really an interesting thing). Recently, a vector file with large amount of data was loaded and rendered in the foreground and crashed directly. At this node, I made the following thinking:

  • Rearrange the scene logic to see if you can reduce the amount of data in the background now, and then render in the foreground. The overall idea is: in the background, according to the business logic and the vector merge function union of turf.js, the vector elements are finally formed according to the business indicators (the node information will be merged in the process of merging to reduce the file size). I think it's OK, but... It takes a long time to execute with node (at present, it has been executed for two hours without any result). You can only give up first
  • The above scheme is not feasible, suddenly think of background processing interpolation algorithm density map. For example, try mapnik to render grid directly in the background. The specific code is as follows
var mapnik = require("mapnik");
var fs = require("fs");

 var img = new mapnik.Image(2082,1617);
 fs.readFile('PTAL_gridCoefficient.txt','utf-8',function(err,data){
          if(err){
               console.error(err);
            }else{
               data = data.toString().split("\r\n");
               data.forEach(function(v){
                  var tmp=v.split(","),color=[65,105,225,1];
                  if(tmp[1] == 0){
                      color=[255,255,255,0];
                  }else if( tmp[1] >0 && tmp[1]<=2.5 ){
                      color=[22,73,125,255];
                  }else if( tmp[1] >2.5 && tmp[1]<=5 ){
                      color=[17,111,184,255];
                  }else if( tmp[1] >5 && tmp[1]<=10 ){
                      color=[39,173,227,255];
                  }else if( tmp[1] >10 && tmp[1]<=15 ){
                      color=[145,201,83,255];
                  }else if( tmp[1] >15 && tmp[1]<=20 ){
                      color=[255,241,1,255];
                  }else if( tmp[1] >20 && tmp[1]<=25 ){
                      color=[251,192,142,255];
                  }else if( tmp[1] >25 && tmp[1]<=40 ){
                      color=[238,29,35,255];
                  }else if( tmp[1] >40){
                      color=[132,21,23,255];
                  }
                  var x=parseInt(tmp[0].substr(0,6))-115423,
                      y=1617-(parseInt(tmp[0].substr(6,5))-39443);
                  img.setPixel(x==2082?2081:x,y==1617?1616:y,new mapnik.Color(color[0],color[1],color[2],color[3]));
               });
               img.encode('png', function(err,buffer) {
                  if (err) throw err;
                  fs.writeFile('map.png',buffer, function(err) {
                      if (err) throw err;
                  });
               });
            }
         }
     );

ps: the key points are as follows

  • Grid length and width: take the extent range of geographic layer (minx,miny,maxx,maxy)
  • Starting point: the upper left corner of the grid starting point, and the smallest geographical point is the lower left corner

Posted on Sat, 08 Feb 2020 12:03:05 -0500 by drawmack