Handle<Value> Dataset::setGCPs(const Arguments& args) { Dataset *ds = ObjectWrap::Unwrap<Dataset>(args.This()); if(!ds->this_) return NODE_THROW("Dataset object has already been destroyed"); Handle<Array> gcps; std::string projection(""); NODE_ARG_ARRAY(0, "gcps", gcps); NODE_ARG_OPT_STR(1, "projection", projection); GDAL_GCP* list = new GDAL_GCP [gcps->Length()]; GDAL_GCP* gcp = list; for (unsigned int i = 0; i < gcps->Length(); ++i) { Local<Value> val = gcps->Get(i); if (!val->IsObject()) { return NODE_THROW("list of GCPs must only contain objects"); } Local<Object> obj = val->ToObject(); NODE_STR_FROM_OBJ_OPT(obj, "pszId", gcp->pszId); NODE_STR_FROM_OBJ_OPT(obj, "pszInfo", gcp->pszInfo); NODE_DOUBLE_FROM_OBJ(obj, "dfGCPPixel", gcp->dfGCPPixel); NODE_DOUBLE_FROM_OBJ(obj, "dfGCPLine", gcp->dfGCPLine); NODE_DOUBLE_FROM_OBJ(obj, "dfGCPX", gcp->dfGCPX); NODE_DOUBLE_FROM_OBJ(obj, "dfGCPY", gcp->dfGCPY); NODE_DOUBLE_FROM_OBJ_OPT(obj, "dfGCPZ", gcp->dfGCPZ); gcp++; } if (list) delete [] list; CPLErr err = ds->this_->SetGCPs(gcps->Length(), list, projection.c_str()); if(err) return NODE_THROW_CPLERR(err); return Undefined(); }
Handle<Value> LineStringPoints::add(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); LineString *geom = ObjectWrap::Unwrap<LineString>(parent); int n = args.Length(); if(n == 0) { return NODE_THROW("Point must be given"); } else if(n == 1) { if(!args[0]->IsObject()) { return NODE_THROW("Point, object, or array of points expected"); } if(IS_WRAPPED(args[0], Point)){ //set from Point object Point* pt = ObjectWrap::Unwrap<Point>(args[0]->ToObject()); geom->get()->addPoint(pt->get()); } else if (args[0]->IsArray()) { //set from array of points Handle<Array> array = Handle<Array>::Cast(args[0]); int length = array->Length(); for (int i = 0; i < length; i++){ Handle<Value> element = array->Get(i); if(!element->IsObject()) { return NODE_THROW("All points must be Point objects or objects"); } Handle<Object> element_obj = element->ToObject(); if(IS_WRAPPED(element_obj, Point)){ //set from Point object Point* pt = ObjectWrap::Unwrap<Point>(element_obj); geom->get()->addPoint(pt->get()); } else { //set from object {x: 0, y: 5} double x, y; NODE_DOUBLE_FROM_OBJ(element_obj, "x", x); NODE_DOUBLE_FROM_OBJ(element_obj, "y", y); Handle<String> z_prop_name = String::NewSymbol("z"); if (element_obj->HasOwnProperty(z_prop_name)) { Handle<Value> z_val = element_obj->Get(z_prop_name); if (!z_val->IsNumber()) { return NODE_THROW("z property must be number"); } geom->get()->addPoint(x, y, z_val->NumberValue()); } else { geom->get()->addPoint(x, y); } } } } else { //set from object {x: 0, y: 5} Handle<Object> obj = args[0]->ToObject(); double x, y; NODE_DOUBLE_FROM_OBJ(obj, "x", x); NODE_DOUBLE_FROM_OBJ(obj, "y", y); Handle<String> z_prop_name = String::NewSymbol("z"); if (obj->HasOwnProperty(z_prop_name)) { Handle<Value> z_val = obj->Get(z_prop_name); if (!z_val->IsNumber()) { return NODE_THROW("z property must be number"); } geom->get()->addPoint(x, y, z_val->NumberValue()); } else { geom->get()->addPoint(x, y); } } } else { //set x, y, z from numeric arguments if(!args[0]->IsNumber()){ return NODE_THROW("Number expected for first argument"); } if(!args[1]->IsNumber()){ return NODE_THROW("Number expected for second argument"); } if(n == 2){ geom->get()->addPoint(args[0]->NumberValue(), args[1]->NumberValue()); } else { if(!args[2]->IsNumber()){ return NODE_THROW("Number expected for third argument"); } geom->get()->addPoint(args[0]->NumberValue(), args[1]->NumberValue(), args[2]->NumberValue()); } } return Undefined(); }
Handle<Value> LineStringPoints::set(const Arguments& args) { HandleScope scope; Handle<Object> parent = args.This()->GetHiddenValue(String::NewSymbol("parent_"))->ToObject(); LineString *geom = ObjectWrap::Unwrap<LineString>(parent); int i; NODE_ARG_INT(0, "index", i); if(i < 0 || i >= geom->get()->getNumPoints()) { return NODE_THROW("Point index out of range"); } int n = args.Length() - 1; if(n == 0) { return NODE_THROW("Point must be given"); } else if(n == 1) { if(!args[1]->IsObject()) { return NODE_THROW("Point or object expected for second argument"); } if(IS_WRAPPED(args[1], Point)){ //set from Point object Point* pt = ObjectWrap::Unwrap<Point>(args[1]->ToObject()); geom->get()->setPoint(i, pt->get()); } else { Handle<Object> obj = args[1]->ToObject(); //set from object {x: 0, y: 5} double x, y; NODE_DOUBLE_FROM_OBJ(obj, "x", x); NODE_DOUBLE_FROM_OBJ(obj, "y", y); Handle<String> z_prop_name = String::NewSymbol("z"); if (obj->HasOwnProperty(z_prop_name)) { Handle<Value> z_val = obj->Get(z_prop_name); if (!z_val->IsNumber()) { return NODE_THROW("z property must be number"); } geom->get()->setPoint(i, x, y, z_val->NumberValue()); } else { geom->get()->setPoint(i, x, y); } } } else { //set x, y, z from numeric arguments if(!args[1]->IsNumber()){ return NODE_THROW("Number expected for second argument"); } if(!args[2]->IsNumber()){ return NODE_THROW("Number expected for third argument"); } if(n == 2){ geom->get()->setPoint(i, args[1]->NumberValue(), args[2]->NumberValue()); } else { if(!args[3]->IsNumber()){ return NODE_THROW("Number expected for fourth argument"); } geom->get()->setPoint(i, args[1]->NumberValue(), args[2]->NumberValue(), args[3]->NumberValue()); } } return Undefined(); }