Skip to content

Commit 7043a59

Browse files
committed
Check on iteration.
1 parent c9b7246 commit 7043a59

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

src/dataset.jl

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ struct Dataset{Version}
33
filehandle::Ptr{Cvoid}
44
header::LazHeader # this enables iterating without unsafe_load everytime
55
point::Ptr{RawPoint}
6+
am::CoordinateTransformations.AffineMap
67
end
78

89
function Base.show(io::IO, ds::Dataset{Version}) where {Version}
@@ -36,20 +37,23 @@ function open(f::AbstractString)
3637
point_ptr = Ref{Ptr{RawPoint}}()
3738
@check laszip_reader[] laszip_get_point_pointer(laszip_reader[], point_ptr)
3839

40+
# Get coordinate transformation
41+
am = CoordinateTransformations.AffineMap(Diagonal(SA_F64[header.x_scale_factor 0 0; 0 header.y_scale_factor 0; 0 0 header.z_scale_factor]), SA_F64[header.x_offset, header.y_offset, header.z_offset])
42+
3943
header.point_data_format > 3 && @warn "The LAS 1.4+ format is not fully supported yet."
40-
Dataset{header.point_data_format}(f, laszip_reader[], header, point_ptr[])
44+
Dataset{header.point_data_format}(f, laszip_reader[], header, point_ptr[], am)
4145
end
4246

4347
function CoordinateTransformations.AffineMap(ds::Dataset)
44-
CoordinateTransformations.AffineMap(Diagonal(SA_F64[ds.header.x_scale_factor 0 0; 0 ds.header.y_scale_factor 0; 0 0 ds.header.z_scale_factor]), SA_F64[ds.header.x_offset, ds.header.y_offset, ds.header.z_offset])
48+
ds.am
4549
end
4650

4751
"""Iteration of LAZ file."""
4852
function Base.iterate(ds::Dataset, state::Int)
4953
if state >= length(ds)
5054
return nothing
5155
else
52-
laszip_read_point(ds.filehandle)
56+
@check ds.filehandle laszip_read_point(ds.filehandle)
5357
return eltype(ds)(unsafe_load(ds.point), CoordinateTransformations.AffineMap(ds)), state + 1
5458
end
5559
end
@@ -59,25 +63,26 @@ function Base.iterate(ds::Dataset)
5963
if length(ds) == 0
6064
nothing
6165
else
62-
laszip_seek_point(ds.filehandle, 0)
63-
laszip_read_point(ds.filehandle)
66+
@check ds.filehandle laszip_seek_point(ds.filehandle, 0)
67+
@check ds.filehandle laszip_read_point(ds.filehandle)
6468
eltype(ds)(unsafe_load(ds.point), CoordinateTransformations.AffineMap(ds)), 1
6569
end
6670
end
6771

6872
function Base.getindex(ds::Dataset, i::Integer)
6973
(1 <= i <= length(ds)) || throw(BoundsError(ds, i))
70-
laszip_seek_point(ds.filehandle, i - 1)
71-
laszip_read_point(ds.filehandle)
74+
@check ds.filehandle laszip_seek_point(ds.filehandle, i - 1)
75+
@check ds.filehandle laszip_read_point(ds.filehandle)
7276
eltype(ds)(unsafe_load(ds.point), CoordinateTransformations.AffineMap(ds))
7377
end
7478

7579
function Base.getindex(ds::Dataset, i::UnitRange{<:Integer})
7680
out = Vector{eltype(ds)}(undef, length(i))
81+
isempty(i) && return out
7782
(1 <= i[begin] <= length(ds)) && (1 <= i[end] <= length(ds)) || throw(BoundsError(ds, i))
78-
laszip_seek_point(ds.filehandle, i[begin] - 1)
83+
@check ds.filehandle laszip_seek_point(ds.filehandle, i[begin] - 1)
7984
for I in eachindex(out)
80-
laszip_read_point(ds.filehandle)
85+
@check ds.filehandle laszip_read_point(ds.filehandle)
8186
out[I] = eltype(ds)(unsafe_load(ds.point), CoordinateTransformations.AffineMap(ds))
8287
end
8388
out
@@ -87,8 +92,8 @@ function Base.getindex(ds::Dataset, i::StepRange{<:Integer,<:Integer})
8792
out = Vector{eltype(ds)}(undef, length(i))
8893
(1 <= i[begin] <= length(ds)) && (1 <= i[end] <= length(ds)) || throw(BoundsError(ds, i))
8994
for I in eachindex(out)
90-
laszip_seek_point(ds.filehandle, i[I] - 1)
91-
laszip_read_point(ds.filehandle)
95+
@check ds.filehandle laszip_seek_point(ds.filehandle, i[I] - 1)
96+
@check ds.filehandle laszip_read_point(ds.filehandle)
9297
out[I] = eltype(ds)(unsafe_load(ds.point), CoordinateTransformations.AffineMap(ds))
9398
end
9499
out

src/laszip_h.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Base.@kwdef mutable struct LazVLR
1313
record_id::UInt16 = UInt16(0)
1414
record_length_after_header::UInt16 = UInt16(0)
1515
description::NTuple{32,UInt8} = ntuple(i -> UInt8(0x0), 32)
16-
data::Ptr{UInt8} = pointer("")
16+
data::Ptr{UInt8} = C_NULL
1717
end
1818

1919
Base.convert(::Type{LasIO.LasVariableLengthRecord}, vlr::LazVLR) =
@@ -68,10 +68,10 @@ Base.@kwdef mutable struct LazHeader
6868
extended_number_of_points_by_return::NTuple{15,UInt64} = ntuple(i -> UInt64(0), 15)
6969
# extended_number_of_points_by_return::Array{UInt64, 1} = Array{UInt64, 1}(zeros(0, 15))
7070
user_data_in_header_size::UInt32 = UInt32(0)
71-
user_data_in_header::Ptr{UInt8} = pointer("")
72-
vlrs::Ptr{LazVLR} = pointer("")
71+
user_data_in_header::Ptr{UInt8} = C_NULL
72+
vlrs::Ptr{LazVLR} = C_NULL
7373
user_data_after_header_size::UInt32 = UInt32(0)
74-
user_data_after_header::Ptr{UInt8} = pointer("")
74+
user_data_after_header::Ptr{UInt8} = C_NULL
7575
end
7676

7777
function bounds(h::LazHeader)
@@ -126,7 +126,7 @@ Base.@kwdef mutable struct RawPoint
126126
rgb::NTuple{4,UInt16} = ntuple(i -> UInt16(0), 4)
127127
wave_packet::NTuple{29,UInt8} = ntuple(i -> UInt8(0), 29)
128128
num_extra_bytes::Int32 = Int32(0)
129-
extra_bytes::Ptr{UInt8} = pointer("")
129+
extra_bytes::Ptr{UInt8} = C_NULL
130130
end
131131

132132
const classes = (created=0,

test/test_convert.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ using LazIO
5151
rgb=ntuple(i -> UInt16(65535), 4),
5252
wave_packet=ntuple(i -> UInt8(255), 29),
5353
num_extra_bytes=Int32(24244),
54-
extra_bytes=pointer("")
54+
extra_bytes=C_NULL
5555
)
5656

5757
las0 = convert(LazIO.LasPoint0, lazp)

test/testio.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ end
4646
@inferred ds[1:2]
4747
@inferred ds[1:2:5]
4848
@inferred collect(ds)
49+
@time collect(ds)
4950
@test LazIO.boundingbox(ds) == (xmin=1.44e6, ymin=375000.03, zmin=832.1800000000001, xmax=1.44499996e6, ymax=379999.99, zmax=972.6700000000001)
5051
@test first(ds).return_number == 0x00
5152
@test first(ds).number_of_returns == 0x00

0 commit comments

Comments
 (0)