Пример #1
0
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());
}
Пример #2
0
Handle<Value> OsmMapJs::getElementCount(const Arguments& args) {
  HandleScope scope;

  OsmMapJs* obj = ObjectWrap::Unwrap<OsmMapJs>(args.This());

  return scope.Close(Number::New(obj->getConstMap()->getElementCount()));
}
Пример #3
0
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));
  }
}
Пример #4
0
Handle<Value> FeatureExtractorJs::extract(const Arguments& args) {
  HandleScope scope;

  FeatureExtractorJs* feJs = ObjectWrap::Unwrap<FeatureExtractorJs>(args.This());

  if (args.Length() != 3)
  {
    throw IllegalArgumentException("Expected exactly three argument in extract (map, e1, e2)");
  }

  OsmMapJs* mapJs = ObjectWrap::Unwrap<OsmMapJs>(args[0]->ToObject());
  ElementJs* e1Js = ObjectWrap::Unwrap<ElementJs>(args[1]->ToObject());
  ElementJs* e2Js = ObjectWrap::Unwrap<ElementJs>(args[2]->ToObject());

  double result = feJs->getFeatureExtractor()->extract(
        *(mapJs->getConstMap()),
        e1Js->getConstElement(),
        e2Js->getConstElement());

  if (result == feJs->getFeatureExtractor()->nullValue())
  {
    return scope.Close(Null());
  }
  else
  {
    return scope.Close(Number::New(result));
  }
}
Пример #5
0
Handle<Value> OsmMapJs::New(const Arguments& args)
{
  HandleScope scope;

  OsmMapJs* obj = new OsmMapJs();
  obj->Wrap(args.This());

  return args.This();
}
Пример #6
0
Handle<Object> OsmMapJs::create(ConstOsmMapPtr map)
{
  HandleScope scope;

  Handle<Object> result = _constructor->NewInstance();
  OsmMapJs* from = ObjectWrap::Unwrap<OsmMapJs>(result);
  from->_setMap(map);

  return scope.Close(result);
}
Пример #7
0
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());
}
Пример #8
0
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());
}
Пример #9
0
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);
}
Handle<Value> SublineStringMatcherJs::findMatch(const Arguments& args)
{
    HandleScope scope;

    SublineStringMatcherJs* smJs = ObjectWrap::Unwrap<SublineStringMatcherJs>(args.This());

    if (args.Length() != 3)
    {
        throw IllegalArgumentException("Expected exactly three argument in findMatch (map, e1, e2)");
    }

    OsmMapJs* mapJs = ObjectWrap::Unwrap<OsmMapJs>(args[0]->ToObject());
    ElementJs* e1Js = ObjectWrap::Unwrap<ElementJs>(args[1]->ToObject());
    ElementJs* e2Js = ObjectWrap::Unwrap<ElementJs>(args[2]->ToObject());

    WaySublineMatchString match = smJs->getSublineStringMatcher()->findMatch(
                                      mapJs->getConstMap(),
                                      e1Js->getConstElement(),
                                      e2Js->getConstElement());

    WaySublineMatchStringPtr result(new WaySublineMatchString(match));

    return scope.Close(WaySublineMatchStringJs::New(result));
}
Пример #11
0
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));
  }
}
Handle<Value> SublineStringMatcherJs::extractMatchingSublines(const Arguments& args)
{
    HandleScope scope;

    SublineStringMatcherJs* smJs = ObjectWrap::Unwrap<SublineStringMatcherJs>(args.This());
    SublineStringMatcherPtr sm = smJs->getSublineStringMatcher();

    OsmMapJs* mapJs = ObjectWrap::Unwrap<OsmMapJs>(args[0]->ToObject());
    ElementJs* e1Js = ObjectWrap::Unwrap<ElementJs>(args[1]->ToObject());
    ElementJs* e2Js = ObjectWrap::Unwrap<ElementJs>(args[2]->ToObject());
    ConstOsmMapPtr m = mapJs->getConstMap();
    ConstElementPtr e1 = e1Js->getConstElement();
    ConstElementPtr e2 = e2Js->getConstElement();

    Handle<Value> result;

    try
    {
        WaySublineMatchString match = sm->findMatch(m, e1, e2);

        if (match.isEmpty())
        {
            return Undefined();
        }

        // convert match into elements in a new map.
        set<ElementId> eids;
        eids.insert(e1->getElementId());
        eids.insert(e2->getElementId());
        OsmMapPtr copiedMap(new OsmMap(m->getProjection()));
        CopySubsetOp(m, eids).apply(copiedMap);
        WaySublineMatchString copiedMatch(match, copiedMap);

        // split the shared line based on the matching subline
        ElementPtr match1, scraps1;
        ElementPtr match2, scraps2;
        WaySublineString string1 = copiedMatch.getSublineString1();
        WaySublineString string2 = copiedMatch.getSublineString2();

        try
        {
            MultiLineStringSplitter().split(copiedMap, string1, copiedMatch.getReverseVector1(), match1,
                                            scraps1);
            MultiLineStringSplitter().split(copiedMap, string2, copiedMatch.getReverseVector2(), match2,
                                            scraps2);
        }
        catch (const IllegalArgumentException& e)
        {
            // this is unusual print out some information useful to debugging.
            MapReprojector::reprojectToWgs84(copiedMap);
            LOG_WARN(OsmWriter::toString(copiedMap));
            throw e;
        }

        if (!match1 || !match2)
        {
            result = Undefined();
        }
        else
        {
            Handle<Object> obj = Object::New();
            obj->Set(String::NewSymbol("map"), OsmMapJs::create(copiedMap));
            obj->Set(String::NewSymbol("match1"), ElementJs::New(match1));
            obj->Set(String::NewSymbol("match2"), ElementJs::New(match2));
            result = obj;
        }
    }
    catch (const HootException& e)
    {
        return v8::ThrowException(HootExceptionJs::create(e));
    }

    return scope.Close(result);
}