-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_shapefiles.go
87 lines (75 loc) · 1.94 KB
/
create_shapefiles.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
78
79
80
81
82
83
84
85
86
87
package main
import (
"bytes"
"fmt"
"log"
"os/exec"
"strings"
)
func createShapefiles() error {
log.Println("Shapefiles creating...")
dir := "output/warped"
fileList := getFileList(dir)
for _, file := range fileList {
newFile, layer, err := gdalPolygonize(file)
if err != nil {
return err
}
err = removeFrame(newFile)
if err != nil {
return err
}
err = unionGeometries(newFile, layer)
if err != nil {
return err
}
}
return nil
}
// gdalPolygonize is used for creating shapefile from tiff image
func gdalPolygonize(file string) (string, string, error) {
infoLayer := "info"
shpLayer := strings.Replace(strings.Split(file, ".")[0], "output/warped/", "", 1)
shpFile := strings.Replace(file, "warped", "shapefiles", 1)
shpFile = strings.Replace(shpFile, "tif", "shp", 1)
args := []string{file, "-f", "ESRI Shapefile"}
args = append(args, shpFile, shpLayer, infoLayer)
cmd := exec.Command(polygonize, args...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Println(stderr.String())
return "", "", err
}
return shpFile, shpLayer, nil
}
// removeFrame is used for remove frame that appears after gdalPolygonize()
func removeFrame(file string) error {
infoLayer := "info"
condition := fmt.Sprintf("%s > 0", infoLayer)
args := []string{file, file, "-where", condition}
cmd := exec.Command(ogr2ogr, args...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Println(stderr.String())
return err
}
return nil
}
// unionGeometries is used for union all geometries in one in layer
func unionGeometries(file, layer string) error {
sql := fmt.Sprintf("SELECT ST_Union(geometry) AS geometry FROM %s", layer)
args := []string{file, file, "-dialect", "sqlite", "-sql", sql}
cmd := exec.Command(ogr2ogr, args...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Println(stderr.String())
return err
}
return nil
}