-
Notifications
You must be signed in to change notification settings - Fork 19
/
postgis.go
65 lines (60 loc) · 2.08 KB
/
postgis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"database/sql"
)
// PostGIS is a DB access type allowing non-batch based interactions
// with a PostGIS database.
type PostGIS struct {
db *sql.DB
}
// WKTIsValidWithReason checks if a WKT is valid, and if not gives the reason.
func (p PostGIS) WKTIsValidWithReason(wkt string) (bool, string) {
var isValid bool
var reason string
err := p.db.QueryRow(`
SELECT
ST_IsValid(ST_GeomFromText($1)),
ST_IsValidReason(ST_GeomFromText($1))`,
wkt,
).Scan(&isValid, &reason)
if err != nil {
// It's not possible to distinguish between problems with the geometry
// and problems with the database except by string-matching. It's
// better to just report all errors, even if this means there will be
// some false errors in the case of connectivity problems (or similar).
return false, err.Error()
}
return isValid, reason
}
// WKBIsValidWithReason checks if a WKB is valid and, if not, gives the reason.
// It's unable to differentiate between errors due to problems with the WKB and
// general problems with the database.
func (p PostGIS) WKBIsValidWithReason(wkb string) (bool, string) {
var isValid bool
err := p.db.QueryRow(`SELECT ST_IsValid(ST_GeomFromWKB(decode($1, 'hex')))`, wkb).Scan(&isValid)
if err != nil {
return false, err.Error()
}
var reason string
err = p.db.QueryRow(`SELECT ST_IsValidReason(ST_GeomFromWKB(decode($1, 'hex')))`, wkb).Scan(&reason)
if err != nil {
return false, err.Error()
}
return isValid, reason
}
// WKBIsValidWithReason checks if a GeoJSON value is valid and, if not, gives
// the reason. It's unable to differentiate between errors due to problems
// with the GeoJSON value and general problems with the database.
func (p PostGIS) GeoJSONIsValidWithReason(geojson string) (bool, string) {
var isValid bool
err := p.db.QueryRow(`SELECT ST_IsValid(ST_GeomFromGeoJSON($1))`, geojson).Scan(&isValid)
if err != nil {
return false, err.Error()
}
var reason string
err = p.db.QueryRow(`SELECT ST_IsValidReason(ST_GeomFromGeoJSON($1))`, geojson).Scan(&reason)
if err != nil {
return false, err.Error()
}
return isValid, reason
}