Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hisham waleed karam committed Sep 29, 2018
0 parents commit c5e211f
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 0 deletions.
53 changes: 53 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.DS_Store
*.[56789ao]
*.a[56789o]
*.so
*.pyc
._*
.nfs.*
[56789a].out
*~
*.orig
*.rej
*.exe
.*.swp
core
*.cgo*.go
*.cgo*.c
_cgo_*
_obj
_test
_testmain.go

/VERSION.cache
/bin/
/build.out
/doc/articles/wiki/*.bin
/goinstall.log
/last-change
/misc/cgo/life/run.out
/misc/cgo/stdio/run.out
/misc/cgo/testso/main
/pkg/
/src/*.*/
/src/cmd/cgo/zdefaultcc.go
/src/cmd/dist/dist
/src/cmd/go/internal/cfg/zdefaultcc.go
/src/cmd/go/internal/cfg/zosarch.go
/src/cmd/internal/objabi/zbootstrap.go
/src/go/build/zcgo.go
/src/go/doc/headscan
/src/runtime/internal/sys/zversion.go
/src/unicode/maketables
/test.out
/test/garbage/*.out
/test/pass.out
/test/run.out
/test/times.out

# This file includes artifacts of Go build that should not be checked in.
# For files created by specific development environment (e.g. editor),
# use alternative ways to exclude files from git.
# For example, set up .git/info/exclude or use a global .gitignore.

loca_config.yaml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2018 Hisham Karam

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# GisManager
Publish Your Gis Data to Postgis and Geoserver
16 changes: 16 additions & 0 deletions datastore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gismanager

import "fmt"

type datastore struct {
Host string `yaml:"host"`
Port uint `yaml:"port"`
DBName string `yaml:"database"`
DBUser string `yaml:"username"`
DBPass string `yaml:"password"`
Name string `yaml:"name"`
}

func (ds *datastore) BuildConnectionString() string {
return fmt.Sprintf("PG: host=%s port=%d dbname=%s user=%s password=%s", ds.Host, ds.Port, ds.DBName, ds.DBUser, ds.DBPass)
}
14 changes: 14 additions & 0 deletions example/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
geoserver:
url: http://localhost/geoserver
username: admin
password: geoserver
workspace: 'sample_workspace'
datastore:
host: localhost
port: 5432
database: gis_data
username: db_user
password: db_pass
name: geoserver_datastore
source:
path: /xx/xx/xx/x.gpkg
47 changes: 47 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"errors"
"flag"
"os"

"github.com/hishamkaram/gismanager"
)

func main() {
logger := gismanager.GetLogger()
configFile := flag.String("config", "", "Config File")
flag.Parse()
if *configFile == "" {
panic(errors.New("config 'Parameter required'"))
}
if _, err := os.Stat(*configFile); os.IsNotExist(err) {
panic(errors.New("Config File Doesn't exist"))
}
manager, confErr := gismanager.FromConfig(*configFile)
if confErr != nil {
panic(confErr)
}
source, ok := manager.OpenSource(manager.Source.Path, 0)
targetSource, targetOK := manager.OpenSource(manager.Datastore.BuildConnectionString(), 1)
if ok && targetOK {
for index := 0; index < source.LayerCount(); index++ {
layer := source.LayerByIndex(index)
gLayer := gismanager.GdalLayer{
Layer: &layer,
}
if newLayer := gLayer.LayerToPostgis(targetSource); newLayer.Layer != nil {
ok, pubErr := manager.PublishGeoserverLayer(newLayer)
if pubErr != nil {
logger.Error(pubErr)
}
if !ok {
logger.Error("Failed to Publish")
} else {
logger.Info("published")
}
}

}
}
}
23 changes: 23 additions & 0 deletions layer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gismanager

import (
"fmt"

"github.com/lukeroth/gdal"
)

//GdalLayer Layer
type GdalLayer struct {
*gdal.Layer
}

//LayerToPostgis Layer to Postgis
func (layer *GdalLayer) LayerToPostgis(targetSource gdal.DataSource) (newLayer *GdalLayer) {
if layer.Layer != nil {
_layer := targetSource.CopyLayer(*layer.Layer, layer.Name(), []string{fmt.Sprintf("GEOMETRY_NAME=%s", layer.GeometryColumn())})
newLayer = &GdalLayer{
Layer: &_layer,
}
}
return
}
13 changes: 13 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gismanager

import "github.com/sirupsen/logrus"

//GetLogger return logger
func GetLogger() (logger *logrus.Logger) {
logger = logrus.New()
Formatter := new(logrus.TextFormatter)
Formatter.TimestampFormat = "02-01-2006 15:04:05"
Formatter.FullTimestamp = true
logger.Formatter = Formatter
return
}
87 changes: 87 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package gismanager

import (
"errors"
"path/filepath"
"strings"

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

type geoserver struct {
WorkspaceName string `yaml:"workspace"`
ServerURL string `yaml:"url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}

type source struct {
Path string `yaml:"path"`
}

//ManagerConfig is the configuration Object
type ManagerConfig struct {
Geoserver geoserver `yaml:"geoserver"`
Datastore datastore `yaml:"datastore"`
Source source `yaml:"source"`
logger *logrus.Logger
}

//GetGeoserverCatalog publish return geoserver Catalog
func (manager *ManagerConfig) GetGeoserverCatalog() *gsconfig.GeoServer {
gsCatalog := gsconfig.GetCatalog(manager.Geoserver.ServerURL, manager.Geoserver.Username, manager.Geoserver.Password)
return gsCatalog
}

//PublishGeoserverLayer publish Layer to postgis
func (manager *ManagerConfig) PublishGeoserverLayer(layer *GdalLayer) (ok bool, err error) {
if layer != nil {
gsCatalog := manager.GetGeoserverCatalog()
exists, storeErr := gsCatalog.DatastoreExists(manager.Geoserver.WorkspaceName, manager.Datastore.Name, true)
if storeErr != nil {
err = storeErr
}
if exists {
ok, err = gsCatalog.PublishPostgisLayer(manager.Geoserver.WorkspaceName, manager.Datastore.Name, layer.Name(), layer.Name())
}
}
return
}

//OpenSource open Datasource
func (manager *ManagerConfig) OpenSource(path string, access int) (source gdal.DataSource, ok bool) {
driver, err := manager.GetDriver(path)
if err != nil {
panic(err)
}
source, ok = driver.Open(path, access)
return
}

//GetDriver open Datasource
func (manager *ManagerConfig) GetDriver(path string) (driver gdal.OGRDriver, err error) {
if pgRegex.MatchString(path) {
driver = gdal.OGRDriverByName(postgreSQLDriver)
} else {
switch strings.ToLower(filepath.Ext(path)) {
case ".gpkg":
driver = gdal.OGRDriverByName(geopackageDriver)
break
case ".shp", ".zip":
driver = gdal.OGRDriverByName(shapeFileDriver)
break
case ".json":
driver = gdal.OGRDriverByName(geoJSONDriver)
break
case ".kml":
driver = gdal.OGRDriverByName(kmlDriver)
break
default:
err = errors.New("Can't Find the Proper Driver")
manager.logger.Error(err)
}
}
return
}
25 changes: 25 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gismanager

import (
"io/ioutil"

yaml "gopkg.in/yaml.v2"
)

//FromConfig load geoserver config from yaml file
func FromConfig(configFile string) (config *ManagerConfig, err error) {
gpkgConfig := ManagerConfig{}
gpkgConfig.logger = GetLogger()
yamlFile, err := ioutil.ReadFile(configFile)
if err != nil {
gpkgConfig.logger.Errorf("yamlFile.Get err %v ", err)
return
}
err = yaml.Unmarshal(yamlFile, &gpkgConfig)
if err != nil {
gpkgConfig.logger.Errorf("Unmarshal: %v", err)
return
}
config = &gpkgConfig
return
}
15 changes: 15 additions & 0 deletions vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gismanager

import "regexp"

var pgRegex = regexp.MustCompile(`^\s?PG:\s?.*$`)

const (
geopackageDriver = "GPKG"
postgreSQLDriver = "PostgreSQL"
shapeFileDriver = "ESRI Shapefile"
geoJSONDriver = "GeoJSON"
kmlDriver = "KML"
openFileGDBDriver = "OpenFileGDB"
esriJSONDriver = "ESRIJSON"
)

0 comments on commit c5e211f

Please sign in to comment.