-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.go
77 lines (61 loc) · 1.53 KB
/
info.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
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"github.com/energyaccessexplorer/gdal"
"log"
"strconv"
)
type bounds struct {
Left float64
Bottom float64
Right float64
Top float64
}
type dataset_info struct {
Fields []string `json:"fields"`
FeatureCount int `json:"featurecount"`
Bounds bounds `json:"bounds"`
}
func info_bounds(in filename) gdal.Geometry {
none := gdal.Create(gdal.GT_None)
t := gdal.CreateSpatialReference("")
t.FromEPSG(4326)
src := gdal.OpenDataSource(in, 0)
defer src.Destroy()
layer := src.LayerByIndex(0)
env, err := layer.Extent(true)
if err != nil {
log.Println("Failed to get layer extent")
return none
}
// text := fmt.Sprintf(
// "MULTIPOINT (%.32f %.32f, %.32f %.32f)",
// env.MinX(), env.MaxX(), env.MinY(), env.MaxY(),
// )
text := fmt.Sprintf(
`POLYGON ((%.32f %.32f), (%.32f %.32f), (%.32f %.32f), (%.32f %.32f), (%.32f %.32f))`,
env.MaxX(), env.MinY(), env.MaxX(), env.MaxY(), env.MinX(), env.MaxY(), env.MinX(), env.MinY(),
env.MaxX(), env.MinY())
// TODO: this looks like we are going around uselessly...
//
layer.ResetReading()
geom := layer.NextFeature().Geometry()
v, ok := geom.SpatialReference().AttrValue("AUTHORITY", 1)
if !ok {
log.Println("Failed setting AUTHORITY to spatial reference")
return none
}
s := gdal.CreateSpatialReference("")
i, err := strconv.Atoi(v)
if err != nil {
log.Println(err.Error())
return none
}
s.FromEPSG(i)
g, err := gdal.CreateFromWKT(text, s)
if err != nil {
log.Println(err.Error())
return none
}
return g
}