-
Notifications
You must be signed in to change notification settings - Fork 104
/
example_pointer_test.go
48 lines (39 loc) · 1.13 KB
/
example_pointer_test.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
package geojson_test
import (
"fmt"
"log"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/planar"
"github.com/paulmach/orb/quadtree"
)
type CentroidPoint struct {
*geojson.Feature
}
func (cp CentroidPoint) Point() orb.Point {
// this is where you would decide how to define
// the representative point of the feature.
c, _ := planar.CentroidArea(cp.Geometry)
return c
}
func Example_centroid() {
qt := quadtree.New(orb.Bound{Min: orb.Point{0, 0}, Max: orb.Point{1, 1}})
// feature with center {0.5, 0.5} but centroid {0.25, 0.25}
f := geojson.NewFeature(orb.MultiPoint{{0, 0}, {0, 0}, {0, 0}, {1, 1}})
f.Properties["centroid"] = "0.25"
err := qt.Add(CentroidPoint{f})
if err != nil {
log.Fatalf("unexpected error: %v", err)
}
// feature with centroid {0.6, 0.6}
f = geojson.NewFeature(orb.Point{0.6, 0.6})
f.Properties["centroid"] = "0.6"
err = qt.Add(CentroidPoint{f})
if err != nil {
log.Fatalf("unexpected error: %v", err)
}
feature := qt.Find(orb.Point{0.5, 0.5}).(CentroidPoint).Feature
fmt.Printf("centroid=%s", feature.Properties["centroid"])
// Output:
// centroid=0.6
}