Skip to content

chrusty/go-circle-to-polygon

Repository files navigation

Circle-to-Polygon

A Go library to convert a circle to a polygon (port of https://github.com/gabzim/circle-to-polygon).

Example

Usage

The library offers some basic geometry components:

  • Point: A geographic coordinate (lat/lon)
  • Circle: A point with a radius (in metres)
  • Polygon: A list of points

Each of these structures offers a Validate() method which returns an error if the data doesn't make sense (lat/lon out of bounds, or an open polygon).

A working example can be found under cmd/circle. This renders a custom circle as GeoJSON, which you can then load into https://geojson.io to view!

Make a circle

    circle := &circletopolygon.Circle{
        Centre: &circletopolygon.Point{
            Latitude:  -41.270634,
            Longitude: 173.283966,
        },
        Radius: 20000, // 20km
    }

Convert a circle to a polygon with 32 edges:

    polygon, err := circle.ToPolygon(32)
    if err != nil {
        panic(err)
    }

Render a polygon as GeoJSON:

    geoJSON, err := polygon.GeoJSON()
    if err != nil {
        panic(err)
    }

Do other things with the polygon:

    for _, point := range polygon {
        fmt.Printf("Lat, Lon: %f, %f", point.Latitude, point.Longitude)
    }