tgo

package module
v0.0.0-...-39e10e4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 5 Imported by: 0

README

tgo

Tests

GoDoc

Go bindings for tidwall/tg Geometry library for C - Fast point-in-polygon

This is partial but functional, tg is a very small self contained C library, tgo compiles tg, no external dependencies needed.

Usage

Simply go get this library with CGO enabled (you'll need a C compiler).

Read from WKT
// Unmarshal from WKT
input := "POLYGON((0 0,0 1,1 1,1 0,0 0))"
g, _ := tgo.UnmarshalWKT(input)

// Marshal to WKT
output := g.AsText()
fmt.Println(output) // Prints: POLYGON((0 0,0 1,1 1,1 0,0 0))
Read from GeoJSON
input := `{"type":"Feature","properties":{},"geometry":{"coordinates":[-79.20159897229003,43.636785010689835],"type":"Point"}}`
g, _ := tgo.UnmarshalGeoJSON(input)
Read from WKB
input := []byte{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 248, 63, 0, 0, 0, 0, 0, 0, 4, 64}
g, _ := tgo.UnmarshalWKB(input)
Intersects
if Intersects(g1, g2) {
	fmt.Println("Intersects")
}
Point in Polygon on large FeatureCollections

// load your collection using UnmarshalGeoJSON
found := g.StabOne(2, 48)
if found != nil {
	fmt.Println(found.Properties())
}
// Output: {"properties":{ "ADMIN": "France", "ISO_A2": "FR", "ISO_A3": "FRA" }}
Types
input := "POLYGON((0 0,0 1,1 1,1 0,0 0))"
g, _ := tgo.UnmarshalWKT(input)

if g.Types() == tgo.Polygon() {
	p, _ := g.AsPoly()
	p.HolesCount()
}

Tests

Some tests are borrowed from simplefeatures.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(g1, g2 *Geom) bool

func CoveredBy

func CoveredBy(g1, g2 *Geom) bool

func Covers

func Covers(g1, g2 *Geom) bool

func Disjoint

func Disjoint(g1, g2 *Geom) bool

func Equals

func Equals(g1, g2 *Geom) bool

Equals returns true if the two geometries are equal

func Intersects

func Intersects(g1, g2 *Geom) bool

func Touches

func Touches(g1, g2 *Geom) bool

func Within

func Within(g1, g2 *Geom) bool

Types

type Geom

type Geom struct {
	// contains filtered or unexported fields
}

func UnmarshalGeoJSON

func UnmarshalGeoJSON(data []byte) (*Geom, error)

UnmarshalGeoJSON parses geometries from a GeoJSON representation. Using the Natural indexation.

func UnmarshalGeoJSONAndIndex

func UnmarshalGeoJSONAndIndex(data []byte, idxt IndexType) (*Geom, error)

UnmarshalGeoJSONAndIndex parses geometries from a GeoJSON representation, and sets the indexation type.

func UnmarshalWKB

func UnmarshalWKB(data []byte) (*Geom, error)

UnmarshalWKB parses geometries from a WKB representation. Using the Natural indexation.

func UnmarshalWKBAndIndex

func UnmarshalWKBAndIndex(data []byte, idxt IndexType) (*Geom, error)

UnmarshalWKBAndIndex parses geometries from a WKB representation, and sets the indexation type.

func UnmarshalWKT

func UnmarshalWKT(data string) (*Geom, error)

UnmarshalWKT parses geometries from a WKT representation. Using the Natural indexation.

func UnmarshalWKTAndIndex

func UnmarshalWKTAndIndex(data string, idxt IndexType) (*Geom, error)

UnmarshalWKTAndIndex parses geometries from a WKT representation, and sets the indexation type.

func (*Geom) AsMultiPoly

func (g *Geom) AsMultiPoly() (*MultiPoly, bool)

AsMultiPoly returns a MultiPoly of the geometry, returns false if not applicable.

func (*Geom) AsPoly

func (g *Geom) AsPoly() (*Poly, bool)

AsPoly returns a Poly of the geometry, returns false if not applicable.

func (*Geom) AsText

func (g *Geom) AsText() string

AsText returns geometry as WKT

func (*Geom) MemSize

func (g *Geom) MemSize() int

MemSize returns the allocation size of the geometry in the C world.

func (*Geom) Properties

func (g *Geom) Properties() string

Properties returns a string that represents any extra JSON from a parsed GeoJSON geometry. Such as the "id" or "properties" fields.

func (*Geom) StabOne

func (g *Geom) StabOne(x, y float64) *Geom

StabOne performs a Point in Polygon query using the point (x,y) and returns the first encountered child geometry

func (*Geom) Type

func (g *Geom) Type() GeomType

Type returns the geometry type.

type GeomType

type GeomType uint8
const (
	Point              GeomType = iota + 1 // Point.
	LineString                             // LineString.
	Polygon                                // Polygon.
	MultiPoint                             // MultiPoint, collection of points.
	MultiLineString                        // MultiLineString, collection of linestrings.
	MultiPolygon                           // MultiPolygon, collection of polygons.
	GeometryCollection                     // GeometryCollection, collection of geometries.
)

type IndexType

type IndexType uint32
const (
	None     IndexType = iota + 1 // no indexing available, or disabled
	Natural                       // indexing with natural ring order, for rings/lines
	YStripes                      // indexing using segment striping, rings only
)

type MultiPoly

type MultiPoly struct {
	// contains filtered or unexported fields
}

func (*MultiPoly) AsGeom

func (mp *MultiPoly) AsGeom() *Geom

AsGeom returns a Geom of the multipolygon without cloning.

func (*MultiPoly) AsText

func (mp *MultiPoly) AsText() string

AsText returns the representation of the multipoly as WKT.

func (*MultiPoly) PolygonAt

func (mp *MultiPoly) PolygonAt(index int) (*Poly, bool)

PolygonAt returns the Poly at index, true if it's applicable.

func (*MultiPoly) PolygonsCount

func (mp *MultiPoly) PolygonsCount() int

PolygonsCount returns the count of polygons in the multipoly.

type Poly

type Poly struct {
	// contains filtered or unexported fields
}

func (*Poly) AsGeom

func (p *Poly) AsGeom() *Geom

AsGeom returns a Geom of the polygon without cloning.

func (*Poly) AsText

func (p *Poly) AsText() string

AsText returns the representation of the poly as WKT.

func (*Poly) Exterior

func (p *Poly) Exterior() *Ring

Exterior returns the exterior Ring of the poly.

func (*Poly) HolesCount

func (p *Poly) HolesCount() int

HolesCount returns the holes count.

func (*Poly) IsClockWise

func (p *Poly) IsClockWise() bool

IsClockWise returns true if the polygon is clock wise.

type Ring

type Ring struct {
	// contains filtered or unexported fields
}

func (*Ring) Area

func (r *Ring) Area() float64

func (*Ring) AsGeom

func (r *Ring) AsGeom() *Geom

func (*Ring) AsPoly

func (r *Ring) AsPoly() *Poly

func (*Ring) AsText

func (r *Ring) AsText() string

AsText returns the representation of the ring as WKT.

func (*Ring) Perimeter

func (r *Ring) Perimeter() float64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL