Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

return early in async isSolid, and add to grid api

  • Loading branch information...
commit aea77e1e5d19188b7d691e06eef556599c44574c 1 parent e4ba050
@springmeyer springmeyer authored
View
71 src/mapnik_grid_view.cpp
@@ -31,6 +31,7 @@ void GridView::Initialize(Handle<Object> target) {
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, "isSolidSync", isSolidSync);
NODE_SET_PROTOTYPE_METHOD(constructor, "getPixel", getPixel);
target->Set(String::NewSymbol("GridView"),constructor->GetFunction());
@@ -100,10 +101,80 @@ Handle<Value> GridView::height(const Arguments& args)
return scope.Close(Integer::New(g->get()->height()));
}
+typedef struct {
+ uv_work_t request;
+ GridView* g;
+ std::string error_name;
+ Persistent<Function> cb;
+ bool result;
+} is_solid_grid_view_baton_t;
+
+
Handle<Value> GridView::isSolid(const Arguments& args)
{
HandleScope scope;
GridView* g = ObjectWrap::Unwrap<GridView>(args.This());
+
+ if (args.Length() == 0) {
+ return isSolidSync(args);
+ }
+ // ensure callback is a function
+ Local<Value> callback = args[args.Length()-1];
+ if (!args[args.Length()-1]->IsFunction())
+ return ThrowException(Exception::TypeError(
+ String::New("last argument must be a callback function")));
+
+ is_solid_grid_view_baton_t *closure = new is_solid_grid_view_baton_t();
+ closure->request.data = closure;
+ closure->g = g;
+ closure->result = true;
+ closure->cb = Persistent<Function>::New(Handle<Function>::Cast(callback));
+ uv_queue_work(uv_default_loop(), &closure->request, EIO_IsSolid, EIO_AfterIsSolid);
+ g->Ref();
+ return Undefined();
+}
+void GridView::EIO_IsSolid(uv_work_t* req)
+{
+ is_solid_grid_view_baton_t *closure = static_cast<is_solid_grid_view_baton_t *>(req->data);
+ grid_view_ptr view = closure->g->get();
+ if (view->width() > 0 && view->height() > 0)
+ {
+ mapnik::grid_view::value_type first_pixel = view->getRow(0)[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])
+ {
+ closure->result = false;
+ return;
+ }
+ }
+ }
+ }
+}
+
+void GridView::EIO_AfterIsSolid(uv_work_t* req)
+{
+ HandleScope scope;
+ is_solid_grid_view_baton_t *closure = static_cast<is_solid_grid_view_baton_t *>(req->data);
+ TryCatch try_catch;
+ Local<Value> argv[2] = { Local<Value>::New(Null()), Local<Value>::New(Boolean::New(closure->result)) };
+ closure->cb->Call(Context::GetCurrent()->Global(), 2, argv);
+ if (try_catch.HasCaught())
+ {
+ FatalException(try_catch);
+ }
+ closure->g->Unref();
+ closure->cb.Dispose();
+ delete closure;
+}
+
+Handle<Value> GridView::isSolidSync(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)
{
View
3  src/mapnik_grid_view.hpp
@@ -31,6 +31,9 @@ class GridView: public node::ObjectWrap {
static Handle<Value> width(const Arguments &args);
static Handle<Value> height(const Arguments &args);
static Handle<Value> isSolid(const Arguments &args);
+ static void EIO_IsSolid(uv_work_t* req);
+ static void EIO_AfterIsSolid(uv_work_t* req);
+ static Handle<Value> isSolidSync(const Arguments &args);
static Handle<Value> getPixel(const Arguments &args);
GridView(Grid * JSGrid);
View
2  src/mapnik_image_view.cpp
@@ -34,6 +34,7 @@ void ImageView::Initialize(Handle<Object> target) {
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, "isSolidSync", isSolidSync);
NODE_SET_PROTOTYPE_METHOD(constructor, "getPixel", getPixel);
target->Set(String::NewSymbol("ImageView"),constructor->GetFunction());
@@ -136,6 +137,7 @@ void ImageView::EIO_IsSolid(uv_work_t* req)
if (first_pixel != row[x])
{
closure->result = false;
+ return;
}
}
}
View
12 test/grid_view.test.js
@@ -17,6 +17,13 @@ describe('mapnik.GridView ', function() {
assert.equal(view.isSolid(), true);
});
+ it('should be solid (async)', function(done) {
+ view.isSolid(function(err,solid) {
+ assert.equal(solid, true);
+ done();
+ });
+ });
+
it('should have zero value for pixel', function() {
var pixel = view.getPixel(0, 0);
if (mapnik.versions.mapnik_number < 200100) {
@@ -39,7 +46,10 @@ describe('mapnik.GridView ', function() {
assert.equal(view.isSolid(), false);
// hit alaska (USA is id 207)
assert.equal(view.getPixel(25, 100), 207);
- done();
+ view.isSolid(function(err,solid){
+ assert.equal(solid, false);
+ done();
+ });
});
});
});
View
21 test/image_view.test.js
@@ -36,4 +36,25 @@ describe('mapnik.ImageView ', function() {
assert.equal(pixel.a, 2);
assert.equal(view.getPixel(99999999, 9999999), undefined);
});
+
+ it('isSolid async works if true', function(done) {
+ var im = new mapnik.Image(256, 256);
+ var view = im.view(0, 0, 256, 256);
+ assert.equal(view.isSolid(), true);
+ view.isSolid(function(err,solid) {
+ assert.equal(solid, true);
+ done();
+ });
+ });
+
+ it('isSolid async works if false', function(done) {
+ var im = new mapnik.Image.open('./test/support/a.png');
+ var view = im.view(0, 0, im.width(), im.height());
+ assert.equal(view.isSolid(), false);
+ view.isSolid(function(err,solid) {
+ assert.equal(solid, false);
+ done();
+ });
+ });
+
});
Please sign in to comment.
Something went wrong with that request. Please try again.