Skip to content

Commit e48d3b4

Browse files
authored
Merge pull request #45 from fugro-oss/VLRRegisterDefault
Default to raw bytes for vlr types
2 parents 8ff77dd + c8d17f3 commit e48d3b4

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LASDatasets"
22
uuid = "cc498e2a-d443-4943-8f26-2a8a0f3c7cdb"
33
authors = ["BenCurran98 <[email protected]>"]
4-
version = "0.3.0"
4+
version = "0.3.1"
55

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

2222
[compat]
2323
BufferedStreams = "1"
24-
ColorTypes = "0.11"
24+
ColorTypes = "0.11, 0.12"
2525
DocStringExtensions = "0.9"
2626
FileIO = "1"
2727
FixedPointNumbers = "0.8"

src/vlrs.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ official_record_ids(::Type{TData}) where TData = error("Official record IDs not
129129
130130
Get the data type associated with a particular `user_id` and `record_id`.
131131
This is used to automatically parse VLR data types on reading
132+
**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.
132133
"""
133134
function data_type_from_ids(user_id::String, record_id::Integer)
134135
registered_vlr_types = get_all_vlr_types()
@@ -139,7 +140,9 @@ function data_type_from_ids(user_id::String, record_id::Integer)
139140
end
140141
end
141142

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

145148
function get_all_vlr_types()

test/vlrs.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,50 @@
136136
write(io, vlr)
137137
seek(io, 0)
138138
@test read(io, LasVariableLengthRecord) == vlr
139+
140+
# if we haven't registered a VLR type for a struct, we should only read raw bytes back out
141+
struct Thing
142+
a::Int
143+
144+
b::Float64
145+
end
146+
147+
Base.:(==)(t1::Thing, t2::Thing) = (t1.a == t2.a) && (t1.b == t2.b)
148+
149+
function Base.write(io::IO, thing::Thing)
150+
write(io, thing.a)
151+
write(io, thing.b)
152+
end
153+
154+
function Base.read(io::IO, ::Type{Thing})
155+
a = read(io, Int)
156+
b = read(io, Float64)
157+
return Thing(a, b)
158+
end
159+
160+
thing = Thing(1, 2.0)
161+
# get the bytes for this thing
162+
io = IOBuffer()
163+
write(io, thing)
164+
seek(io, 0)
165+
bytes = read(io)
166+
167+
# 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
168+
vlr = LasVariableLengthRecord("Thing", 0, "Can't parse!", thing)
169+
170+
# make sure we can save and load
171+
io = IOBuffer()
172+
write(io, vlr)
173+
seek(io, 0)
174+
out_vlr = read(io, LasVariableLengthRecord)
175+
@test get_data(out_vlr) == bytes
176+
177+
# if we register a type it should be fine
178+
@register_vlr_type Thing "Thing" 0
179+
180+
io = IOBuffer()
181+
write(io, vlr)
182+
seek(io, 0)
183+
@test read(io, LasVariableLengthRecord) == vlr
139184
end
140185
end

0 commit comments

Comments
 (0)