Handle<Value> Geometry::New(const Arguments& args) { HandleScope scope; Geometry *f; if (!args.IsConstructCall()) { return NODE_THROW("Cannot call constructor as function, you need to use 'new' keyword"); } if (args[0]->IsExternal()) { Local<External> ext = Local<External>::Cast(args[0]); void* ptr = ext->Value(); f = static_cast<Geometry *>(ptr); } else { return NODE_THROW("Geometry doesnt have a constructor, use Geometry.createFromWkt(), Geometry.create() or type-specific constructor. ie. new ogr.Point()"); OGRwkbGeometryType geometry_type; NODE_ARG_ENUM(0, "geometry type", OGRwkbGeometryType, geometry_type); OGRGeometry *geom = OGRGeometryFactory::createGeometry(geometry_type); f = new Geometry(geom); } f->Wrap(args.This()); return args.This(); }
void FieldDefn::justificationSetter(Local<String> property, Local<Value> value, const AccessorInfo &info) { HandleScope scope; FieldDefn *def = ObjectWrap::Unwrap<FieldDefn>(info.This()); OGRJustification justification; std::string str = TOSTR(value); if(value->IsString()){ if(str == "Left") { justification = OJLeft; } else if (str == "Right") { justification = OJRight; } else if (str == "Undefined") { justification = OJUndefined; } else { NODE_THROW("Unrecognized justification"); return; } } else if (value->IsNull() || value->IsUndefined()){ justification = OJUndefined; } else { NODE_THROW("justification must be a string or undefined"); return; } def->this_->SetJustify(justification); }
Handle<Value> Polygon::New(const Arguments& args) { HandleScope scope; Polygon *f; if (!args.IsConstructCall()) { return NODE_THROW("Cannot call constructor as function, you need to use 'new' keyword"); } if (args[0]->IsExternal()) { Local<External> ext = Local<External>::Cast(args[0]); void* ptr = ext->Value(); f = static_cast<Polygon *>(ptr); } else { if (args.Length() != 0) { return NODE_THROW("Polygon constructor doesn't take any arguments"); } f = new Polygon(new OGRPolygon()); } Handle<Value> rings = PolygonRings::New(args.This()); args.This()->SetHiddenValue(String::NewSymbol("rings_"), rings); f->Wrap(args.This()); return args.This(); }
Handle<Value> LayerFields::getNames(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); Layer *layer = ObjectWrap::Unwrap<Layer>(parent); if (!layer->get()) { return NODE_THROW("Layer object already destroyed"); } OGRFeatureDefn *def = layer->get()->GetLayerDefn(); if (!def) { return NODE_THROW("Layer has no layer definition set"); } int n = def->GetFieldCount(); Handle<Array> result = Array::New(n); for (int i = 0; i < n; i++) { OGRFieldDefn *field_def = def->GetFieldDefn(i); result->Set(i, SafeString::New(field_def->GetNameRef())); } return scope.Close(result); }
Handle<Value> LayerFields::remove(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); Layer *layer = ObjectWrap::Unwrap<Layer>(parent); if (!layer->get()) { return NODE_THROW("Layer object already destroyed"); } if (args.Length() < 1) { return NODE_THROW("Field index or name must be given"); } OGRFeatureDefn *def = layer->get()->GetLayerDefn(); if (!def) { return NODE_THROW("Layer has no layer definition set"); } int field_index; ARG_FIELD_ID(0, def, field_index); int err = layer->get()->DeleteField(field_index); if (err) { return NODE_THROW_OGRERR(err); } return Undefined(); }
Handle<Value> FeatureDefn::New(const Arguments& args) { HandleScope scope; FeatureDefn *f; if (!args.IsConstructCall()) { return NODE_THROW("Cannot call constructor as function, you need to use 'new' keyword"); } if (args[0]->IsExternal()) { Local<External> ext = Local<External>::Cast(args[0]); void* ptr = ext->Value(); f = static_cast<FeatureDefn *>(ptr); } else { if (args.Length() != 0) { return NODE_THROW("FeatureDefn constructor doesn't take any arguments"); } f = new FeatureDefn(new OGRFeatureDefn()); f->this_->Reference(); } Handle<Value> fields = FeatureDefnFields::New(args.This()); args.This()->SetHiddenValue(String::NewSymbol("fields_"), fields); f->Wrap(args.This()); return args.This(); }
Handle<Value> FieldDefn::New(const Arguments& args) { HandleScope scope; if (!args.IsConstructCall()) { return NODE_THROW("Cannot call constructor as function, you need to use 'new' keyword"); } if (args[0]->IsExternal()) { Local<External> ext = Local<External>::Cast(args[0]); void* ptr = ext->Value(); FieldDefn *f = static_cast<FieldDefn *>(ptr); f->Wrap(args.This()); return args.This(); } else { std::string field_name(""); std::string type_name("string"); NODE_ARG_STR(0, "field name", field_name); NODE_ARG_STR(1, "field type", type_name); int field_type = getFieldTypeByName(type_name); if (field_type < 0) { return NODE_THROW("Unrecognized field type"); } FieldDefn* def = new FieldDefn(new OGRFieldDefn(field_name.c_str(), static_cast<OGRFieldType>(field_type))); def->owned_ = true; def->Wrap(args.This()); } return args.This(); }
Handle<Value> DatasetBands::get(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); Dataset *ds = ObjectWrap::Unwrap<Dataset>(parent); if (ds->uses_ogr){ OGRDataSource* raw = ds->getDatasource(); if (!raw) { return NODE_THROW("Dataset object has already been destroyed"); } return Null(); } else { GDALDataset* raw = ds->getDataset(); if (!raw) { return NODE_THROW("Dataset object has already been destroyed"); } int band_id; NODE_ARG_INT(0, "band id", band_id); GDALRasterBand *band = raw->GetRasterBand(band_id); return scope.Close(RasterBand::New(band)); } }
Handle<Value> Dataset::setGeoTransform(const Arguments& args) { Dataset *ds = ObjectWrap::Unwrap<Dataset>(args.This()); if(!ds->this_) return NODE_THROW("Dataset object has already been destroyed"); Handle<Array> transform; NODE_ARG_ARRAY(0, "transform", transform); if (transform->Length() != 6) { return NODE_THROW("Transform array must have 6 elements") } double buffer[6]; for (int i = 0; i < 6; i++) { Local<Value> val = transform->Get(i); if (!val->IsNumber()) { return NODE_THROW("Transform array must only contain numbers"); } buffer[i] = val->NumberValue(); } CPLErr err = ds->this_->SetGeoTransform(buffer); if (err) return NODE_THROW_CPLERR(err); return Undefined(); }
Handle<Value> MultiPoint::New(const Arguments& args) { HandleScope scope; MultiPoint *f; if (!args.IsConstructCall()) { return NODE_THROW("Cannot call constructor as function, you need to use 'new' keyword"); } if (args[0]->IsExternal()) { Local<External> ext = Local<External>::Cast(args[0]); void* ptr = ext->Value(); f = static_cast<MultiPoint *>(ptr); } else { if (args.Length() != 0) { return NODE_THROW("MultiPoint constructor doesn't take any arguments"); } f = new MultiPoint(new OGRMultiPoint()); } Handle<Value> children = GeometryCollectionChildren::New(args.This()); args.This()->SetHiddenValue(String::NewSymbol("children_"), children); f->Wrap(args.This()); return args.This(); }
Handle<Value> DatasetBands::create(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); Dataset *ds = ObjectWrap::Unwrap<Dataset>(parent); if (ds->uses_ogr){ return NODE_THROW("Dataset does not support getting creating bands"); } GDALDataset* raw = ds->getDataset(); if (!raw) { return NODE_THROW("Dataset object has already been destroyed"); } GDALDataType type; Handle<Array> band_options = Array::New(0); char **options = NULL; std::string *options_str = NULL; //NODE_ARG_ENUM(0, "data type", GDALDataType, type); if(args.Length() < 1) { return NODE_THROW("data type argument needed"); } if(args[0]->IsString()){ std::string type_name = TOSTR(args[0]); type = GDALGetDataTypeByName(type_name.c_str()); } else if (args[0]->IsNull() || args[0]->IsUndefined()) { type = GDT_Unknown; } else { return NODE_THROW("data type must be string or undefined"); } NODE_ARG_ARRAY_OPT(1, "band creation options", band_options); if (band_options->Length() > 0) { options = new char* [band_options->Length()]; options_str = new std::string [band_options->Length()]; for (unsigned int i = 0; i < band_options->Length(); ++i) { options_str[i] = TOSTR(band_options->Get(i)); options[i] = (char*) options_str[i].c_str(); } } CPLErr err = raw->AddBand(type, options); if(options) delete [] options; if(options_str) delete [] options_str; if (err) { return NODE_THROW_CPLERR(err); } return scope.Close(RasterBand::New(raw->GetRasterBand(raw->GetRasterCount()), raw)); }
Handle<Value> Geometry::exportToWKB(const Arguments& args) { HandleScope scope; Geometry *geom = ObjectWrap::Unwrap<Geometry>(args.This()); int size = geom->this_->WkbSize(); unsigned char *data = (unsigned char*) malloc(size); //byte order OGRwkbByteOrder byte_order; std::string order = "MSB"; NODE_ARG_OPT_STR(0, "byte order", order); if (order == "MSB") { byte_order = wkbXDR; } else if (order == "LSB") { byte_order = wkbNDR; } else { return NODE_THROW("byte order must be 'MSB' or 'LSB'"); } #if GDAL_VERSION_NUM > 1100000 //wkb variant OGRwkbVariant wkb_variant; std::string variant = "OGC"; NODE_ARG_OPT_STR(1, "wkb variant", variant); if (variant == "OGC") { wkb_variant = wkbVariantOgc; } else if (order == "ISO") { wkb_variant = wkbVariantIso; } else { return NODE_THROW("byte order must be 'OGC' or 'ISO'"); } OGRErr err = geom->this_->exportToWkb(byte_order, data, wkb_variant); #else OGRErr err = geom->this_->exportToWkb(byte_order, data); #endif //^^ export to wkb and fill buffer ^^ //TODO: avoid extra memcpy in FastBuffer::New and have exportToWkb write directly into buffer if (err) { free(data); return NODE_THROW_OGRERR(err); } Handle<Value> result = FastBuffer::New(data, size); free(data); return scope.Close(result); }
Handle<Value> LayerFields::reorder(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); Layer *layer = ObjectWrap::Unwrap<Layer>(parent); if (!layer->get()) { return NODE_THROW("Layer object already destroyed"); } OGRFeatureDefn *def = layer->get()->GetLayerDefn(); if (!def) { return NODE_THROW("Layer has no layer definition set"); } Handle<Array> field_map = Array::New(0); NODE_ARG_ARRAY(0, "field map", field_map); int n = def->GetFieldCount(); OGRErr err = 0; if ((int)field_map->Length() != n) { return NODE_THROW("Array length must match field count"); } int *field_map_array = new int[n]; for (int i = 0; i < n; i++) { Handle<Value> val = field_map->Get(i); if (!val->IsNumber()) { delete [] field_map_array; return NODE_THROW("Array must only contain integers"); } int key = val->IntegerValue(); if (key < 0 || key >= n) { delete [] field_map_array; return NODE_THROW("Values must be between 0 and field count - 1"); } field_map_array[i] = key; } err = layer->get()->ReorderFields(field_map_array); delete [] field_map_array; if (err) { return NODE_THROW_OGRERR(err); } return Undefined(); }
Handle<Value> Dataset::addBand(const Arguments& args) { HandleScope scope; Dataset *ds = ObjectWrap::Unwrap<Dataset>(args.This()); if(!ds->this_) return NODE_THROW("Dataset object has already been destroyed"); GDALDataType type; Handle<Array> band_options = Array::New(0); char **options = NULL; NODE_ARG_ENUM(0, "data type", GDALDataType, type); NODE_ARG_ARRAY_OPT(1, "band creation options", band_options); if (band_options->Length() > 0) { options = new char* [band_options->Length()]; for (unsigned int i = 0; i < band_options->Length(); ++i) { options[i] = TOSTR(band_options->Get(i)); } } CPLErr err = ds->this_->AddBand(type, options); if (options) { delete [] options; } if(err) return NODE_THROW_CPLERR(err); return scope.Close(RasterBand::New(ds->this_->GetRasterBand(ds->this_->GetRasterCount()))); }
Handle<Value> Geometry::getArea(const Arguments& args) { HandleScope scope; Geometry *geom = ObjectWrap::Unwrap<Geometry>(args.This()); Handle<Value> area = Undefined(); switch (geom->this_->getGeometryType()) { case wkbPolygon: area = Number::New(((OGRPolygon *)geom->this_)->get_Area()); break; case wkbMultiPolygon: area = Number::New(((OGRMultiPolygon *)geom->this_)->get_Area()); break; case wkbLinearRing: area = Number::New(((OGRLinearRing *)geom->this_)->get_Area()); break; case wkbGeometryCollection: area = Number::New(((OGRGeometryCollection *)geom->this_)->get_Area()); break; default: area = NODE_THROW("geometry must be either a polygon, multi-polygon, linear ring, or a geometry collection to use the area function."); break; } return scope.Close(area); }
Handle<Value> Dataset::getGCPs(const Arguments& args) { HandleScope scope; Dataset *ds = ObjectWrap::Unwrap<Dataset>(args.This()); if(!ds->this_) return NODE_THROW("Dataset object has already been destroyed"); int n = ds->this_->GetGCPCount(); const GDAL_GCP *gcps = ds->this_->GetGCPs(); Handle<Array> results = Array::New(0); if (!gcps) { return scope.Close(results); } for (int i = 0; i < n; i++) { GDAL_GCP gcp = gcps[i]; Local<Object> obj = Object::New(); obj->Set(String::NewSymbol("pszId"), String::New(gcp.pszId)); obj->Set(String::NewSymbol("pszInfo"), String::New(gcp.pszInfo)); obj->Set(String::NewSymbol("dfGCPPixel"), Number::New(gcp.dfGCPPixel)); obj->Set(String::NewSymbol("dfGCPLine"), Number::New(gcp.dfGCPLine)); obj->Set(String::NewSymbol("dfGCPX"), Number::New(gcp.dfGCPX)); obj->Set(String::NewSymbol("dfGCPY"), Number::New(gcp.dfGCPY)); obj->Set(String::NewSymbol("dfGCPZ"), Number::New(gcp.dfGCPZ)); results->Set(i, obj); } return scope.Close(results); }
Handle<Value> Geometry::createFromWkb(const Arguments &args) { HandleScope scope; std::string wkb_string; SpatialReference *srs = NULL; Handle<Object> wkb_obj; NODE_ARG_OBJECT(0, "wkb", wkb_obj); NODE_ARG_WRAPPED_OPT(1, "srs", SpatialReference, srs); std::string obj_type = TOSTR(wkb_obj->GetConstructorName()); if(obj_type != "Buffer"){ return NODE_THROW("Argument must be a buffer object"); } unsigned char* data = (unsigned char *) Buffer::Data(wkb_obj); size_t length = Buffer::Length(wkb_obj); OGRGeometry *geom = NULL; OGRSpatialReference *ogr_srs = NULL; if (srs) { ogr_srs = srs->get(); } OGRErr err = OGRGeometryFactory::createFromWkb(data, ogr_srs, &geom, length); if (err) { return NODE_THROW_OGRERR(err); } return scope.Close(Geometry::New(geom, true)); }
Handle<Value> Geometry::New(OGRGeometry *geom, bool owned) { HandleScope scope; if (!geom) { return Null(); } switch(getGeometryType_fixed(geom)) { case wkbPoint : return scope.Close(Point::New(static_cast<OGRPoint*>(geom), owned)); case wkbLineString : return scope.Close(LineString::New(static_cast<OGRLineString*>(geom), owned)); case wkbLinearRing : return scope.Close(LinearRing::New(static_cast<OGRLinearRing*>(geom), owned)); case wkbPolygon : return scope.Close(Polygon::New(static_cast<OGRPolygon*>(geom), owned)); case wkbGeometryCollection : return scope.Close(GeometryCollection::New(static_cast<OGRGeometryCollection*>(geom), owned)); case wkbMultiPoint : return scope.Close(MultiPoint::New(static_cast<OGRMultiPoint*>(geom), owned)); case wkbMultiLineString : return scope.Close(MultiLineString::New(static_cast<OGRMultiLineString*>(geom), owned)); case wkbMultiPolygon : return scope.Close(MultiPolygon::New(static_cast<OGRMultiPolygon*>(geom), owned)); default : return NODE_THROW("Tried to create unsupported geometry type"); } }
Handle<Value> DatasetBands::New(const Arguments& args) { HandleScope scope; if (!args.IsConstructCall()) { return NODE_THROW("Cannot call constructor as function, you need to use 'new' keyword"); } if (args[0]->IsExternal()) { Local<External> ext = Local<External>::Cast(args[0]); void* ptr = ext->Value(); DatasetBands *f = static_cast<DatasetBands *>(ptr); f->Wrap(args.This()); return args.This(); } else { return NODE_THROW("Cannot create DatasetBands directly"); } }
void FieldDefn::typeSetter(Local<String> property, Local<Value> value, const AccessorInfo &info) { HandleScope scope; FieldDefn *def = ObjectWrap::Unwrap<FieldDefn>(info.This()); if(!value->IsString()){ NODE_THROW("type must be a string"); return; } std::string name = TOSTR(value); int type = getFieldTypeByName(name); if(type < 0){ NODE_THROW("Unrecognized field type"); } else { def->this_->SetType(OGRFieldType(type)); } }
Handle<Value> Dataset::getRasterBand(const Arguments& args) { HandleScope scope; GDALRasterBand *poBand; int band_id; NODE_ARG_INT(0, "band id", band_id); Dataset *ds = ObjectWrap::Unwrap<Dataset>(args.This()); if(!ds->this_) return NODE_THROW("Dataset object has already been destroyed"); poBand = ds->this_->GetRasterBand(band_id); if (poBand == NULL) { return NODE_THROW("Specified band not found"); } return scope.Close(RasterBand::New(poBand)); }
Handle<Value> LayerFields::count(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); Layer *layer = ObjectWrap::Unwrap<Layer>(parent); if (!layer->get()) { return NODE_THROW("Layer object already destroyed"); } OGRFeatureDefn *def = layer->get()->GetLayerDefn(); if (!def) { return NODE_THROW("Layer has no layer definition set"); } return scope.Close(Integer::New(def->GetFieldCount())); }
Handle<Value> LayerFields::add(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); Layer *layer = ObjectWrap::Unwrap<Layer>(parent); if (!layer->get()) { return NODE_THROW("Layer object already destroyed"); } if (args.Length() < 1) { return NODE_THROW("field definition(s) must be given"); } FieldDefn *field_def; int err; bool approx = true; NODE_ARG_BOOL_OPT(1, "approx", approx); if (args[0]->IsArray()) { Handle<Array> array = Handle<Array>::Cast(args[0]); int n = array->Length(); for (int i = 0; i < n; i++) { Handle<Value> element = array->Get(i); if (IS_WRAPPED(element, FieldDefn)) { field_def = ObjectWrap::Unwrap<FieldDefn>(element->ToObject()); err = layer->get()->CreateField(field_def->get(), approx); if (err) { return NODE_THROW_OGRERR(err); } } else { return NODE_THROW("All array elements must be FieldDefn objects"); } } } else if (IS_WRAPPED(args[0], FieldDefn)) { field_def = ObjectWrap::Unwrap<FieldDefn>(args[0]->ToObject()); err = layer->get()->CreateField(field_def->get(), approx); if (err) { return NODE_THROW_OGRERR(err); } } else { return NODE_THROW("field definition(s) must be a FieldDefn object or array of FieldDefn objects"); } return Undefined(); }
Handle<Value> Dataset::close(const Arguments& args) { Dataset *ds = ObjectWrap::Unwrap<Dataset>(args.This()); if(!ds->this_) return NODE_THROW("Dataset object has already been destroyed"); ds->dispose(); return Undefined(); }
void FeatureDefn::geomTypeSetter(Local<String> property, Local<Value> value, const AccessorInfo &info) { HandleScope scope; FeatureDefn *def = ObjectWrap::Unwrap<FeatureDefn>(info.This()); if(!value->IsInt32()){ NODE_THROW("geomType must be an integer"); return; } def->this_->SetGeomType(OGRwkbGeometryType(value->IntegerValue())); }
void FeatureDefn::geomIgnoredSetter(Local<String> property, Local<Value> value, const AccessorInfo &info) { HandleScope scope; FeatureDefn *def = ObjectWrap::Unwrap<FeatureDefn>(info.This()); if(!value->IsBoolean()){ NODE_THROW("geomIgnored must be a boolean"); return; } def->this_->SetGeometryIgnored(value->IntegerValue()); }
void FieldDefn::ignoredSetter(Local<String> property, Local<Value> value, const AccessorInfo &info) { HandleScope scope; FieldDefn *def = ObjectWrap::Unwrap<FieldDefn>(info.This()); if(!value->IsBoolean()){ NODE_THROW("ignored must be a boolean"); return; } def->this_->SetIgnored(value->BooleanValue()); }
void FieldDefn::precisionSetter(Local<String> property, Local<Value> value, const AccessorInfo &info) { HandleScope scope; FieldDefn *def = ObjectWrap::Unwrap<FieldDefn>(info.This()); if(!value->IsInt32()){ NODE_THROW("precision must be an integer"); return; } def->this_->SetPrecision(value->IntegerValue()); }
void FieldDefn::nameSetter(Local<String> property, Local<Value> value, const AccessorInfo &info) { HandleScope scope; FieldDefn *def = ObjectWrap::Unwrap<FieldDefn>(info.This()); if(!value->IsString()){ NODE_THROW("Name must be string"); return; } def->this_->SetName(TOSTR(value)); }
Handle<Value> LayerFields::indexOf(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); Layer *layer = ObjectWrap::Unwrap<Layer>(parent); if (!layer->get()) { return NODE_THROW("Layer object already destroyed"); } OGRFeatureDefn *def = layer->get()->GetLayerDefn(); if (!def) { return NODE_THROW("Layer has no layer definition set"); } std::string name(""); NODE_ARG_STR(0, "field name", name); return scope.Close(Integer::New(def->GetFieldIndex(name.c_str()))); }