postgis

command
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: MIT Imports: 9 Imported by: 0

README

PostGIS example

This example demonstrates:

  • Connecting to a PostgreSQL/PostGIS database.
  • Importing data in GeoJSON format and storing it in the database.
  • Exporting data from the database and converting it to GeoJSON.

Quick start

Change to this directory:

$ cd ${GOPATH}/src/github.com/twpayne/go-geos/examples/postgis

Create a database called geomtest:

$ createdb geomtest

Save the data source name in an environment variable, for example:

$ DSN="postgres://username:password@localhost/geomtest?binary_parameters=yes&sslmode=disable"

Create the database schema, including the PostGIS extension and a table with a geometry column:

$ go run . -dsn $DSN -create

Populate the database using pq.CopyIn:

$ go run . -dsn $DSN -populate

Write data from the database in GeoJSON format:

$ go run . -dsn $DSN -write
{"id":1,"name":"London","geometry":{"type":"Point","coordinates":[0.1275,51.50722]}}
{"id":2,"name":"Berlin","geometry":{"type":"Point","coordinates":[13.405,52.52]}}

Import new data into the database in GeoJSON format:

$ echo '{"name":"Paris","geometry":{"type":"Point","coordinates":[2.3508,48.8567]}}' | go run . -dsn $DSN -read

Verify that the data was imported:

$ go run . -dsn $DSN -write
{"id":1,"name":"London","geometry":{"type":"Point","coordinates":[0.1275,51.50722]}}
{"id":2,"name":"Berlin","geometry":{"type":"Point","coordinates":[13.405,52.52]}}
{"id":3,"name":"Paris","geometry":{"type":"Point","coordinates":[2.3508,48.8567]}}

Delete the database:

$ dropdb geomtest

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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