Skip to content

Commit

Permalink
Merge pull request #45 from fugro-oss/VLRRegisterDefault
Browse files Browse the repository at this point in the history
Default to raw bytes for vlr types
  • Loading branch information
BenCurran98 authored Oct 28, 2024
2 parents 8ff77dd + c8d17f3 commit e48d3b4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
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

2 comments on commit e48d3b4

@BenCurran98
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/118182

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.1 -m "<description of version>" e48d3b44b48ceb4a8f04f1649b782c3d62833bd4
git push origin v0.3.1

Please sign in to comment.