Skip to content

Commit

Permalink
Accept kwargs in table interface that are passed to the Header constr…
Browse files Browse the repository at this point in the history
…uctor. (#31)
  • Loading branch information
evetion authored Feb 26, 2021
1 parent fdf01b0 commit 2474255
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LazIO"
uuid = "c3605908-9f0f-11e8-0a72-0d361c15a277"
authors = ["Maarten Pronk <git@evetion.nl>"]
version = "0.3.1"
version = "0.3.2"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
9 changes: 4 additions & 5 deletions src/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ const column_names = (fieldnames(LazPoint)..., :number_of_returns)
function determine_offset(min_value, max_value, scale; threshold=10^7)
s = round(Int64, ((min_value + max_value) / 2) / scale / threshold)
s *= threshold * scale

# Try to convert back and forth and check overflow
(muladd(round(Int32, (min_value - s) / scale), scale, s) > 0) == (min_value > 0) || error("Can't fit offset in this scale, try to coarsen it.")
(muladd(round(Int32, (max_value - s) / scale), scale, s) > 0) == (max_value > 0) || error("Can't fit offset in this scale, try to coarsen it.")
(muladd(round(Int32, (min_value - s) / scale), scale, s) > 0) == (min_value > 0) || error("Can't fit offset for min with this scale, try to coarsen it.")
(muladd(round(Int32, (max_value - s) / scale), scale, s) > 0) == (max_value > 0) || error("Can't fit offset for max with this scale, try to coarsen it.")
s
end

Expand All @@ -42,15 +41,15 @@ function Base.setproperty!(p::LazPoint, name, value, header)
end
end

function write(fn::AbstractString, table, bbox; scale=0.01)
function write(fn::AbstractString, table, bbox; scale=0.01, kwargs...)

schema = Tables.schema(table)
isnothing(schema) && error("A Schema is required")
all(name in column_names for name in schema.names) || error("Can't map all columns to LazPoint")

rows = Tables.rows(table)

header = LazHeader()
header = LazHeader(;kwargs...)
header.x_offset = determine_offset(bbox.min_x, bbox.max_x, scale)
header.y_offset = determine_offset(bbox.min_y, bbox.max_y, scale)
header.z_offset = determine_offset(bbox.min_z, bbox.max_z, scale)
Expand Down
24 changes: 24 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,27 @@ end
macro check(obj, ex)
return :( $(esc(ex)) == 0 ? nothing : laszip_error($(esc(obj))) )
end


function readstring(s::NTuple{T,UInt8}) where T
lastchar = findlast(s .!= 0)
if isnothing(lastchar)
return ""
else
return join(Char.(s[1:lastchar]))
end
end

function writestring(str::AbstractString, nb::Integer)
s = ascii(str)
n = length(s)
npad = nb - n
if npad < 0
error("string too long")
elseif npad == 0
NTuple{nb,UInt8}(codeunits(ascii(s)))
else
writestr = string(s * "\0"^npad)
NTuple{nb,UInt8}(codeunits(ascii(writestr)))
end
end
3 changes: 2 additions & 1 deletion test/testio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ end
return_number = [1,2,1],
number_of_returns = [1,2,3])
bounds = (min_x = 11000.01, max_x = 32000., min_y = 11000., max_y = 32000., min_z = 11000., max_z = 32000.01)
LazIO.write(manual_fn, table, bounds, scale=0.01)
LazIO.write(manual_fn, table, bounds, scale=0.01, system_identifier=LazIO.writestring("Laser shooter, pew pew!", 32))

ds = LazIO.open(manual_fn)

@test LazIO.readstring(ds.header.system_identifier) == "Laser shooter, pew pew!"
@test length(ds) == 3
@test first(ds).X == 1100001
@test muladd(first(ds).X, ds.header.x_scale_factor, ds.header.x_offset) 11000.01
Expand Down

2 comments on commit 2474255

@evetion
Copy link
Owner 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/30866

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.2 -m "<description of version>" 2474255bfeda7da4232174d6e4bfd79202938f7d
git push origin v0.3.2

Please sign in to comment.