Handle<Value> OsmMapJs::visit(const Arguments& args) { HandleScope scope; OsmMapJs* map = ObjectWrap::Unwrap<OsmMapJs>(args.This()); if (args[0]->IsFunction()) { Persistent<Function> func = Persistent<Function>::New(Handle<Function>::Cast(args[0])); JsFunctionVisitor v; v.addFunction(func); map->getMap()->visitRw(v); } else { shared_ptr<ElementVisitor> v = ObjectWrap::Unwrap<ElementVisitorJs>(args[0]->ToObject())->getVisitor(); map->getMap()->visitRw(*v); } return scope.Close(Undefined()); }
Handle<Value> OsmMapJs::setIdGenerator(const Arguments& args) { HandleScope scope; OsmMapJs* obj = ObjectWrap::Unwrap<OsmMapJs>(args.This()); shared_ptr<IdGenerator> idGen = toCpp<shared_ptr<IdGenerator> >(args[0]); if (obj->getMap()) { obj->getMap()->setIdGenerator(idGen); } return scope.Close(Undefined()); }
Handle<Value> OsmMapJs::getElement(const Arguments& args) { HandleScope scope; try { OsmMapJs* obj = ObjectWrap::Unwrap<OsmMapJs>(args.This()); LOG_VAR(args[0]->ToObject()); ElementId eid = toCpp<ElementId>(args[0]); if (obj->isConst()) { return scope.Close(toV8(obj->getConstMap()->getElement(eid))); } else { return scope.Close(toV8(obj->getMap()->getElement(eid))); } } catch (const HootException& e) { return v8::ThrowException(HootExceptionJs::create(e)); } }
Handle<Value> OsmMapJs::removeElement(const Arguments& args) { HandleScope scope; OsmMapJs* obj = ObjectWrap::Unwrap<OsmMapJs>(args.This()); ElementId eid = toCpp<ElementId>(args[0]); obj->getMap()->removeElement(eid); return scope.Close(Undefined()); }
Handle<Value> OsmMapJs::clone(const Arguments& args) { HandleScope scope; OsmMapJs* from = ObjectWrap::Unwrap<OsmMapJs>(args.This()); OsmMapPtr newMap(new OsmMap(from->getMap())); const unsigned argc = 1; Handle<Value> argv[argc] = { args[0] }; Local<Object> result = _constructor->NewInstance(argc, argv); OsmMapJs* obj = ObjectWrap::Unwrap<OsmMapJs>(result); obj->_map = newMap; return scope.Close(result); }
v8::Handle<v8::Value> PoiMergerJs::jsPoiMerge(const v8::Arguments& args) { HandleScope scope; try{ if (args.Length() != 2) { return v8::ThrowException(HootExceptionJs::create(IllegalArgumentException( "Expected exactly two arguments to 'poiMerge'."))); } // Argument 1: script -- note second param is directory to search under (~/hoot/rules) const QString scriptPath = ConfPath::search(toCpp<QString>(args[0]), "rules"); // Argument 2: Map with POIs OsmMapJs* mapJs = node::ObjectWrap::Unwrap<OsmMapJs>(args[1]->ToObject()); // Pull out internal POI map OsmMapPtr map( mapJs->getMap() ); // Instantiate script merger shared_ptr<PluginContext> script(new PluginContext()); v8::HandleScope handleScope; v8::Context::Scope context_scope(script->getContext()); script->loadScript(scriptPath, "plugin"); v8::Handle<v8::Object> global = script->getContext()->Global(); if (global->Has(String::New("plugin")) == false) { return v8::ThrowException(HootExceptionJs::create(IllegalArgumentException( "Expected the script to have exports."))); } v8::Handle<v8::Value> pluginValue = global->Get(String::New("plugin")); v8::Persistent<v8::Object> plugin = v8::Persistent<v8::Object>::New(v8::Handle<v8::Object>::Cast(pluginValue)); if (plugin.IsEmpty() || plugin->IsObject() == false) { return v8::ThrowException(HootExceptionJs::create(IllegalArgumentException( "Expected plugin to be a valid object."))); } // Got in Map with POIs A, B, C, D, E // // Make a set of pairs to indicate all are same object: // A->B, A->C, A->D, A->E // // ...then pass those pairs one at a time through the merger, since it only merges pairs OsmMap::NodeMap nodes = map->getNodeMap(); OsmMapPtr mergedMap(map); const ElementId firstId = ElementId::node((*(nodes.constBegin()))->getId()); //LOG_DEBUG("First ID: " << firstId.getId()); for (OsmMap::NodeMap::const_iterator it = nodes.constBegin() + 1; it != nodes.constEnd(); ++it) { const boost::shared_ptr<const Node>& n = it.value(); std::set< std::pair< ElementId, ElementId> > matches; matches.insert(std::pair<ElementId,ElementId>(firstId, ElementId::node(n->getId()))); // Now create scriptmerger, and invoke apply method which will apply apply merge transformation, reducing the POIs down to one ScriptMerger merger(script, plugin, matches); OsmMapPtr mergedMap(map); std::vector< std::pair< ElementId, ElementId > > replacedNodes; merger.apply(mergedMap, replacedNodes ); if ( replacedNodes.size() == 1 ) { /* LOG_DEBUG("POI merge: replacing node #" << replacedNodes[0].first.getId() << " with updated version of node #" << replacedNodes[0].second.getId() ); */ mergedMap->replaceNode(replacedNodes[0].first.getId(), replacedNodes[0].second.getId()); } } // Hand merged POIs back to caller in OsmMap v8::Handle<v8::Object> returnMap = OsmMapJs::create(mergedMap); return scope.Close(returnMap); } catch ( const HootException& e ) { return v8::ThrowException(HootExceptionJs::create(e)); } }