ewkb

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ewkb decodes Extended Well-Known Byte format.

EWKB is encoded in hexadecimal. There are 2 parts:

  • The header

  • The data

HEADER

Byte 0: 0 means big endian, 1 means little endian.

Bytes 1-4:

  • bit 64 means with Z.

  • bit 63 means with M.

  • bit 62 means with SRID (System Reference ID).

  • bits 0-61 are for the type of the geometry.

If SRID bit is 1, then the 4 following bytes are the SRID (32bit unsigned integer).

After that, come the data part.

DATA

  • Point: depending on the format (XY, XYM or XYZM), there are 2, 3 or 4 float64 (8 bytes).
  • LineString: It's an array of Point. First of all, there is a uint32 (4 bytes) for the dimension of the array. The points are read as below.

Index

Constants

View Source
const (
	// ErrWrongByteOrder occurs when byte order is not recognized.
	ErrWrongByteOrder = Error("wrong byte order")

	// ErrIncompatibleFormat occurs when geometry formats are incompatible.
	ErrIncompatibleFormat = Error("incompatible format")

	// ErrWrongGeometryType occurs when geometry type is not the expected one.
	ErrWrongGeometryType = Error("wrong geometry type")
)
View Source
const (
	// ErrCircularStringWrongSize occurs when the number of verticies is not odd or is less than 3.
	ErrCircularStringWrongSize = Error("circularstring has a wrong size (odd number of vertices >1)")
)
View Source
const (
	// ErrMissingWellKnownGeometry is when no well known geometries were specified.
	ErrMissingWellKnownGeometry = Error("missing well known geometries")
)
View Source
const (

	// ErrTriangleWrongSize occurs when a triangle has more than 1 polygon,
	// or the polygon hasn't 3 vertices.
	ErrTriangleWrongSize = Error("triangle has a wrong size")
)

Variables

This section is empty.

Functions

func IsEWKB added in v0.0.1

func IsEWKB(data interface{}) bool

IsEWKB checks if data is potentially Extended Well Known Bytes.

func Marshal

func Marshal(geoShape Marshaler) ([]byte, error)

Marshal converts Geometry to EWKB array of bytes.

func Unmarshal

func Unmarshal(geoShape Unmarshaler, value interface{}) error

Unmarshal converts EWKB array of bytes to Geometry.

Types

type CircularString added in v0.0.8

type CircularString struct {
	SRID *SystemReferenceID
	CoordinateSet
}

CircularString is a CIRCULARSTRING in database.

CircularString is the basic curve type, similar to a LineString in the linear world. A single arc segment is specified by three points: the start and end points (first and third) and some other point on the arc. To specify a closed circle the start and end points are the same and the middle point is the opposite point on the circle diameter (which is the center of the arc). In a sequence of arcs the end point of the previous arc is the start point of the next arc, just like the segments of a LineString. This means that a CircularString must have an odd number of points greater than 1.

func (CircularString) Layout added in v0.0.8

func (c CircularString) Layout() Layout

Layout implements the Marshaler interface.

func (CircularString) MarshalEWBK added in v0.0.8

func (c CircularString) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (CircularString) SystemReferenceID added in v0.0.8

func (c CircularString) SystemReferenceID() *SystemReferenceID

SystemReferenceID implements the Marshaler interface.

func (CircularString) Type added in v0.0.8

func (c CircularString) Type() GeometryType

Type implements the Geometry interface.

func (*CircularString) UnmarshalEWBK added in v0.0.8

func (c *CircularString) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type Coordinate added in v0.0.1

type Coordinate map[byte]float64

Coordinate is coordinate system.

func NewNullCoordinate added in v0.0.1

func NewNullCoordinate(layout Layout) Coordinate

NewNullCoordinate creates a null coordinate system.

func (Coordinate) IsNull added in v0.0.1

func (c Coordinate) IsNull() bool

IsNull checks if coordinate is null.

func (Coordinate) Layout added in v0.0.1

func (c Coordinate) Layout() Layout

Layout implements the Marshaler interface.

func (Coordinate) MarshalEWBK added in v0.0.1

func (c Coordinate) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (*Coordinate) UnmarshalEWBK added in v0.0.1

func (c *Coordinate) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type CoordinateGroup added in v0.0.1

type CoordinateGroup []CoordinateSet

CoordinateGroup is a group of set of coordinates.

func (CoordinateGroup) MarshalEWBK added in v0.0.1

func (c CoordinateGroup) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (*CoordinateGroup) UnmarshalEWBK added in v0.0.1

func (c *CoordinateGroup) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type CoordinateSet added in v0.0.1

type CoordinateSet []Coordinate

CoordinateSet is a set of coordinates.

func (CoordinateSet) MarshalEWBK added in v0.0.1

func (c CoordinateSet) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (*CoordinateSet) UnmarshalEWBK added in v0.0.1

func (c *CoordinateSet) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type Decoder added in v0.0.1

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

Decoder is a Extended Well Known Byte decoder.

func NewDecoder added in v0.0.1

func NewDecoder(reader io.Reader) *Decoder

NewDecoder creates a EWKB decoder.

func (*Decoder) Decode added in v0.0.1

func (d *Decoder) Decode(geoShape Unmarshaler) error

Decode decodes to a Geometry.

type Encoder added in v0.0.1

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

Encoder is a Extended Well Known Byte encoder.

func NewEncoder added in v0.0.1

func NewEncoder(writer io.Writer) *Encoder

NewEncoder creates a EWKB encoder.

func (*Encoder) Encode added in v0.0.1

func (e *Encoder) Encode(geoShape Marshaler) error

Encode encodes geometry to EWKB.

type Error

type Error string

Error is a database error.

func (Error) Error

func (e Error) Error() string

type ExtendedWellKnownBytes

type ExtendedWellKnownBytes struct {
	ExtendedWellKnownBytesHeader
	DataStream io.Reader
	IsNil      bool
}

ExtendedWellKnownBytes is the EWKB.

func DecodeHeader

func DecodeHeader(reader io.Reader) (*ExtendedWellKnownBytes, error)

DecodeHeader decodes EWKB header.

func (ExtendedWellKnownBytes) ReadFloat64

func (e ExtendedWellKnownBytes) ReadFloat64() (float64, error)

ReadFloat64 reads 64-bit float from the stream.

func (ExtendedWellKnownBytes) ReadUint32

func (e ExtendedWellKnownBytes) ReadUint32() (uint32, error)

ReadUint32 reads 32-bit unsigned integer from the stream.

type ExtendedWellKnownBytesHeader

type ExtendedWellKnownBytesHeader struct {
	SRID      *SystemReferenceID
	ByteOrder binary.ByteOrder
	Type      GeometryType
	Layout    Layout
}

ExtendedWellKnownBytesHeader is the header from EWKB data.

type Geometry

type Geometry interface {
	Unmarshaler
	Marshaler
}

Geometry is a geometrical shape.

func DefaultWellKnownGeometry added in v1.1.0

func DefaultWellKnownGeometry() []Geometry

DefaultWellKnownGeometry is the default well known geometry set.

type GeometryCollection added in v1.1.0

type GeometryCollection struct {
	SRID       *SystemReferenceID
	Collection []Geometry
	// contains filtered or unexported fields
}

GeometryCollection is a heterogeneous (mixed) collection of geometries.

func NewGeometryCollection added in v1.1.0

func NewGeometryCollection(wellknownGeometry ...Geometry) *GeometryCollection

NewGeometryCollection creates a new empty collection of geometries.

func (GeometryCollection) Layout added in v1.1.0

func (g GeometryCollection) Layout() Layout

Layout implements the Marshaler interface.

func (GeometryCollection) MarshalEWBK added in v1.1.0

func (g GeometryCollection) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (GeometryCollection) SystemReferenceID added in v1.1.0

func (g GeometryCollection) SystemReferenceID() *SystemReferenceID

SystemReferenceID implements the Marshaler interface.

func (GeometryCollection) Type added in v1.1.0

Type implements the Geometry interface.

func (*GeometryCollection) UnmarshalEWBK added in v1.1.0

func (g *GeometryCollection) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type GeometryType

type GeometryType uint8

GeometryType is the type of the geometry (bits 0-61 of the bytes 1-4 of the header).

const (
	// GeometryTypePoint stands for point.
	GeometryTypePoint GeometryType = 1

	// GeometryTypeLineString stands for lineString.
	GeometryTypeLineString GeometryType = 2

	// GeometryTypePolygon stands for polygon.
	GeometryTypePolygon GeometryType = 3

	// GeometryTypeMultiPoint stands for multiPoint.
	GeometryTypeMultiPoint GeometryType = 4

	// GeometryTypeMultiLineString stands for multiLineString.
	GeometryTypeMultiLineString GeometryType = 5

	// GeometryTypeMultiPolygon stands for multiPolygon.
	GeometryTypeMultiPolygon GeometryType = 6

	// GeometryTypeGeometryCollection stands for geometryCollection.
	GeometryTypeGeometryCollection GeometryType = 7

	// GeometryTypeCircularString stands for a curved string.
	GeometryTypeCircularString GeometryType = 8

	// GeometryTypeCompound stands for compound.
	GeometryTypeCompound GeometryType = 9

	// GeometryTypeCurvePoly stands for curvepoly.
	GeometryTypeCurvePoly GeometryType = 10

	// GeometryTypeMultiCurve stands for multicurve.
	GeometryTypeMultiCurve GeometryType = 11

	// GeometryTypeMultiSurface stands for multisurface.
	GeometryTypeMultiSurface GeometryType = 12

	// GeometryTypePolyhedralSurface stands for polyhedralSurface.
	GeometryTypePolyhedralSurface GeometryType = 13

	// GeometryTypeTin stands for tin.
	GeometryTypeTin GeometryType = 15

	// GeometryTypeTriangle stands for triangle.
	GeometryTypeTriangle GeometryType = 17
)

type Layout

type Layout uint8

Layout is the EWKB layout.

func LayoutWith

func LayoutWith(hasM bool, hasZ bool) Layout

LayoutWith builds a layout.

func (Layout) Format

func (l Layout) Format() string

Format is the coordinate format.

func (Layout) Size

func (l Layout) Size() uint32

Size is the number of coordinates in the layout.

func (Layout) Uint32

func (l Layout) Uint32() uint32

Uint32 convert layout in uint32.

type LineString added in v0.0.5

type LineString struct {
	SRID *SystemReferenceID
	CoordinateSet
}

LineString is a set of lines.

A LineString is a 1-dimensional line formed by a contiguous sequence of line segments. Each line segment is defined by two points, with the end point of one segment forming the start point of the next segment. An OGC-valid LineString has either zero or two or more points, but PostGIS also allows single-point LineStrings. LineStrings may cross themselves (self-intersect). A LineString is closed if the start and end points are the same. A LineString is simple if it does not self-intersect.

func (LineString) Layout added in v0.0.5

func (l LineString) Layout() Layout

Layout implements the Marshaler interface.

func (LineString) MarshalEWBK added in v0.0.5

func (l LineString) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (LineString) SystemReferenceID added in v0.0.5

func (l LineString) SystemReferenceID() *SystemReferenceID

SystemReferenceID implements the Marshaler interface.

func (LineString) Type added in v0.0.5

func (l LineString) Type() GeometryType

Type implements the Geometry interface.

func (*LineString) UnmarshalEWBK added in v0.0.5

func (l *LineString) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type Marshaler

type Marshaler interface {
	// MarshalEWBK must only generate the data part of the EWKB (not the header part).
	MarshalEWBK(binary.ByteOrder) ([]byte, error)

	// SystemReferenceID is the optional SRID.
	SystemReferenceID() *SystemReferenceID

	// Layout is the Layout used by the geometry.
	Layout() Layout

	// Type is the type of geometry.
	Type() GeometryType
}

Marshaler is the Geometry to byte array converter.

type MultiLineString added in v0.0.5

type MultiLineString struct {
	SRID        *SystemReferenceID
	LineStrings []LineString
}

MultiLineString is a MULTILINESTRING in database.

A MultiLineString is a collection of LineStrings. A MultiLineString is closed if each of its elements is closed.

func (MultiLineString) Layout added in v0.0.5

func (m MultiLineString) Layout() Layout

Layout implements the Marshaler interface.

func (MultiLineString) MarshalEWBK added in v0.0.5

func (m MultiLineString) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (MultiLineString) SystemReferenceID added in v0.0.5

func (m MultiLineString) SystemReferenceID() *SystemReferenceID

SystemReferenceID implements the Marshaler interface.

func (MultiLineString) Type added in v0.0.5

func (m MultiLineString) Type() GeometryType

Type implements the Geometry interface.

func (*MultiLineString) UnmarshalEWBK added in v0.0.5

func (m *MultiLineString) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type MultiPoint added in v0.0.2

type MultiPoint struct {
	SRID   *SystemReferenceID
	Points []Point
}

MultiPoint is a MULTIPOINT in database.

A MultiPoint is a collection of Points.

func (MultiPoint) Layout added in v0.0.2

func (m MultiPoint) Layout() Layout

Layout implements the Marshaler interface.

func (MultiPoint) MarshalEWBK added in v0.0.2

func (m MultiPoint) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (MultiPoint) SystemReferenceID added in v0.0.2

func (m MultiPoint) SystemReferenceID() *SystemReferenceID

SystemReferenceID implements the Marshaler interface.

func (MultiPoint) Type added in v0.0.2

func (m MultiPoint) Type() GeometryType

Type implements the Geometry interface.

func (*MultiPoint) UnmarshalEWBK added in v0.0.2

func (m *MultiPoint) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type MultiPolygon added in v0.0.6

type MultiPolygon struct {
	SRID     *SystemReferenceID
	Polygons []Polygon
}

MultiPolygon is a MULTILINESTRING in database.

A MultiPolygon is a collection of non-overlapping, non-adjacent Polygons. Polygons in the collection may touch only at a finite number of points.

func (MultiPolygon) Layout added in v0.0.6

func (m MultiPolygon) Layout() Layout

Layout implements the Marshaler interface.

func (MultiPolygon) MarshalEWBK added in v0.0.6

func (m MultiPolygon) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (MultiPolygon) SystemReferenceID added in v0.0.6

func (m MultiPolygon) SystemReferenceID() *SystemReferenceID

SystemReferenceID implements the Marshaler interface.

func (MultiPolygon) Type added in v0.0.6

func (m MultiPolygon) Type() GeometryType

Type implements the Geometry interface.

func (*MultiPolygon) UnmarshalEWBK added in v0.0.6

func (m *MultiPolygon) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type Point

type Point struct {
	SRID *SystemReferenceID
	Coordinate
}

Point is a POINT in database.

A Point is a 0-dimensional geometry that represents a single location in coordinate space.

func (Point) Layout added in v0.0.1

func (p Point) Layout() Layout

Layout implements the Marshaler interface.

func (Point) MarshalEWBK

func (p Point) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (Point) SystemReferenceID added in v0.0.1

func (p Point) SystemReferenceID() *SystemReferenceID

SystemReferenceID implements the Marshaler interface.

func (Point) Type

func (p Point) Type() GeometryType

Type implements the Geometry interface.

func (*Point) UnmarshalEWBK

func (p *Point) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type Polygon added in v0.0.1

type Polygon struct {
	SRID *SystemReferenceID
	CoordinateGroup
}

Polygon is a POLYGON in database.

A Polygon is a 2-dimensional planar region, delimited by an exterior boundary (the shell) and zero or more interior boundaries (holes). Each boundary is a LinearRing.

func (Polygon) Layout added in v0.0.1

func (p Polygon) Layout() Layout

Layout implements the Marshaler interface.

func (Polygon) MarshalEWBK added in v0.0.1

func (p Polygon) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (Polygon) SystemReferenceID added in v0.0.1

func (p Polygon) SystemReferenceID() *SystemReferenceID

SystemReferenceID implements the Marshaler interface.

func (Polygon) Type added in v0.0.1

func (p Polygon) Type() GeometryType

Type implements the Geometry interface.

func (*Polygon) UnmarshalEWBK added in v0.0.1

func (p *Polygon) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type SystemReferenceID

type SystemReferenceID uint32

SystemReferenceID is the identifier of the system reference for projection.

const (
	// SystemReferenceWGS84 stands for GCS WGS 84.
	SystemReferenceWGS84 SystemReferenceID = 4326

	// SystemReferenceUTMZone stands for UTM Zone 17N NAD 27.
	SystemReferenceUTMZone SystemReferenceID = 26717

	// SystemReferenceTennesseeZone stands for SPCS Tennessee Zone NAD 83.
	SystemReferenceTennesseeZone SystemReferenceID = 6576
)

func WithSRID

func WithSRID(srid SystemReferenceID) *SystemReferenceID

WithSRID converts SystemReferenceID to pointer.

type Triangle added in v0.0.7

type Triangle struct {
	SRID *SystemReferenceID
	CoordinateSet
}

Triangle is a TRIANGLE in database.

func (Triangle) Layout added in v0.0.7

func (t Triangle) Layout() Layout

Layout implements the Marshaler interface.

func (Triangle) MarshalEWBK added in v0.0.7

func (t Triangle) MarshalEWBK(byteOrder binary.ByteOrder) ([]byte, error)

MarshalEWBK implements the Marshaler interface.

func (Triangle) SystemReferenceID added in v0.0.7

func (t Triangle) SystemReferenceID() *SystemReferenceID

SystemReferenceID implements the Marshaler interface.

func (Triangle) Type added in v0.0.7

func (t Triangle) Type() GeometryType

Type implements the Geometry interface.

func (*Triangle) UnmarshalEWBK added in v0.0.7

func (t *Triangle) UnmarshalEWBK(record ExtendedWellKnownBytes) error

UnmarshalEWBK implements the Unmarshaler interface.

type Unmarshaler

type Unmarshaler interface {
	// UnmarshalEWBK is to extract Geometry information from the EWKB record.
	UnmarshalEWBK(ExtendedWellKnownBytes) error
}

Unmarshaler is the byte array to Geometry converter.

Jump to

Keyboard shortcuts

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