Skip to content

Commit 0a74f33

Browse files
committed
Resolves felt#195
1 parent 3362867 commit 0a74f33

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

lib/geo/json/decoder.ex

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ defmodule Geo.JSON.Decoder do
1313
MultiLineStringZ,
1414
MultiPolygon,
1515
MultiPolygonZ,
16-
GeometryCollection
16+
GeometryCollection,
17+
Unlocated
1718
}
1819

1920
defmodule DecodeError do
@@ -194,7 +195,9 @@ defmodule Geo.JSON.Decoder do
194195
%MultiPolygonZ{coordinates: coordinates, srid: get_srid(crs), properties: properties}
195196
end
196197

197-
defp do_decode("Feature", nil, _properties, _id), do: nil
198+
defp do_decode("Feature", nil, properties, _id) do
199+
%Unlocated{properties: properties}
200+
end
198201

199202
defp do_decode("Feature", geometry, properties, _id) do
200203
do_decode(Map.get(geometry, "type"), Map.get(geometry, "coordinates"), properties, nil)

lib/geo/json/encoder.ex

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ defmodule Geo.JSON.Encoder do
1515
MultiLineStringZ,
1616
MultiPolygon,
1717
MultiPolygonZ,
18-
GeometryCollection
18+
GeometryCollection,
19+
Unlocated
1920
}
2021

2122
defmodule EncodeError do
@@ -149,7 +150,7 @@ defmodule Geo.JSON.Encoder do
149150

150151
defp do_encode(%LineStringZM{coordinates: coordinates}) do
151152
# GeoJSON does not allow "m", and does not recognize "LineStringZM" as
152-
# a type
153+
# a type
153154
coordinates = Enum.map(coordinates, fn {x, y, z, _m} -> [x, y, z] end)
154155
%{"type" => "LineString", "coordinates" => coordinates}
155156
end
@@ -224,6 +225,10 @@ defmodule Geo.JSON.Encoder do
224225
%{"type" => "MultiPolygon", "coordinates" => coordinates}
225226
end
226227

228+
defp do_encode(%Unlocated{} = _geom) do
229+
%{"type" => "Feature", "geometry" => nil}
230+
end
231+
227232
defp do_encode(data) do
228233
raise EncodeError, message: "Unable to encode given value: #{inspect(data)}"
229234
end

lib/geo/unlocated.ex

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule Geo.Unlocated do
2+
@moduledoc """
3+
Defines the Unlocated struct.
4+
"""
5+
6+
@type t :: %Geo.Unlocated{srid: integer | nil, properties: map}
7+
defstruct srid: nil, properties: %{}
8+
end

test/geo/json_test.exs

+14-2
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,17 @@ defmodule Geo.JSON.Test do
363363
"""
364364

365365
geom = Jason.decode!(json) |> Geo.JSON.decode!()
366-
assert is_nil(geom)
366+
%Geo.Unlocated{} = geom
367+
assert geom.properties["context"] == "80, Somme, Picardie"
368+
end
369+
370+
test "Encode unlocted feature" do
371+
geom = %Geo.Unlocated{properties: %{hi: "there"}}
372+
json = Geo.JSON.encode!(geom) |> Jason.encode!()
373+
374+
assert(
375+
json == "{\"geometry\":null,\"properties\":{\"hi\":\"there\"},\"type\":\"Feature\"}"
376+
)
367377
end
368378

369379
test "Decode feature in a feature collection with null geometry" do
@@ -384,7 +394,9 @@ defmodule Geo.JSON.Test do
384394
"""
385395

386396
geom = Jason.decode!(json) |> Geo.JSON.decode!()
387-
assert geom.geometries == []
397+
[geom] = geom.geometries
398+
%Geo.Unlocated{} = geom
399+
assert geom.properties["context"] == "80, Somme, Picardie"
388400
end
389401

390402
property "encodes and decodes back to the correct Point struct" do

0 commit comments

Comments
 (0)