Skip to content

Commit

Permalink
create datastore if doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
hisham waleed karam committed Oct 5, 2018
1 parent ca85cd7 commit 692ecfe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/gismanager/gismanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
gLayer := gismanager.GdalLayer{
Layer: &layer,
}
if newLayer := gLayer.LayerToPostgis(targetSource); newLayer.Layer != nil {
if newLayer := gLayer.LayerToPostgis(targetSource, manager); newLayer.Layer != nil {
ok, pubErr := manager.PublishGeoserverLayer(newLayer)
if pubErr != nil {
logger.Error(pubErr)
Expand Down
35 changes: 31 additions & 4 deletions layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gismanager
import (
"fmt"

gsconfig "github.com/hishamkaram/geoserver"
"github.com/lukeroth/gdal"
)

Expand All @@ -18,9 +19,35 @@ type LayerField struct {
}

//LayerToPostgis Layer to Postgis
func (layer *GdalLayer) LayerToPostgis(targetSource gdal.DataSource) (newLayer *GdalLayer) {
func (layer *GdalLayer) LayerToPostgis(targetSource gdal.DataSource, manager *ManagerConfig) (newLayer *GdalLayer) {
catalog := gsconfig.GetCatalog(manager.Geoserver.ServerURL, manager.Geoserver.Username, manager.Geoserver.Password)
storeExits, datastoreErr := catalog.DatastoreExists(manager.Geoserver.WorkspaceName, manager.Datastore.Name, true)
if datastoreErr != nil {
manager.logger.Error(datastoreErr)
return
}
if !storeExits {
datastoreConnection := gsconfig.DatastoreConnection{
Name: manager.Datastore.Name,
Host: manager.Datastore.Host,
Port: int(manager.Datastore.Port),
DBName: manager.Datastore.DBName,
DBUser: manager.Datastore.DBUser,
DBPass: manager.Datastore.DBPass,
}
created, createErr := catalog.CreateDatastore(datastoreConnection, manager.Geoserver.WorkspaceName)
if createErr != nil || !created {
manager.logger.Error(createErr)
return
}
}
if layer.Layer != nil {
_layer := targetSource.CopyLayer(*layer.Layer, layer.Name(), []string{fmt.Sprintf("GEOMETRY_NAME=%s", layer.GeometryColumn())})
var options []string
geomName := layer.GeometryColumn()
if geomName != "" {
options = append(options, fmt.Sprintf("GEOMETRY_NAME=%s", layer.GeometryColumn()))
}
_layer := targetSource.CopyLayer(*layer.Layer, layer.Name(), options)
newLayer = &GdalLayer{
Layer: &_layer,
}
Expand Down Expand Up @@ -57,8 +84,8 @@ func (layer *GdalLayer) GetLayerSchema() (fields []*LayerField) {
return
}

//GetFeature Get Layer Features
func (layer *GdalLayer) GetFeature() (features []*gdal.Feature) {
//GetFeatures Get Layer Features
func (layer *GdalLayer) GetFeatures() (features []*gdal.Feature) {
if layer.Layer != nil {
count, ok := layer.Layer.FeatureCount(true)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (manager *ManagerConfig) GetDriver(path string) (driver gdal.OGRDriver, err
case ".shp", ".zip":
driver = gdal.OGRDriverByName(shapeFileDriver)
break
case ".json":
case ".json", ".geojson":
driver = gdal.OGRDriverByName(geoJSONDriver)
break
case ".kml":
Expand Down

0 comments on commit 692ecfe

Please sign in to comment.