Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves felt/geo#193 #194

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/geo/json/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,19 @@ defmodule Geo.JSON.Decoder do
defp do_decode("Feature", nil, _properties, _id), do: nil

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

Map.get(geometry, "type") == "GeometryCollection" ->
geometry_collection = decode!(geometry)

%GeometryCollection{
geometries: geometry_collection.geometries,
properties: properties
}

true ->
do_decode(Map.get(geometry, "type"), Map.get(geometry, "coordinates"), properties, nil)
s3cur3 marked this conversation as resolved.
Show resolved Hide resolved
end
end

defp do_decode(type, [x, y, _z], properties, crs) do
Expand Down
53 changes: 53 additions & 0 deletions test/geo/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,57 @@ defmodule Geo.JSON.Test do

assert(Geo.JSON.encode!(gc, feature: true) == expected)
end

test "Decode Feature with GeometryCollection geometry" do
# Similar to response from https://api.weather.gov/zones/county/FLC017
json = """
{
"@context": {
"@version": "1.1"
},
"id": "https://api.weather.gov/zones/county/FLC017",
"type": "Feature",
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "MultiPolygon",
"coordinates": [[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]]
},
{
"type": "MultiPolygon",
"coordinates": [[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]]
}
]
},
"properties": {
"@id": "https://api.weather.gov/zones/county/FLC017",
"@type": "wx:Zone",
"id": "FLC017",
"type": "county",
"name": "Citrus",
"effectiveDate": "2023-09-19T18:00:00+00:00",
"expirationDate": "2200-01-01T00:00:00+00:00",
"state": "FL",
"cwa": [
"TBW"
],
"forecastOffices": [
"https://api.weather.gov/offices/TBW"
],
"timeZone": [
"America/New_York"
],
"observationStations": [],
"radarStation": null
}
}
"""

geom = Jason.decode!(json) |> Geo.JSON.decode!()

assert(Enum.count(geom.geometries) == 2)
assert(Enum.each(geom.geometries, fn (%Geo.MultiPolygon{} = g) -> true end))
assert geom.properties["id"] == "FLC017"
s3cur3 marked this conversation as resolved.
Show resolved Hide resolved
end
end
Loading