Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FieldDefn subtype #113

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ toc:
- OFTTime
- OFTWideString
- OFTWideStringList
- name: Feature Field Sub-Types
children:
- OFSTNone
- OFSTBoolean
- OFSTInt16
- OFSTFloat32
- name: Justification
children:
- OJLeft
Expand Down
15 changes: 15 additions & 0 deletions lib/gdal.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,17 @@ function fieldTypeFromValue(val) {
throw new Error('Value cannot be converted into OGRFieldType')
}

function fieldSubtypeFromValue(val) {
const type = typeof val
if (type === 'boolean') {
return gdal.OFSTBoolean
} else if (val instanceof Array) {
return fieldSubtypeFromValue(val[0])
} else {
return gdal.OFSTNone
}
}

/**
* Creates a LayerFields instance from an object of keys and values.
*
Expand Down Expand Up @@ -611,7 +622,11 @@ gdal.LayerFields.prototype.fromObject = function (obj, approx_ok) {
approx_ok = approx_ok || false
Object.entries(obj).forEach(([ k, v ]) => {
const type = fieldTypeFromValue(v)
const subtype = fieldSubtypeFromValue(v)
const def = new gdal.FieldDefn(k, type)
if (subtype) {
def.subtype = subtype
}
this.add(def, approx_ok)
})
}
Expand Down
35 changes: 35 additions & 0 deletions src/gdal_field_defn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void FieldDefn::Initialize(Local<Object> target) {

ATTR(lcons, "name", nameGetter, nameSetter);
ATTR(lcons, "type", typeGetter, typeSetter);
ATTR(lcons, "subtype", subtypeGetter, subtypeSetter);
ATTR(lcons, "justification", justificationGetter, justificationSetter);
ATTR(lcons, "width", widthGetter, widthSetter);
ATTR(lcons, "precision", precisionGetter, precisionSetter);
Expand Down Expand Up @@ -142,6 +143,25 @@ NAN_GETTER(FieldDefn::typeGetter) {
info.GetReturnValue().Set(SafeString::New(getFieldTypeName(def->this_->GetType())));
}

/**
* Data subtype (see {@link OFST|OFST constants}
*
* @kind member
* @name subtype
* @instance
* @memberof FieldDefn
* @type {string}
*/
NAN_GETTER(FieldDefn::subtypeGetter) {
FieldDefn *def = Nan::ObjectWrap::Unwrap<FieldDefn>(info.This());
OGRFieldSubType subtype = def->this_->GetSubType();
if (subtype == OFSTNone) {
info.GetReturnValue().Set(Nan::Undefined());
return;
}
info.GetReturnValue().Set(SafeString::New(getFieldSubTypeName(def->this_->GetSubType())));
}

/**
* @kind member
* @name ignored
Expand Down Expand Up @@ -226,6 +246,21 @@ NAN_SETTER(FieldDefn::typeSetter) {
}
}

NAN_SETTER(FieldDefn::subtypeSetter) {
FieldDefn *def = Nan::ObjectWrap::Unwrap<FieldDefn>(info.This());
if (!value->IsString()) {
Nan::ThrowError("subtype must be a string");
return;
}
std::string name = *Nan::Utf8String(value);
int subtype = getFieldSubTypeByName(name.c_str());
if (subtype < 0) {
Nan::ThrowError("Unrecognized field subtype");
} else {
def->this_->SetSubType(OGRFieldSubType(subtype));
}
}

NAN_SETTER(FieldDefn::justificationSetter) {
FieldDefn *def = Nan::ObjectWrap::Unwrap<FieldDefn>(info.This());

Expand Down
2 changes: 2 additions & 0 deletions src/gdal_field_defn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class FieldDefn : public Nan::ObjectWrap {

static NAN_GETTER(nameGetter);
static NAN_GETTER(typeGetter);
static NAN_GETTER(subtypeGetter);
static NAN_GETTER(justificationGetter);
static NAN_GETTER(precisionGetter);
static NAN_GETTER(widthGetter);
static NAN_GETTER(ignoredGetter);

static NAN_SETTER(nameSetter);
static NAN_SETTER(typeSetter);
static NAN_SETTER(subtypeSetter);
static NAN_SETTER(justificationSetter);
static NAN_SETTER(precisionSetter);
static NAN_SETTER(widthSetter);
Expand Down
37 changes: 37 additions & 0 deletions src/node_gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,43 @@ static void Init(Local<Object> target, Local<v8::Value>, void *) {
*/
Nan::Set(target, Nan::New("OFTDateTime").ToLocalChecked(), Nan::New(getFieldTypeName(OFTDateTime)).ToLocalChecked());

/*
* Field sub-types
*/

/**
* @final
* @constant
* @name OFSTNone
* @type {string}
*/
Nan::Set(
target, Nan::New("OFSTNone").ToLocalChecked(), Nan::New(getFieldSubTypeName(OFSTNone)).ToLocalChecked());
/**
* @final
* @constant
* @name OFSTBoolean
* @type {string}
*/
Nan::Set(
target, Nan::New("OFSTBoolean").ToLocalChecked(), Nan::New(getFieldSubTypeName(OFSTBoolean)).ToLocalChecked());
/**
* @final
* @constant
* @name OFSTInt16
* @type {string}
*/
Nan::Set(
target, Nan::New("OFSTInt16").ToLocalChecked(), Nan::New(getFieldSubTypeName(OFSTInt16)).ToLocalChecked());
/**
* @final
* @constant
* @name OFSTFloat32
* @type {string}
*/
Nan::Set(
target, Nan::New("OFSTFloat32").ToLocalChecked(), Nan::New(getFieldSubTypeName(OFSTFloat32)).ToLocalChecked());

/*
* Resampling options that can be used with the gdal.reprojectImage() and gdal.RasterBandPixels.read methods.
*/
Expand Down
23 changes: 22 additions & 1 deletion src/utils/field_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,25 @@ inline int getFieldTypeByName(std::string name) {
return -1;
}

#endif
// OFSTNone = 0, OFSTBoolean = 1, OFSTInt16 = 2, OFSTFloat32 = 3

static const char *const FIELD_SUBTYPES[] = {
/* 0 */ "none",
/* 1 */ "boolean",
/* 2 */ "integer16",
/* 3 */ "float32"};

inline const char *getFieldSubTypeName(OGRFieldSubType subtype) {
int i = static_cast<int>(subtype);
if (i < 0 || i > OFSTFloat32) { return "invalid"; }
return FIELD_SUBTYPES[i];
}

inline int getFieldSubTypeByName(std::string name) {
for (int i = 0; i <= OFSTFloat32; i++) {
if (name == FIELD_SUBTYPES[i]) return i;
}
return 0;
}

#endif