Skip to content

Commit

Permalink
expose image_view and grid_view
Browse files Browse the repository at this point in the history
  • Loading branch information
Dane Springmeyer committed Jun 16, 2011
1 parent 565e65d commit a6d065c
Show file tree
Hide file tree
Showing 13 changed files with 629 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/js_grid_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static void write_features(T const& grid_type,
Local<Object>& feature_data,
std::vector<typename T::lookup_type> const& key_order)
{
std::string const& key = grid_type.get_join_field(); // get_key();
std::string const& key = grid_type.get_key(); // get_key();
std::set<std::string> const& attributes = grid_type.property_names();
typename T::feature_type const& g_features = grid_type.get_grid_features();
typename T::feature_type::const_iterator feat_itr = g_features.begin();
Expand Down
7 changes: 5 additions & 2 deletions src/mapnik_.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "utils.hpp"
#include "mapnik_•.hpp"

#include <boost/make_shared.hpp>

Persistent<FunctionTemplate> •::constructor;

void •::Initialize(Handle<Object> target) {
Expand All @@ -15,13 +18,13 @@ void •::Initialize(Handle<Object> target) {

•::•(std::string const& name) :
ObjectWrap(),
this_(new mapnik::•(name)) {}
this_(boost::make_shared<mapnik::•>(name)) {}

•::~•()
{
}

Handle<Value> •::New(const Arguments& args)
{
HandleScope scope;
HandleScope scope;
}
24 changes: 22 additions & 2 deletions src/mapnik_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
// mapnik
#include <mapnik/image_data.hpp>

// boost
#include <boost/make_shared.hpp>

#include "mapnik_grid.hpp"
#include "mapnik_grid_view.hpp"
#include "js_grid_utils.hpp"
#include "utils.hpp"

Expand All @@ -27,6 +30,7 @@ void Grid::Initialize(Handle<Object> target) {
// methods
NODE_SET_PROTOTYPE_METHOD(constructor, "encode", encode);
NODE_SET_PROTOTYPE_METHOD(constructor, "fields", fields);
NODE_SET_PROTOTYPE_METHOD(constructor, "view", view);
NODE_SET_PROTOTYPE_METHOD(constructor, "width", width);
NODE_SET_PROTOTYPE_METHOD(constructor, "height", height);

Expand All @@ -38,7 +42,7 @@ void Grid::Initialize(Handle<Object> target) {

Grid::Grid(unsigned int width, unsigned int height, std::string const& key, unsigned int resolution) :
ObjectWrap(),
this_(new mapnik::grid(width,height,key,resolution)) {}
this_(boost::make_shared<mapnik::grid>(width,height,key,resolution)) {}

Grid::Grid(grid_ptr this_) :
ObjectWrap(),
Expand Down Expand Up @@ -100,7 +104,6 @@ Handle<Value> Grid::New(const Arguments& args)
}
}


Grid* g = new Grid(args[0]->IntegerValue(),args[1]->IntegerValue(),key,resolution);
g->Wrap(args.This());
return args.This();
Expand Down Expand Up @@ -261,5 +264,22 @@ Handle<Value> Grid::encode(const Arguments& args) // format, resolution
return ThrowException(Exception::Error(
String::New(ex.what())));
}
}

Handle<Value> Grid::view(const Arguments& args)
{
HandleScope scope;

if ( (!args.Length() == 4) || (!args[0]->IsNumber() && !args[1]->IsNumber() && !args[2]->IsNumber() && !args[3]->IsNumber() ))
return ThrowException(Exception::TypeError(
String::New("requires 4 integer arguments: x, y, width, height")));

unsigned x = args[0]->IntegerValue();
unsigned y = args[1]->IntegerValue();
unsigned w = args[2]->IntegerValue();
unsigned h = args[3]->IntegerValue();

Grid* g = ObjectWrap::Unwrap<Grid>(args.This());
return scope.Close(GridView::New(g->get(),x,y,w,h));
}

5 changes: 3 additions & 2 deletions src/mapnik_grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class Grid: public node::ObjectWrap {
static void Initialize(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
static Handle<Value> encode(const Arguments &args);
static Handle<Value> fields(const Arguments &args);
static Handle<Value> view(const Arguments &args);
static Handle<Value> width(const Arguments &args);
static Handle<Value> height(const Arguments &args);
static Handle<Value> view(const Arguments &args);

static Handle<Value> fields(const Arguments &args);

static Handle<Value> get_prop(Local<String> property,
const AccessorInfo& info);
Expand Down
184 changes: 184 additions & 0 deletions src/mapnik_grid_view.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@

// node
#include <node_buffer.h>
#include <node_version.h>

// mapnik
#include <mapnik/grid/grid_view.hpp>

// boost
#include <boost/make_shared.hpp>

#include "mapnik_grid_view.hpp"
#include "js_grid_utils.hpp"
#include "utils.hpp"

// std
#include <exception>

Persistent<FunctionTemplate> GridView::constructor;

void GridView::Initialize(Handle<Object> target) {

HandleScope scope;

constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(GridView::New));
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(String::NewSymbol("GridView"));

NODE_SET_PROTOTYPE_METHOD(constructor, "encode", encode);
NODE_SET_PROTOTYPE_METHOD(constructor, "width", width);
NODE_SET_PROTOTYPE_METHOD(constructor, "height", height);

target->Set(String::NewSymbol("GridView"),constructor->GetFunction());
}


GridView::GridView(grid_view_ptr gp) :
ObjectWrap(),
this_(gp) {}

GridView::~GridView()
{
}

Handle<Value> GridView::New(const Arguments& args)
{
HandleScope scope;
if (!args.IsConstructCall())
return ThrowException(String::New("Cannot call constructor as function, you need to use 'new' keyword"));

if (args[0]->IsExternal())
{
Local<External> ext = Local<External>::Cast(args[0]);
void* ptr = ext->Value();
GridView* g = static_cast<GridView*>(ptr);
g->Wrap(args.This());
return args.This();
} else {
return ThrowException(String::New("Cannot create this object from Javascript"));
}

return Undefined();
}

Handle<Value> GridView::New(boost::shared_ptr<mapnik::grid> grid_ptr,
unsigned x,
unsigned y,
unsigned w,
unsigned h
)
{
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);
Handle<Value> ext = External::New(gv);
Handle<Object> obj = constructor->GetFunction()->NewInstance(1, &ext);
return scope.Close(obj);
}

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

GridView* g = ObjectWrap::Unwrap<GridView>(args.This());
return scope.Close(Integer::New(g->get()->width()));
}

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

GridView* g = ObjectWrap::Unwrap<GridView>(args.This());
return scope.Close(Integer::New(g->get()->height()));
}


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

GridView* g = ObjectWrap::Unwrap<GridView>(args.This());

// defaults
std::string format("utf");
unsigned int resolution = 4;
bool add_features = true;

// accept custom format
if (args.Length() >= 1){
if (!args[0]->IsString())
return ThrowException(Exception::TypeError(
String::New("first arg, 'format' must be a string")));
format = TOSTR(args[0]);
}

// options hash
if (args.Length() >= 2) {
if (!args[1]->IsObject())
return ThrowException(Exception::TypeError(
String::New("optional second arg must be an options object")));

Local<Object> options = args[1]->ToObject();

if (options->Has(String::New("resolution")))
{
Local<Value> bind_opt = options->Get(String::New("resolution"));
if (!bind_opt->IsNumber())
return ThrowException(Exception::TypeError(
String::New("'resolution' must be an Integer")));

resolution = bind_opt->IntegerValue();
}

if (options->Has(String::New("features")))
{
Local<Value> bind_opt = options->Get(String::New("features"));
if (!bind_opt->IsBoolean())
return ThrowException(Exception::TypeError(
String::New("'features' must be an Boolean")));

add_features = bind_opt->BooleanValue();
}
}

try {

Local<Array> grid_array = Array::New();
std::vector<mapnik::grid_view::lookup_type> key_order;
node_mapnik::grid2utf<mapnik::grid_view>(*g->get(),grid_array,key_order,resolution);

// convert key order to proper javascript array
Local<Array> keys_a = Array::New(key_order.size());
std::vector<std::string>::iterator it;
unsigned int i;
for (it = key_order.begin(), i = 0; it < key_order.end(); ++it, ++i)
{
keys_a->Set(i, String::New((*it).c_str()));
}

// gather feature data
Local<Object> feature_data = Object::New();
if (add_features) {
node_mapnik::write_features<mapnik::grid_view>(*g->get(),
feature_data,
key_order
);
}

// Create the return hash.
Local<Object> json = Object::New();
json->Set(String::NewSymbol("grid"), grid_array);
json->Set(String::NewSymbol("keys"), keys_a);
json->Set(String::NewSymbol("data"), feature_data);
return json;

}
catch (std::exception & ex)
{
return ThrowException(Exception::Error(
String::New(ex.what())));
}

}
35 changes: 35 additions & 0 deletions src/mapnik_grid_view.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef __NODE_MAPNIK_GRID_VIEW_H__
#define __NODE_MAPNIK_GRID_VIEW_H__

#include <v8.h>
#include <node.h>
#include <node_object_wrap.h>
#include <mapnik/grid/grid_view.hpp>
#include <mapnik/grid/grid.hpp>
#include <boost/shared_ptr.hpp>

using namespace v8;
using namespace node;

typedef boost::shared_ptr<mapnik::grid_view> grid_view_ptr;

class GridView: public node::ObjectWrap {
public:
static Persistent<FunctionTemplate> constructor;
static void Initialize(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
static Handle<Value> New(boost::shared_ptr<mapnik::grid> grid_ptr,
unsigned x,unsigned y, unsigned w, unsigned h);
static Handle<Value> encode(const Arguments &args);
static Handle<Value> width(const Arguments &args);
static Handle<Value> height(const Arguments &args);

GridView(grid_view_ptr gp);
inline grid_view_ptr get() { return this_; }

private:
~GridView();
grid_view_ptr this_;
};

#endif
Loading

0 comments on commit a6d065c

Please sign in to comment.