Пример #1
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...)
}
Пример #2
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)
}