How to use the mapnik.Layer function in mapnik

To help you get started, we’ve selected a few mapnik examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mapnik / node-mapnik / test / render-composite.js View on Github external
it('should allow a rendering vector tiles and raster images to a vector tile', function(done) {
        // first we create a vector tile on the fly
        var vtile_global = new mapnik.VectorTile(0,0,0);
        var map = new mapnik.Map(256,256,'+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over');
        map.extent = [-20037508.34, -20037508.34, 20037508.34, 20037508.34];
        var global = new mapnik.Layer('global',map.srs)
        var options = {
            type: 'shape',
            file: './test/data/world_merc.shp',
            encoding: 'iso-8859-1'
        };
        global.datasource = new mapnik.Datasource(options);
        map.add_layer(global);
        map.render(vtile_global,{},function(err, vtile_global) {
            if (err) throw err;
            // ensure the vtile contains one layer named 'global'
            assert.deepEqual(vtile_global.names(),['global']);

            // now load a vector tile for some deeper zoom level
            // in this case we grab a tile for japan from the tests
            var japan_vtile = new mapnik.VectorTile(5,28,12);
            japan_vtile.setData(fs.readFileSync("./test/data/vector_tile/tile3.vector.pbf"));
github azavea / tilegarden / node_lambnik / src / tiler / src / _mapnik-hello-world.js View on Github external
export const serveTile = (z, x, y, inlet) => {
    // console.log('Attempting to serve tile.')
    const proj4 = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over' // noqa
    const map = new mapnik.Map(256, 256, proj4)
    // console.log(`Initial map: ${JSON.stringify(map)}`)
    const layer = new mapnik.Layer('tile', proj4)
    // console.log(`Initial layer: ${JSON.stringify(map)}`)
    const postgis = getDatasource()
    // console.log('# Datasource info:')
    // console.log(`### Description: ${JSON.stringify(postgis.describe())}`)
    // console.log(`### Fields: ${JSON.stringify(postgis.fields())}`)
    // console.log(`### Extent: ${JSON.stringify(postgis.extent())}`)
    // console.log(`### Featureset: ${JSON.stringify(postgis.featureset())}`)
    const bbox = mercator.xyz_to_envelope(x, y, z, false)// new SphericalMercator().bbox(x, y, z, false)
    // console.log(`Initial bounding box: ${JSON.stringify(bbox)}`)
    // console.log('Created constants.')


    layer.datasource = postgis
    layer.styles = ['point']

    map.bufferSize = 64
github springmeyer / coalition / server / app.js View on Github external
} else {
        
      if (query &&
          query.x !== undefined &&
          query.y !== undefined &&
          query.z !== undefined &&
          query.sql !== undefined &&
          query.style !== undefined
          ) {
    
          var bbox = mercator.xyz_to_envelope(parseInt(query.x),
                                              parseInt(query.y),
                                              parseInt(query.z), false);
          var map = new mapnik.Map(256, 256, mercator.srs);
          map.buffer_size(50);
          var layer = new mapnik.Layer('tile', mercator.srs);
          try {
              settings.postgis.table = unescape(query.sql);
              var postgis = new mapnik.Datasource(settings.postgis);
              layer.datasource = postgis;
              styles = [query.style];
              map.load(path.join(settings.styles, query.style + '.xml'));
              // labels
              styles.push('text');
              map.load(path.join(settings.styles, 'text.xml'));
              layer.styles = styles;
              map.add_layer(layer);
              // show map in terminal with toString()
              //console.log(map.toString());
          }
          catch (err) {
              res.end(err.message);
github AmericanRedCross / GeoForce / Chubbs / endpoints / mapnik / index.js View on Github external
//Validate where - TODO

            //If a where clause was passed in, and we're using a postgis datasource, allow it
            if (_self.settings.mapnik_datasource.type.toLowerCase() == 'postgis') {
                _self.settings.mapnik_datasource.table = (args.fields ? '(SELECT ' + _self.settings.routeProperties.geom_field + (args.fields ? ',' + args.fields : '') + ' from "' + _self.settings.routeProperties.table + '"' + (args.where ? ' WHERE ' + args.where : '') + ') as "' + _self.settings.routeProperties.table + '"' : '"' + _self.settings.routeProperties.table + '"');
            }
        }

      //Make the mapnik datasource.  We wait until now in case the table definition changes if a where clause is passed in above.
      _self.mapnikDatasource = (_self.settings.mapnik_datasource.describe ? _self.settings.mapnik_datasource : new mapnik.Datasource(_self.settings.mapnik_datasource));

      try {
        //create map
        var map = new mapnik.Map(256, 256, mercator.proj4);

        var layer = new mapnik.Layer(_self.settings.routeProperties.name, ((_self.epsg && (_self.epsg == 3857 || _self.epsg == 3587)) ? mercator.proj4 : geographic.proj4));

        var bbox = mercator.xyz_to_envelope(+req.param('x'), +req.param('y'), +req.param('z'), false);

        layer.datasource = _self.mapnikDatasource;
        layer.styles = [_self.table, 'default'];

        map.bufferSize = 10;

        map.add_layer(layer);
        console.log(map.toXML());

        //From Tilelive-Bridge - getTile
        // set source _maxzoom cache to prevent repeat calls to map.parameters
        if (_self._maxzoom === undefined) {
          _self._maxzoom = map.parameters.maxzoom ? parseInt(map.parameters.maxzoom, 10) : 14;
        }
github koopjs / koop-core / lib / Tiles.js View on Github external
map.extent = mercator.bbox(x, y, z, false, '900913');

              map.render( vtile, {}, function( err, vtile ) {
                if (err) {
                  callback( err, null );
                } else {
                  fs.writeFileSync( file, vtile.getData() );
                  callback( null, file );
                }
              });

            } else if ( format == 'utf') {
              var grid = new mapnik.Grid(256, 256, {key: '__id__'});
              map = new mapnik.Map(256, 256);
              map.loadSync(__dirname + '/../templates/renderers/style.xml');
              layer = new mapnik.Layer('tile');
              layer.datasource = new mapnik.Datasource( { type: 'geojson', file: jsonFile } );
              // add styles 
              var options = {layer:0};
              if (geojson && geojson.features && geojson.features.length){
                layer.styles = [geojson.features[0].geometry.type.toLowerCase()];
                options.fields = Object.keys( geojson.features[0].properties );
              }
              map.add_layer(layer);
              map.extent = mercator.bbox(x, y, z, false, '900913');


              map.render( grid, options, function( err, g ) {
                if (err) {
                  callback( err, null );
                } else {
                    var utf = g.encodeSync('utf', {resolution: 4});
github spatialdev / PGRestAPI / endpoints / tiles / index.js View on Github external
res.end(missing);
        return;
      }

      //If user passes in where clause, then build the query here and set it with the table property of postgis_settings
      if (args.where) {
        //Validate where - TODO
      }

      //We're all good. Make the picture.
      try {
        //create map and layer
        var map = new mapnik.Map(parseInt(args.width), parseInt(args.height), mercator.proj4);

        //width, height
        var layer = new mapnik.Layer(_self.table, ((_self.epsg && (_self.epsg == 3857 || _self.epsg == 3587)) ? mercator.proj4 : geographic.proj4));

        var floatbbox = args.bbox.split(",");

        var bbox = [floatbbox[0], floatbbox[1], floatbbox[2], floatbbox[3]];
        //ll lat, ll lon, ur lat, ur lon

        layer.datasource = _self.memoryDatasource;
        layer.styles = [_self.table, 'default'];
        map.bufferSize = 64;

        map.load(path.join(fullpath), {
          strict: true
        }, function (err, map) {

          map.add_layer(layer);
github spatialdev / PGRestAPI / endpoints / mapnik / index.js View on Github external
return;
                }

                if (fullpath) {

                    var geojson_settings = {
                        type: 'geojson',
                        file: fullpath
                    };

                    //We're all good. Make the picture.
                    try {
                        //create map and layer
                        var map = new mapnik.Map(parseInt(args.width), parseInt(args.height), mercator.proj4);
                        //width, height
                        var layer = new mapnik.Layer(id, ((_self.epsg && (_self.epsg == 3857 || _self.epsg == 3587)) ? mercator.proj4 : geographic.proj4));
                        //check to see if 3857.  If not, assume WGS84
                        var geojson_ds = new mapnik.Datasource(geojson_settings);

                        var floatbbox = args.bbox.split(",");

                        var bbox = [floatbbox[0], floatbbox[1], floatbbox[2], floatbbox[3]];
                        //ll lat, ll lon, ur lat, ur lon

                        layer.datasource = geojson_ds;
                        layer.styles = [id, 'default'];

                        map.bufferSize = 64;

                        var stylepath = __dirname + '/cartocss/' + _defaultMSS;

                        map.load(path.join(stylepath), {
github mapnik / node-mapnik-sample-code / tile / elastic / app.js View on Github external
} else {
                      //console.log(JSON.stringify(result.facets, null, 2));
                      //console.log(result.took + 'ms / ' + result.hits.total + ' results');
                      result.hits.hits.forEach(function(hit) {
                             var x = hit._source.project.location.lon;
                             var y = hit._source.project.location.lat;
                             var name = hit._source.project.name;
                             var pop2005 = hit._source.project.pop2005;
                             //console.log('x: ' + x + ' y: ' + y);
                             mem_ds.add({ 'x' : x,
                                          'y' : y,
                                          'properties' : { 'NAME': name, 'pop2005': pop2005 }
                                         });
                      });

                      var l = new mapnik.Layer('test');
                      l.srs = map.srs;
                      l.styles = ['style'];
                      l.datasource = mem_ds;
                      map.add_layer(l);
                      map.extent = bbox;
                      var im = new mapnik.Image(map.width, map.height);
                      map.render(im, function(err, im) {
                          if (err) {
                              res.writeHead(500, {
                                'Content-Type': 'text/plain'
                              });
                              res.end(err.message);
                          } else {
                              res.writeHead(200, {
                                'Content-Type': 'image/png'
                              });
github mapnik / node-mapnik / examples / tile / elastic / app.js View on Github external
} else {
                      //console.log(JSON.stringify(result.facets, null, 2));
                      //console.log(result.took + 'ms / ' + result.hits.total + ' results');
                      result.hits.hits.forEach( function(hit) {
                             var x = hit._source.project.location.lon;
                             var y = hit._source.project.location.lat;
                             var name = hit._source.project.name;
                             var pop2005 = hit._source.project.pop2005;
                             //console.log('x: ' + x + ' y: ' + y);
                             mem_ds.add({ 'x' : x,
                                          'y' : y,
                                          'properties' : { 'NAME':name,'pop2005':pop2005 }
                                         });
                      });
                
                      var l = new mapnik.Layer('test');
                      l.srs = map.srs;
                      l.styles = ["style"];
                      l.datasource = mem_ds;
                      map.add_layer(l);
                      map.extent = bbox;
                      var im = new mapnik.Image(map.width,map.height);
                      map.render(im, function(err, im) {
                          if (err) {
                              res.writeHead(500, {
                                'Content-Type': 'text/plain'
                              });
                              res.end(err.message);
                          } else {
                              res.writeHead(200, {
                                'Content-Type': 'image/png'
                              });
github tomalrussell / colouring-london / app / src / tiles / tile.ts View on Github external
function renderTile(tileset, z, x, y, geometryId, cb) {
    const bbox = getBbox(z, x, y)

    const map = new mapnik.Map(TILE_SIZE, TILE_SIZE, PROJ4_STRING);
    map.bufferSize = TILE_BUFFER_SIZE;
    const layer = new mapnik.Layer('tile', PROJ4_STRING);

    const tableDefinition = (tileset === 'highlight') ?
        getHighlightTableDefinition(geometryId)
        : MAP_STYLE_TABLE_DEFINITIONS[tileset];

    const conf = Object.assign({ table: tableDefinition }, DATASOURCE_CONFIG)

    var postgis;
    try {
        postgis = new mapnik.Datasource(conf);
        layer.datasource = postgis;
        layer.styles = [tileset]

        map.load(
            path.join(__dirname, '..', 'map_styles', 'polygon.xml'),
            { strict: true },