Beispiel #1
0
void* TypedArray::Validate(Handle<Object> obj, GDALDataType type, int min_length){
	//validate array
	NanEscapableScope();

	if(!obj->HasIndexedPropertiesInExternalArrayData()) {
		NanThrowError("Object has no external array data");
		return NULL;
	}
	GDALDataType src_type = TypedArray::Identify(obj);
	if(type == GDT_Unknown) {
		NanThrowTypeError("Unable to identify GDAL datatype of passed array object");
		return NULL;
	}
	if(src_type != type) {
		std::ostringstream ss;
		ss << "Array type does not match band data type (" 
		   << "array: " << GDALGetDataTypeName(src_type)
		   << " band: " << GDALGetDataTypeName(type) << ")";

		NanThrowTypeError(ss.str().c_str());
		return NULL;
	}
	if(TypedArray::Length(obj) < min_length) {
		std::ostringstream ss;
		ss << "Array length must be greater than or equal to " << min_length; 

		NanThrowError(ss.str().c_str());
		return NULL;
	}
	return TypedArray::Data(obj);
}
Beispiel #2
0
Handle<Value> TypedArray::New(GDALDataType type, unsigned int length)  {
	NanEscapableScope();

	Handle<Value> val;
	Handle<Function> constructor;
	Local<Object> global = NanGetCurrentContext()->Global();

	const char *name;
	switch(type) {
		case GDT_Byte:    name = "Uint8Array";   break;
		case GDT_Int16:   name = "Int16Array";   break;
		case GDT_UInt16:  name = "Uint16Array";  break;
		case GDT_Int32:   name = "Int32Array";   break;
		case GDT_UInt32:  name = "Uint32Array";  break;
		case GDT_Float32: name = "Float32Array"; break;
		case GDT_Float64: name = "Float64Array"; break;
		default: 
			NanThrowError("Unsupported array type"); 
			return NanEscapeScope(NanUndefined());
	}


	// make ArrayBuffer
	val = global->Get(NanNew("ArrayBuffer"));

	if(val.IsEmpty() || !val->IsFunction()) {
		NanThrowError("Error getting ArrayBuffer constructor");
		return NanEscapeScope(NanUndefined());
	}

	constructor = val.As<Function>();
	Local<Value> size = NanNew<Integer>(length * GDALGetDataTypeSize(type) / 8);
	Local<Value> array_buffer = constructor->NewInstance(1, &size);

	if(array_buffer.IsEmpty() || !array_buffer->IsObject()) {
		NanThrowError("Error allocating ArrayBuffer");
		return NanEscapeScope(NanUndefined());
	}


	// make TypedArray
	val = global->Get(NanNew(name));

	if(val.IsEmpty() || !val->IsFunction()) {
		NanThrowError("Error getting typed array constructor");
		return NanEscapeScope(NanUndefined());
	}

	constructor = val.As<Function>();
	Local<Object> array = constructor->NewInstance(1, &array_buffer);

	if(array.IsEmpty() || !array->IsObject()) {
		NanThrowError("Error creating TypedArray");
		return NanEscapeScope(NanUndefined());
	}

	return NanEscapeScope(array);
}
  static NAN_METHOD(New) {
    NanScope();

    if (args.Length() != 1) {
      EIGENJS_THROW_ERROR_INVALID_ARGUMENT()
      NanReturnUndefined();
    }

    if (!args.IsConstructCall()) {
      v8::Local<v8::Value> argv[] ={ args[0] };
      NanReturnValue(
        base_type::new_instance(
          args
        , sizeof(argv) / sizeof(v8::Local<v8::Value>)
        , argv
        )
      );
    }

    if (Matrix::is_matrix(args[0])) {
      const Matrix* const& rhs_obj =
          node::ObjectWrap::Unwrap<Matrix>(args[0]->ToObject());
      const typename Matrix::value_type& rhs_matrix = **rhs_obj;

      if (rhs_matrix.rows() != rhs_matrix.cols()) {
        NanThrowError("PartialPivLU is only for square "
                      "(and moreover invertible) matrices");
        NanReturnUndefined();
      }

      PartialPivLU* obj = new PartialPivLU(rhs_matrix);
      obj->Wrap(args.This());
      NanReturnValue(args.This());
    } else if (MatrixBlock::is_matrixblock(args[0])) {
      const MatrixBlock* const& rhs_obj =
          node::ObjectWrap::Unwrap<MatrixBlock>(args[0]->ToObject());
      const typename MatrixBlock::value_type& rhs_matrixblock = **rhs_obj;

      if (rhs_matrixblock.rows() != rhs_matrixblock.cols()) {
        NanThrowError("PartialPivLU is only for square "
                      "(and moreover invertible) matrices");
        NanReturnUndefined();
      }

      PartialPivLU* obj = new PartialPivLU(rhs_matrixblock);
      obj->Wrap(args.This());
      NanReturnValue(args.This());
    }

    EIGENJS_THROW_ERROR_INVALID_ARGUMENT()
    NanReturnUndefined();
  }
static inline NAN_METHOD(register_fonts)
{
    NanScope();

    try
    {
        if (args.Length() == 0 || !args[0]->IsString())
        {
            NanThrowTypeError("first argument must be a path to a directory of fonts");
            NanReturnUndefined();
        }

        bool found = false;

        std::vector<std::string> const names_before = mapnik::freetype_engine::face_names();

        // option hash
        if (args.Length() == 2){
            if (!args[1]->IsObject())
            {
                NanThrowTypeError("second argument is optional, but if provided must be an object, eg. { recurse:Boolean }");
                NanReturnUndefined();
            }

            Local<Object> options = args[1].As<Object>();
            if (options->Has(NanNew("recurse")))
            {
                Local<Value> recurse_opt = options->Get(NanNew("recurse"));
                if (!recurse_opt->IsBoolean())
                {
                    NanThrowTypeError("'recurse' must be a Boolean");
                    NanReturnUndefined();
                }

                bool recurse = recurse_opt->BooleanValue();
                std::string path = TOSTR(args[0]);
                found = mapnik::freetype_engine::register_fonts(path,recurse);
            }
        }
        else
        {
            std::string path = TOSTR(args[0]);
            found = mapnik::freetype_engine::register_fonts(path);
        }

        std::vector<std::string> const& names_after = mapnik::freetype_engine::face_names();
        if (names_after.size() == names_before.size())
            found = false;

        NanReturnValue(NanNew(found));
    }
    catch (std::exception const& ex)
    {
        NanThrowError(ex.what());
        NanReturnUndefined();
    }
}
Beispiel #5
0
Handle<Value> Layer::New(OGRLayer *raw, OGRDataSource *raw_parent, bool result_set)
#endif
{
	NanEscapableScope();

	if (!raw) {
		return NanEscapeScope(NanNull());
	}
	if (cache.has(raw)) {
		return NanEscapeScope(NanNew(cache.get(raw)));
	}

	Layer *wrapped = new Layer(raw);
	wrapped->is_result_set = result_set;

	Handle<Value> ext = NanNew<External>(wrapped);
	Handle<Object> obj = NanNew(Layer::constructor)->GetFunction()->NewInstance(1, &ext);

	cache.add(raw, obj);

	//add reference to datasource so datasource doesnt get GC'ed while layer is alive
	if (raw_parent) {
		Handle<Value> ds;
		#if GDAL_VERSION_MAJOR >= 2
			if (Dataset::dataset_cache.has(raw_parent)) {
				ds = NanNew(Dataset::dataset_cache.get(raw_parent));
			}
		#else
			if (Dataset::datasource_cache.has(raw_parent)) {
				ds = NanNew(Dataset::datasource_cache.get(raw_parent));
			}
		#endif
		else {
			LOG("Layer's parent dataset disappeared from cache (layer = %p, dataset = %p)", raw, raw_parent);
			NanThrowError("Layer's parent dataset disappeared from cache");
			return NanEscapeScope(NanUndefined());
			//ds = Dataset::New(raw_parent); //should never happen
		}

		wrapped->parent_ds = raw_parent;
		obj->SetHiddenValue(NanNew("ds_"), ds);
	}

	return NanEscapeScope(obj);
}
Beispiel #6
0
Handle<Value> FeatureFields::get(OGRFeature *f, int field_index)
{
	//#throws : caller must check if return_val.IsEmpty() and bail out if true
	NanEscapableScope();

	if(!f->IsFieldSet(field_index)) return NanEscapeScope(NanNull());

	OGRFieldDefn *field_def = f->GetFieldDefnRef(field_index);
	switch(field_def->GetType()) {
		case OFTInteger:
			return NanEscapeScope(NanNew<Integer>(f->GetFieldAsInteger(field_index)));
		#if defined(GDAL_VERSION_MAJOR) && (GDAL_VERSION_MAJOR >= 2)
		case OFTInteger64:
			return NanEscapeScope(NanNew<Number>(f->GetFieldAsInteger64(field_index)));
		#endif
		case OFTReal:
			return NanEscapeScope(NanNew<Number>(f->GetFieldAsDouble(field_index)));
		case OFTString:
			return NanEscapeScope(SafeString::New(f->GetFieldAsString(field_index)));
		case OFTIntegerList:
			return NanEscapeScope(getFieldAsIntegerList(f, field_index));
		case OFTRealList:
			return NanEscapeScope(getFieldAsDoubleList(f, field_index));
		case OFTStringList:
			return NanEscapeScope(getFieldAsStringList(f, field_index));
		case OFTBinary:
			return NanEscapeScope(getFieldAsBinary(f, field_index));
		case OFTDate:
		case OFTTime:
		case OFTDateTime:
			return NanEscapeScope(getFieldAsDateTime(f, field_index));
		default:
			NanThrowError("Unsupported field type");
			return NanEscapeScope(NanUndefined());
	}
}
Beispiel #7
0
static void describe_datasource(Local<Object> description, mapnik::datasource_ptr ds)
{
    NanScope();
    try
    {
        // type
        if (ds->type() == mapnik::datasource::Raster)
        {
            description->Set(NanNew("type"), NanNew<String>("raster"));
        }
        else
        {
            description->Set(NanNew("type"), NanNew<String>("vector"));
        }

        mapnik::layer_descriptor ld = ds->get_descriptor();

        // encoding
        description->Set(NanNew("encoding"), NanNew<String>(ld.get_encoding().c_str()));

        // field names and types
        Local<Object> fields = NanNew<Object>();
        std::vector<mapnik::attribute_descriptor> const& desc = ld.get_descriptors();
        std::vector<mapnik::attribute_descriptor>::const_iterator itr = desc.begin();
        std::vector<mapnik::attribute_descriptor>::const_iterator end = desc.end();
        while (itr != end)
        {
            unsigned field_type = itr->get_type();
            std::string type("");
            if (field_type == mapnik::Integer) type = "Number";
            else if (field_type == mapnik::Float) type = "Number";
            else if (field_type == mapnik::Double) type = "Number";
            else if (field_type == mapnik::String) type = "String";
            else if (field_type == mapnik::Boolean) type = "Boolean";
            else if (field_type == mapnik::Geometry) type = "Geometry";
            else if (field_type == mapnik::Object) type = "Object";
            else type = "Unknown";
            fields->Set(NanNew(itr->get_name().c_str()), NanNew(type.c_str()));
            fields->Set(NanNew(itr->get_name().c_str()), NanNew(type.c_str()));
            ++itr;
        }
        description->Set(NanNew("fields"), fields);

        Local<String> js_type = NanNew<String>("unknown");
        if (ds->type() == mapnik::datasource::Raster)
        {
            js_type = NanNew<String>("raster");
        }
        else
        {
            boost::optional<mapnik::datasource::geometry_t> geom_type = ds->get_geometry_type();
            if (geom_type)
            {
                mapnik::datasource::geometry_t g_type = *geom_type;
                switch (g_type)
                {
                case mapnik::datasource::Point:
                {
                    js_type = NanNew<String>("point");
                    break;
                }
                case mapnik::datasource::LineString:
                {
                    js_type = NanNew<String>("linestring");
                    break;
                }
                case mapnik::datasource::Polygon:
                {
                    js_type = NanNew<String>("polygon");
                    break;
                }
                case mapnik::datasource::Collection:
                {
                    js_type = NanNew<String>("collection");
                    break;
                }
                default:
                {
                    break;
                }
                }
            }
        }
        description->Set(NanNew("geometry_type"), js_type);
        for (auto const& param : ld.get_extra_parameters()) 
        {
            node_mapnik::params_to_object(description,param.first, param.second);
        }
    }
    catch (std::exception const& ex)
    {
        NanThrowError(ex.what());
    }
    catch (...)
    {
        NanThrowError("unknown exception happened when calling describe_datasource, please file bug");
    }
}