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() map[string]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) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (point Point) MarshalJSON() ([]byte, error)
- func (point Point) MarshalStruct() GeoJSONPoint
- func (point *Point) UnmarshalBSON(data []byte) error
- func (point *Point) UnmarshalBSONValue(dataType bsontype.Type, 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
Examples ¶
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 Mobilizon
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) JSONLD ¶ added in v0.0.2
JSONLD returns a JSON-LD representation of this object
Example ¶
JSONLD emits a schema.org Place, omitting every field that is empty.
address := Address{
Name: "Santa Monica Pier",
Street1: "200 Santa Monica Pier",
Locality: "Santa Monica",
Region: "CA",
}
data, _ := json.Marshal(address.JSONLD())
fmt.Println(string(data))
Output: {"address":{"locality":"Santa Monica","region":"CA","street1":"200 Santa Monica Pier"},"name":"Santa Monica Pier","type":"Place"}
func (Address) LatLon ¶ added in v0.0.3
LatLon returns the coordinates as a "latitude,longitude" string.
Example ¶
address := Address{Latitude: 34.02116, Longitude: -118.40853}
fmt.Println(address.LatLon())
Output: 34.0211600000,-118.4085300000
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.
func (*Address) SetString ¶ added in v0.0.2
SetString sets the "name" or "formatted" property and returns TRUE if the property name is writable. Changing "formatted" resets any geocoded fields.
Example ¶
Setting "formatted" to a new value clears every geocoded field, because a new formatted address invalidates the old geocode.
address := Address{
Formatted: "123 Main St",
Locality: "Springfield",
Latitude: 39.7817,
}
address.SetString(AddressPropertyFormatted, "456 Oak Ave")
fmt.Println(address.Formatted)
fmt.Printf("%q %v\n", address.Locality, address.Latitude)
Output: 456 Oak Ave "" 0
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. It 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 NewPoint ¶
NewPoint returns a Point at the given longitude and latitude (no altitude).
Example ¶
point := NewPoint(-118.40853, 34.02116) data, _ := json.Marshal(point) fmt.Println(string(data))
Output: {"type":"Point","coordinates":[-118.40853,34.02116]}
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.
Example ¶
LonLat and LatLon exist because consumers disagree about coordinate order.
point := NewPoint(-118.40853, 34.02116) fmt.Println(point.LonLat()) fmt.Println(point.LatLon())
Output: -118.4085300000,34.0211600000 34.0211600000,-118.4085300000
func (Point) MarshalBSON ¶
MarshalBSON serializes this Point into a GeoJSON object. It implements bson.Marshaler, for callers that marshal a Point on its own.
func (Point) MarshalBSONValue ¶ added in v0.3.0
MarshalBSONValue serializes this Point into a GeoJSON object. It implements bson.ValueMarshaler.
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`
Example (Zero) ¶
A zero Point marshals to null, so it disappears from a field tagged `json:",omitzero"`.
data, _ := json.Marshal(Point{})
fmt.Println(string(data))
Output: null
func (Point) MarshalStruct ¶
func (point Point) MarshalStruct() GeoJSONPoint
MarshalStruct returns this Point as a strongly-typed GeoJSONPoint.
func (*Point) UnmarshalBSON ¶
UnmarshalBSON deserializes a GeoJSON object into this Point. It implements bson.Unmarshaler, for callers that unmarshal a Point on its own.
func (*Point) UnmarshalBSONValue ¶ added in v0.3.0
UnmarshalBSONValue deserializes a GeoJSON object, or a bare coordinate array, into this Point. It implements bson.ValueUnmarshaler.
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.6
func NewPolygon ¶
NewPolygon returns a Polygon made up of the given positions.
Example ¶
polygon := NewPolygon( NewPosition(-118.5, 34.0), NewPosition(-118.0, 34.0), NewPosition(-118.0, 34.5), ) data, _ := json.Marshal(polygon) fmt.Println(string(data))
Output: {"type":"Polygon","coordinates":[[[-118.5,34],[-118,34],[-118,34.5]]]}
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.
Example ¶
Unparseable values and an unpaired trailing coordinate are silently dropped.
polygon := NewPolygonFromString("-118.5,34.0,-118.0,34.0")
fmt.Println(polygon.String())
Output: -118.5,34,-118,34
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 coordinate, with an optional altitude 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).
Example ¶
Coordinates are longitude-first, per the GeoJSON specification -- not the "latitude, longitude" order that people usually say aloud.
position := NewPosition(-118.40853, 34.02116) fmt.Println(position.String())
Output: -118.40853,34.02116
func NewPositionWithAltitude ¶
NewPositionWithAltitude returns a Position at the given longitude, latitude, and altitude.
Example ¶
position := NewPositionWithAltitude(-118.40853, 34.02116, 38.4) data, _ := json.Marshal(position) fmt.Println(string(data))
Output: [-118.40853,34.02116,38.4]
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
Example ¶
A Position marshals as a bare GeoJSON coordinate array, not an object.
data, _ := json.Marshal(NewPosition(1, 2)) fmt.Println(string(data))
Output: [1,2]
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.