Skip to content

Commit

Permalink
Resolves #193 (#194)
Browse files Browse the repository at this point in the history
* Resolves #193

* Apply suggestions from code review

* Fix formatting

* One more attempt to fix formatting (sigh)

---------

Co-authored-by: Tyler A. Young <[email protected]>
  • Loading branch information
carstenpiepel and s3cur3 authored Aug 30, 2024
1 parent 9c7847f commit 6dc06aa
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/geo/json/decoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,16 @@ 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)
if geometry["type"] == "GeometryCollection" do
geometry_collection = decode!(geometry)

%GeometryCollection{
geometries: geometry_collection.geometries,
properties: properties
}
else
do_decode(Map.get(geometry, "type"), Map.get(geometry, "coordinates"), properties, nil)
end
end

defp do_decode(type, [x, y, _z], properties, crs) do
Expand Down
54 changes: 54 additions & 0 deletions test/geo/json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,58 @@ 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 %GeometryCollection{} = geom

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 24.3.4)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 23.3.4)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.11.4, 24.3.4)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.11.4, 22.3)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.12.3, 24.3.4)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.14.4, 26.0.2)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 25.3.2)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.14.4, 24.3.4)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.12.3, 22.3)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.14.4, 25.3.2)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.12.3, 23.3.4)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.14.4, 23.3.4)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.13.4, 22.3)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code

Check failure on line 525 in test/geo/json_test.exs

View workflow job for this annotation

GitHub Actions / Elixir Unit Tests (1.11.4, 23.3.4)

** (CompileError) test/geo/json_test.exs:525: GeometryCollection.__struct__/0 is undefined, cannot expand struct GeometryCollection. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
assert length(geom.geometries) == 2
assert Enum.all?(geom.geometries, &match?(%Geo.MultiPolygon{}, &1))
assert geom.properties["id"] == "FLC017"
end
end

0 comments on commit 6dc06aa

Please sign in to comment.