Handle<Value> OSMNodeWrap::wkb(const Arguments& args) { HandleScope scope; std::string wkb { wkb_factory.create_point(wrapped(args.This())) }; #if NODE_VERSION_AT_LEAST(0, 10, 0) return scope.Close(node::Buffer::New(wkb.data(), wkb.size())->handle_); #else return scope.Close(node::Buffer::New(const_cast<char*>(wkb.data()), wkb.size())->handle_); #endif }
v8::Handle<v8::Value> OSMNodeWrap::wkb(const v8::Arguments& args) { v8::HandleScope scope; try { std::string wkb { wkb_factory.create_point(wrapped(args.This())) }; #if NODE_VERSION_AT_LEAST(0, 10, 0) return scope.Close(node::Buffer::New(wkb.data(), wkb.size())->handle_); #else return scope.Close(node::Buffer::New(const_cast<char*>(wkb.data()), wkb.size())->handle_); #endif } catch (std::runtime_error& e) { return ThrowException(v8::Exception::Error(v8::String::New(e.what()))); } }
#if __BYTE_ORDER == __LITTLE_ENDIAN #include "area_helper.hpp" #include "wnl_helper.hpp" std::string to_wkb(const OGRGeometry* geometry) { std::string buffer; buffer.resize(geometry->WkbSize()); geometry->exportToWkb(wkbNDR, reinterpret_cast<unsigned char*>(&*buffer.begin())); return buffer; } TEST_CASE("compare WKB point against GDAL/OGR") { osmium::geom::WKBFactory<> wkb_factory{osmium::geom::wkb_type::wkb}; osmium::geom::OGRFactory<> ogr_factory; const osmium::Location loc{3.2, 4.2}; const std::string wkb{wkb_factory.create_point(loc)}; const std::unique_ptr<OGRPoint> geometry = ogr_factory.create_point(loc); REQUIRE(to_wkb(geometry.get()) == wkb); } TEST_CASE("compare WKB linestring against GDAL/OGR") { osmium::geom::WKBFactory<> wkb_factory{osmium::geom::wkb_type::wkb}; osmium::geom::OGRFactory<> ogr_factory; osmium::memory::Buffer buffer{10000}; const auto& wnl = create_test_wnl_okay(buffer);
namespace node_osmium { extern v8::Persistent<v8::Object> module; extern osmium::geom::WKBFactory<> wkb_factory; extern osmium::geom::WKTFactory<> wkt_factory; v8::Persistent<v8::FunctionTemplate> OSMNodeWrap::constructor; void OSMNodeWrap::Initialize(v8::Handle<v8::Object> target) { v8::HandleScope scope; constructor = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New(OSMNodeWrap::New)); constructor->Inherit(OSMObjectWrap::constructor); constructor->InstanceTemplate()->SetInternalFieldCount(1); constructor->SetClassName(v8::String::NewSymbol("Node")); node::SetPrototypeMethod(constructor, "wkb", wkb); node::SetPrototypeMethod(constructor, "wkt", wkt); auto attributes = static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete); set_accessor(constructor, "location", get_coordinates, attributes); set_accessor(constructor, "coordinates", get_coordinates, attributes); set_accessor(constructor, "lon", get_lon, attributes); set_accessor(constructor, "lat", get_lat, attributes); target->Set(v8::String::NewSymbol("Node"), constructor->GetFunction()); } v8::Handle<v8::Value> OSMNodeWrap::New(const v8::Arguments& args) { if (args.Length() == 1 && args[0]->IsExternal()) { v8::Local<v8::External> ext = v8::Local<v8::External>::Cast(args[0]); static_cast<OSMNodeWrap*>(ext->Value())->Wrap(args.This()); return args.This(); } else { return ThrowException(v8::Exception::TypeError(v8::String::New("osmium.Node cannot be created in Javascript"))); } } v8::Handle<v8::Value> OSMNodeWrap::get_coordinates(v8::Local<v8::String> /* property */, const v8::AccessorInfo& info) { v8::HandleScope scope; auto cf = module->Get(v8::String::NewSymbol("Coordinates")); assert(cf->IsFunction()); const osmium::Location& location = wrapped(info.This()).location(); if (!location) { return scope.Close(v8::Local<v8::Function>::Cast(cf)->NewInstance()); } v8::Local<v8::Value> lon = v8::Number::New(location.lon_without_check()); v8::Local<v8::Value> lat = v8::Number::New(location.lat_without_check()); v8::Local<v8::Value> argv[2] = { lon, lat }; return scope.Close(v8::Local<v8::Function>::Cast(cf)->NewInstance(2, argv)); } v8::Handle<v8::Value> OSMNodeWrap::get_lon(v8::Local<v8::String> /* property */, const v8::AccessorInfo& info) { v8::HandleScope scope; try { return scope.Close(v8::Number::New(wrapped(info.This()).location().lon())); } catch (osmium::invalid_location&) { return scope.Close(v8::Undefined()); } } v8::Handle<v8::Value> OSMNodeWrap::get_lat(v8::Local<v8::String> /* property */, const v8::AccessorInfo& info) { v8::HandleScope scope; try { return scope.Close(v8::Number::New(wrapped(info.This()).location().lat())); } catch (osmium::invalid_location&) { return scope.Close(v8::Undefined()); } } v8::Handle<v8::Value> OSMNodeWrap::wkb(const v8::Arguments& args) { v8::HandleScope scope; try { std::string wkb { wkb_factory.create_point(wrapped(args.This())) }; #if NODE_VERSION_AT_LEAST(0, 10, 0) return scope.Close(node::Buffer::New(wkb.data(), wkb.size())->handle_); #else return scope.Close(node::Buffer::New(const_cast<char*>(wkb.data()), wkb.size())->handle_); #endif } catch (std::runtime_error& e) { return ThrowException(v8::Exception::Error(v8::String::New(e.what()))); } } v8::Handle<v8::Value> OSMNodeWrap::wkt(const v8::Arguments& args) { v8::HandleScope scope; try { std::string wkt { wkt_factory.create_point(wrapped(args.This())) }; return scope.Close(v8::String::New(wkt.c_str())); } catch (std::runtime_error& e) { return ThrowException(v8::Exception::Error(v8::String::New(e.what()))); } } } // namespace node_osmium
namespace node_osmium { extern osmium::geom::WKBFactory wkb_factory; extern osmium::geom::WKTFactory wkt_factory; Persistent<FunctionTemplate> OSMNodeWrap::constructor; void OSMNodeWrap::Initialize(Handle<Object> target) { HandleScope scope; constructor = Persistent<FunctionTemplate>::New(FunctionTemplate::New(OSMNodeWrap::New)); constructor->InstanceTemplate()->SetInternalFieldCount(1); constructor->SetClassName(String::NewSymbol("Node")); NODE_SET_PROTOTYPE_METHOD(constructor, "tags", tags); NODE_SET_PROTOTYPE_METHOD(constructor, "wkb", wkb); NODE_SET_PROTOTYPE_METHOD(constructor, "wkt", wkt); enum PropertyAttribute attributes = static_cast<PropertyAttribute>(v8::ReadOnly | v8::DontDelete); SET_ACCESSOR(constructor, "id", get_id, attributes); SET_ACCESSOR(constructor, "version", get_version, attributes); SET_ACCESSOR(constructor, "changeset", get_changeset, attributes); SET_ACCESSOR(constructor, "visible", get_visible, attributes); SET_ACCESSOR(constructor, "timestamp", get_timestamp, attributes); SET_ACCESSOR(constructor, "uid", get_uid, attributes); SET_ACCESSOR(constructor, "user", get_user, attributes); SET_ACCESSOR(constructor, "lon", get_lon, attributes); SET_ACCESSOR(constructor, "lat", get_lat, attributes); target->Set(String::NewSymbol("Node"), constructor->GetFunction()); } OSMNodeWrap::OSMNodeWrap(const input_iterator& it) : OSMObjectWrap(it) { } OSMNodeWrap::~OSMNodeWrap() { } Handle<Value> OSMNodeWrap::New(const Arguments& args) { HandleScope scope; if (args[0]->IsExternal()) { Local<External> ext = Local<External>::Cast(args[0]); void* ptr = ext->Value(); OSMNodeWrap* node = static_cast<OSMNodeWrap*>(ptr); node->Wrap(args.This()); return args.This(); } else { return ThrowException(Exception::TypeError(String::New("osmium.Node cannot be created in Javascript"))); } return scope.Close(Undefined()); } Handle<Value> OSMNodeWrap::get_lon(Local<String> property,const AccessorInfo& info) { HandleScope scope; return scope.Close(Number::New(wrapped(info.This()).lon())); } Handle<Value> OSMNodeWrap::get_lat(Local<String> property,const AccessorInfo& info) { HandleScope scope; return scope.Close(Number::New(wrapped(info.This()).lat())); } Handle<Value> OSMNodeWrap::wkb(const Arguments& args) { HandleScope scope; std::string wkb { wkb_factory.create_point(wrapped(args.This())) }; #if NODE_VERSION_AT_LEAST(0, 10, 0) return scope.Close(node::Buffer::New(wkb.data(), wkb.size())->handle_); #else return scope.Close(node::Buffer::New(const_cast<char*>(wkb.data()), wkb.size())->handle_); #endif } Handle<Value> OSMNodeWrap::wkt(const Arguments& args) { HandleScope scope; std::string wkt { wkt_factory.create_point(wrapped(args.This())) }; return scope.Close(String::New(wkt.c_str())); } } // namespace node_osmium