void area(const osmium::Area& area) { try { m_out << m_factory.create_multipolygon(area) << "\n"; } catch (osmium::geometry_error& e) { m_out << "GEOMETRY ERROR: " << e.what() << "\n"; } }
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())); }
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()))); } }
void area(const osmium::Area& area) { if (m_first_out) { m_out << "[\n"; m_first_out = false; } else { m_out << ",\n"; } m_out << "{\n \"test_id\": " << (area.orig_id() / 1000) << ",\n \"area_id\": " << area.id() << ",\n \"from_id\": " << area.orig_id() << ",\n \"from_type\": \"" << (area.from_way() ? "way" : "relation") << "\",\n \"wkt\": \""; try { std::string wkt = m_wkt_factory.create_multipolygon(area); m_out << wkt << "\",\n \"tags\": {"; auto tagmap = create_map(area.tags()); bool first = true; for (auto& tag : tagmap) { if (first) { first = false; } else { m_out << ", "; } m_out << '"' << tag.first << "\": \"" << tag.second << '"'; } m_out << "}\n}"; } catch (osmium::geometry_error&) { m_out << "INVALID\"\n}"; } try { std::unique_ptr<OGRMultiPolygon> ogr_polygon = m_ogr_factory.create_multipolygon(area); OGRFeature* feature = OGRFeature::CreateFeature(m_layer_polygon->GetLayerDefn()); feature->SetGeometry(ogr_polygon.get()); feature->SetField("id", static_cast<int>(area.orig_id())); std::string from_type; if (area.from_way()) { from_type = "w"; } else { from_type = "r"; } feature->SetField("from_type", from_type.c_str()); if (m_layer_polygon->CreateFeature(feature) != OGRERR_NONE) { std::cerr << "Failed to create feature.\n"; exit(1); } OGRFeature::DestroyFeature(feature); } catch (osmium::geometry_error&) { std::cerr << "Ignoring illegal geometry for area " << area.id() << " created from " << (area.from_way() ? "way" : "relation") << " with id=" << area.orig_id() << ".\n"; } }
#include "catch.hpp" #include <osmium/geom/mercator_projection.hpp> #include <osmium/geom/wkt.hpp> #include "area_helper.hpp" #include "wnl_helper.hpp" TEST_CASE("WKT geometry for point") { const osmium::geom::WKTFactory<> factory; const std::string wkt{factory.create_point(osmium::Location{3.2, 4.2})}; REQUIRE(wkt == "POINT(3.2 4.2)"); } TEST_CASE("WKT geometry for empty point") { const osmium::geom::WKTFactory<> factory; REQUIRE_THROWS_AS(factory.create_point(osmium::Location()), const osmium::invalid_location&); } TEST_CASE("WKT geometry for point in ekwt") { const osmium::geom::WKTFactory<> factory{7, osmium::geom::wkt_type::ewkt}; const std::string wkt{factory.create_point(osmium::Location{3.2, 4.2})}; REQUIRE(wkt == "SRID=4326;POINT(3.2 4.2)"); } TEST_CASE("WKT geometry for point in ekwt in web mercator") { const osmium::geom::WKTFactory<osmium::geom::MercatorProjection> factory{2, osmium::geom::wkt_type::ewkt}; const std::string wkt{factory.create_point(osmium::Location{3.2, 4.2})}; REQUIRE(wkt == "SRID=3857;POINT(356222.37 467961.14)");
#include "catch.hpp" #include <osmium/builder/builder_helper.hpp> #include <osmium/geom/wkt.hpp> #include "../basic/helper.hpp" TEST_CASE("WKT_Geometry") { SECTION("point") { osmium::geom::WKTFactory<> factory; std::string wkt {factory.create_point(osmium::Location(3.2, 4.2))}; REQUIRE(std::string{"POINT(3.2 4.2)"} == wkt); } SECTION("empty_point") { osmium::geom::WKTFactory<> factory; REQUIRE_THROWS_AS(factory.create_point(osmium::Location()), osmium::invalid_location); } SECTION("linestring") { osmium::geom::WKTFactory<> factory; osmium::memory::Buffer buffer(10000); auto& wnl = osmium::builder::build_way_node_list(buffer, { {1, {3.2, 4.2}}, {3, {3.5, 4.7}}, {4, {3.5, 4.7}}, {2, {3.6, 4.9}}
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