From 692ecfe570ad29e2d0830b39a9b96b9047031629 Mon Sep 17 00:00:00 2001 From: hisham waleed karam Date: Fri, 5 Oct 2018 16:23:04 +0200 Subject: [PATCH] create datastore if doesn't exist --- cmd/gismanager/gismanager.go | 2 +- layer.go | 35 +++++++++++++++++++++++++++++++---- manager.go | 2 +- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/cmd/gismanager/gismanager.go b/cmd/gismanager/gismanager.go index 99f82b4..161ebd6 100644 --- a/cmd/gismanager/gismanager.go +++ b/cmd/gismanager/gismanager.go @@ -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) diff --git a/layer.go b/layer.go index 78d2faf..ffa5752 100644 --- a/layer.go +++ b/layer.go @@ -3,6 +3,7 @@ package gismanager import ( "fmt" + gsconfig "github.com/hishamkaram/geoserver" "github.com/lukeroth/gdal" ) @@ -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, } @@ -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 { diff --git a/manager.go b/manager.go index 850e701..7ae40cb 100644 --- a/manager.go +++ b/manager.go @@ -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":