Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should allow a rendering vector tiles and raster images to an image', 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
japan_vtile.setData(fs.readFileSync("./test/data/vector_tile/tile3.vector.pbf"));
// ensure the vtile contains one layer named 'world'
assert.deepEqual(japan_vtile.names(),['world']);
// now load up a raster image to composite into the final rendered image
// 128 is used here just for testing purposed - you will want to stick to 256 px images
var raster = new mapnik.Image(128,128);
// semi transparent blue
raster.background = new mapnik.Color('rgba(0,0,255,.5)');
// image has alpha so it needs to be premultiplied before passing into renderer
raster.premultiply();
// okay, sweet, now render these various sources into a new vector tile
var sources = [vtile_global,{'name':'raster','image':raster},japan_vtile];
var opts = {scale:1.0,buffer_size:256};
var map_composite = new mapnik.Map(256,256);
map_composite.loadSync('./test/data/render-composite/composite.xml');
var z = 5;
var x = 28;
var y = 12;
mapnik.render(z,x,y,map_composite,new mapnik.VectorTile(z,x,y),sources,opts,function(err,v) {
if (err) throw err;
var actual = './test/data/render-composite/composite-actual.json';
var expected = actual.replace('actual','expected');
fs.writeFileSync(actual,JSON.stringify(v.toJSON(),null,1));
assert.equal(fs.readFileSync(actual).length,
fs.readFileSync(expected).length);
done();
});
});
});
getTile(z, x, y, callback) {
const encoding = this._imageEncoding;
const size = this._size;
const map = new mapnik.Map(size, size);
map.bufferSize = this._bufferSize;
try {
// TODO: It is not smart or performant to create a new mapnik instance for each tile rendering
this.stats.rendered += 1;
map.fromString(this._xml, this._mapOptions, function onMapLoaded(err) {
if (err) return callback(err);
map.extent = sm.bbox(x, y, z, false, '900913');
map.render(new mapnik.Image(size, size), {}, function onImageRendered(err, image) {
if (err) return callback(err);
image.encode(encoding, function onImageEncoded(err, encodedImage) {
callback(err, encodedImage);
});
});
});
} catch (e) {
s += ' ';
s += ' ';
s += ' ';
s += ' ';
s += ' ';
s += ' ';
s += ' ';
s += ' ';
s += ' ';
s += ' ';
s += ' ';
s += '';
s += '';
// create map object
var map = new mapnik.Map(256,256);
map.from_string(s,'.');
// go get some arbitrary data that we can stream
var shp = path.join(__dirname,'../data/world_merc');
var ds = new mapnik.Datasource({
type: 'shape',
file: shp
});
// get the featureset that exposes lazy next() iterator
var featureset = ds.featureset();
var mem_datasource = new mapnik.MemoryDatasource(
{'extent':'-20037508.342789,-8283343.693883,20037508.342789,18365151.363070'}
)
}
//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
//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.where ? '(SELECT ' + _self.settings.routeProperties.geom_field + ' from "' + _self.settings.routeProperties.name + '" WHERE ' + args.where + ') as "' + _self.settings.routeProperties.name + '"' : '"' + _self.settings.routeProperties.name + '"');
}
}
//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.settings.routeProperties.name, ((_self.settings.routeProperties.srid && (_self.settings.routeProperties.srid == 3857 || _self.settings.routeProperties.srid == 3587)) ? mercator.proj4 : geographic.proj4));
var floatbbox = args.bbox.split(",");
//ll lat, ll lon, ur lat, ur lon
var bbox = [floatbbox[0], floatbbox[1], floatbbox[2], floatbbox[3]];
var datasource = new mapnik.Datasource(_self.settings.mapnik_datasource);
layer.datasource = datasource;
layer.styles = [_self.settings.routeProperties.name, _self.settings.routeProperties.defaultStyle || 'default'];
map.bufferSize = 64;
map.load(path.join(fullpath), {
async function toPdf(destFile, xml, zoom, bbox0, scale = 1, width, cancelHolder) {
if (pdfLockCount >= renderToPdfConcurrency) {
await new Promise((unlock) => {
pdfUnlocks.push(unlock);
});
}
if (cancelHolder && cancelHolder.cancelled) {
throw new Error('Cancelled');
}
pdfLockCount++;
try {
const bbox = merc.forward(bbox0);
const q = Math.pow(2, zoom) / 200000;
const map = new mapnik.Map(
width || (bbox[2] - bbox[0]) * q,
width ? (bbox[3] - bbox[1]) / (bbox[2] - bbox[0]) * width : (bbox[3] - bbox[1]) * q,
);
await map.fromStringAsync(xml);
map.zoomToBox(bbox);
await map.renderFileAsync(destFile, { format: 'pdf', buffer_size: 256, scale_denominator: zoomDenoms[zoom], scale });
} finally {
const unlock = pdfUnlocks.shift();
if (unlock) {
unlock();
}
pdfLockCount--;
}
}
Map.prototype.create = function() {
if (this.mapnik) return true;
this.mapnik = new mapnik.Map(
this.options.width,
this.options.height
);
try {
this.mapnik.load(this.mapfilePos());
this.mapnik.buffer_size(128);
return true;
} catch (err) {
return false;
}
};
create: function(cb) {
var obj = new mapnik.Map(options.width || 256, options.height || 256);
obj.load(id, {strict: true},function(err,obj) {
if (options.bufferSize) {
obj.bufferSize = options.bufferSize;
}
cb(err,obj);
});
},
destroy: function(obj) {
if (err) {
//TODO: Handle this.
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), geographic.proj4);
//width, height
var layer = new mapnik.Layer("geojson", ((_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 bboxArray = [bbox.xmin, bbox.ymax, bbox.xmax, bbox.ymin];
layer.datasource = geojson_ds;
layer.styles = ["geojson", 'default'];
map.bufferSize = 64;
var stylepath = __dirname + '/cartocss/' + _defaultMSS;
map.load(path.join(stylepath), {
strict: true