/
geometry.go
105 lines (72 loc) · 2.02 KB
/
geometry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package galleries
import (
"context"
"fmt"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/planar"
"github.com/whosonfirst/go-reader"
wof_reader "github.com/whosonfirst/go-whosonfirst-reader"
)
// Derive a MultiPoint geoemtry for one or more gallery IDs.
func GeometryForGalleryIDs(ctx context.Context, r reader.Reader, gallery_ids ...int64) (orb.Geometry, error) {
count_galleries := len(gallery_ids)
switch count_galleries {
case 0:
return nil, fmt.Errorf("Please Null Terminal me")
case 1:
points, err := multipoints(ctx, r, gallery_ids[0])
if err != nil {
return nil, err
}
return orb.MultiPoint(points), nil
default:
points := make([]orb.Point, 0)
for _, wofid := range gallery_ids {
mp, err := multipoints(ctx, r, wofid)
if err != nil {
return nil, err
}
for _, p := range mp {
points = append(points, p)
}
}
return orb.MultiPoint(points), nil
}
}
// Derive a MultiPoint geometry for a Who's On First (gallery) ID.
// In the future we expect that all galleries will be defined as MultiPolygons but
// today they are not.
func multipoints(ctx context.Context, r reader.Reader, wofid int64) (orb.MultiPoint, error) {
body, err := wof_reader.LoadBytes(ctx, r, wofid)
if err != nil {
return nil, err
}
f, err := geojson.UnmarshalFeature(body)
if err != nil {
return nil, err
}
geom := f.Geometry
switch geom.GeoJSONType() {
case "Point":
points := []orb.Point{
geom.(orb.Point),
}
return orb.MultiPoint(points), nil
case "MultiPoint":
return geom.(orb.MultiPoint), nil
case "MultiPolygon":
points := make([]orb.Point, 0)
for _, poly := range geom.(orb.MultiPolygon) {
pt, _ := planar.CentroidArea(poly)
points = append(points, pt)
}
return orb.MultiPoint(points), nil
case "Polygon":
pt, _ := planar.CentroidArea(geom)
points := []orb.Point{pt}
return orb.MultiPoint(points), nil
default:
return nil, fmt.Errorf("Weirdo geometry type for gallery %d, %s", wofid, geom.GeoJSONType())
}
}