1

I have an iOS GIS app written in Swift that relies heavily on GDAL 2.2 for the spatial data management. I've written Swift wrappers around the parts of the standard-C GDAL that I want to use. I'm adding options to the Swift wrappers to export data in various other formats.

However, when exporting to CSV, I'd like to include the coordinates (for point features). However, the X/Y columns never get created, despite passing the "GEOMETRY=AS_XYZ" option to GDALCreate. (I was also unable to find any examples when doing a web search - just lots of examples of doing it with the ogr2ogr utility.)

I've not used options with GDALCreate before, so it's possible that I'm doing it wrong. There are also some complications with doing this from Swift. In particular, converting from Swift string objects to standard C strings (char pointer) and especially from a Swift array of strings to standard C array of strings (pointer to char pointer).

Here's how I'm attempting to do this:

let gdalDriver = GDALGetDriverByName("CSV".cString(using: .utf8))
let options = ["GEOMETRY=AS_XYZ", "CREATE_CSVT=YES"]
var cOpts = options.map { strdup($0) }
cOpts.append(nil)
ogrDS = GDALCreate(gdalDriver, url.path.cString(using: .utf8), 0, 0, 0, GDT_Unknown, &cOpts)
for ptr in cOpts { free(ptr) }

From this point, I can copy GDAL features to it from my source points dataset, and that works fine (all features with all attributes appear in the CSV) except that there are no coorinates (and no columns for the coordinates).

Has anybody here had any success creating CSV data sets with GDAL standard C (or C++) including X/Y columns for points? What am I doing wrong here?

(Should I post this in Stack Overflow instead of or as well as here?)

2

1 Answer 1

1

Solved! The problem was that I should be passing these particular option(s) to the function that creates the layer, and not to the function that creates the data source.

Eg, like this:

let options = ["GEOMETRY=AS_XYZ", "CREATE_CSVT=YES"]
var cOpts = options.map { strdup($0) }
cOpts.append(nil)
let ogrLayer = GDALDatasetCreateLayer(ds, name.cString(using: .utf8), srs, geometryType, &cOpts)
for ptr in cOpts { free(ptr) }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.