# How to create a valid \`FeatureCollection\` I can interface with \`GeoJSON\`?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197>\
**Category:** Geo\
**Created:** [December 3, 2022, 11:25pm UTC](https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197 "2022-12-03T23:25:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctrebbau](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@ctrebbau](https://discourse.julialang.org/u/ctrebbau)\
**Post date:** [December 3, 2022, 11:25pm UTC](https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197/1 "2022-12-03T23:25:10Z")

</div>

```julia
using GeoJSON

EPS = 5
MAD_BOUNDS = (40.2257307, 40.6072783, -3.9536568, -3.4525022)
grid = Dict("Lat" => range(MAD_BOUNDS[1], MAD_BOUNDS[2], EPS),
            "Lng" => range(MAD_BOUNDS[3], MAD_BOUNDS[4], EPS),
            "Eps" => EPS)

fc = Dict("type" => "FeatureCollection",
          "features" => [],
          "properties" => Dict{String, Any}())

tile_features = [Dict("type" => "Feature",
                      "properties" => Dict{String, Any}(),
                      "geometry" => Dict("type" => "Polygon",
                                         "coordinates" => [
                                            [
                                                [grid["Lng"][i], grid["Lat"][j]],
                                                [grid["Lng"][i+1], grid["Lat"][j+1]],
                                            ]
                                         ])
                      )
                 for i in 1:grid["Eps"]-1, j in 1:grid["Eps"]-1]

fc["features"] = tile_features

GeoJSON.parseFeature(fc["features"][1]) # works
geo_data = GeoJSON.parseFeatureCollection(fc)

```

Last line throws a `MethodError` on `FeatureCollection(::Matrix{Feature})`  
I think it’s getting hung up on type of the `coordinates`  
Any clues?

---

<div class="post-metadata">

**Author:** ![ctrebbau](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@ctrebbau](https://discourse.julialang.org/u/ctrebbau)\
**Post date:** [December 4, 2022, 12:02pm UTC](https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197/2 "2022-12-04T12:02:12Z")

</div>

Well, it turns out I had a couple of mistakes. First and most importantly I wasn’t really defining a closed form polygon (make sure to follow the right-hand rule, most usually)

```julia

tile_features = [Dict("type" => "Feature",
                      "properties" => Dict{String, Any}(),
                      "geometry" => Dict("type" => "Polygon",
                                         "coordinates" => [
                                            [
                                                [grid["Lng"][i], grid["Lat"][j]],
                                                [grid["Lng"][i], grid["Lat"][j+1]],
                                                [grid["Lng"][i+1], grid["Lat"][j+1]],
                                                [grid["Lng"][i+1], grid["Lat"][j]],
                                                [grid["Lng"][i], grid["Lat"][j]]
                                            ]
                                         ])
                      )
                 for i in 1:grid["Eps"]-1, j in 1:grid["Eps"]-1]

```

And then I was able to turn it into a `FeatureCollection` object more properly by first

```julia
open("mad_grid.geojson", "w") do io
    JSON3.pretty(io, fc)
end

```

And reading it back on memory

```julia
jsonbytes = read("mad_grid.geojson")
fc = GeoJSON.read(jsonbytes)

```

Seems like a roundabout way of doing things, so won’t mark as a solution just in case someone has a more direct way of achieving this 😄

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [December 4, 2022, 12:31pm UTC](https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197/3 "2022-12-04T12:31:03Z")

</div>

What version of GeoJSON.jl are you using?

---

<div class="post-metadata">

**Author:** ![ctrebbau](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@ctrebbau](https://discourse.julialang.org/u/ctrebbau)\
**Post date:** [December 4, 2022, 1:06pm UTC](https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197/4 "2022-12-04T13:06:31Z")

</div>

Hi Raf, I’m using v0.5.1

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [December 4, 2022, 2:00pm UTC](https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197/5 "2022-12-04T14:00:05Z")

</div>

Ok. Much of this syntax has changed with v0.6, with GeoInterface.jl v1.0 and using JSON3.jl underneath rather than JSON.jl.

---

<div class="post-metadata">

**Author:** ![ctrebbau](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@ctrebbau](https://discourse.julialang.org/u/ctrebbau)\
**Post date:** [December 4, 2022, 2:14pm UTC](https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197/6 "2022-12-04T14:14:16Z")

</div>

Seems like `Turf.jl` is restricting to me to v0.5.1  
Everything is well for now, though. Will give a shout out if not 😄

---

<div class="post-metadata">

**Author:** ![Raf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raf/32/3383_2.png) [@Raf](https://discourse.julialang.org/u/Raf)\
**Post date:** [December 4, 2022, 5:29pm UTC](https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197/7 "2022-12-04T17:29:20Z")

</div>

Ok seems you might be staying there! turf is 2 years without change.

It would be good to get Turf.jl moved to JuilaGeo so it gets updated with these changes, I didn’t even know it existed.

---

<div class="post-metadata">

**Author:** ![ctrebbau](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@ctrebbau](https://discourse.julialang.org/u/ctrebbau)\
**Post date:** [December 4, 2022, 8:11pm UTC](https://discourse.julialang.org/t/how-to-create-a-valid-featurecollection-i-can-interface-with-geojson/91197/8 "2022-12-04T20:11:00Z")

</div>

Oh I’m not married to it, but given that I don’t really know the ecosystem I chose it because it’s 100% Julia version a JS library by the same name (might as kill two birds with one stone type of thing… if I ever need the JS version 😄 ), its listed here: [https://juliageo.org/](https://juliageo.org/), plus I thought it might be well tested.

I just need to do some spatial queries, like getting the area of the tiles above etc… I expect my FeatureCollection to get big so the above is not very efficient. What do you suggest?
