Skip to content

Commit

Permalink
Handle setting geometry column name for GDAL 3.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Cabion committed Mar 21, 2024
1 parent 41946e3 commit 79cc9be
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func ErrLogger(fn ErrorHandler) interface {
SetNoDataOption
SetScaleOffsetOption
SetGeoTransformOption
SetGeometryColumnNameOption
SetProjectionOption
SetSpatialRefOption
SieveFilterOption
Expand Down Expand Up @@ -241,6 +242,9 @@ func (ec errorCallback) setFillnodataOpt(o *fillnodataOpts) {
func (ec errorCallback) setGeojsonOpt(o *geojsonOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGeometryColumnNameOpt(o *setGeometryColumnNameOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setGeometryTransformOpt(o *geometryTransformOpts) {
o.errorHandler = ec.fn
}
Expand Down
14 changes: 14 additions & 0 deletions godal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,20 @@ void godalLayerSetFeature(cctx *ctx, OGRLayerH layer, OGRFeatureH feat) {
godalUnwrap();
}

void godalLayerSetGeometryColumnName(cctx *ctx, OGRLayerH layer, char *name) {
godalWrap(ctx);
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 6, 0)
OGRGeomFieldDefnH fieldWithNewName = OGR_GFld_Create(name, OGRwkbGeometryType(0));
OGRErr gret = OGR_L_AlterGeomFieldDefn(layer, 0, fieldWithNewName, ALTER_GEOM_FIELD_DEFN_NAME_FLAG);
if(gret!=0){
forceOGRError(ctx,gret);
}
#else
CPLError(CE_Failure, CPLE_NotSupported, "OGR_L_AlterGeomFieldDefn is only supported in GDAL version >= 3.6");
#endif
godalUnwrap();
}

void godalFeatureSetGeometry(cctx *ctx, OGRFeatureH feat, OGRGeometryH geom) {
godalWrap(ctx);
OGRErr gret = OGR_F_SetGeometry(feat,geom);
Expand Down
14 changes: 14 additions & 0 deletions godal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,7 @@ func (f *Feature) SetGeometry(geom *Geometry, opts ...SetGeometryOption) error {
return cgc.close()
}

// Deprecated: when running with GDAL 3.6+, you may use SetGeometryColumnName on Layer
// SetGeometryColumnName set the name of feature first geometry field.
func (f *Feature) SetGeometryColumnName(name string) {
cname := C.CString(name)
Expand Down Expand Up @@ -3271,6 +3272,19 @@ func (layer Layer) DeleteFeature(feat *Feature, opts ...DeleteFeatureOption) err
return cgc.close()
}

// SetGeometryColumnName set the name of feature first geometry field.
func (layer Layer) SetGeometryColumnName(name string, opts ...SetGeometryColumnNameOption) error {
so := &setGeometryColumnNameOpts{}
for _, o := range opts {
o.setGeometryColumnNameOpt(so)
}
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
cgc := createCGOContext(nil, so.errorHandler)
C.godalLayerSetGeometryColumnName(cgc.cPointer(), layer.handle(), (*C.char)(cname))
return cgc.close()
}

// CreateLayer creates a new vector layer
//
// Available CreateLayerOptions are
Expand Down
1 change: 1 addition & 0 deletions godal.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ extern "C" {
void godalLayerCreateFeature(cctx *ctx, OGRLayerH layer, OGRFeatureH feat);
OGRFeatureH godalLayerNewFeature(cctx *ctx, OGRLayerH layer, OGRGeometryH geom);
void godalLayerDeleteFeature(cctx *ctx, OGRLayerH layer, OGRFeatureH feat);
void godalLayerSetGeometryColumnName(cctx *ctx, OGRLayerH layer, char *name);
void godalFeatureSetGeometry(cctx *ctx, OGRFeatureH feat, OGRGeometryH geom);
void godalFeatureSetFieldInteger(cctx *ctx, OGRFeatureH feat, int fieldIndex, int value);
void godalFeatureSetFieldInteger64(cctx *ctx, OGRFeatureH feat, int fieldIndex, long long value);
Expand Down
14 changes: 14 additions & 0 deletions godal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2677,6 +2677,11 @@ func TestLayerModifyFeatures(t *testing.T) {
ds, _ := Open("testdata/test.geojson") //read-only
defer ds.Close()
l := ds.Layers()[0]

ehc := eh()
err := l.SetGeometryColumnName("error", ErrLogger(ehc.ErrorHandler)) // can't set geometry colum name on geojson
assert.Error(t, err)

for {
ff := l.NextFeature()
if ff == nil {
Expand All @@ -2696,6 +2701,15 @@ func TestLayerModifyFeatures(t *testing.T) {
dsm, _ := ds.VectorTranslate("", []string{"-of", "Memory"})
defer dsm.Close()
l = dsm.Layers()[0]

err = l.SetGeometryColumnName("no_error_after_3.6")
runtimeVersion := Version()
if runtimeVersion.Major() <= 3 && runtimeVersion.Minor() < 6 {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}

for {
ff := l.NextFeature()
if ff == nil {
Expand Down
13 changes: 13 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ type updateFeatureOpts struct {
type UpdateFeatureOption interface {
setUpdateFeatureOpt(o *updateFeatureOpts)
}

type deleteFeatureOpts struct {
errorHandler ErrorHandler
}
Expand All @@ -590,6 +591,18 @@ type DeleteFeatureOption interface {
setDeleteFeatureOpt(o *deleteFeatureOpts)
}

type setGeometryColumnNameOpts struct {
errorHandler ErrorHandler
}

// SetGeometryColumnNameOption is an option passed to Layer.SetGeometryColumnName()
//
// Available options are:
// - ErrLogger
type SetGeometryColumnNameOption interface {
setGeometryColumnNameOpt(o *setGeometryColumnNameOpts)
}

type geometryTransformOpts struct {
errorHandler ErrorHandler
}
Expand Down

0 comments on commit 79cc9be

Please sign in to comment.