Exemplo n.º 1
0
//GetBoundingBox computes the bounding box for a given geometry
func GetBoundingBox(g *geos.Geometry) (geographic.BoundingBox, error) {
	bbox := geographic.BoundingBox{}

	envelope, err := g.Envelope()
	if err != nil {
		return bbox, err
	}

	envelope, err = envelope.Shell()
	if err != nil {
		return bbox, err
	}

	centroid, err := envelope.Centroid()
	if err != nil {
		return bbox, err
	}

	bbox.LatitudeMinDeg, _ = centroid.Y()
	bbox.LongitudeMinDeg, _ = centroid.X()
	bbox.LatitudeMaxDeg = bbox.LatitudeMinDeg
	bbox.LongitudeMaxDeg = bbox.LongitudeMinDeg

	npoints, err := envelope.NPoint()
	if err != nil {
		return bbox, err
	}
	for i := 0; i < npoints; i++ {
		pt := geos.Must(envelope.Point(i))
		lon, _ := pt.X()
		lat, _ := pt.Y()

		if lon < bbox.LongitudeMinDeg {
			bbox.LongitudeMinDeg = lon
		}
		if lon > bbox.LongitudeMaxDeg {
			bbox.LongitudeMaxDeg = lon
		}
		if lat < bbox.LatitudeMinDeg {
			bbox.LatitudeMinDeg = lat
		}
		if lat > bbox.LatitudeMaxDeg {
			bbox.LatitudeMaxDeg = lat
		}
	}

	return bbox, nil
}
Exemplo n.º 2
0
func envelope(g *geos.Geometry) Envelope {
	env, err := g.Envelope()
	if err != nil {
		log.Fatal(err)
	}
	ring, err := env.ExteriorRing()
	if err != nil {
		log.Fatal(err)
	}
	cs, err := ring.coordSeq()
	if err != nil {
		log.Fatal(err)
	}
	getX := getOrd(cs, (*geos.CoordSeq).GetX)
	getY := getOrd(cs, (*geos.CoordSeq).GetY)
	return Env(getX(0), getY(0), getX(2), getY(2))
}
Exemplo n.º 3
0
// Returns the bounding box of the geos.Geometry as a rtreego.Rect
func RtreeBboxg(g *geos.Geometry, tol float64) (*rtreego.Rect, error) {
	env, err := g.Envelope()
	if err != nil {
		return nil, err
	}
	shell, _ := env.Shell()
	if err != nil {
		return nil, err
	}
	c, err := shell.Coords()
	if err != nil {
		return nil, err
	}
	return rtreego.NewRect(rtreego.Point{c[0].X, c[0].Y},
		[]float64{math.Max(tol, c[2].X-c[0].X),
			math.Max(tol, c[2].Y-c[0].Y)})
}