Skip to content

Commit

Permalink
add postgres+postgis support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed May 29, 2022
1 parent f211e46 commit 2ed97ee
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 23 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lib/pq v1.10.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
Expand Down
11 changes: 10 additions & 1 deletion mada/commune.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"os"

"github.com/blevesearch/bleve/v2"
"github.com/twpayne/go-geom"
Expand All @@ -24,6 +25,14 @@ type CommuneService struct {
}

func NewCommuneService() *CommuneService {
if os.Getenv("MADA_POSTGRES_URL") != "" {
db, err := sql.Open("postgres", os.Getenv("MADA_POSTGRES_URL"))
if err != nil {
panic(err)
}
return &CommuneService{db: db}
}

db, err := OpenSQLiteConnection()

if err != nil {
Expand Down Expand Up @@ -60,7 +69,7 @@ func (c *CommuneService) List(outputInJSON bool, limit int) {
}

func (c *CommuneService) ShowCommune(id string, outputInJSON bool) {
rows, _ := c.db.Query("SELECT uid, name, region, district, country, ST_AsText(geom) FROM commune WHERE uid = ?", id)
rows, _ := c.db.Query("SELECT uid, name, region, district, country, ST_AsText(geom) FROM commune WHERE uid = $1", id)
defer rows.Close()
var uid, name, region, district, country, g string
rows.Next()
Expand Down
11 changes: 10 additions & 1 deletion mada/district.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"os"

"github.com/blevesearch/bleve/v2"
"github.com/twpayne/go-geom"
Expand All @@ -23,6 +24,14 @@ type DistrictService struct {
}

func NewDistrictService() *DistrictService {
if os.Getenv("MADA_POSTGRES_URL") != "" {
db, err := sql.Open("postgres", os.Getenv("MADA_POSTGRES_URL"))
if err != nil {
panic(err)
}
return &DistrictService{db: db}
}

db, err := OpenSQLiteConnection()

if err != nil {
Expand Down Expand Up @@ -59,7 +68,7 @@ func (d *DistrictService) List(outputInJSON bool, limit int) {
}

func (d *DistrictService) ShowDistrict(id string, outputInJSON bool) {
rows, _ := d.db.Query("SELECT uid, name, region, ST_AsText(geom) FROM district WHERE uid = ?", id)
rows, _ := d.db.Query("SELECT uid, name, region, ST_AsText(geom) FROM district WHERE uid = $1", id)
defer rows.Close()
var uid, name, region, g string
rows.Next()
Expand Down
15 changes: 14 additions & 1 deletion mada/fokontany.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"database/sql"
"encoding/json"
"fmt"
"log"
"os"

"github.com/blevesearch/bleve/v2"
"github.com/twpayne/go-geom"
Expand All @@ -25,6 +27,14 @@ type FokontanyService struct {
}

func NewFokontanyService() *FokontanyService {
if os.Getenv("MADA_POSTGRES_URL") != "" {
db, err := sql.Open("postgres", os.Getenv("MADA_POSTGRES_URL"))
if err != nil {
panic(err)
}
return &FokontanyService{db: db}
}

db, err := OpenSQLiteConnection()

if err != nil {
Expand Down Expand Up @@ -61,7 +71,10 @@ func (f *FokontanyService) List(outputInJSON bool, limit int) {
}

func (f *FokontanyService) ShowFokontany(id string, outputInJSON bool) {
rows, _ := f.db.Query("SELECT uid, name, commune, region, district, country, ST_AsText(geom) FROM fokontany WHERE uid = ?", id)
rows, err := f.db.Query("SELECT uid, name, commune, region, district, country, ST_AsText(geom) FROM fokontany WHERE uid = $1", id)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var uid, name, commune, district, region, country, g string
rows.Next()
Expand Down
86 changes: 67 additions & 19 deletions mada/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/blevesearch/bleve/v2"
"github.com/everystreet/go-shapefile"
_ "github.com/lib/pq"
"github.com/mattn/go-sqlite3"
"github.com/mitchellh/go-homedir"
"github.com/twpayne/go-geom"
Expand Down Expand Up @@ -70,18 +71,30 @@ func Init() (bleve.Index, error) {
log.Fatal(err)
}

db, err := OpenSQLiteConnection()
var db *sql.DB

if err != nil {
log.Fatal(err)
if os.Getenv("MADA_POSTGRES_URL") != "" {
db, err = OpenPostgresConnection()

if err != nil {
log.Fatal(err)
}
createPostgresTables(db)
addPostgresGeometryColumns(db)
} else {
db, err = OpenSQLiteConnection()

if err != nil {
log.Fatal(err)
}

initializeSpatialMetadata(db)
createTables(db)
addGeometryColumns(db)
}

defer db.Close()

initializeSpatialMetadata(db)
createTables(db)
addGeometryColumns(db)

/*
level 0 - country
level 1 - region
Expand Down Expand Up @@ -112,6 +125,18 @@ func OpenSQLiteConnection() (*sql.DB, error) {
return sql.Open("sqlite3_with_spatialite", filepath.Join(CreateConfigDir(), "spatialmada.db"))
}

func OpenPostgresConnection() (*sql.DB, error) {
db, err := sql.Open("postgres", os.Getenv("MADA_POSTGRES_URL"))

if err != nil {
log.Fatal(err)
}

db.Exec("CREATE EXTENSION postgis;")

return db, nil
}

func CreateOrOpenBleve() (bleve.Index, error) {
if _, err := os.Stat(DATABASE_PATH); os.IsNotExist(err) {
geometryMapping := bleve.NewDocumentDisabledMapping()
Expand Down Expand Up @@ -291,20 +316,25 @@ func createTables(db *sql.DB) {
}
}

func addGeometryColumns(db *sql.DB) {
rows, err := db.Query("SELECT COUNT(*) AS CNTREC FROM pragma_table_info('commune') WHERE name='geom';")
if err != nil {
log.Fatal(err)
}
count := 0
for rows.Next() {
rows.Scan(&count)
func createPostgresTables(db *sql.DB) {
queries := []string{
"CREATE TABLE IF NOT EXISTS country (id SERIAL PRIMARY KEY, uid TEXT NOT NULL UNIQUE, name TEXT);",
"CREATE TABLE IF NOT EXISTS region (id SERIAL PRIMARY KEY, uid TEXT NOT NULL UNIQUE, name TEXT, country TEXT);",
"CREATE TABLE IF NOT EXISTS district (id SERIAL PRIMARY KEY, uid TEXT NOT NULL UNIQUE, name TEXT, region TEXT, country TEXT);",
"CREATE TABLE IF NOT EXISTS commune (id SERIAL PRIMARY KEY, uid TEXT NOT NULL UNIQUE, name TEXT, district TEXT, region TEXT, country TEXT);",
"CREATE TABLE IF NOT EXISTS fokontany (id SERIAL PRIMARY KEY, uid TEXT NOT NULL UNIQUE, name TEXT, commune TEXT, district TEXT, region TEXT, country TEXT);",
"CREATE UNIQUE INDEX IF NOT EXISTS country_uid_idx ON country (uid);",
"CREATE UNIQUE INDEX IF NOT EXISTS region_uid_idx ON region (uid);",
"CREATE UNIQUE INDEX IF NOT EXISTS district_uid_idx ON district (uid);",
"CREATE UNIQUE INDEX IF NOT EXISTS commune_uid_idx ON commune (uid);",
"CREATE UNIQUE INDEX IF NOT EXISTS fokontany_uid_idx ON fokontany (uid);",
}

if count == 1 {
return
for _, query := range queries {
runQuery(db, query)
}
}

func addGeometryColumns(db *sql.DB) {
queries := []string{
"SELECT AddGeometryColumn('country', 'geom', 4326, 'POLYGON', 2);",
"SELECT AddGeometryColumn('region', 'geom', 4326, 'POLYGON', 2);",
Expand All @@ -318,7 +348,21 @@ func addGeometryColumns(db *sql.DB) {
"SELECT CreateSpatialIndex('fokontany', 'geom');",
}
for _, query := range queries {
runQuery(db, query)
db.Exec(query)
}

}

func addPostgresGeometryColumns(db *sql.DB) {
queries := []string{
"SELECT AddGeometryColumn('public', 'country', 'geom', 4326, 'POLYGON', 2);",
"SELECT AddGeometryColumn('public', 'region', 'geom', 4326, 'POLYGON', 2);",
"SELECT AddGeometryColumn('public', 'district', 'geom', 4326, 'POLYGON', 2);",
"SELECT AddGeometryColumn('public', 'commune', 'geom', 4326, 'POLYGON', 2);",
"SELECT AddGeometryColumn('public', 'fokontany', 'geom', 4326, 'POLYGON', 2);",
}
for _, query := range queries {
db.Exec(query)
}

}
Expand Down Expand Up @@ -347,6 +391,10 @@ func saveToDatabase(db *sql.DB, index bleve.Index, id string, polygon Polygon) {

geom := fmt.Sprintf("GeomFromText('POLYGON(%s)', 4326)", coordsText)

if os.Getenv("MADA_POSTGRES_URL") != "" {
geom = fmt.Sprintf("ST_GeomFromText('POLYGON(%s)', 4326)", coordsText)
}

q := ""

if polygon.Type == "country" {
Expand Down
11 changes: 10 additions & 1 deletion mada/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"os"

"github.com/blevesearch/bleve/v2"
"github.com/twpayne/go-geom"
Expand All @@ -22,6 +23,14 @@ type RegionService struct {
}

func NewRegionService() *RegionService {
if os.Getenv("MADA_POSTGRES_URL") != "" {
db, err := sql.Open("postgres", os.Getenv("MADA_POSTGRES_URL"))
if err != nil {
panic(err)
}
return &RegionService{db: db}
}

db, err := OpenSQLiteConnection()

if err != nil {
Expand Down Expand Up @@ -58,7 +67,7 @@ func (r *RegionService) List(outputInJSON bool, limit int) {
}

func (r *RegionService) ShowRegion(id string, outputInJSON bool) {
rows, _ := r.db.Query("SELECT uid, name, ST_AsText(geom) FROM region WHERE uid = ?", id)
rows, _ := r.db.Query("SELECT uid, name, ST_AsText(geom) FROM region WHERE uid = $1", id)
defer rows.Close()
var uid, name, g string
rows.Next()
Expand Down

0 comments on commit 2ed97ee

Please sign in to comment.