yeesian / ArchGDAL.jl

A high level API for GDAL - Geospatial Data Abstraction Library
https://yeesian.github.io/ArchGDAL.jl/stable/
Other
137 stars 25 forks source link

Document better how to write vector data #400

Open maxfreu opened 7 months ago

maxfreu commented 7 months ago

Hi! This is half an issue and half a reminder to myself that I / we should improve the documentation on how to write vector data. Just struggled to come up with code to write a dumb file containing points. Had to read the tests to do it and even then it didn't work without tweaking. Here's what I came up with for reference:

function write_result_sqlite(filename, points)
    AG.create(filename; driver=AG.getdriver("SQLite")) do ds
        AG.createlayer(name = "geom",
                       dataset = ds,
                       geom = AG.wkbPoint,
                       spatialref = AG.importEPSG(25832)) do layer
            AG.addfielddefn!(layer, "point_id", AG.OFTInteger)
            id_idx = AG.findfieldindex(AG.layerdefn(layer), "point_id")
            i = 0
            for chunk in Iterators.partition(points, 20000)
                GDAL.ogr_l_starttransaction(layer)
                for pt in chunk
                    AG.addfeature(layer) do feature
                        AG.setfid!(feature, i)
                        AG.setfield!(feature, id_idx, i)
                        AG.setgeom!(feature, 0, AG.createpoint(pt...))
                        return nothing
                    end
                    i += 1 
                end
                GDAL.ogr_l_committransaction(layer)
            end
        end
    end
    return nothing
end

Speed is good, but it's a bit clunky. A higher level API for this kind of tasks would be really nice as well.

yeesian commented 7 months ago

This was something I ran into too, and fully agree with, thank you for writing this up and providing the code snippet for it!