Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hisham waleed karam committed Oct 14, 2018
1 parent 7359b03 commit 0948cb7
Show file tree
Hide file tree
Showing 25 changed files with 625 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ _testmain.go
# 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.

local_config.yaml
local_config.yaml
play
36 changes: 36 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
sudo: required
language: go
go:
- "1.9.x"
- "1.10.x"
- "1.11.x"
services:
- docker
env:
- DOCKER_COMPOSE_VERSION=1.20.1
addons:
hosts:
- postgis
- geoserver
before_install:
- sudo rm /usr/local/bin/docker-compose
- curl -L https://github.com/docker/compose/releases/download/${DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` > docker-compose
- chmod +x docker-compose
- sudo mv docker-compose /usr/local/bin
- sudo apt-get install realpath -y
- sudo apt-get purge 'postgresql-*' -y
- sudo apt-get autoremove 'postgresql-*' -y
- echo "deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main" | sudo tee -a /etc/apt/sources.list
- wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
- sudo apt-get update -qq
- sudo apt-get install postgresql-client-10 -y
- sudo chmod +x ./scripts/*.sh
- sudo ./scripts/gdal.sh
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
- go get -u github.com/lukeroth/gdal
- docker-compose build
- docker-compose up -d
- docker ps
script:
- bash -c "./scripts/postgis.sh && ./scripts/entry.sh"
2 changes: 1 addition & 1 deletion cmd/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ geoserver:
url: http://localhost/geoserver
username: admin
password: geoserver
workspace: 'sample_workspace'
workspace: sample_workspace
datastore:
host: localhost
port: 5432
Expand Down
39 changes: 22 additions & 17 deletions cmd/gismanager/gismanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"flag"
"fmt"
"os"

"github.com/hishamkaram/gismanager"
Expand All @@ -22,26 +23,30 @@ func main() {
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, manager); newLayer.Layer != nil {
ok, pubErr := manager.PublishGeoserverLayer(newLayer)
if pubErr != nil {
logger.Error(pubErr)
files, _ := gismanager.GetGISFiles(manager.Source.Path)
fmt.Println(files)
for _, file := range files {
source, ok := manager.OpenSource(file, 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 !ok {
logger.Error("Failed to Publish")
} else {
logger.Info("published")
if newLayer, postgisErr := gLayer.LayerToPostgis(targetSource, manager, true); newLayer.Layer != nil || postgisErr != nil {
ok, pubErr := manager.PublishGeoserverLayer(newLayer)
if pubErr != nil {
logger.Error(pubErr)
}
if !ok {
logger.Error("Failed to Publish")
} else {
logger.Info("published")
}
}
}

}
}
}
}
22 changes: 13 additions & 9 deletions cmd/layerSchema/layerSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ func main() {
if confErr != nil {
panic(confErr)
}
source, ok := manager.OpenSource(manager.Source.Path, 0)
if ok {
for index := 0; index < source.LayerCount(); index++ {
layer := source.LayerByIndex(index)
gLayer := gismanager.GdalLayer{
Layer: &layer,
}
for _, f := range gLayer.GetLayerSchema() {
fmt.Printf("\n%+v\n", *f)
files, _ := gismanager.GetGISFiles(manager.Source.Path)
for _, file := range files {
source, ok := manager.OpenSource(file, 0)
if ok {
for index := 0; index < source.LayerCount(); index++ {
layer := source.LayerByIndex(index)
gLayer := gismanager.GdalLayer{
Layer: &layer,
}
fmt.Println(layer.Name())
for _, f := range gLayer.GetLayerSchema() {
fmt.Printf("\n%+v\n", *f)
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions datastore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package gismanager

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestBuildConnectionString(t *testing.T) {
ds := datastore{
Host: "localhost",
Port: 5432,
DBName: "gis",
DBUser: "gis",
DBPass: "gis",
}
conn := ds.BuildConnectionString()
assert.NotNil(t, conn)
assert.NotEqual(t, "", conn)
assert.True(t, pgRegex.Match([]byte(conn)))

}
27 changes: 27 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '3'

services:
geoserver:
image: cartoview/geoserver_docker:tomcat
restart: unless-stopped
hostname: geoserver
build:
context: ./
dockerfile: docker/Dockerfile
user: root
ports:
- "8080:8080"
postgis:
image: kartoza/postgis:10.0-2.4
volumes:
- 'postgis-data:/var/lib/postgresql'
environment:
- POSTGRES_DB=gis
- POSTGRES_USER=golang
- POSTGRES_PASS=golang
- ALLOW_IP_RANGE=0.0.0.0/0
ports:
- 5432:5432
restart: unless-stopped
volumes:
postgis-data:
21 changes: 21 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM tomcat:9.0.4-jre8
LABEL "MAINTAINER"="Cartologic Development Team"
ENV JAVA_HOME /usr/lib/jvm/default-java
ENV GEOSERVER_VERSION 2.13.0
RUN ln -s /usr/lib/jvm/java-8-openjdk-amd64 /usr/lib/jvm/default-java
RUN apt-get autoremove --purge -y openjdk-8-jre-headless && \
echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true \
| debconf-set-selections && \
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" \
> /etc/apt/sources.list.d/webupd8team-java.list && \
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886 && \
rm -rf /var/lib/apt/lists/* && \
apt-get update && \
apt-get install -y oracle-java8-installer oracle-java8-set-default && \
ln -s --force /usr/lib/jvm/java-8-oracle /usr/lib/jvm/default-java

RUN cd /tmp && \
wget https://netix.dl.sourceforge.net/project/geoserver/GeoServer/${GEOSERVER_VERSION}/geoserver-${GEOSERVER_VERSION}-war.zip
RUN unzip /tmp/geoserver-${GEOSERVER_VERSION}-war.zip -d /tmp/geoserver
RUN mv /tmp/geoserver/geoserver.war /usr/local/tomcat/webapps
EXPOSE 8080
62 changes: 44 additions & 18 deletions layer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gismanager

import (
"errors"
"fmt"

gsconfig "github.com/hishamkaram/geoserver"
Expand All @@ -18,39 +19,62 @@ type LayerField struct {
Type string
}

//LayerToPostgis Layer to Postgis
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
//PublishGeoserverLayer publish Layer to postgis
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
}
}
if layer.Layer != nil {
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,
}
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) {
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
}
Expand Down Expand Up @@ -86,11 +110,13 @@ func (layer *GdalLayer) GetLayerSchema() (fields []*LayerField) {

//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)
Expand Down
Loading

0 comments on commit 0948cb7

Please sign in to comment.