Documentation
¶
Overview ¶
Package geo provides geographic value types — Position, Point, Polygon, and Address — along with JSON and BSON (GeoJSON) marshalling for each.
Position, Point, and Polygon follow the GeoJSON specification (https://datatracker.ietf.org/doc/html/rfc7946), while Address models a human-readable postal address with optional geocoded coordinates.
Index ¶
- Constants
- func AddressSchema() schema.Element
- type Address
- func (address Address) GeoJSON() mapof.Any
- func (address Address) GeoPoint() Point
- func (address Address) GetFloat(name string) float64
- func (address Address) GetFloatOK(name string) (float64, bool)
- func (address Address) GetString(name string) string
- func (address Address) GetStringOK(name string) (string, bool)
- func (address Address) HasAddress() bool
- func (address Address) HasGeocode() bool
- func (address Address) IsZero() bool
- func (address Address) JSONLD() mapof.Any
- func (address Address) LatLon() string
- func (address Address) LonLat() string
- func (address Address) MarshalMap() mapof.Any
- func (address Address) NotZero() bool
- func (address *Address) Reset()
- func (address *Address) SetFloat(name string, value float64) bool
- func (address *Address) SetPoint(point Point)
- func (address *Address) SetString(name string, value string) bool
- func (address *Address) UnmarshalMap(value mapof.Any) error
- type GeoJSONPoint
- type GeoJSONPolygon
- type Point
- func (point Point) GeoJSON() map[string]any
- func (point Point) LatLon() string
- func (point Point) LonLat() string
- func (point Point) MarshalBSON() ([]byte, error)
- func (point Point) MarshalJSON() ([]byte, error)
- func (point Point) MarshalStruct() GeoJSONPoint
- func (point *Point) UnmarshalBSON(data []byte) error
- func (point *Point) UnmarshalJSON(data []byte) error
- func (point *Point) UnmarshalMap(data mapof.Any) error
- type Polygon
- func (polygon Polygon) GeoJSON() map[string]any
- func (polygon Polygon) IsZero() bool
- func (polygon Polygon) MarshalBSON() ([]byte, error)
- func (polygon Polygon) MarshalJSON() ([]byte, error)
- func (polygon Polygon) MarshalSlice() [][]float64
- func (polygon Polygon) MarshalStruct() GeoJSONPolygon
- func (polygon Polygon) NotZero() bool
- func (polygon Polygon) String() string
- func (polygon *Polygon) UnmarshalBSON(data []byte) error
- func (polygon *Polygon) UnmarshalJSON(data []byte) error
- func (polygon *Polygon) UnmarshalStruct(data GeoJSONPolygon) error
- type Position
- func (position Position) IsZero() bool
- func (position Position) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (position Position) MarshalJSON() ([]byte, error)
- func (position Position) MarshalSlice() []float64
- func (position Position) NotZero() bool
- func (position Position) String() string
- func (position *Position) UnmarshalBSONValue(dataType bsontype.Type, data []byte) error
- func (position *Position) UnmarshalJSON(data []byte) error
- func (position *Position) UnmarshalSlice(coordinates sliceof.Float) error
Constants ¶
const ( // AddressPropertyName is the human-readable name of the address. AddressPropertyName = "name" // AddressPropertyFormatted is the full, unparsed value of the address. AddressPropertyFormatted = "formatted" // AddressPropertyStreet1 is the first line of the street address. AddressPropertyStreet1 = "street1" // AddressPropertyStreet2 is the second line of the street address. AddressPropertyStreet2 = "street2" // AddressPropertyLocality is the city or town of the address. AddressPropertyLocality = "locality" // AddressPropertyRegion is the state or province of the address. AddressPropertyRegion = "region" // AddressPropertyPostalCode is the postal code of the address. AddressPropertyPostalCode = "postalCode" // AddressPropertyCountry is the country of the address. AddressPropertyCountry = "country" // AddressPropertyLongitude is the longitude of the address. AddressPropertyLongitude = "longitude" // AddressPropertyLatitude is the latitude of the address. AddressPropertyLatitude = "latitude" // AddressPropertyTimezone is the IANA time zone name of the address. AddressPropertyTimezone = "timezone" // AddressPropertyPlusCode is the Google Plus Code of the address. AddressPropertyPlusCode = "plusCode" )
Property names used to read, write, and marshal the fields of an Address.
const ( // PropertyType is the GeoJSON "type" property. PropertyType = "type" // PropertyCoordinates is the GeoJSON "coordinates" property. PropertyCoordinates = "coordinates" )
GeoJSON property names used in marshalled output.
const ( // PropertyTypePoint is the GeoJSON type value for a Point. PropertyTypePoint = "Point" // PropertyTypePolygon is the GeoJSON type value for a Polygon. PropertyTypePolygon = "Polygon" )
GeoJSON "type" values supported by this package.
Variables ¶
This section is empty.
Functions ¶
func AddressSchema ¶ added in v0.0.2
AddressSchema returns the rosetta schema that describes an Address.
Types ¶
type Address ¶ added in v0.0.2
type Address struct {
Name string `json:"name" bson:"name,omitempty"` // Human-readable name of the address
Formatted string `json:"formatted" bson:"formatted,omitempty"` // Full, unparsed value of the address
Street1 string `json:"street1" bson:"street1,omitempty"` // Parsed street address line 1 of the address
Street2 string `json:"street2" bson:"street2,omitempty"` // Parsed street address line 2 of the address
Locality string `json:"locality" bson:"locality,omitempty"` // Parsed city or town of the address
Region string `json:"region" bson:"region,omitempty"` // Parsed state or province of the address
PostalCode string `json:"postalCode" bson:"postalCode,omitempty"` // Parsed postal code of the address
Country string `json:"country" bson:"country,omitempty"` // Parsed country of the address
PlusCode string `json:"plusCode" bson:"plusCode,omitempty"` // PlusCode for this location https://maps.google.com/pluscodes/
Timezone string `json:"timezone" bson:"timezone,omitempty"` // Time zone in tzdatabase format (https://en.wikipedia.org/wiki/Tz_database)
Latitude float64 `json:"latitude" bson:"latitude,omitempty"` // Latitude of the address
Longitude float64 `json:"longitude" bson:"longitude,omitempty"` // Longitude of the address
}
Address represents a physical address on the planet It maps to https://www.w3.org/TR/activitystreams-vocabulary/#dfn-address and uses https://schema.org/PostalAddress to match Mobilizion
func (Address) GeoJSON ¶ added in v0.0.2
GeoJSON returns this Address as a GeoJSON Point object. https://www.mongodb.com/docs/manual/reference/geojson/
func (Address) GetFloat ¶ added in v0.0.2
GetFloat returns the named property as a float64, or 0 if it is not a float property.
func (Address) GetFloatOK ¶ added in v0.1.0
GetFloatOK returns the named property as a float64, and a boolean that is TRUE when the property name is recognized ("latitude" or "longitude").
func (Address) GetString ¶ added in v0.0.3
GetString returns the named property as a string, or "" if it is not a string property.
func (Address) GetStringOK ¶ added in v0.0.2
GetStringOK returns the named property as a string, and a boolean that is TRUE when the property name is recognized.
func (Address) HasAddress ¶ added in v0.0.2
HasAddress returns TRUE if this Address has ANY street address information
func (Address) HasGeocode ¶ added in v0.0.2
HasGeocode returns TRUE if this Address has ANY Lat/Long information
func (Address) IsZero ¶ added in v0.0.2
IsZero returns TRUE if this Address has no coordinates and no formatted value.
func (Address) LatLon ¶ added in v0.0.3
LatLon returns the coordinates as a "latitude,longitude" string.
func (Address) LonLat ¶ added in v0.0.3
LonLat returns the coordinates as a "longitude,latitude" string.
func (Address) MarshalMap ¶ added in v0.0.2
MarshalMap returns this Address as a mapof.Any keyed by its property names.
func (*Address) Reset ¶ added in v0.0.2
func (address *Address) Reset()
Reset clears all geocoding information from this Address
func (*Address) SetFloat ¶ added in v0.0.2
SetFloat sets the "latitude" or "longitude" property and returns TRUE if the property name is writable.
func (*Address) SetPoint ¶ added in v0.0.2
SetPoint copies the longitude and latitude from the given Point into this Address.
type GeoJSONPoint ¶
type GeoJSONPoint struct {
Type string `json:"type" bson:"type"` // This should always be "Point"
Coordinates []float64 `json:"coordinates" bson:"coordinates"` // Whatevs
}
GeoJSONPoint represents the "strict" format for a Point in GeoJSON
type GeoJSONPolygon ¶
type GeoJSONPolygon struct {
Type string `json:"type" bson:"type"` // this should always be "Polygon"
Coordinates [][][]float64 `json:"coordinates" bson:"coordinates"` // ick. Thanks IETF.
}
GeoJSONPolygon represents the "strict" format for a Polygon in GeoJSON. is is used here to simplify conversion to/from serialization formats
type Point ¶
type Point struct {
Position
}
Point represents a GeoJSON "Point" object https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.2
func NewPointWithAltitude ¶
NewPointWithAltitude returns a Point at the given longitude, latitude, and altitude.
func (Point) LatLon ¶ added in v0.0.3
LatLon returns the coordinates as a "latitude,longitude" string.
func (Point) LonLat ¶ added in v0.0.3
LonLat returns the coordinates as a "longitude,latitude" string.
func (Point) MarshalBSON ¶
MarshalBSON is a custom BSON marshaller that serializes this Point into a GeoJSON object.
func (Point) MarshalJSON ¶
MarshalJSON is a custom json.Marshaller that returns this Point as a GeoJSON object. This marshaller works with `omitzero` but not `omitempty`
func (Point) MarshalStruct ¶
func (point Point) MarshalStruct() GeoJSONPoint
MarshalStruct returns this Point as a strongly-typed GeoJSONPoint.
func (*Point) UnmarshalBSON ¶
UnmarshalBSON is a custom BSON unmarshaller that deserializes a GeoJSON object into this Point structure.
func (*Point) UnmarshalJSON ¶
UnmarshalJSON is a custom json.Unmarshaller that parses a GeoJSON object into this Point object.
type Polygon ¶
Polygon represents a GeoJSON "Polygon" object https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.2
func NewPolygon ¶
NewPolygon returns a Polygon made up of the given positions.
func NewPolygonFromString ¶
NewPolygonFromString parses a comma-delimited list of coordinates ("lon,lat,lon,lat,...") into a Polygon. Unparseable values and an unpaired trailing coordinate are silently dropped.
func (Polygon) MarshalBSON ¶
MarshalBSON is a custom BSON marshaller that serializes this Polygon into a GeoJSON object.
func (Polygon) MarshalJSON ¶
MarshalJSON is a custom json.Marshaller that returns this Polygon as a GeoJSON object.
func (Polygon) MarshalSlice ¶
MarshalSlice returns (a slice of (a slice of floats)), which is the standard way of representing a GeoJSON polygon
func (Polygon) MarshalStruct ¶
func (polygon Polygon) MarshalStruct() GeoJSONPolygon
MarshalStruct returns this Polygon as a strongly-typed GeoJSONPolygon.
func (Polygon) String ¶
String returns the coordinates as a comma-delimited "lon,lat,lon,lat,..." string.
func (*Polygon) UnmarshalBSON ¶
UnmarshalBSON is a custom BSON unmarshaller that deserializes a GeoJSON object into this Polygon structure.
func (*Polygon) UnmarshalJSON ¶
UnmarshalJSON is a custom json.Unmarshaller that parses a GeoJSON object into this Polygon object.
func (*Polygon) UnmarshalStruct ¶
func (polygon *Polygon) UnmarshalStruct(data GeoJSONPolygon) error
UnmarshalStruct populates this Polygon from a strongly-typed GeoJSONPolygon, which must contain exactly one ring of coordinates.
type Position ¶
Position represents a Longitude/Latitude pair https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.1
func NewPosition ¶
NewPosition returns a Position at the given longitude and latitude (no altitude).
func NewPositionWithAltitude ¶
NewPositionWithAltitude returns a Position at the given longitude, latitude, and altitude.
func (Position) MarshalBSONValue ¶ added in v0.1.0
MarshalBSONValue is a custom BSON marshaller that serializes this Position into a GeoJSON coordinate pair. It implements bson.ValueMarshaler (rather than bson.Marshaler) because a coordinate pair is a BSON array, not a document.
func (Position) MarshalJSON ¶
MarshalJSON is a custom JSON marshaller that serializes this Position into a GeoJSON coordinate pair
func (Position) MarshalSlice ¶
MarshalSlice returns a longitude/latitude coordinate pair
func (*Position) UnmarshalBSONValue ¶ added in v0.1.0
UnmarshalBSONValue is a custom BSON unmarshaller that deserializes a BSON / GeoJSON coordinate pair into this Position structure. It implements bson.ValueUnmarshaler to match MarshalBSONValue's array encoding.
func (*Position) UnmarshalJSON ¶
UnmarshalJSON is a custom JSON unmarshaller that deserializes a GeoJSON coordinate pair into this Position structure.