Fork me on GitHub

gogeos - Go library for spatial data operations and geometric algorithms

gogeos is a library for Go that provides operations on spatial data and geometric algorithms.

It provides bindings to the GEOS C library.

Download

Current release: v0.1.2 (Thursday, May 29, 2014)

Download .zip Download .tar.gz View source

Quick start

package main

import (
    "fmt"
    "log"

    "github.com/paulsmith/gogeos/geos"
)

func main() {
    line, err := geos.FromWKT("LINESTRING (0 0, 10 10, 20 20)")
    if err != nil {
        log.Fatal(err)
    }

    buf, err := line.Buffer(2.5)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(buf)
    // POLYGON ((18.2322330470336311 21.7677669529663689, 18.61…
}

Overview

Functionality

gogeos is an open source project (MIT license).

Community

Installation

Requirements

GEOS must be installed on your system to build gogeos.

Ubuntu

$ apt-get install libgeos-dev

OS X - homebrew

$ brew install geos

From source (all OSes)

$ wget http://download.osgeo.org/geos/geos-3.3.8.tar.bz2
$ tar xvfj geos-3.3.8.tar.bz2
$ cd geos-3.3.8
$ ./configure
$ make
$ sudo make install

Installing gogeos

$ go get github.com/paulsmith/gogeos/geos

Examples

Binary topology operations

gogeos provides binary topology operations, which produce a new geometry from spatial analysis performed on two input geometries. For example, say we have these two overlapping polygons, A in blue and B in orange:

(NB: these graphics weren't produced by gogeos directly - I used the excellent draw2d package to render the output of gogeos functions.)

Then the following operations will produce new geometries in magenta:

A.Intersection(B)

A.Union(B)

A.Difference(B)

B.Difference(A)

A.SymDifference(B)

Unary topology operations

gogeos can produce new geometries based on a operation performed on a single geometry, perhaps with some input. For example, given a linestring geom (orange), the Buffer() and ConvexHull() methods produce a new polygons (blue):

geom.Buffer(2.5)

geom.ConvexHull()

Merging linestrings

For a collection of fully noded linestrings, a new collection can be produced that merges together the linestrings that touch only at their start and end points. This is provided by calling the LineMerge() method on a MultiLineString collection:

var linestrings = []*geos.Geometry{
    // ...
}
coll := geos.Must(geos.NewCollection(geos.MULTILINESTRING, linestrings...))
coll.LineMerge()

Before

After

These examples were inspired by the developer’s guide to the JTS