Skip to content

Commit 0de86cc

Browse files
visryeesian
andauthored
let ccall get the pointers using unsafe_convert (#349)
* fix undefined ArchGDAL in tests * unsafe_convert to Ptr{Cvoid} for all wrapper types * pass wrapper types to gdal, not pointers * remove a few more pointer calls * Reduce scope of for-loop * Reverse order of nested for-loops There isn't any logical changes here; just refactoring. * Using let-block to contain scope of variables * Pull createlinearring functions out of inner for-loop They do not follow the patterns for the other geometry types. * Some bugfixes Namely (i) to finalize the internal geometries that were cloned and (ii) to appropriately parameterize the geometries returned. * Remove outdated tests of internal methods `_infergeomtype(...)` should be operating on pointers and not geometries. Co-authored-by: Yeesian Ng <[email protected]>
1 parent 7b7f559 commit 0de86cc

22 files changed

+570
-542
lines changed

docs/src/memory.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mutable struct RasterBand <: AbstractRasterBand
4545
end
4646

4747
unsafe_getband(dataset::AbstractDataset, i::Integer) =
48-
RasterBand(GDAL.getrasterband(dataset.ptr, i))
48+
RasterBand(GDAL.getrasterband(dataset, i))
4949

5050
function destroy(rb::AbstractRasterBand)
5151
rb.ptr = C_NULL
@@ -79,7 +79,7 @@ mutable struct IRasterBand <: AbstractRasterBand
7979
end
8080

8181
getband(dataset::AbstractDataset, i::Integer) =
82-
IRasterBand(GDAL.getrasterband(dataset.ptr, i), ownedby = dataset)
82+
IRasterBand(GDAL.getrasterband(dataset, i), ownedby = dataset)
8383

8484
function destroy(rasterband::IRasterBand)
8585
rasterband.ptr = C_NULL

src/base/iterators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function Base.iterate(
44
)::Union{Nothing,Tuple{IFeature,Int64}}
55
layer.ptr == C_NULL && return nothing
66
state == 0 && resetreading!(layer)
7-
ptr = GDAL.ogr_l_getnextfeature(layer.ptr)
7+
ptr = GDAL.ogr_l_getnextfeature(layer)
88
return if ptr == C_NULL
99
resetreading!(layer)
1010
nothing

src/dataset.jl

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ function copywholeraster!(
2828
progressdata::Any = C_NULL,
2929
)::D where {D<:AbstractDataset}
3030
result = GDAL.gdaldatasetcopywholeraster(
31-
source.ptr,
32-
dest.ptr,
31+
source,
32+
dest,
3333
options,
3434
@cplprogress(progressfunc),
3535
progressdata,
@@ -98,9 +98,9 @@ function unsafe_copy(
9898
)::Dataset
9999
return Dataset(
100100
GDAL.gdalcreatecopy(
101-
driver.ptr,
101+
driver,
102102
filename,
103-
dataset.ptr,
103+
dataset,
104104
strict,
105105
options,
106106
@cplprogress(progressfunc),
@@ -157,9 +157,9 @@ function copy(
157157
)::IDataset
158158
return IDataset(
159159
GDAL.gdalcreatecopy(
160-
driver.ptr,
160+
driver,
161161
filename,
162-
dataset.ptr,
162+
dataset,
163163
strict,
164164
options,
165165
@cplprogress(progressfunc),
@@ -178,21 +178,21 @@ Writes the dataset to the designated filename.
178178
* `filename`: The filename, UTF-8 encoded.
179179
180180
### Keyword Arguments
181-
* `driver` (ArchGDAL.Driver): The driver to use, you have to manually select the right driver via `getdriver(drivername)` matching the file extension you wish.
181+
* `driver` (ArchGDAL.Driver): The driver to use, you have to manually select the right driver via `getdriver(drivername)` matching the file extension you wish.
182182
Otherwise the driver of the source dataset will be used.
183183
* `options` (Vector{String}): A vector of strings containing KEY=VALUE pairs for driver-specific creation options.
184184
* `layer_options`: Driver specific options for layer creation. The options can either be a Vector{String} to provide the
185-
same options for each layer, or a Vector{Vector{String}} to provide individual options per layer, in the order of their
186-
appearance in the dataset. The strings have to be KEY=VALUE pairs. If you give less individual options than there are layers,
187-
the remaining layers use the default creation options. An example for two layers:
185+
same options for each layer, or a Vector{Vector{String}} to provide individual options per layer, in the order of their
186+
appearance in the dataset. The strings have to be KEY=VALUE pairs. If you give less individual options than there are layers,
187+
the remaining layers use the default creation options. An example for two layers:
188188
`[["FORMAT=WKT", "LAUNDER=NO"], ["STRICT=NO"]]`
189-
* `use_gdal_copy` (Bool): Set this to false (default is true) to achieve higher write speeds at the cost of possible errors.
189+
* `use_gdal_copy` (Bool): Set this to false (default is true) to achieve higher write speeds at the cost of possible errors.
190190
Note that when set to true, no coordinate transformations are possible while writing the features.
191-
* `chunksize` (Integer): Number of features to write in one database transaction. Neglected when `use_gdal_copy` is true.
191+
* `chunksize` (Integer): Number of features to write in one database transaction. Neglected when `use_gdal_copy` is true.
192192
Default is 20000.
193193
* `strict` (Bool): Set this to `true` if the written dataset should be a 1:1 copy of the source data, default is `false`,
194194
which allows the driver to adapt if necessary.
195-
195+
196196
### Returns
197197
`nothing`
198198
"""
@@ -204,8 +204,8 @@ function write(
204204
if nraster(dataset) > 0 && nlayer(dataset) > 0
205205
drivername = shortname(get(kwargs, :driver, getdriver(dataset)))
206206
error(
207-
"Writing datasets with raster and vector data is not supported when using driver $drivername.
208-
Please file an issue at https://github.com/yeesian/ArchGDAL.jl/issues
207+
"Writing datasets with raster and vector data is not supported when using driver $drivername.
208+
Please file an issue at https://github.com/yeesian/ArchGDAL.jl/issues
209209
including following dataset information: \n\n$dataset",
210210
)
211211
elseif nraster(dataset) > 0
@@ -226,7 +226,7 @@ _getlayeroptions(options::Union{Ptr{Cstring},Vector{String}}, i) = options
226226
writelayers(dataset, filename; kwargs...)
227227
228228
Writes the vector dataset to the designated filename. The options are passed to the newly created dataset and
229-
have to be given as a list of strings in KEY=VALUE format. The chunksize controls the number of features written
229+
have to be given as a list of strings in KEY=VALUE format. The chunksize controls the number of features written
230230
in each database transaction, e.g. for SQLite. This function can also be used to copy datasets on disk.
231231
232232
Currently working drivers: FlatGeobuf, GeoJSON, GeoJSONSeq, GML, GPKG, JML, KML, MapML, ESRI Shapefile, SQLite
@@ -239,11 +239,11 @@ Currently working drivers: FlatGeobuf, GeoJSON, GeoJSONSeq, GML, GPKG, JML, KML,
239239
* `driver`: The driver to use, you have to manually select the right driver for the file extension you wish
240240
* `options`: A vector of strings containing KEY=VALUE pairs for driver-specific creation options
241241
* `layer_options`: Driver specific options for layer creation. The options can either be a Vector{String} to provide the
242-
same options for each layer, or a Dict(layer_index => Vector{String}) to provide individual options per layer.
243-
Note that layer indexing in GDAL starts with 0. The strings have to be KEY=VALUE pairs. An example for two layers:
242+
same options for each layer, or a Dict(layer_index => Vector{String}) to provide individual options per layer.
243+
Note that layer indexing in GDAL starts with 0. The strings have to be KEY=VALUE pairs. An example for two layers:
244244
`[["FORMAT=WKT", "LAUNDER=NO"], ["STRICT=NO"]]`
245245
* `chunksize`: Number of features to write in one database transaction. Neglected when `use_gdal_copy` is true.
246-
* `use_gdal_copy`: Set this to false (default is true) to achieve higher write speeds at the cost of possible errors.
246+
* `use_gdal_copy`: Set this to false (default is true) to achieve higher write speeds at the cost of possible errors.
247247
Note that when set to true, no coordinate transformations are possible while writing the features.
248248
249249
### Returns
@@ -314,11 +314,11 @@ Dict{<:Integer, Vector{String}} to set individual options per layer.",
314314

315315
# iterate over features in chunks to get better speed than gdaldatasetcopylayer
316316
for chunk in Iterators.partition(sourcelayer, chunksize)
317-
GDAL.ogr_l_starttransaction(targetlayer.ptr)
317+
GDAL.ogr_l_starttransaction(targetlayer)
318318
for feature in chunk
319319
addfeature!(targetlayer, feature)
320320
end
321-
GDAL.ogr_l_committransaction(targetlayer.ptr)
321+
GDAL.ogr_l_committransaction(targetlayer)
322322
end
323323
end # createlayer
324324
end # if
@@ -361,7 +361,7 @@ function unsafe_create(
361361
options = StringList(C_NULL),
362362
)::Dataset
363363
result = GDAL.gdalcreate(
364-
driver.ptr,
364+
driver,
365365
filename,
366366
width,
367367
height,
@@ -382,7 +382,7 @@ function unsafe_create(
382382
options = StringList(C_NULL),
383383
)::Dataset
384384
result = GDAL.gdalcreate(
385-
driver.ptr,
385+
driver,
386386
filename,
387387
width,
388388
height,
@@ -434,7 +434,7 @@ function create(
434434
options = StringList(C_NULL),
435435
)::IDataset
436436
result = GDAL.gdalcreate(
437-
driver.ptr,
437+
driver,
438438
filename,
439439
width,
440440
height,
@@ -455,7 +455,7 @@ function create(
455455
options = StringList(C_NULL),
456456
)::IDataset
457457
result = GDAL.gdalcreate(
458-
driver.ptr,
458+
driver,
459459
filename,
460460
width,
461461
height,
@@ -601,38 +601,38 @@ unsafe_update(filename::AbstractString; flags = OF_UPDATE, kwargs...)::Dataset =
601601
602602
Fetch raster width in pixels.
603603
"""
604-
width(dataset::AbstractDataset)::Integer = GDAL.gdalgetrasterxsize(dataset.ptr)
604+
width(dataset::AbstractDataset)::Integer = GDAL.gdalgetrasterxsize(dataset)
605605

606606
"""
607607
height(dataset::AbstractDataset)
608608
609609
Fetch raster height in pixels.
610610
"""
611-
height(dataset::AbstractDataset)::Integer = GDAL.gdalgetrasterysize(dataset.ptr)
611+
height(dataset::AbstractDataset)::Integer = GDAL.gdalgetrasterysize(dataset)
612612

613613
"""
614614
nraster(dataset::AbstractDataset)
615615
616616
Fetch the number of raster bands on this dataset.
617617
"""
618618
nraster(dataset::AbstractDataset)::Integer =
619-
GDAL.gdalgetrastercount(dataset.ptr)
619+
GDAL.gdalgetrastercount(dataset)
620620

621621
"""
622622
nlayer(dataset::AbstractDataset)
623623
624624
Fetch the number of feature layers on this dataset.
625625
"""
626626
nlayer(dataset::AbstractDataset)::Integer =
627-
GDAL.gdaldatasetgetlayercount(dataset.ptr)
627+
GDAL.gdaldatasetgetlayercount(dataset)
628628

629629
"""
630630
getdriver(dataset::AbstractDataset)
631631
632632
Fetch the driver that the dataset was created with
633633
"""
634634
getdriver(dataset::AbstractDataset)::Driver =
635-
Driver(GDAL.gdalgetdatasetdriver(dataset.ptr))
635+
Driver(GDAL.gdalgetdatasetdriver(dataset))
636636

637637
"""
638638
filelist(dataset::AbstractDataset)
@@ -648,7 +648,7 @@ The returned filenames will normally be relative or absolute paths depending on
648648
the path used to originally open the dataset. The strings will be UTF-8 encoded
649649
"""
650650
filelist(dataset::AbstractDataset)::Vector{String} =
651-
GDAL.gdalgetfilelist(dataset.ptr)
651+
GDAL.gdalgetfilelist(dataset)
652652

653653
"""
654654
getlayer(dataset::AbstractDataset, i::Integer)
@@ -659,7 +659,7 @@ The returned layer remains owned by the `dataset` and should not be deleted by
659659
the application.
660660
"""
661661
getlayer(dataset::AbstractDataset, i::Integer)::IFeatureLayer =
662-
IFeatureLayer(GDAL.gdaldatasetgetlayer(dataset.ptr, i), ownedby = dataset)
662+
IFeatureLayer(GDAL.gdaldatasetgetlayer(dataset, i), ownedby = dataset)
663663

664664
"""
665665
getlayer(dataset::AbstractDataset)
@@ -673,17 +673,17 @@ function getlayer(dataset::AbstractDataset)::IFeatureLayer
673673
nlayer(dataset) == 1 ||
674674
error("Dataset has multiple layers. Specify the layer number or name")
675675
return IFeatureLayer(
676-
GDAL.gdaldatasetgetlayer(dataset.ptr, 0),
676+
GDAL.gdaldatasetgetlayer(dataset, 0),
677677
ownedby = dataset,
678678
)
679679
end
680680

681681
unsafe_getlayer(dataset::AbstractDataset, i::Integer)::FeatureLayer =
682-
FeatureLayer(GDAL.gdaldatasetgetlayer(dataset.ptr, i))
682+
FeatureLayer(GDAL.gdaldatasetgetlayer(dataset, i))
683683
function unsafe_getlayer(dataset::AbstractDataset)::FeatureLayer
684684
nlayer(dataset) == 1 ||
685685
error("Dataset has multiple layers. Specify the layer number or name")
686-
return FeatureLayer(GDAL.gdaldatasetgetlayer(dataset.ptr, 0))
686+
return FeatureLayer(GDAL.gdaldatasetgetlayer(dataset, 0))
687687
end
688688

689689
"""
@@ -696,13 +696,13 @@ the application.
696696
"""
697697
function getlayer(dataset::AbstractDataset, name::AbstractString)::IFeatureLayer
698698
return IFeatureLayer(
699-
GDAL.gdaldatasetgetlayerbyname(dataset.ptr, name),
699+
GDAL.gdaldatasetgetlayerbyname(dataset, name),
700700
ownedby = dataset,
701701
)
702702
end
703703

704704
unsafe_getlayer(dataset::AbstractDataset, name::AbstractString)::FeatureLayer =
705-
FeatureLayer(GDAL.gdaldatasetgetlayerbyname(dataset.ptr, name))
705+
FeatureLayer(GDAL.gdaldatasetgetlayerbyname(dataset, name))
706706

707707
"""
708708
deletelayer!(dataset::AbstractDataset, i::Integer)
@@ -714,7 +714,7 @@ Delete the indicated layer (at index i; between `0` to `nlayer()-1`)
714714
is not supported for this dataset.
715715
"""
716716
function deletelayer!(dataset::T, i::Integer)::T where {T<:AbstractDataset}
717-
result = GDAL.gdaldatasetdeletelayer(dataset.ptr, i)
717+
result = GDAL.gdaldatasetdeletelayer(dataset, i)
718718
@ogrerr result "Failed to delete layer"
719719
return dataset
720720
end
@@ -746,7 +746,7 @@ the strings themselves to avoid misspelling.
746746
* `capability`: the capability to test.
747747
"""
748748
testcapability(dataset::AbstractDataset, capability::AbstractString)::Bool =
749-
Bool(GDAL.gdaldatasettestcapability(dataset.ptr, capability))
749+
Bool(GDAL.gdaldatasettestcapability(dataset, capability))
750750

751751
function listcapability(
752752
dataset::AbstractDataset,
@@ -803,9 +803,9 @@ function unsafe_executesql(
803803
)::FeatureLayer
804804
return FeatureLayer(
805805
GDAL.gdaldatasetexecutesql(
806-
dataset.ptr,
806+
dataset,
807807
query,
808-
spatialfilter.ptr,
808+
spatialfilter,
809809
dialect,
810810
),
811811
)
@@ -828,7 +828,7 @@ function releaseresultset(
828828
dataset::AbstractDataset,
829829
layer::FeatureLayer,
830830
)::Nothing
831-
GDAL.gdaldatasetreleaseresultset(dataset.ptr, layer.ptr)
831+
GDAL.gdaldatasetreleaseresultset(dataset, layer)
832832
destroy(layer)
833833
return nothing
834834
end
@@ -840,10 +840,10 @@ end
840840
Fetch a band object for a dataset from its index.
841841
"""
842842
getband(dataset::AbstractDataset, i::Integer)::IRasterBand =
843-
IRasterBand(GDAL.gdalgetrasterband(dataset.ptr, i), ownedby = dataset)
843+
IRasterBand(GDAL.gdalgetrasterband(dataset, i), ownedby = dataset)
844844

845845
unsafe_getband(dataset::AbstractDataset, i::Integer)::RasterBand =
846-
RasterBand(GDAL.gdalgetrasterband(dataset.ptr, i))
846+
RasterBand(GDAL.gdalgetrasterband(dataset, i))
847847

848848
"""
849849
getgeotransform!(dataset::AbstractDataset, transform::Vector{Cdouble})
@@ -877,7 +877,7 @@ function getgeotransform!(
877877
transform::Vector{Cdouble},
878878
)::Vector{Cdouble}
879879
@assert length(transform) == 6
880-
result = GDAL.gdalgetgeotransform(dataset.ptr, pointer(transform))
880+
result = GDAL.gdalgetgeotransform(dataset, pointer(transform))
881881
if result != GDAL.CE_None
882882
# The default geotransform.
883883
transform .= (0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
@@ -898,7 +898,7 @@ function setgeotransform!(
898898
transform::Vector{Cdouble},
899899
)::T where {T<:AbstractDataset}
900900
@assert length(transform) == 6
901-
result = GDAL.gdalsetgeotransform(dataset.ptr, pointer(transform))
901+
result = GDAL.gdalsetgeotransform(dataset, pointer(transform))
902902
@cplerr result "Failed to transform raster dataset"
903903
return dataset
904904
end
@@ -908,7 +908,7 @@ end
908908
909909
Get number of GCPs for this dataset. Zero if there are none.
910910
"""
911-
ngcp(dataset::AbstractDataset)::Integer = GDAL.gdalgetgcpcount(dataset.ptr)
911+
ngcp(dataset::AbstractDataset)::Integer = GDAL.gdalgetgcpcount(dataset)
912912

913913
"""
914914
getproj(dataset::AbstractDataset)
@@ -920,7 +920,7 @@ projection definition is not available an empty (but not `NULL`) string is
920920
returned.
921921
"""
922922
getproj(dataset::AbstractDataset)::String =
923-
GDAL.gdalgetprojectionref(dataset.ptr)
923+
GDAL.gdalgetprojectionref(dataset)
924924

925925
"""
926926
setproj!(dataset::AbstractDataset, projstring::AbstractString)
@@ -931,7 +931,7 @@ function setproj!(
931931
dataset::T,
932932
projstring::AbstractString,
933933
)::T where {T<:AbstractDataset}
934-
result = GDAL.gdalsetprojection(dataset.ptr, projstring)
934+
result = GDAL.gdalsetprojection(dataset, projstring)
935935
@cplerr result "Could not set projection"
936936
return dataset
937937
end
@@ -965,7 +965,7 @@ function buildoverviews!(
965965
progressdata = C_NULL,
966966
)::T where {T<:AbstractDataset}
967967
result = GDAL.gdalbuildoverviews(
968-
dataset.ptr,
968+
dataset,
969969
resampling,
970970
length(overviewlist),
971971
overviewlist,
@@ -979,7 +979,7 @@ function buildoverviews!(
979979
end
980980

981981
function destroy(dataset::AbstractDataset)::Nothing
982-
GDAL.gdalclose(dataset.ptr)
982+
GDAL.gdalclose(dataset)
983983
dataset.ptr = C_NULL
984984
return nothing
985985
end

0 commit comments

Comments
 (0)