Example #1
0
func ToGjFeature(g *geos.Geometry) (gjFeature, error) {
	var gjf gjFeature
	gjf.Type = "Feature"
	gjf.Geometry.Type = "MultiPolygon"
	ngeoms, _ := g.NGeometry()
	gjf.Geometry.Coords = make([][][][]float64, ngeoms)

	for i := 0; i < ngeoms; i++ {
		subg, err := g.Geometry(i)
		if err != nil {
			return gjf, err
		}
		holes, err := subg.Holes()
		if err != nil {
			return gjf, err
		}
		gjf.Geometry.Coords[i] = make([][][]float64, 1+len(holes))

		shell, err := subg.Shell()
		if err != nil {
			return gjf, err
		}

		coords, err := shell.Coords()
		if err != nil {
			return gjf, err
		}
		gjf.Geometry.Coords[i][0] = make([][]float64, len(coords))
		for j, coord := range coords {
			gjf.Geometry.Coords[i][0][j] = make([]float64, 2)
			gjf.Geometry.Coords[i][0][j][0] = coord.X
			gjf.Geometry.Coords[i][0][j][1] = coord.Y
		}

		for k, hole := range holes {
			coords, err := hole.Coords()
			if err != nil {
				return gjf, err
			}

			gjf.Geometry.Coords[i][1+k] = make([][]float64, len(coords))
			for j, coord := range coords {
				gjf.Geometry.Coords[i][1+k][j] = make([]float64, 2)
				gjf.Geometry.Coords[i][1+k][j][0] = coord.X
				gjf.Geometry.Coords[i][1+k][j][1] = coord.Y
			}
		}
	}
	return gjf, nil
}
Example #2
0
func drawPolygon(ctxt draw2d.GraphicContext, g *geos.Geometry, fillColor color.Color, strokeColor color.Color, width float64, scale func(x, y float64) (float64, float64)) {
	ctxt.SetFillColor(fillColor)
	ctxt.SetStrokeColor(strokeColor)
	ctxt.SetLineWidth(width)
	// exterior ring
	ring := geos.Must(g.ExteriorRing())
	cs, err := ring.coordSeq()
	if err != nil {
		log.Fatal(err)
	}
	lineCoordSeq(ctxt, cs, scale)
	ctxt.FillStroke()
	// interior rings...
}
Example #3
0
func drawLine(ctxt draw2d.GraphicContext, g *geos.Geometry, c color.Color, width float64, scale func(x, y float64) (float64, float64)) {
	if c != nil {
		ctxt.SetStrokeColor(c)
	}
	if width != 0.0 {
		ctxt.SetLineWidth(width)
	}
	// XXX: should get a [] of points
	cs, err := g.coordSeq()
	if err != nil {
		log.Fatal(err)
	}
	lineCoordSeq(ctxt, cs, scale)
	ctxt.Stroke()
}
Example #4
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
}
Example #5
0
//IntersectsFilter creates a Filter allowing to skip tiles outside of a given geometry
func IntersectsFilter(g *geos.Geometry) raster.Filter {
	//TODO: use a prepared geometry

	return func(level, x, y int) (bool, error) {

		tile, err := newTilePolygon(level, x, y)
		if err != nil {
			return false, err
		}
		intersects, err := g.Intersects(tile)
		if err != nil {
			return false, err
		}

		return !intersects, nil //Tiles that do not intersect are excluded
	}
}
Example #6
0
func drawPoint(ctxt draw2d.GraphicContext, g *geos.Geometry, c color.Color, radius float64, scale func(x, y float64) (float64, float64)) {
	if c != nil {
		ctxt.SetFillColor(c)
	}
	x, err := g.X()
	if err != nil {
		log.Fatal(err)
	}
	y, err := g.Y()
	if err != nil {
		log.Fatal(err)
	}
	x, y = scale(x, y)
	ctxt.MoveTo(x, y)
	ctxt.ArcTo(x, y, radius, radius, 0, 2*math.Pi)
	ctxt.Fill()
}
Example #7
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))
}
Example #8
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)})
}