Closed iwpnd closed 2 years ago
I doubled checked the docs and noticed Children()
that I could iterate over.
A FeatureCollection
is supposed to inherit Children()
from a Collection
, but doesn't so I'm stuck again. :/
Hi Ben,
A FeatureCollection should inherit Collection, and it does include a Children and ForEach functions. Both should be effectively the same thing, except ForEach is exposed to all Object types, while Children really only indented for FeatureCollection and GeometryCollection types.
That said, I just tested this code to see what you are talking about.
https://play.golang.org/p/UW-w4T3nWvJ
package main
import (
"fmt"
"github.com/tidwall/geojson"
)
func main() {
json := `{"type":"FeatureCollection","features":[
{"type":"Feature","id":"A","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}},
{"type":"Feature","id":"B","geometry":{"type":"Point","coordinates":[3,4]},"properties":{}},
{"type":"Feature","id":"C","geometry":{"type":"Point","coordinates":[5,6]},"properties":{}},
{"type":"Feature","id":"D","geometry":{"type":"Point","coordinates":[7,8]},"properties":{}}
]}`
f, err := geojson.Parse(json, nil)
if err != nil {
panic(err)
}
objsA := f.(*geojson.FeatureCollection).Children()
var objsB []geojson.Object
f.ForEach(func(geom geojson.Object) bool {
objsB = append(objsB, geom)
return true
})
for i := 0; i < len(objsA) && i < len(objsB); i++ {
fmt.Printf("%d: %s\n%d: %s\n", i, objsA[i], i, objsB[i])
}
}
Output
0: {"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"id":"A","properties":{}}
0: {"type":"Point","coordinates":[1,2]}
1: {"type":"Feature","geometry":{"type":"Point","coordinates":[3,4]},"id":"B","properties":{}}
1: {"type":"Point","coordinates":[3,4]}
2: {"type":"Feature","geometry":{"type":"Point","coordinates":[5,6]},"id":"C","properties":{}}
2: {"type":"Point","coordinates":[5,6]}
3: {"type":"Feature","geometry":{"type":"Point","coordinates":[7,8]},"id":"D","properties":{}}
3: {"type":"Point","coordinates":[7,8]}
This is clearly wrong. It should output the entire Feature. 🤦♂️
Found the issue. Just pushed a fix, see v1.3.1
Awesome, thank you! 🙏
You're welcome and thanks for reporting. :)
Hi :wave:
I want to load a geojson with a feature collection and iterate over the features with the
ForEach
message that theFeatureCollection
provides.If I run this I would expect to iterate over the an array of
Feature
and notPolygon
.Is this intentional and I have to rethink my approach?