Handle<Value> Feature::attributes(const Arguments& args)
{
    HandleScope scope;

    Feature* fp = node::ObjectWrap::Unwrap<Feature>(args.This());

    Local<Object> feat = Object::New();

#if MAPNIK_VERSION >= 200100
    mapnik::feature_ptr feature = fp->get();
    mapnik::feature_impl::iterator itr = feature->begin();
    mapnik::feature_impl::iterator end = feature->end();
    for ( ;itr!=end; ++itr)
    {
        node_mapnik::params_to_object serializer( feat , boost::get<0>(*itr));
        boost::apply_visitor( serializer, boost::get<1>(*itr).base() );
    }
#else
    std::map<std::string,mapnik::value> const& fprops = fp->get()->props();
    std::map<std::string,mapnik::value>::const_iterator it = fprops.begin();
    std::map<std::string,mapnik::value>::const_iterator end = fprops.end();
    for (; it != end; ++it)
    {
        node_mapnik::params_to_object serializer( feat , it->first);
        boost::apply_visitor( serializer, it->second.base() );
    }
#endif
    return scope.Close(feat);
}
Exemple #2
0
Handle<Value> Feature::addAttributes(const Arguments& args)
{
    HandleScope scope;

    Feature* fp = ObjectWrap::Unwrap<Feature>(args.This());

    if (args.Length() > 0 ) {
        Local<Value> value = args[0];
        if (value->IsNull() || value->IsUndefined()) {
            return ThrowException(Exception::TypeError(String::New("object expected")));
        } else {
            Local<Object> attr = value->ToObject();
            try
            {
                Local<Array> names = attr->GetPropertyNames();
                uint32_t i = 0;
                uint32_t a_length = names->Length();
                boost::scoped_ptr<mapnik::transcoder> tr(new mapnik::transcoder("utf8"));
                while (i < a_length) {
                    Local<Value> name = names->Get(i)->ToString();
                    Local<Value> value = attr->Get(name);
                    if (value->IsString()) {
                        UnicodeString ustr = tr->transcode(TOSTR(value));
                        boost::put(*fp->get(),TOSTR(name),ustr);
                    } else if (value->IsNumber()) {
                        double num = value->NumberValue();
                        // todo - round
                        if (num == value->IntegerValue()) {
                            int integer = value->IntegerValue();
                            boost::put(*fp->get(),TOSTR(name),integer);
                        } else {
                            double dub_val = value->NumberValue();
                            boost::put(*fp->get(),TOSTR(name),dub_val);
                        }
                    } else {
                        std::clog << "unhandled type for property: " << TOSTR(name) << "\n";
                    }
                    i++;
                }
            }
            catch (const std::exception & ex )
            {
                return ThrowException(Exception::Error(
                  String::New(ex.what())));
            }
            catch (...) {
                return ThrowException(Exception::Error(
                  String::New("Unknown exception happended - please report bug")));
            }
        }
    }

    return Undefined();
}
Exemple #3
0
void raster_colorizer::colorize(raster_ptr const& raster, Feature const& f) const
{
    unsigned *imageData = raster->data_.getData();

    int len = raster->data_.width() * raster->data_.height();

    bool hasNoData = false;
    float noDataValue = 0;

    //std::map<std::string,value>::const_iterator fi = Props.find("NODATA");
    if (f.has_key("NODATA"))
    {
        hasNoData = true;
        noDataValue = static_cast<float>(f.get("NODATA").to_double());
    }

    for (int i=0; i<len; ++i)
    {
        // the GDAL plugin reads single bands as floats
        float value = *reinterpret_cast<float *> (&imageData[i]);
        if (hasNoData && noDataValue == value)
            imageData[i] = color(0,0,0,0).rgba();
        else
            imageData[i] = get_color(value).rgba();
    }
}
Exemple #4
0
Handle<Value> Feature::toString(const Arguments& args)
{
    HandleScope scope;

    Feature* fp = ObjectWrap::Unwrap<Feature>(args.This());
    return scope.Close(String::New(fp->get()->to_string().c_str()));
}
Handle<Value> Feature::id(const Arguments& args)
{
    HandleScope scope;

    Feature* fp = node::ObjectWrap::Unwrap<Feature>(args.This());
    return scope.Close(Number::New(fp->get()->id()));
}
Exemple #6
0
Handle<Value> Feature::id(const Arguments& args)
{
    HandleScope scope;

    Feature* fp = ObjectWrap::Unwrap<Feature>(args.This());
    // TODO - provide custom 64 bit integer type?
    return scope.Close(Integer::New(fp->get()->id()));
}
Exemple #7
0
Handle<Value> Feature::extent(const Arguments& args)
{
    HandleScope scope;

    Feature* fp = ObjectWrap::Unwrap<Feature>(args.This());

    Local<Array> a = Array::New(4);
    mapnik::box2d<double> const& e = fp->get()->envelope();
    a->Set(0, Number::New(e.minx()));
    a->Set(1, Number::New(e.miny()));
    a->Set(2, Number::New(e.maxx()));
    a->Set(3, Number::New(e.maxy()));
 
    return scope.Close(a);
}
Exemple #8
0
Handle<Value> Feature::attributes(const Arguments& args)
{
    HandleScope scope;

    Feature* fp = ObjectWrap::Unwrap<Feature>(args.This());
    
    Local<Object> feat = Object::New();

    std::map<std::string,mapnik::value> const& fprops = fp->get()->props();
    std::map<std::string,mapnik::value>::const_iterator it = fprops.begin();
    std::map<std::string,mapnik::value>::const_iterator end = fprops.end();
    for (; it != end; ++it)
    {
        node_mapnik::params_to_object serializer( feat , it->first);
        boost::apply_visitor( serializer, it->second.base() );
    }
    
    return scope.Close(feat);
}
Handle<Value> Feature::toWKB(const Arguments& args)
{
    HandleScope scope;

    std::string wkt;
#if BOOST_VERSION >= 104700 && MAPNIK_VERSION >= 200100
    Feature* fp = node::ObjectWrap::Unwrap<Feature>(args.This());

    mapnik::util::wkb_buffer_ptr wkb = mapnik::util::to_wkb(fp->get()->paths(), mapnik::util::wkbNDR);
    #if NODE_VERSION_AT_LEAST(0, 11, 0)
    return scope.Close(node::Buffer::New(wkb->buffer(), wkb->size()));
    #else
    return scope.Close(node::Buffer::New(wkb->buffer(), wkb->size())->handle_);
    #endif
#else
    return ThrowException(Exception::Error(
                              String::New("WKB output requires at least boost 1.47 and mapnik 2.1.x")));
#endif
}
Handle<Value> Feature::toWKT(const Arguments& args)
{
    HandleScope scope;

    std::string wkt;
#if BOOST_VERSION >= 104700 && MAPNIK_VERSION >= 200100
    Feature* fp = node::ObjectWrap::Unwrap<Feature>(args.This());

    if (!mapnik::util::to_wkt(wkt, fp->get()->paths()))
    {
        return ThrowException(Exception::Error(
                                String::New("Failed to generate WKT")));
    }
#else
    return ThrowException(Exception::Error(
                              String::New("WKT output requires at least boost 1.47 and mapnik 2.1.x")));
#endif

    return scope.Close(String::New(wkt.c_str()));
}
Handle<Value> Feature::toJSON(const Arguments& args)
{
    HandleScope scope;

    std::string json;
#if BOOST_VERSION >= 104700 && MAPNIK_VERSION >= 200100
    Feature* fp = node::ObjectWrap::Unwrap<Feature>(args.This());
    // TODO - create once?
    if (!generator.generate(json,*(fp->get())))
    {
        return ThrowException(Exception::Error(
                                  String::New("Failed to generate GeoJSON")));
    }
#else
    return ThrowException(Exception::Error(
                              String::New("GeoJSON output requires at least boost 1.47 and mapnik 2.1.x")));
#endif

    return scope.Close(String::New(json.c_str()));
}
Exemple #12
0
// TODO void?
Handle<Value> Feature::addGeometry(const Arguments& args)
{
    HandleScope scope;

    Feature* fp = ObjectWrap::Unwrap<Feature>(args.This());

    if (args.Length() >= 1 ) {
        Local<Value> value = args[0];
        if (value->IsNull() || value->IsUndefined()) {
            return ThrowException(Exception::TypeError(String::New("mapnik.Geometry instance expected")));
        } else {
            Local<Object> obj = value->ToObject();
            if (Geometry::constructor->HasInstance(obj)) {
                Geometry* g = ObjectWrap::Unwrap<Geometry>(obj);
            
                try
                {
                    std::auto_ptr<mapnik::geometry_type> geom_ptr = g->get();
                    if (geom_ptr.get()) {
                        fp->get()->add_geometry(geom_ptr.get());
                        geom_ptr.release();
                    } else {
                        return ThrowException(Exception::Error(
                          String::New("empty geometry!")));
                    }
                }
                catch (const std::exception & ex )
                {
                    return ThrowException(Exception::Error(
                      String::New(ex.what())));
                }
                catch (...) {
                    return ThrowException(Exception::Error(
                      String::New("Unknown exception happended - please report bug")));
                }
            }
        }
    }

    return Undefined();
}
Handle<Value> Expression::evaluate(const Arguments& args)
{
    HandleScope scope;

    if (args.Length() < 1) {
        return ThrowException(Exception::Error(
                                  String::New("requires a mapnik.Feature as an argument")));
    }

    Local<Object> obj = args[0]->ToObject();
    if (obj->IsNull() || obj->IsUndefined()) {
        return ThrowException(Exception::TypeError(String::New("first argument is invalid, must be a mapnik.Feature not null/undefined")));
    }

    if (!Feature::constructor->HasInstance(obj)) {
        return ThrowException(Exception::TypeError(String::New("first argument is invalid, must be a mapnik.Feature")));
    }

    Feature* f = node::ObjectWrap::Unwrap<Feature>(obj);

    Expression* e = node::ObjectWrap::Unwrap<Expression>(args.This());
    mapnik::value value_obj = boost::apply_visitor(mapnik::evaluate<mapnik::Feature,mapnik::value>(*(f->get())),*(e->get()));
    return scope.Close(boost::apply_visitor(node_mapnik::value_converter(),value_obj.base()));
}
Exemple #14
0
Handle<Value> Feature::numGeometries(const Arguments& args)
{
    HandleScope scope;
    Feature* fp = ObjectWrap::Unwrap<Feature>(args.This());
    return scope.Close(Integer::New(fp->get()->num_geometries()));
}
Handle<Value> Feature::addAttributes(const Arguments& args)
{
    HandleScope scope;

    Feature* fp = node::ObjectWrap::Unwrap<Feature>(args.This());

    if (args.Length() > 0 ) {
        Local<Value> val = args[0];
        if (val->IsNull() || val->IsUndefined()) {
            return ThrowException(Exception::TypeError(String::New("object expected")));
        } else {
            Local<Object> attr = val->ToObject();
            try
            {
                Local<Array> names = attr->GetPropertyNames();
                unsigned int i = 0;
                unsigned int a_length = names->Length();
                boost::scoped_ptr<mapnik::transcoder> tr(new mapnik::transcoder("utf8"));
                while (i < a_length) {
                    Local<Value> name = names->Get(i)->ToString();
                    Local<Value> value = attr->Get(name);
                    if (value->IsString()) {
                        mapnik::value_unicode_string ustr = tr->transcode(TOSTR(value));
#if MAPNIK_VERSION >= 200100
                        fp->get()->put_new(TOSTR(name),ustr);
#else
                        boost::put(*fp->get(),TOSTR(name),ustr);
#endif
                    } else if (value->IsNumber()) {
                        double num = value->NumberValue();
                        // todo - round
                        if (num == value->IntegerValue()) {
#if MAPNIK_VERSION >= 200100
                            fp->get()->put_new(TOSTR(name),static_cast<node_mapnik::value_integer>(value->IntegerValue()));
#else
                            boost::put(*fp->get(),TOSTR(name),static_cast<int>(value->IntegerValue()));
#endif

                        } else {
                            double dub_val = value->NumberValue();
#if MAPNIK_VERSION >= 200100
                            fp->get()->put_new(TOSTR(name),dub_val);
#else
                            boost::put(*fp->get(),TOSTR(name),dub_val);
#endif
                        }
                    } else if (value->IsNull()) {
#if MAPNIK_VERSION >= 200100
                        fp->get()->put_new(TOSTR(name),mapnik::value_null());
#else
                        boost::put(*fp->get(),TOSTR(name),mapnik::value_null());
#endif
                    } else {
                        std::clog << "unhandled type for property: " << TOSTR(name) << "\n";
                    }
                    i++;
                }
            }
            catch (std::exception const& ex )
            {
                return ThrowException(Exception::Error(
                                          String::New(ex.what())));
            }
        }
    }

    return Undefined();
}