Пример #1
0
func test() {
	g1 := geos.Must(geos.FromWKT("LINESTRING(0 0, 3 3, 3 2, 4 2, 7 5)"))
	g2 := geos.Must(geos.FromWKT("LINESTRING(0 10, 1 0, 0 10, 10 10)"))
	g3 := geos.Must(geos.FromWKT("POINT(5 5)"))
	g4 := geos.Must(g3.Buffer(1))
	g5 := geos.Must(geos.FromWKT("MULTIPOINT(5 6, 6 6, 3 8, 7 8)"))
	example("test.png", g1, g2, g4, g3, g5)
}
Пример #2
0
func drawGeoms(m image.Image, ctxt draw2d.GraphicContext, geoms ...*geos.Geometry) {
	// get envelope and calculate scale fn
	var env Envelope
	if len(geoms) > 1 {
		coll, err := geos.NewCollection(geos.GEOMETRYCOLLECTION, geoms...)
		if err != nil {
			log.Fatal(err)
		}
		union, err := coll.UnaryUnion()
		if err != nil {
			log.Fatal(err)
		}
		env = envelope(union)
	} else {
		env = envelope(geoms[0])
	}
	scale := func(x, y float64) (float64, float64) {
		x = env.Px(x)*(WIDTH-2*PAD) + PAD
		y = HEIGHT - (env.Py(y)*(HEIGHT-2*PAD) + PAD)
		return x, y
	}

	var draw func(geoms ...*geos.Geometry)

	draw = func(geoms ...*geos.Geometry) {
		for i, g := range geoms {
			// pick color
			c := colors[i%len(colors)]
			// switch type
			_type, err := g.Type()
			if err != nil {
				log.Fatal(err)
			}
			switch _type {
			case geos.POINT:
				drawPoint(ctxt, g, c, 4.0, scale)
			case geos.LINESTRING, geos.LINEARRING:
				drawLine(ctxt, g, c, 4.0, scale)
			case geos.POLYGON:
				drawPolygon(ctxt, g, c, darken(c), 3.0, scale)
			case geos.MULTIPOINT, geos.MULTILINESTRING, geos.MULTIPOLYGON, geos.GEOMETRYCOLLECTION:
				n, err := g.NGeometry()
				if err != nil {
					log.Fatal(err)
				}
				var subgeoms []*geos.Geometry
				for i := 0; i < n; i++ {
					subgeoms = append(subgeoms, geos.Must(g.Geometry(i)))
				}
				draw(subgeoms...)
			default:
				log.Fatalf("unknown geometry type %v", _type)
			}
		}
	}

	draw(geoms...)
}
Пример #3
0
func ex9() {
	wkt := []string{
		"LINESTRING (2 10, 3 10, 4 9, 5 8)",
		"LINESTRING (5 8, 6 6, 6 5)",
		"LINESTRING (6 5, 9 6, 10 7, 11 9)",
		"LINESTRING (11 9, 11 10, 10 11)",
		"LINESTRING (6 5, 3 4, 2 3)",
		"LINESTRING (2 3, 1.5 2.5, 1 1, 1 0)",
		"LINESTRING (1 0, 0 0, 0 -1, 1 -2)",
		"LINESTRING (1 -2, 2 -2, 3 -1, 3 0, 1 0)",
		"LINESTRING (6 5, 6 3, 6.5 2.5)",
		"LINESTRING (6.5 2.5, 7.5 2, 8.5 1.5)",
		"LINESTRING (8.5 1.5, 9 0.5, 10 0.5, 11 1.5)",
	}
	var linestrings []*geos.Geometry
	for i := range wkt {
		linestrings = append(linestrings, geos.Must(geos.FromWKT(wkt[i])))
	}
	example("example9-unmerged-linestrings.png", linestrings...)
	merged := geos.Must(geos.Must(geos.NewCollection(geos.MULTILINESTRING, linestrings...)).LineMerge())
	example("example9-merged-linestrings.png", merged)
}
Пример #4
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...
}
Пример #5
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
}
Пример #6
0
func main() {
	flag.Parse()

	poly := geos.Must(geos.FromWKT(*aoi))
	bbox, err := geosconverter.GetBoundingBox(poly)
	if err != nil {
		log.Fatal(err)
	}

	//Source
	if len(*srcDriver) == 0 {
		*srcDriver = raster.FindDriverName(*src)
	}
	input, err := raster.Open(*srcDriver, *src)
	if err != nil {
		log.Fatal(err)
	}
	if c, ok := input.(closer); ok {
		defer c.Close()
	}
	var inputReader raster.TileReader
	if len(*srcLayer) > 0 {
		inputReader, err = input.OpenTileLayer(*srcLayer)
	} else {
		inputReader, err = raster.OpenTileLayerAt(input, 0)
	}
	if err != nil {
		log.Fatal(err)
	}

	//Destination
	if len(*dstDriver) == 0 {
		*dstDriver = raster.FindDriverName(*dst)
	}
	outputReader, err := raster.Open(*dstDriver, *dst)
	if err != nil {
		log.Fatal(err)
	}
	output, ok := outputReader.(raster.WritableTileSource)
	if !ok {
		log.Fatal("Output driver does not allow writing")
	}
	if c, ok := output.(closer); ok {
		defer c.Close()
	}
	outputWriter, err := output.CreateTileLayer(*dstLayer)
	if err != nil {
		log.Fatal(err)
	}

	//Initialize the copy
	copier, err := raster.NewCopier(inputReader, outputWriter)
	if err != nil {
		log.Fatal(err)
	}

	polygonFilter := geosconverter.IntersectsFilter(poly)
	if *replace {
		copier.Filter = polygonFilter
	} else {
		copier.Filter = raster.Any(outputWriter.Contains, polygonFilter)
	}

	//Iterate on each requested level and performs the copy
	for level := *lvlmin; level <= *lvlmax; level++ {
		log.Print("Level: ", level)

		if *replace {
			log.Print("Level ", level, " clearing in database")
			err := outputWriter.Clear(level)
			if err != nil {
				log.Fatal(err)
			}
			log.Print("Level ", level, " cleared in database")
		}

		tiles, err := raster.GetTileBlock(bbox, level)
		if err != nil {
			log.Fatal(err)
		}
		log.Print("BBOX (tiles): ", tiles)

		log.Print("Nb tiles in BBOX: ", tiles.Count())

		bar := pb.StartNew(tiles.Count())

		processed, err := copier.CopyBlock(tiles, func(level, x, y int, processed bool) {
			bar.Increment()
		})
		if err != nil {
			log.Fatal(err)
		}
		bar.FinishPrint("End")
		log.Print("Nb tiles processed: ", processed)
	}

}
Пример #7
0
func ex8() {
	l := geos.Must(geos.FromWKT("LINESTRING (0 2, 6 2, 3 4, 4.5 8, 1 8, 3 0)"))
	b := geos.Must(l.Buffer(0.5))
	example("example8-buffer.png", b, l)
}
Пример #8
0
func ex7() {
	example("example7-symdifference.png", geos.Must(a.SymDifference(b)))
}
Пример #9
0
func ex6() {
	example("example6-difference.png", a, b, geos.Must(b.Difference(a)))
}
Пример #10
0
func ex5() {
	example("example5-difference.png", a, b, geos.Must(a.Difference(b)))
}
Пример #11
0
func ex10() {
	l := geos.Must(geos.FromWKT("LINESTRING (0 2, 6 2, 3 4, 4.5 8, 1 8, 3 0)"))
	b := geos.Must(l.ConvexHull())
	example("example10-convex-hull.png", b, l)
}
Пример #12
0
func ex3() {
	example("example3-intersection.png", a, b, geos.Must(a.Intersection(b)))
}
Пример #13
0
func ex2() {
	a := geos.Must(geos.FromWKT("LINEARRING (0 3, 2 3, 3 1, 1 0, 0 1.5, 0 3)"))
	b := geos.Must(geos.FromWKT("LINEARRING (1 2, 1.5 4, 3.5 4, 4.5 2.5, 1 2)"))
	example("example2-a-b.png", a, b)
}
Пример #14
0
func ex1() {
	example("example1.png", geos.Must(geos.FromWKT("LINESTRING (0 0, 10 10, 20 20)")))
}
Пример #15
0
func ex4() {
	example("example4-union.png", a, b, geos.Must(a.Union(b)))
}
Пример #16
0
func ExampleGeometry_LineInterpolatePoint() {
	line := geos.Must(geos.FromWKT("LINESTRING(25 50, 100 125, 150 190)"))
	pt := geos.Must(line.LineInterpolatePoint(0.20))
	fmt.Println(pt)
	// Output: POINT (51.5974135047432014 76.5974135047432014)
}
Пример #17
0
	ex3()
	ex4()
	ex5()
	ex6()
	ex7()
	ex8()
	ex9()
	ex10()
}

func ex1() {
	example("example1.png", geos.Must(geos.FromWKT("LINESTRING (0 0, 10 10, 20 20)")))
}

var (
	a = geos.Must(geos.FromWKT("POLYGON ((0 3, 2 3, 3 1, 1 0, 0 1.5, 0 3))"))
	b = geos.Must(geos.FromWKT("POLYGON ((1 2, 1.5 4, 3.5 4, 4.5 2.5, 1 2))"))
)

func ex2() {
	a := geos.Must(geos.FromWKT("LINEARRING (0 3, 2 3, 3 1, 1 0, 0 1.5, 0 3)"))
	b := geos.Must(geos.FromWKT("LINEARRING (1 2, 1.5 4, 3.5 4, 4.5 2.5, 1 2)"))
	example("example2-a-b.png", a, b)
}

func ex3() {
	example("example3-intersection.png", a, b, geos.Must(a.Intersection(b)))
}

func ex4() {
	example("example4-union.png", a, b, geos.Must(a.Union(b)))