postgis

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 9 Imported by: 0

README

sqlkit/postgis

sqlkit/postgis provides a small database/sql-compatible PostGIS Point value and inspectable spatial statements. Coordinates use X=longitude and Y=latitude; valid values retain an explicit SRID in EWKB.

point, _ := postgis.NewWGS84Point(37.4979, 127.0276)
stmt, _ := postgis.InsertPoint("places", "location", point)
_, err := stmt.Exec(ctx, db)

CreateSpatialTableSQL emits a geometry(Point, srid) NOT NULL column and CreateSpatialIndexSQL emits a GIST index. WithinDistance uses the indexed ST_DWithin predicate for geometries with matching SRID; WithinBounds uses the bounding-box operator. Scan accepts EWKB/WKB/WKT and clears the receiver on failure.

The package owns neither a database handle nor migrations, projections, ORM state, or a cross-dialect abstraction. The local integration test uses the digest-pinned PostGIS fixture in testcontainers/postgis.

go test -p 1 -count=1 -timeout=10m ./testcontainers/postgis ./sqlkit/postgis
go test -race -count=1 ./sqlkit/postgis

Documentation

Overview

Package postgis PostgreSQL PostGIS의 좁은 Point 값과 database/sql용 SQL helper를 제공한다.

이 패키지는 ORM, migration manager, 다른 데이터베이스 dialect를 소유하지 않는다. 공간 연산의 단위와 연결 수명은 호출자가 관리한다.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPoint 좌표, EWKB 또는 WKT Point 값이 유효하지 않을 때 반환된다.
	ErrInvalidPoint = errors.New("postgis: invalid point")

	// ErrInvalidSRID SRID가 음수이거나 지원 범위를 벗어날 때 반환된다.
	ErrInvalidSRID = errors.New("postgis: invalid SRID")

	// ErrInvalidArgument SQL helper 인자가 비어 있거나 안전하지 않을 때 반환된다.
	ErrInvalidArgument = errors.New("postgis: invalid argument")

	// ErrInvalidDistance 거리 값이 유한하지 않거나 음수일 때 반환된다.
	ErrInvalidDistance = errors.New("postgis: invalid distance")
)

Functions

func CreateExtensionSQL

func CreateExtensionSQL() string

CreateExtensionSQL 는 PostGIS extension을 명시적으로 활성화하는 SQL을 반환한다.

func CreateSpatialIndexSQL

func CreateSpatialIndexSQL(table, column string) (string, error)

CreateSpatialIndexSQL 는 PostGIS geometry column에 대한 GIST index DDL을 만든다.

func CreateSpatialTableSQL

func CreateSpatialTableSQL(table, column string, srid int) (string, error)

CreateSpatialTableSQL 는 SRID 제약이 있는 PostGIS Point 테이블 DDL을 만든다.

func InsertPoint

func InsertPoint(table, column string, point Point) (sqlkit.Statement, error)

InsertPoint 는 EWKB를 bind argument로 사용하는 point insert statement를 만든다.

func SelectPointSQL

func SelectPointSQL(table, column string) (string, error)

SelectPointSQL 는 geometry column의 WKB와 SRID를 함께 읽는 SQL statement를 만든다.

func WithinBounds

func WithinBounds(table, column string, minX, minY, maxX, maxY float64, srid int) (sqlkit.Statement, error)

WithinBounds 는 inclusive bounding box predicate statement를 만든다.

func WithinDistance

func WithinDistance(table, column string, center Point, distance float64) (sqlkit.Statement, error)

WithinDistance 는 SRID가 같은 geometry에서 거리 단위의 indexed predicate를 만든다.

Example
package main

import (
	"context"
	"database/sql"

	"github.com/bluetape4k/bluetape-go/sqlkit/postgis"
)

func main() {
	ctx := context.Background()
	var db *sql.DB
	point, _ := postgis.NewPoint(127.0276, 37.4979, 4326)
	stmt, _ := postgis.WithinDistance("places", "location", point, 500)
	if db != nil {
		_, _ = db.ExecContext(ctx, stmt.SQL, stmt.Args...)
	}
}

Types

type Point

type Point struct {
	X     float64
	Y     float64
	SRID  int
	Valid bool
}

Point 는 WGS84 경도(X)·위도(Y)와 SRID를 보관하는 유효한 공간 값이다.

X는 -180..180, Y는 -90..90의 degree 값이다. zero value는 SQL NULL을 나타내며, Valid가 true일 때만 Value가 non-NULL EWKB를 반환한다.

Example
package main

import (
	"github.com/bluetape4k/bluetape-go/sqlkit/postgis"
)

func main() {
	point, err := postgis.NewPoint(127.0276, 37.4979, 4326)
	if err != nil {
		panic(err)
	}
	value, err := point.Value()
	if err != nil {
		panic(err)
	}
	_ = value
}

func NewPoint

func NewPoint(x, y float64, srid int) (Point, error)

NewPoint 는 경도(X), 위도(Y), SRID를 검증한 유효한 Point를 생성한다.

func NewWGS84Point

func NewWGS84Point(latitude, longitude float64) (Point, error)

NewWGS84Point 는 위도와 경도를 받아 SRID 4326 Point를 생성한다.

func ParseEWKB

func ParseEWKB(raw []byte) (Point, error)

ParseEWKB 는 EWKB 또는 SRID 없는 WKB Point를 검증해 반환한다.

func (Point) MarshalEWKB

func (p Point) MarshalEWKB() ([]byte, error)

MarshalEWKB 는 Point를 호출자가 소유하는 새 EWKB byte slice로 인코딩한다.

func (*Point) Scan

func (p *Point) Scan(src any) error

Scan 은 nil, EWKB/WKB byte slice 또는 POINT WKT를 Point로 decode한다.

Scan은 decode 성공 후에만 값을 공개하며, 실패하면 receiver를 zero value로 되돌린다. driver가 소유한 byte slice는 내부에 보관하지 않는다.

func (Point) Value

func (p Point) Value() (driver.Value, error)

Value 는 유효한 Point를 SRID가 포함된 little-endian EWKB로 반환한다.

Jump to

Keyboard shortcuts

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