Skip to content

Commit 5401777

Browse files
committed
Much faster reading.
1 parent 7043a59 commit 5401777

File tree

7 files changed

+73
-17
lines changed

7 files changed

+73
-17
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LASzip_jll = "8372b9c3-1e34-5cc3-bfab-1a98e101de11"
1414
LasIO = "570499db-eae3-5eb6-bdd5-a5326f375e68"
1515
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
1616
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
17+
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
1718
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1819
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1920
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
@@ -33,9 +34,10 @@ Extents = "0.1"
3334
FileIO = "1.2"
3435
FixedPointNumbers = "0.5, 0.6, 0.7, 0.8"
3536
GeoInterface = "1"
36-
Makie = "0.20, 0.21"
3737
LasIO = "0.3"
38+
Makie = "0.20, 0.21"
3839
RecipesBase = "1.3.4"
40+
Setfield = "1.1.1"
3941
StaticArrays = "1"
4042
Tables = "1.0"
4143
julia = "1.10"

src/LazIO.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using Libdl
1212
using LinearAlgebra
1313
using StaticArrays
1414
using UUIDs
15+
using Setfield
1516

1617
# Module initialization function
1718
function __init__()

src/dataset.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +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
6+
am::CoordinateTransformations.AffineMap{LinearAlgebra.Diagonal{Float64,StaticArraysCore.SVector{3,Float64}},StaticArraysCore.SVector{3,Float64}}
77
end
88

99
function Base.show(io::IO, ds::Dataset{Version}) where {Version}

src/laszip_h.jl

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,59 @@ function bounds(h::LazHeader)
7878
(min_x=h.min_x, max_x=h.max_x, min_y=h.min_y, max_y=h.max_y, min_z=h.min_z, max_z=h.max_z)
7979
end
8080

81-
Base.@kwdef mutable struct RawPoint
81+
abstract type AbstractRawPoint end
82+
Base.@kwdef struct RawPoint <: AbstractRawPoint
83+
X::Int32 = Int32(0)
84+
Y::Int32 = Int32(0)
85+
Z::Int32 = Int32(0)
86+
intensity::UInt16 = UInt16(0)
87+
88+
# Evil types
89+
# U8 return_number : 3;
90+
# U8 number_of_returns : 3;
91+
# U8 scan_direction_flag : 1;
92+
# U8 edge_of_flight_line : 1; 8
93+
# U8 classification : 5;
94+
# U8 synthetic_flag : 1;
95+
# U8 keypoint_flag : 1;
96+
# U8 withheld_flag : 1; 8
97+
return_number::UInt8 = UInt8(0)
98+
# number_of_returns::UInt8 = UInt8(0)
99+
# scan_direction_flag::UInt8 = UInt8(0)
100+
# edge_of_flight_line::UInt8 = UInt8(0)
101+
classification::UInt8 = UInt8(0)
102+
# synthetic_flag::UInt8 = UInt8(0)
103+
# keypoint_flag::UInt8 = UInt8(0)
104+
# withheld_flag::UInt8 = UInt8(0)
105+
scan_angle_rank::Int8 = Int8(0)
106+
user_data::UInt8 = UInt8(0)
107+
point_source_ID::UInt16 = UInt16(0)
108+
109+
110+
# Another evil type
111+
# I16 extended_scan_angle;
112+
# U8 extended_point_type : 2;
113+
# U8 extended_scanner_channel : 2;
114+
# U8 extended_classification_flags : 4; 8
115+
# U8 extended_classification;
116+
# U8 extended_return_number : 4;
117+
# U8 extended_number_of_returns : 4; 8
118+
extended_scan_angle::Int16 = Int16(0)
119+
extended_point_type::UInt8 = UInt8(0)
120+
# extended_scanner_channel::UInt8 = UInt8(0)
121+
# extended_classification_flags::UInt8 = UInt8(0)
122+
extended_classification::UInt8 = UInt8(0)
123+
extended_return_number::UInt8 = UInt8(0)
124+
# extended_number_of_returns::UInt8 = UInt8(0)
125+
dummy::NTuple{7,UInt8} = ntuple(i -> UInt8(0), 7)
126+
gps_time::Float64 = Float64(0.0)
127+
rgb::NTuple{4,UInt16} = ntuple(i -> UInt16(0), 4)
128+
wave_packet::NTuple{29,UInt8} = ntuple(i -> UInt8(0), 29)
129+
num_extra_bytes::Int32 = Int32(0)
130+
extra_bytes::Ptr{UInt8} = C_NULL
131+
end
132+
133+
Base.@kwdef mutable struct MutableRawPoint <: AbstractRawPoint
82134
X::Int32 = Int32(0)
83135
Y::Int32 = Int32(0)
84136
Z::Int32 = Int32(0)
@@ -162,14 +214,14 @@ const classes_extended = (created=0,
162214
noise_high=18,)
163215
const user_defined_class_extended = 64
164216

165-
LasIO.return_number(p::LazIO.RawPoint) = (p.return_number & 0b00000111)
166-
LasIO.number_of_returns(p::LazIO.RawPoint) = (p.return_number & 0b00111000) >> 3
167-
LasIO.scan_direction(p::LazIO.RawPoint) = Bool((p.return_number & 0b01000000) >> 6)
168-
LasIO.edge_of_flight_line(p::LazIO.RawPoint) = Bool((p.return_number & 0b10000000) >> 7)
217+
LasIO.return_number(p::LazIO.AbstractRawPoint) = (p.return_number & 0b00000111)
218+
LasIO.number_of_returns(p::LazIO.AbstractRawPoint) = (p.return_number & 0b00111000) >> 3
219+
LasIO.scan_direction(p::LazIO.AbstractRawPoint) = Bool((p.return_number & 0b01000000) >> 6)
220+
LasIO.edge_of_flight_line(p::LazIO.AbstractRawPoint) = Bool((p.return_number & 0b10000000) >> 7)
169221

170-
LasIO.classification(p::LazIO.RawPoint) = (p.classification & 0b00011111)
171-
LasIO.synthetic(p::LazIO.RawPoint) = Bool((p.classification & 0b00100000) >> 5)
172-
LasIO.key_point(p::LazIO.RawPoint) = Bool((p.classification & 0b01000000) >> 6)
173-
LasIO.withheld(p::LazIO.RawPoint) = Bool((p.classification & 0b10000000) >> 7)
222+
LasIO.classification(p::LazIO.AbstractRawPoint) = (p.classification & 0b00011111)
223+
LasIO.synthetic(p::LazIO.AbstractRawPoint) = Bool((p.classification & 0b00100000) >> 5)
224+
LasIO.key_point(p::LazIO.AbstractRawPoint) = Bool((p.classification & 0b01000000) >> 6)
225+
LasIO.withheld(p::LazIO.AbstractRawPoint) = Bool((p.classification & 0b10000000) >> 7)
174226

175-
Dates.DateTime(p::LazIO.RawPoint) = Dates.unix2datetime(p.gps_time) + gps_epoch
227+
Dates.DateTime(p::LazIO.AbstractRawPoint) = Dates.unix2datetime(p.gps_time) + gps_epoch

src/table.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function determine_offset(min_value, max_value, scale; threshold=10^7)
2020
end
2121

2222
"""Correctly set fields that require conversion or packing."""
23-
function Base.setproperty!(p::RawPoint, name, value, header)
23+
function Base.setproperty!(p::MutableRawPoint, name, value, header)
2424
if name == :x && typeof(value) != Int32
2525
p.X = round(Int32, (value - header.x_offset) / header.x_scale_factor)
2626
elseif name == :y && typeof(value) != Int32
@@ -74,7 +74,7 @@ function write(fn::AbstractString, table, bbox; scalex=0.01, scaley=0.01, scalez
7474
header.y_scale_factor = scaley
7575
header.z_scale_factor = scalez
7676

77-
p = RawPoint()
77+
p = MutableRawPoint()
7878

7979
LazIO.write(fn, header) do io
8080
for row in rows

src/write.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ function write(f::Function, path::AbstractString, header::LazIO.LazHeader)
3333
end
3434
end
3535

36-
function writepoint(writer::Ptr{Cvoid}, p::LazIO.RawPoint)
36+
function writepoint(writer::Ptr{Cvoid}, p::LazIO.AbstractRawPoint)
3737
LazIO.@check writer LazIO.laszip_set_point(writer, Ref(p))
3838
LazIO.@check writer LazIO.laszip_write_point(writer)
3939
LazIO.@check writer LazIO.laszip_update_inventory(writer)
4040
end
4141

4242
function writepoint(writer::Ptr{Cvoid}, p::LazIO.Point, header)
43-
rp = LazIO.RawPoint()
43+
rp = LazIO.MutableRawPoint()
4444
for key in fieldnames(typeof(p))
4545
setproperty!(rp, key, getfield(p, key), header)
4646
end

test/testio.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using LasIO
55
using LazIO
66
using Tables
77
using Test
8+
using Setfield
89

910
workdir = @__DIR__
1011
lasio_testdir = joinpath(dirname(pathof(LasIO)), "..", "test")
@@ -124,7 +125,7 @@ end
124125

125126
# modify point
126127
p = unsafe_load(point_ptr[])
127-
p.classification = LasIO.raw_classification(
128+
p = @set p.classification = LasIO.raw_classification(
128129
0x05,
129130
Bool((p.classification & 0b00100000) >> 5),
130131
Bool((p.classification & 0b01000000) >> 6),

0 commit comments

Comments
 (0)