Skip to content

Commit

Permalink
Add support for Float32 coordinates (#122)
Browse files Browse the repository at this point in the history
* Add support for Float32 coordinates

* Update tests
  • Loading branch information
eliascarv authored Oct 3, 2024
1 parent 75f9c43 commit b75c1b9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function write(path::AbstractString, o::Writer; force=false)
# Detect the shape type code from the first available geometry
# There can only be one shape type in the file.
# If all values are missing, the shape type is Missing
if iterate(skipmissing(geoms)) == nothing
if isnothing(iterate(skipmissing(geoms)))
trait = Missing
hasz = hasm = false
else
Expand Down Expand Up @@ -295,7 +295,7 @@ function _write(io::IO, trait::GI.AbstractGeometryTrait, geom; kw...)
end
# write x/y part of points
for point in GI.getpoint(geom)
x, y = GI.x(point), GI.y(point)
x, y = Float64(GI.x(point)), Float64(GI.y(point))
bytes += Base.write(io, x)
bytes += Base.write(io, y)
end
Expand Down Expand Up @@ -334,7 +334,7 @@ function _calc_mbr(geom)
low_x = low_y = Inf
high_x = high_y = -Inf
for point in GI.getpoint(geom)
x, y = GI.x(point), GI.y(point)
x, y = Float64(GI.x(point)), Float64(GI.y(point))
low_x = min(low_x, x)
high_x = max(high_x, x)
low_y = min(low_y, y)
Expand All @@ -350,14 +350,14 @@ function _write_others(io, geom;
bytes = 0
# TODO what to do when hasm == false but hasz == true
if hasz
b, zrange = _write_others(GI.z, io, geom)
b, zrange = _write_others(p -> Float64(GI.z(p)), io, geom)
bytes += b
else
zrange = Interval(0.0, 0.0)
end

if hasm
b, mrange = _write_others(GI.m, io, geom)
b, mrange = _write_others(p -> Float64(GI.m(p)), io, geom)
bytes += b
else
mrange = Interval(0.0, 0.0)
Expand Down
33 changes: 33 additions & 0 deletions test/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,39 @@
t = Shapefile.Table(file)
@test only(t.geometry) == Polygon(box, [0], ps)

# Float32 coordinates
struct Point32
x::Float32
y::Float32
end

struct LineString32
points::Vector{Point32}
end

# basic GI interface implementation
GI.isgeometry(::Type{Point32}) = true
GI.geomtrait(::Point32) = GI.PointTrait()
GI.ncoord(::GI.PointTrait, p::Point32) = 2
GI.getcoord(::GI.PointTrait, p::Point32, i) = getfield(p, i)

GI.isgeometry(::Type{LineString32}) = true
GI.geomtrait(::LineString32) = GI.LineStringTrait()
GI.ncoord(::GI.LineStringTrait, p::LineString32) = 2
GI.ngeom(::GI.LineStringTrait, l::LineString32) = length(l.points)
GI.getgeom(::GI.LineStringTrait, l::LineString32, i) = getindex(l.points, i)

file = tempname()
Shapefile.write(file, Point32(0, 0))
t = Shapefile.Table(file)
@test only(t.geometry) == Point(0, 0)

file = tempname()
geom = LineString32([Point32(0, 0), Point32(1, 0), Point32(1, 1), Point32(0, 1)])
Shapefile.write(file, geom)
t = Shapefile.Table(file)
@test only(t.geometry) == Polyline(Rect(0, 0, 1, 1), [0], [Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)])

# feature
struct F end
GI.trait(::F) = GI.FeatureCollectionTrait()
Expand Down

0 comments on commit b75c1b9

Please sign in to comment.