aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/paulmach/orb/geojson/example_pointer_test.go
blob: b27a0fd257cfcc5aa3d3642cb2e8a2e64b47c13d (plain) (blame)
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
}