Skip to content

Commit 4d2a19f

Browse files
authored
docs: Add instructions for PostGIS/GEOS (#3182)
1 parent 8286a65 commit 4d2a19f

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

docs/reference/datatypes.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,71 @@ type Book struct {
208208

209209
### PostGIS
210210

211+
#### Using `github.com/twpayne/go-geos` (pgx/v5 only)
212+
213+
sqlc can be configured to use the [geos](https://github.com/twpayne/go-geos)
214+
package for working with PostGIS geometry types in [GEOS](https://libgeos.org/).
215+
216+
There are three steps:
217+
218+
1. Configure sqlc to use `*github.com/twpayne/go-geos.Geom` for geometry types.
219+
2. Call `github.com/twpayne/pgx-geos.Register` on each
220+
`*github.com/jackc/pgx/v5.Conn`.
221+
3. Annotate your SQL with `::geometry` typecasts, if needed.
222+
223+
```sql
224+
-- Multipolygons in British National Grid (epsg:27700)
225+
create table shapes(
226+
id serial,
227+
name varchar,
228+
geom geometry(Multipolygon, 27700)
229+
);
230+
231+
-- name: GetCentroids :many
232+
SELECT id, name, ST_Centriod(geom)::geometry FROM shapes;
233+
```
234+
235+
```json
236+
{
237+
"version": 2,
238+
"gen": {
239+
"go": {
240+
"overrides": [
241+
{
242+
"db_type": "geometry",
243+
"go_type": {
244+
"import": "github.com/twpayne/go-geos",
245+
"package": "geos",
246+
"pointer": true,
247+
"type": "Geom"
248+
},
249+
"nullable": true
250+
}
251+
]
252+
}
253+
}
254+
}
255+
```
256+
257+
```go
258+
import (
259+
"github.com/twpayne/go-geos"
260+
pgxgeos "github.com/twpayne/pgx-geos"
261+
)
262+
263+
// ...
264+
265+
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
266+
if err := pgxgeos.Register(ctx, conn, geos.NewContext()); err != nil {
267+
return err
268+
}
269+
return nil
270+
}
271+
```
272+
273+
274+
#### Using `github.com/twpayne/go-geom`
275+
211276
sqlc can be configured to use the [geom](https://github.com/twpayne/go-geom)
212277
package for working with PostGIS geometry types.
213278

0 commit comments

Comments
 (0)