Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to raw bytes for vlr types #45

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LASDatasets"
uuid = "cc498e2a-d443-4943-8f26-2a8a0f3c7cdb"
authors = ["BenCurran98 <b.curran@fugro.com>"]
version = "0.3.0"
version = "0.3.1"

[deps]
BufferedStreams = "e1450e63-4bb3-523b-b2a4-4ffa8c0fd77d"
Expand All @@ -21,7 +21,7 @@ TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"

[compat]
BufferedStreams = "1"
ColorTypes = "0.11"
ColorTypes = "0.11, 0.12"
DocStringExtensions = "0.9"
FileIO = "1"
FixedPointNumbers = "0.8"
Expand Down
5 changes: 4 additions & 1 deletion src/vlrs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ official_record_ids(::Type{TData}) where TData = error("Official record IDs not

Get the data type associated with a particular `user_id` and `record_id`.
This is used to automatically parse VLR data types on reading
**NOTE**: If the user and record ID combination hasn't been registered, will default to `Vector{UInt8}` and the *VLR* data will be returned as raw bytes.
"""
function data_type_from_ids(user_id::String, record_id::Integer)
registered_vlr_types = get_all_vlr_types()
Expand All @@ -139,7 +140,9 @@ function data_type_from_ids(user_id::String, record_id::Integer)
end
end

error("Can't find VLR data type to parse for user ID $(user_id) and record ID $(record_id)")
# if we can't find a registered type, just read it out as a Byte Vector
@warn "Can't find VLR data type to parse for user ID $(user_id) and record ID $(record_id) - reading as raw bytes"
return Vector{UInt8}
end

function get_all_vlr_types()
Expand Down
45 changes: 45 additions & 0 deletions test/vlrs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,50 @@
write(io, vlr)
seek(io, 0)
@test read(io, LasVariableLengthRecord) == vlr

# if we haven't registered a VLR type for a struct, we should only read raw bytes back out
struct Thing
a::Int

b::Float64
end

Base.:(==)(t1::Thing, t2::Thing) = (t1.a == t2.a) && (t1.b == t2.b)

function Base.write(io::IO, thing::Thing)
write(io, thing.a)
write(io, thing.b)
end

function Base.read(io::IO, ::Type{Thing})
a = read(io, Int)
b = read(io, Float64)
return Thing(a, b)
end

thing = Thing(1, 2.0)
# get the bytes for this thing
io = IOBuffer()
write(io, thing)
seek(io, 0)
bytes = read(io)

# create a VLR - should be able to write it but can't parse it back to a struct if we haven't registered a type
vlr = LasVariableLengthRecord("Thing", 0, "Can't parse!", thing)

# make sure we can save and load
io = IOBuffer()
write(io, vlr)
seek(io, 0)
out_vlr = read(io, LasVariableLengthRecord)
@test get_data(out_vlr) == bytes

# if we register a type it should be fine
@register_vlr_type Thing "Thing" 0

io = IOBuffer()
write(io, vlr)
seek(io, 0)
@test read(io, LasVariableLengthRecord) == vlr
end
end
Loading