Permalink
Browse files

fix examples to work with latest generic-pool (2.x) and remove a few …

…that are not very good practice and should not be followed - closes #123
  • Loading branch information...
1 parent dfb87e8 commit 9f3aac292e9ca3b2773f243fa0f721cd10cbaecf @springmeyer springmeyer committed Nov 8, 2012
View
6 examples/README.md
@@ -16,7 +16,6 @@ Examples are arranged into topic directories:
* **tile** - TMS style tileservers. Also includes PostGIS example.
* **wms** - WMS servers
* **memory_datasource** - rendering of native javascript objects using in memory features created up front
-* **js_datasource** - rendering of native javascript objects using callback approach (WARNING - does not work outside of main event loop)
## Other files
@@ -29,8 +28,3 @@ Examples are arranged into topic directories:
To run most examples, read the code then execute their app.js file.
In tile examples you can view the output in polymaps via tile/index.html
-
-TODO
------
-* interaction
-* js_datasource + interaction
View
5 examples/simple/blank.js
@@ -9,10 +9,13 @@ var path = require('path');
var port = 8000;
var im = new mapnik.Image(256, 256);
+im.background = new mapnik.Color('steelblue');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'image/png'});
- res.end(im.encodeSync('png8'));
+ im.encode('png8:z=1',function(err,buffer) {
+ res.end(buffer);
+ });
}).listen(port);
console.log('server running on port ' + port);
View
2 examples/simple/render.js
@@ -1,6 +1,6 @@
// This example shows how to use node-mapnik to render
-// a map to a file
+// a map to a image on disk
//
// expected output: http://goo.gl/cyGwo
View
72 examples/simple/simple_pool.js
@@ -1,72 +0,0 @@
-
-// This example shows how to use (generic-pool) node-pool with mapnik
-// to maintain a pool of renderers
-//
-// expected output: http://goo.gl/cyGwo
-
-var http = require('http');
-var mapnik = require('mapnik');
-var mappool = require('../utils/pool.js');
-var path = require('path');
-
-// create a pool of 10 maps
-// this allows us to manage concurrency under high load
-var maps = mappool.create_pool(10);
-
-var port = 8000;
-
-var stylesheet = path.join(__dirname, '../stylesheet.xml');
-
-var aquire = function(id,options,callback) {
- methods = {
- create: function(cb) {
- var obj = new mapnik.Map(options.width || 256, options.height || 256);
- obj.load(id, {strict: true},function(err,obj) {
- if (err) callback(err, null);
- if (options.buffer_size) obj.buffer_size(options.buffer_size);
- cb(obj);
- });
- },
- destroy: function(obj) {
- delete obj;
- }
- };
- maps.acquire(id,
- methods,
- function(obj) {
- callback(null, obj);
- }
- );
-};
-
-http.createServer(function(req, res) {
- aquire(stylesheet, {},function(err,map) {
- if (err) {
- res.writeHead(500, {
- 'Content-Type': 'text/plain'
- });
- res.end(err.message);
- } else {
- // zoom to full extent
- map.zoomAll();
- // real example we would pass a bbox
- var im = new mapnik.Image(map.width, map.height);
- map.render(im, function(err, im) {
- maps.release(stylesheet, map);
- if (err) {
- res.writeHead(500, {
- 'Content-Type': 'text/plain'
- });
- res.end(err.message);
- } else {
- res.writeHead(200, {
- 'Content-Type': 'image/png'
- });
- res.end(im.encodeSync('png'));
- }
- });
- }
- });
-}).listen(port);
-
-console.log('server running on port ' + port);
View
26 examples/tile/pool/app.js
@@ -33,25 +33,23 @@ if (!port) {
}
var aquire = function(id,options,callback) {
- var methods = {
+ methods = {
create: function(cb) {
var obj = new mapnik.Map(options.width || 256, options.height || 256);
obj.load(id, {strict: true},function(err,obj) {
- if (err) callback(err, null);
- if (options.buffer_size) obj.buffer_size(options.buffer_size);
- cb(obj);
+ if (options.bufferSize) {
+ obj.bufferSize = options.bufferSize;
+ }
+ cb(err,obj);
});
},
destroy: function(obj) {
delete obj;
}
};
- maps.acquire(id,
- methods,
- function(obj) {
- callback(null, obj);
- }
- );
+ maps.acquire(id,methods,function(err,obj) {
+ callback(err, obj);
+ });
};
@@ -65,18 +63,22 @@ http.createServer(function(req, res) {
} else {
aquire(stylesheet, {}, function(err, map) {
if (err) {
+ process.nextTick(function() {
+ maps.release(stylesheet, map);
+ });
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end(err.message);
} else {
// bbox for x,y,z
var bbox = mercator.xyz_to_envelope(params.x, params.y, params.z, TMS_SCHEME);
-
map.extent = bbox;
var im = new mapnik.Image(map.width, map.height);
map.render(im, function(err, im) {
- maps.release(stylesheet, map);
+ process.nextTick(function() {
+ maps.release(stylesheet, map);
+ });
if (err) {
res.writeHead(500, {
'Content-Type': 'text/plain'
View
98 examples/wms/wms.js
@@ -1,98 +0,0 @@
-
-var http = require('http');
-var mapnik = require('mapnik');
-var url = require('url');
-
-var port = 8000;
-var pool_size = 10;
-var render_pool = [];
-var async_render = false;
-var use_map_pool = true;
-
-
-var usage = 'usage: wms.js <stylesheet> <port>';
-
-var stylesheet = process.argv[2];
-
-if (!stylesheet) {
- console.log(usage);
- process.exit(1);
-}
-
-var port = process.argv[3];
-
-if (!port) {
- console.log(usage);
- process.exit(1);
-}
-
-
-for (i = 0; i < pool_size; i++) {
- var map = new mapnik.Map(256, 256);
- map.loadSync(stylesheet);
- console.log('adding new map to pool: ' + i);
- render_pool[i] = map;
-}
-
-var next_map_ = 0;
-
-// http://journal.paul.querna.org/articles/2010/09/04/limiting-concurrency-node-js/
-function get_map()
-{
- //console.log('pulling map #'+next_map_+' from map pool');
- var map = render_pool[next_map_];
- ++next_map_;
- if (next_map_ == pool_size)
- next_map_ = 0;
- return map;
-}
-
-http.createServer(function(req, res) {
- var query = url.parse(req.url.toLowerCase(), true).query;
- if (query && query.bbox !== undefined) {
- var bbox = query.bbox.split(',');
- res.writeHead(200, {'Content-Type': 'image/png'});
- var map;
-
- if (use_map_pool) {
- map = get_map();
- }
- else {
- map = new mapnik.Map(256, 256);
- map.loadSync(stylesheet);
- }
-
- if (query.width !== undefined && query.height !== undefined) {
- map.resize(parseInt(query.width, 10), parseInt(query.height, 10));
- }
-
- if (async_render) {
- var im = new mapnik.Image(map.width, map.height);
- map.bufferSize = 128;
- map.extent = bbox;
- map.render(im, function(err, im) {
- if (err) {
- res.writeHead(500, {
- 'Content-Type': 'text/plain'
- });
- res.end(err.message);
- } else {
- res.end(im.encodeSync('png'));
- }
- });
- }
- else {
- map.extent = bbox;
- res.end(map.renderSync('png'));
- }
-
- } else {
- res.writeHead(200, {
- 'Content-Type': 'text/html'
- });
- res.end('No bbox provided! Try a request like <a href="http://127.0.0.1:' + port + '/?BBOX=-20037508.34,-5009377.085697313,-5009377.08569731,15028131.25709193">this</a>');
- }
-}).listen(port);
-
-
-console.log('Server running at http://127.0.0.1:' + port + '/');
View
12 examples/wms/wms_pool.js
@@ -30,23 +30,19 @@ var aquire = function(id,options,callback) {
create: function(cb) {
var obj = new mapnik.Map(options.width || 256, options.height || 256);
obj.load(id, {strict: true},function(err,obj) {
- if (err) callback(err, null);
if (options.bufferSize) {
obj.bufferSize = options.bufferSize;
}
- cb(obj);
+ cb(err,obj);
});
},
destroy: function(obj) {
delete obj;
}
};
- maps.acquire(id,
- methods,
- function(obj) {
- callback(null, obj);
- }
- );
+ maps.acquire(id,methods,function(err,obj) {
+ callback(err, obj);
+ });
};

0 comments on commit 9f3aac2

Please sign in to comment.