Skip to content

Commit

Permalink
Merge 56ee933 into 5d100b6
Browse files Browse the repository at this point in the history
  • Loading branch information
perliedman committed Oct 28, 2015
2 parents 5d100b6 + 56ee933 commit 406075c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/node_osmium.cpp
Expand Up @@ -5,6 +5,7 @@
// osmium
#include <osmium/geom/wkb.hpp>
#include <osmium/geom/wkt.hpp>
#include <osmium/geom/geojson.hpp>
#include <osmium/io/input_iterator.hpp>
#include <osmium/memory/buffer.hpp>
#include <osmium/visitor.hpp>
Expand All @@ -31,6 +32,7 @@ namespace node_osmium {
v8::Persistent<v8::Object> module;
osmium::geom::WKBFactory<> wkb_factory;
osmium::geom::WKTFactory<> wkt_factory;
osmium::geom::GeoJSONFactory<> geojson_factory;

v8::Persistent<v8::String> symbol_OSMEntity;
v8::Persistent<v8::String> symbol_OSMObject;
Expand Down
23 changes: 23 additions & 0 deletions src/osm_area_wrap.cpp
Expand Up @@ -6,6 +6,7 @@
// osmium
#include <osmium/geom/wkb.hpp>
#include <osmium/geom/wkt.hpp>
#include <osmium/geom/geojson.hpp>

// node-osmium
#include "osm_area_wrap.hpp"
Expand All @@ -14,6 +15,7 @@ namespace node_osmium {

extern osmium::geom::WKBFactory<> wkb_factory;
extern osmium::geom::WKTFactory<> wkt_factory;
extern osmium::geom::GeoJSONFactory<> geojson_factory;
extern v8::Persistent<v8::Object> module;

v8::Persistent<v8::FunctionTemplate> OSMAreaWrap::constructor;
Expand All @@ -28,6 +30,7 @@ namespace node_osmium {
set_accessor(constructor, "type", get_type, attributes);
node::SetPrototypeMethod(constructor, "wkb", wkb);
node::SetPrototypeMethod(constructor, "wkt", wkt);
node::SetPrototypeMethod(constructor, "geojson", geojson);
target->Set(symbol_Area, constructor->GetFunction());
}

Expand Down Expand Up @@ -57,6 +60,26 @@ namespace node_osmium {
}
}

v8::Handle<v8::Value> OSMAreaWrap::geojson(const v8::Arguments& args) {
INSTANCE_CHECK(OSMAreaWrap, "Area", "geojson");
v8::HandleScope scope;

try {
std::string geojson { geojson_factory.create_multipolygon(wrapped(args.This())) };

v8::Handle<v8::Context> context = v8::Context::GetCurrent();
v8::Handle<v8::Object> global = context->Global();

v8::Handle<v8::Object> JSON = global->Get(v8::String::New("JSON"))->ToObject();
v8::Handle<v8::Function> JSON_parse = v8::Handle<v8::Function>::Cast(JSON->Get(v8::String::New("parse")));

v8::Handle<v8::Value> jsonString = v8::String::New(geojson.c_str());
return scope.Close(JSON_parse->Call(JSON, 1, &jsonString));
} catch (std::runtime_error& e) {
return ThrowException(v8::Exception::Error(v8::String::New(e.what())));
}
}

v8::Handle<v8::Value> OSMAreaWrap::wkt(const v8::Arguments& args) {
INSTANCE_CHECK(OSMAreaWrap, "Area", "wkt");
v8::HandleScope scope;
Expand Down
1 change: 1 addition & 0 deletions src/osm_area_wrap.hpp
Expand Up @@ -26,6 +26,7 @@ namespace node_osmium {

static v8::Handle<v8::Value> wkb(const v8::Arguments& args);
static v8::Handle<v8::Value> wkt(const v8::Arguments& args);
static v8::Handle<v8::Value> geojson(const v8::Arguments& args);

public:

Expand Down

0 comments on commit 406075c

Please sign in to comment.