geojson

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2022 License: MIT Imports: 1 Imported by: 7

README ¶

🔨 GoDoc GitHub license Go Report Card

GeoJSON

GeoJSON is a GoLang library that implements the GeoJSON format specification

Usage

GeoJSON Objects - Examples

This library implements the following GeoJSON Objects:

Point
point := geometry.NewPoint(lat,lng)
point := geometry.Point{
		Lat: 50.0,
		Lng: 30.0,
	}
MultiPoint
multipoint := geometry.NewPoint(lat,lng)
multipoint := geometry.Point{
		Lat: 50.0,
		Lng: 30.0,
	}
LineString
lineString := geometry.LineString{
	Coordinates: []geometry.Point{
		{
			Lat: 30.0,
			Lng: 23.0,
		},
		{
			Lat: 32.0,
			Lng: 24.0,
		},
	},

}
MultiLineString
multiLineString := geometry.MultiLineString{
		Coordinates: []geometry.LineString{
			{
				Coordinates: []geometry.Point{
					{
						Lat: 43.3,
						Lng: 24.5,
					},
					{
						Lat: 44.2,
						Lng: 25.0,
					},
				},
			},
			{
				Coordinates: []geometry.Point{
					{
						Lat: 38.3,
						Lng: 40.5,
					},
					{
						Lat: 38.2,
						Lng: 5.0,
					},
				},
			},
		},
	}
Polygon
poly := geometry.Polygon{
		Coordinates: []geometry.LineString{
			{
				Coordinates: []geometry.Point{
					{
						Lat: 36.171278341935434,
						Lng: -86.76624298095703,
					},
					{
						Lat: 36.170862616662134,
						Lng: -86.74238204956055,
					},
					{
						Lat: 36.19607929145354,
						Lng: -86.74100875854492,
					},
					{
						Lat: 36.2014818084173,
						Lng: -86.77362442016602,
					},
					{
						Lat: 36.171278341935434,
						Lng: -86.76624298095703,
					},
				},
			},
		},
MultiPolygon
Feature
  • New Feature
	geom := geometry.Geometry{
		GeoJSONType: geojson.Polygon,
		Coordinates: [][]float64{
			{
				36.171278341935434,
				-86.76624298095703,
			},
			{
				36.170862616662134,
				-86.74238204956055,
			},
			{
				36.19607929145354,
				-86.74100875854492,
			},
			{
				36.2014818084173,
				-86.77362442016602,
			},
			{
				36.171278341935434,
				-86.76624298095703,
			},
		},
	}

	f, err := feature.New(geom, []float64{}, nil, "geom-1")```

- FromJSON:

```go
	 gjson: "{ \"type\" : \"Feature\",  \"properties\": {},   \"geometry\": { \"type\": \"Point\",  \"coordinates\": [-71, 41]} }",
	if err != nil {
		t.Errorf("LoadJSONFixture error %v", err)
	}

	f, err := feature.FromJSON(gjson)
Supported Functions:
  • ToPoint
  • ToPolygon
  • ToMultiPolygon
  • ToLineString
  • ToMultiLineString
FeatureCollection
	gjson := "{ \"type\": \"FeatureCollection\", \"features\": [ { \"type\": \"Feature\", \"properties\": {}, \"geometry\": { \"type\": \"Polygon\", \"coordinates\": [ [ [-2.109375, 47.040182144806664], [4.5703125, 44.59046718130883], [7.03125, 49.15296965617042], [-3.515625, 49.83798245308484], [-2.109375, 47.040182144806664] ] ] } }, { \"type\": \"Feature\", \"properties\": {}, \"geometry\": { \"type\": \"Polygon\", \"coordinates\": [ [ [9.64599609375, 47.70976154266637], [9.4482421875, 47.73932336136857], [8.8330078125, 47.47266286861342], [10.21728515625, 46.604167162931844], [11.755371093749998, 46.81509864599243], [11.865234375, 47.90161354142077], [9.64599609375, 47.70976154266637] ] ] } } ] }"

	collection, err := feature.CollectionFromJSON(gjson)
	if err != nil {
		t.Errorf("CollectionFromJSON error: %v", err)
	}	
Geometry
geometry, err := geometry.FromJSON("{ \"type\": \"MultiPolygon\", \"coordinates\": [ [ [ [-116, -45], [-90, -45], [-90, -56], [-116, -56], [-116, -45] ] ], [ [ [-90.351563, 9.102097], [-77.695312, -3.513421], [-65.039063, 12.21118], [-65.742188, 21.616579], [-84.023437, 24.527135], [-90.351563, 9.102097] ] ] ] }")
Supported Functions:
  • ToPoint
  • ToPolygon
  • ToMultiPolygon
  • ToLineString
  • ToMultiLineString
GeometryCollection
gjson := "{ \"TYPE\": \"GeometryCollection\", \"geometries\": [ { \"TYPE\": \"Point\", \"coordinates\": [-7903683.846322424, 5012341.663847514] }, { \"TYPE\": \"LineString\", \"coordinates\": [ [-2269873.991957, 3991847.410438], [-391357.58482, 6026906.856034], [1565430.33928, 1917652.163291] ] } ] }"

gcl, err := geometry.CollectionFromJSON
if err != nil {
	t.Errorf("CollectionFromJSON error: %v", err)
}	

turf-go

You can also use the library turf-go which is an implementation of the turf.js library in GoLang.

Documentation ¶

Overview ¶

The idea is from: https://github.com/miekg/dns/blob/master/version.go

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var Version = v{0, 0, 3}

Version is current version of this library.

Functions ¶

This section is empty.

Types ¶

type BBOX ¶

type BBOX struct {
	West  float64
	South float64
	East  float64
	North float64
}

BBOX defines a Bounding Box A GeoJSON object MAY have a member named "bbox" to include information on the coordinate range for its Geometries, Features, or FeatureCollections. The value of the bbox member MUST be an array of length 2*n where n is the number of dimensions represented in the contained geometries, with all axes of the most southwesterly point followed by all axes of the more northeasterly point. The axes order of a bbox follows the axes order of geometries. https://tools.ietf.org/html/rfc7946#section-5

func NewBBox ¶

func NewBBox(west, south, east, north float64) *BBOX

NewBBox initializes a new Bounding Box

type OBjectType ¶

type OBjectType string

OBjectType is the base class for all GeometryObjects

const (
	// Point Defines a Point Type https://tools.ietf.org/html/rfc7946#section-3.1.2
	Point OBjectType = "Point"
	// MultiPoint Defines a MultiPoint Type https://tools.ietf.org/html/rfc7946#section-3.1.3
	MultiPoint OBjectType = "MultiPoint"
	// LineString Defines a LineString Type https://tools.ietf.org/html/rfc7946#section-3.1.4
	LineString OBjectType = "LineString"
	// MultiLineString Defines a MultiLineString Type https://tools.ietf.org/html/rfc7946#section-3.1.5
	MultiLineString OBjectType = "MultiLineString"
	// Polygon Defines a Polygon Type https://tools.ietf.org/html/rfc7946#section-3.1.6
	Polygon OBjectType = "Polygon"
	// MultiPolygon Defines a MultiPolygon Type https://tools.ietf.org/html/rfc7946#section-3.1.7
	MultiPolygon OBjectType = "MultiPolygon"
	// GeometryCollection Defines a GeometryCollection Type https://tools.ietf.org/html/rfc7946#section-3.1.8
	GeometryCollection OBjectType = "GeometryCollection"
	// Feature Defines a Feature Type https://tools.ietf.org/html/rfc7946#section-3.2
	Feature OBjectType = "Feature"
	// FeatureCollection Defines a FeatureCollection Type https://tools.ietf.org/html/rfc7946#section-3.3
	FeatureCollection OBjectType = "FeatureCollection"
)

type Object ¶

type Object struct {
	// BBox is the bounding box of the coordinate range of the object's Geometries, Features, or Feature Collections.
	// https://tools.ietf.org/html/rfc7946#section-5
	BBox *BBOX
	// GeoJSONType describes the type of GeoJSON Geometry, Feature or FeatureCollection this object is.
	GeoJSONType OBjectType
}

Object type

Directories ¶

Path Synopsis
internal
test

Jump to

Keyboard shortcuts

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