Documentation
¶
Overview ¶
Package postgis adds PostGIS support to gorm: geometry/geography column types backed by go-geom, spatial query clauses, and migration helpers.
Index ¶
- Constants
- func Area(col string) clause.Expression
- func AsGeoJSON(col string) clause.Expression
- func BBoxIntersects(col string, g geom.T) clause.Expression
- func Buffer(col string, meters float64) clause.Expression
- func CheckDrift(db *gorm.DB, models ...any) error
- func Contains(col string, g geom.T) clause.Expression
- func Covers(col string, g geom.T) clause.Expression
- func DWithin(col string, g geom.T, distance float64) clause.Expression
- func DWithinMeters(col string, g geom.T, meters float64) clause.Expression
- func Distance(col string, g geom.T) clause.Expression
- func DistanceMeters(col string, g geom.T) clause.Expression
- func EnsureExtension(db *gorm.DB) error
- func Envelope(minX, minY, maxX, maxY float64, srid int) *geom.Polygon
- func Intersects(col string, g geom.T) clause.Expression
- func MakeEnvelope(minX, minY, maxX, maxY float64, srid int) clause.Expression
- func New(config postgres.Config, opts ...Option) gorm.Dialector
- func Open(dsn string, opts ...Option) gorm.Dialector
- func Transform(col string, srid int) clause.Expression
- func Within(col string, g geom.T) clause.Expression
- func Wrap(d gorm.Dialector, opts ...Option) gorm.Dialector
- type Any
- type Dialector
- type Drift
- type Geography
- func (Geography[T]) GormDBDataType(_ *gorm.DB, field *schema.Field) string
- func (Geography[T]) GormDataType() string
- func (g Geography[T]) GormValue(_ context.Context, db *gorm.DB) clause.Expr
- func (g Geography[T]) MarshalJSON() ([]byte, error)
- func (g *Geography[T]) Scan(src any) error
- func (g *Geography[T]) UnmarshalJSON(data []byte) error
- func (g Geography[T]) Valid() bool
- func (g Geography[T]) Value() (driver.Value, error)
- type Geometry
- func (Geometry[T]) GormDBDataType(_ *gorm.DB, field *schema.Field) string
- func (Geometry[T]) GormDataType() string
- func (g Geometry[T]) GormValue(_ context.Context, db *gorm.DB) clause.Expr
- func (g Geometry[T]) MarshalJSON() ([]byte, error)
- func (g *Geometry[T]) Scan(src any) error
- func (g *Geometry[T]) UnmarshalJSON(data []byte) error
- func (g Geometry[T]) Valid() bool
- func (g Geometry[T]) Value() (driver.Value, error)
- type LineString
- type MultiLineString
- type MultiPoint
- type MultiPolygon
- type Option
- type Point
- type Polygon
Constants ¶
const DefaultSRID = 4326
DefaultSRID is the SRID used in column definitions when no `srid` tag is set.
Variables ¶
This section is empty.
Functions ¶
func Area ¶
func Area(col string) clause.Expression
Area returns the area of col in square meters (geography). The column must use a lon/lat SRID (e.g. 4326); a projected SRID such as 2154 is rejected by PostGIS.
func AsGeoJSON ¶
func AsGeoJSON(col string) clause.Expression
AsGeoJSON renders col as a GeoJSON string server-side, so an API can stream it without decoding the geometry in Go. Scan it into a string or json.RawMessage.
func BBoxIntersects ¶
func BBoxIntersects(col string, g geom.T) clause.Expression
BBoxIntersects: col && g (bounding boxes overlap, index-only).
func Buffer ¶
func Buffer(col string, meters float64) clause.Expression
Buffer returns col buffered by the given number of meters, computed on geography (a geometry buffer would be in degrees for SRID 4326) and cast back to geometry. The column must use a lon/lat SRID (e.g. 4326).
func CheckDrift ¶
CheckDrift compares the geometry/geography columns of the given models with the database, read-only, and returns the first mismatch as an error. Columns of tables that do not exist yet are skipped.
func Contains ¶
func Contains(col string, g geom.T) clause.Expression
Contains: ST_Contains(col, g) — col contains g.
func Covers ¶
func Covers(col string, g geom.T) clause.Expression
Covers: ST_Covers(col, g) — no point of g lies outside col (unlike Contains, boundary points count).
func DWithin ¶
DWithin: ST_DWithin(col, g, distance) with distance in the units of the SRID (degrees for 4326). For meters, use DWithinMeters.
func DWithinMeters ¶
DWithinMeters: ST_DWithin on geography, distance in meters. Works on geometry (cast to geography, so SRID must be geographic) and geography columns. On a geometry column the cast defeats a plain GIST index: index the expression (`(loc::geography)`) or store a geography column.
func Distance ¶
func Distance(col string, g geom.T) clause.Expression
Distance: ST_Distance(col, g), in SRID units. Usable in ORDER BY via db.Clauses(clause.OrderBy{Expression: postgis.Distance(...)}).
func DistanceMeters ¶
func DistanceMeters(col string, g geom.T) clause.Expression
DistanceMeters: ST_Distance on geography, in meters.
func EnsureExtension ¶
EnsureExtension runs CREATE EXTENSION IF NOT EXISTS postgis.
func Envelope ¶
Envelope returns the rectangle (minX,minY)-(maxX,maxY) as a closed polygon with the given SRID.
func Intersects ¶
func Intersects(col string, g geom.T) clause.Expression
Intersects: ST_Intersects(col, g).
func MakeEnvelope ¶
func MakeEnvelope(minX, minY, maxX, maxY float64, srid int) clause.Expression
MakeEnvelope is the server-side rectangle ST_MakeEnvelope(minX, minY, maxX, maxY, srid). Prefer Envelope when the rectangle is passed as a geometry argument to another clause.
func Transform ¶
func Transform(col string, srid int) clause.Expression
Transform reprojects a geometry column to the given SRID (e.g. a metric projection for measurements). It does not accept geography columns.
Types ¶
type Dialector ¶
Dialector is the gorm postgres dialector with a PostGIS-aware migrator. Everything else (error translation, ...) is promoted from the embedded dialector.
type Drift ¶
type Drift int
Drift tells AutoMigrate what to do when an existing geometry/geography column does not have the type declared by the model (geometry type, dimension, SRID).
const ( // DriftError fails the migration with a descriptive error (default). DriftError Drift = iota // DriftAlter runs ALTER COLUMN ... TYPE ... USING ST_SetSRID(col, srid) when only the SRID // differs on a geometry column (this relabels coordinates, it does not reproject them); // any other difference is still an error. DriftAlter // DriftIgnore keeps the previous behaviour: differences are left alone silently. DriftIgnore )
type Geography ¶
Geography is a PostGIS `geography` column holding a T (WGS84 lon/lat, distances in meters). A nil G is stored as NULL.
func (Geography[T]) GormDBDataType ¶
GormDBDataType implements migrator.GormDataTypeInterface.
func (Geography[T]) GormDataType ¶
GormDataType implements schema.GormDataTypeInterface.
func (Geography[T]) MarshalJSON ¶
MarshalJSON encodes the geography as GeoJSON (null when NULL).
func (*Geography[T]) UnmarshalJSON ¶
UnmarshalJSON decodes GeoJSON into the geography.
type Geometry ¶
Geometry is a PostGIS `geometry` column holding a T. A nil G is stored as NULL.
Column definition is taken from T and from the optional field tags `srid:4326` and `geomtype:PointZ`, e.g. `gorm:"srid:2154;index:,type:gist"`.
func (Geometry[T]) GormDBDataType ¶
GormDBDataType implements migrator.GormDataTypeInterface.
func (Geometry[T]) GormDataType ¶
GormDataType implements schema.GormDataTypeInterface.
func (Geometry[T]) GormValue ¶
GormValue makes gorm send the value through ST_GeomFromEWKB, so the server always receives a typed geometry rather than an untyped bytea.
func (Geometry[T]) MarshalJSON ¶
MarshalJSON encodes the geometry as GeoJSON (null when NULL).
func (*Geometry[T]) UnmarshalJSON ¶
UnmarshalJSON decodes GeoJSON into the geometry.
type LineString ¶
type LineString = Geometry[*geom.LineString]
Convenience aliases for the common geometry types.
type MultiLineString ¶
type MultiLineString = Geometry[*geom.MultiLineString]
Convenience aliases for the common geometry types.
type MultiPoint ¶
type MultiPoint = Geometry[*geom.MultiPoint]
Convenience aliases for the common geometry types.
type MultiPolygon ¶
type MultiPolygon = Geometry[*geom.MultiPolygon]
Convenience aliases for the common geometry types.