0
const mapnik = require('mapnik');
const sphericalmercator = require('@mapbox/sphericalmercator');

mapnik.register_default_fonts();
mapnik.register_default_input_plugins();

const TILE_HEIGHT = 256;
const TILE_WIDTH  = 256;

const SRS_MERC = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over";

const mercator = new sphericalmercator({
    size: TILE_WIDTH
});

var map = new mapnik.Map(TILE_WIDTH, TILE_HEIGHT, SRS_MERC);
map.background = new mapnik.Color(255, 255, 255, 0); 
var style = ` 
<Map>
  <Style name="census_wards">
    <Rule>
      <PolygonSymbolizer fill="#00ffff" fill-opacity="0.75"  />  
      <LineSymbolizer stroke="#000000" stroke-width="0.5" stroke-opacity="1.0"  />  
    </Rule>
  </Style>
</Map>
`;
map.fromStringSync(style);
var postgis = { 
  'type': 'postgis',
  'dbname': 'test_db',
  'host': '127.0.0.1',
  'user': 'test_map',
  'password': 'TestUser',
  'table': '(SELECT * FROM z4.census_wards) AS census_wards',
  'geometry_field': 'the_geom_webmercator'
};

var layer = new mapnik.Layer('census_wards', SRS_MERC);
layer.datasource = new mapnik.Datasource(postgis);
layer.styles = ['census_wards'];
map.add_layer(layer);

var box = mercator.bbox(2940, 1846, 12, false, '900913');
map.zoomToBox(box);
console.log('box = ' + box);

var im = new mapnik.Image(TILE_WIDTH, TILE_HEIGHT);
console.log('rendering');
map.render(im, function(err, im) {
    console.log('inside render');
    if (err) throw err;
});
process.exit(0);

Control never reaches the "console.log('rendering');" line. Couple of days ago something like this was working. I am assuming that I am missing a very simple but important part due to my lack of experience with node and mapnik.

Apart from the console.log output, there is no response from the program. No errors, no other output.

Any pointers on how to get this working?

1

The problem was with the last line

process.exit(0);

map.render is a non-blocking function call. Callback given as a parameter to that function will be invoked only when the render is complete. But, process.exit(0) is called immediately after map.render. Hence render was not completed and callback was not invoked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.