-
Notifications
You must be signed in to change notification settings - Fork 10
/
layer.go
137 lines (127 loc) · 3.65 KB
/
layer.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package gismanager
import (
"errors"
"fmt"
gsconfig "github.com/hishamkaram/geoserver"
"github.com/lukeroth/gdal"
)
//GdalLayer Layer
type GdalLayer struct {
*gdal.Layer
}
//LayerField Layer Field
type LayerField struct {
Name string
Type string
}
//PublishGeoserverLayer Publish Layer to Geoserver instance
func (manager *ManagerConfig) PublishGeoserverLayer(layer *GdalLayer) (ok bool, err error) {
catalog := manager.GetGeoserverCatalog()
workspaceExists, _ := catalog.WorkspaceExists(manager.Geoserver.WorkspaceName)
if !workspaceExists {
workspaceCreated, workspaceCreateErr := catalog.CreateWorkspace(manager.Geoserver.WorkspaceName)
if workspaceCreateErr != nil || !workspaceCreated {
manager.logger.Error(workspaceCreateErr)
err = workspaceCreateErr
return
}
}
storeExits, _ := catalog.DatastoreExists(manager.Geoserver.WorkspaceName, manager.Datastore.Name, true)
if !storeExits {
datastoreConnection := gsconfig.DatastoreConnection{
Name: manager.Datastore.Name,
Host: manager.Datastore.Host,
Port: int(manager.Datastore.Port),
DBName: manager.Datastore.DBName,
Type: "postgis",
DBUser: manager.Datastore.DBUser,
DBPass: manager.Datastore.DBPass,
}
created, createErr := catalog.CreateDatastore(datastoreConnection, manager.Geoserver.WorkspaceName)
if createErr != nil || !created {
manager.logger.Error(createErr)
err = createErr
return
}
}
ok, err = catalog.PublishPostgisLayer(manager.Geoserver.WorkspaceName, manager.Datastore.Name, layer.Name(), layer.Name())
return
}
//LayerToPostgis add Layer to Postgis
func (layer *GdalLayer) LayerToPostgis(targetSource *gdal.DataSource, manager *ManagerConfig, overwrite bool) (newLayer *GdalLayer, err error) {
connStr := manager.Datastore.PostgresConnectionString()
dbErr := DBIsAlive("postgres", connStr)
if dbErr != nil {
err = dbErr
return
}
if targetSource == nil {
err = errors.New("Invalid Datasource")
return
}
if layer.Layer == nil {
err = errors.New("Invalid Layer")
return
}
datasource := *targetSource
var options []string
geomName := layer.GeometryColumn()
if geomName != "" {
options = append(options, fmt.Sprintf("GEOMETRY_NAME=%s", layer.GeometryColumn()))
}
if overwrite {
options = append(options, "OVERWRITE=YES")
}
_layer := datasource.CopyLayer(*layer.Layer, layer.Name(), options)
newLayer = &GdalLayer{
Layer: &_layer,
}
return
}
//GetGeomtryName Get Geometry Name point/line/....etc
func (layer *GdalLayer) GetGeomtryName() (geometryName string) {
geom := gdal.Create(layer.Layer.Type())
geometryName = geom.Name()
if len(geometryName) == 0 {
geometryName = "geom"
}
return
}
//GetLayerSchema return slice of layer fields
func (layer *GdalLayer) GetLayerSchema() (fields []*LayerField) {
if layer.Layer != nil {
layerDef := layer.Layer.Definition()
geomName := layer.Layer.GeometryColumn()
geomField := LayerField{
Name: geomName,
Type: layer.GetGeomtryName(),
}
fields = append(fields, &geomField)
for index := 0; index < layerDef.FieldCount(); index++ {
fieldDef := layerDef.FieldDefinition(index)
layerField := LayerField{
Name: fieldDef.Name(),
Type: fieldDef.Type().Name(),
}
fields = append(fields, &layerField)
}
}
return
}
//GetFeatures return layer features
func (layer *GdalLayer) GetFeatures() (features []*gdal.Feature) {
logger := GetLogger()
if layer.Layer != nil {
count, ok := layer.Layer.FeatureCount(true)
if !ok {
logger.Error("Could not read features")
} else {
logger.Infof("We Found %d Feature", count)
for index := 0; index < count; index++ {
f := layer.Layer.Feature(index)
features = append(features, &f)
}
}
}
return
}