Skip to content

Commit b154726

Browse files
committed
refactor wkbtype z mask
1 parent bd2a0ed commit b154726

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

src/extra/gpkg/read.jl

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,18 @@ end
6969
# gpkg_contents table.
7070
function gpkgextract(db; layer=1)
7171
# get the first (and only) feature table returned in sqlite query results
72-
metadata = first(
73-
DBInterface.execute(
74-
db,
75-
"""
76-
SELECT g.table_name AS tablename, g.column_name AS geomcolumn,
77-
c.srs_id AS srsid, srs.organization AS org, srs.organization_coordsys_id AS code
78-
FROM gpkg_geometry_columns g, gpkg_spatial_ref_sys srs
79-
JOIN gpkg_contents c ON ( g.table_name = c.table_name )
80-
WHERE c.data_type = 'features'
81-
AND g.srs_id = c.srs_id
82-
LIMIT 1 OFFSET ($layer-1);
83-
"""
84-
)
85-
)
72+
metadata = first(DBInterface.execute(
73+
db,
74+
"""
75+
SELECT g.table_name AS tablename, g.column_name AS geomcolumn,
76+
c.srs_id AS srsid, srs.organization AS org, srs.organization_coordsys_id AS code
77+
FROM gpkg_geometry_columns g, gpkg_spatial_ref_sys srs
78+
JOIN gpkg_contents c ON ( g.table_name = c.table_name )
79+
WHERE c.data_type = 'features'
80+
AND g.srs_id = c.srs_id
81+
LIMIT 1 OFFSET ($layer-1);
82+
"""
83+
))
8684

8785
# According to https://www.geopackage.org/spec/#r33, feature table geometry columns
8886
# SHALL contain geometries with the srs_id specified for the column by the gpkg_geometry_columns table srs_id column value.

src/extra/gpkg/wkb.jl

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@
44

55
function wkb2geom(buff, crs)
66
byteswap = isone(read(buff, UInt8)) ? ltoh : ntoh
7-
typebits = read(buff, UInt32)
7+
wkbtype = read(buff, UInt32)
88
# Input variants of WKB supported are standard, extended, and ISO WKB geometry with Z dimensions (M/ZM not supported)
99
if CoordRefSystems.ncoords(crs) == 3
1010
# SQL/MM Part 3 and SFSQL 1.2 use offsets of 1000 (Z), 2000 (M) and 3000 (ZM)
1111
# to indicate the present of higher dimensional coordinates in a WKB geometry
12-
if typebits > 3007
12+
if wkbtype >= 1001 && wkbtype <= 1007
13+
# the SFSQL 1.2 offset of 1000 (Z) is present and subtracting a round number of 1000 gives the standard WKB type
14+
wkbtype -= 1000
1315
# 99-402 was a short-lived extension to SFSQL 1.1 that used a high-bit flag to indicate the presence of Z coordinates in a WKB geometry
1416
# the high-bit flag 0x80000000 for Z (or 0x40000000 for M) is set and masking it off gives the standard WKB type
15-
wkbtype = typebits & 0x7FFFFFFF
16-
# check that the WKB typebits is not outside the SFSQL 1.2 Z encoding range (1001-1007)
17-
elseif typebits <= 1007
18-
# the SFSQL 1.2 offset of 1000 (Z) is present and subtracting a round number of 1000 gives the standard WKB type
19-
wkbtype = typebits - 1000
17+
elseif wkbtype > 0x80000000
18+
# the SFSQL 1.1 high-bit flag 0x80000000 (Z) is present and removing the flag reveals the standard WKB type
19+
wkbtype -= 0x80000000
2020
else
21-
@error "Unsupported WKB Geometry Type with M or ZM dimension encoding: $typebits"
21+
@error "Unsupported WKB Geometry Type with M or ZM dimension encoding: $wkbtype"
2222
end
23-
else
24-
# standard WKB typebits without Z dimension encoding
25-
wkbtype = typebits
2623
end
2724

2825
if wkbtype <= 3
@@ -86,7 +83,7 @@ function wkb2chain(buff, crs, byteswap)
8683
npoints = byteswap(read(buff, UInt32))
8784
chain = wkb2points(buff, npoints, crs, byteswap)
8885
if length(chain) >= 2 && first(chain) == last(chain)
89-
Ring(chain[1:end-1])
86+
Ring(chain[1:(end - 1)])
9087
elseif length(chain) >= 2
9188
Rope(chain)
9289
else
@@ -98,7 +95,7 @@ end
9895
function wkb2poly(buff, crs, byteswap)
9996
nrings = byteswap(read(buff, UInt32))
10097
rings = map(1:nrings) do _
101-
wkb2chain(buff, crs, byteswap)
98+
wkb2chain(buff, crs, byteswap)
10299
end
103100
PolyArea(rings)
104101
end

0 commit comments

Comments
 (0)