Skip to content

Commit

Permalink
add isSolid/getPixel implementations for mapnik.GridView - closes map…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Jan 30, 2012
1 parent 4dd846b commit 0345b66
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
63 changes: 60 additions & 3 deletions src/mapnik_grid_view.cpp
Expand Up @@ -30,6 +30,8 @@ void GridView::Initialize(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor, "encode", encode);
NODE_SET_PROTOTYPE_METHOD(constructor, "width", width);
NODE_SET_PROTOTYPE_METHOD(constructor, "height", height);
NODE_SET_PROTOTYPE_METHOD(constructor, "isSolid", isSolid);
NODE_SET_PROTOTYPE_METHOD(constructor, "getPixel", getPixel);

target->Set(String::NewSymbol("GridView"),constructor->GetFunction());
}
Expand Down Expand Up @@ -71,9 +73,8 @@ Handle<Value> GridView::New(boost::shared_ptr<mapnik::grid> grid_ptr,
)
{
HandleScope scope;
typedef boost::shared_ptr<mapnik::grid_view> grid_view_ptr_type;
grid_view_ptr_type grid_view_ptr = boost::make_shared<mapnik::grid_view>(grid_ptr->get_view(x,y,w,h));
GridView* gv = new GridView(grid_view_ptr);
grid_view_ptr gb_ptr = boost::make_shared<mapnik::grid_view>(grid_ptr->get_view(x,y,w,h));
GridView* gv = new GridView(gb_ptr);
Handle<Value> ext = External::New(gv);
Handle<Object> obj = constructor->GetFunction()->NewInstance(1, &ext);
return scope.Close(obj);
Expand All @@ -95,6 +96,62 @@ Handle<Value> GridView::height(const Arguments& args)
return scope.Close(Integer::New(g->get()->height()));
}

Handle<Value> GridView::isSolid(const Arguments& args)
{
HandleScope scope;
GridView* g = ObjectWrap::Unwrap<GridView>(args.This());
grid_view_ptr view = g->get();
if (view->width() > 0 && view->height() > 0)
{
mapnik::grid_view::value_type const* first_row = view->getRow(0);
mapnik::grid_view::value_type const first_pixel = first_row[0];
for (unsigned y = 0; y < view->height(); ++y)
{
mapnik::grid_view::value_type const * row = view->getRow(y);
for (unsigned x = 0; x < view->width(); ++x)
{
if (first_pixel != row[x])
{
return scope.Close(Boolean::New(false));
}
}
}
}
return scope.Close(Boolean::New(true));
}

Handle<Value> GridView::getPixel(const Arguments& args)
{
HandleScope scope;


unsigned x(0);
unsigned y(0);

if (args.Length() >= 2) {
if (!args[0]->IsNumber())
return ThrowException(Exception::TypeError(
String::New("first arg, 'x' must be an integer")));
if (!args[1]->IsNumber())
return ThrowException(Exception::TypeError(
String::New("second arg, 'y' must be an integer")));
x = args[0]->IntegerValue();
y = args[1]->IntegerValue();
} else {
return ThrowException(Exception::TypeError(
String::New("must supply x,y to query pixel color")));
}

GridView* g = ObjectWrap::Unwrap<GridView>(args.This());
grid_view_ptr view = g->get();
if (x < view->width() && y < view->height())
{
mapnik::grid_view::value_type const * row = view->getRow(y);
mapnik::grid_view::value_type const pixel = row[x];
return Integer::New(pixel);
}
return Undefined();
}

Handle<Value> GridView::encodeSync(const Arguments& args)
{
Expand Down
2 changes: 2 additions & 0 deletions src/mapnik_grid_view.hpp
Expand Up @@ -24,6 +24,8 @@ class GridView: public node::ObjectWrap {
static Handle<Value> encode(const Arguments &args);
static Handle<Value> width(const Arguments &args);
static Handle<Value> height(const Arguments &args);
static Handle<Value> isSolid(const Arguments &args);
static Handle<Value> getPixel(const Arguments &args);

GridView(grid_view_ptr gp);
inline grid_view_ptr get() { return this_; }
Expand Down
30 changes: 30 additions & 0 deletions test/grid_view.test.js
@@ -0,0 +1,30 @@
var mapnik = require('mapnik');
var assert = require('assert');
var fs = require('fs');
var path = require('path');


exports['test grid view getPixel'] = function(beforeExit) {
var grid = new mapnik.Grid(256, 256);
var view = grid.view(0,0,256,256);
assert.equal(view.isSolid(),true);

var pixel = view.getPixel(0,0);
assert.equal(pixel,0);

var map = new mapnik.Map(256, 256);
map.loadSync('./examples/stylesheet.xml');
map.zoomAll();
var options = {'layer': 0,
'fields': ['NAME']
};
var grid = new mapnik.Grid(map.width, map.height, {key: '__id__'});
map.render(grid, options, function(err, grid) {

var view = grid.view(0,0,256,256);
assert.equal(view.isSolid(),false);
// hit alaska (USA is id 207)
assert.equal(view.getPixel(25,100),207);
})

};

0 comments on commit 0345b66

Please sign in to comment.