gismanager

package module
Version: v0.0.0-...-dfa2221 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2018 License: MIT Imports: 15 Imported by: 0

README

Go Report Card GitHub license GitHub issues Coverage Status Build Status Documentation GitHub forks GitHub stars Twitter

GISManager

Publish Your GIS Data(Vector Data) to PostGIS and Geoserver

  • How to install:
    • go get -v github.com/hishamkaram/gismanager
  • Usage:
    • testdata folder content:
      ./testdata/
      ├── neighborhood_names_gis.geojson
      ├── nested
      │   └── nyc_wi-fi_hotspot_locations.geojson
      ├── sample.gpkg
      
    • create ManagerConfig instance:
      manager:= gismanager.ManagerConfig{
        Geoserver: gismanager.GeoserverConfig{WorkspaceName: "golang", Username: "admin", Password: "geoserver", ServerURL: "http://localhost:8080/geoserver"},
        Datastore: gismanager.DatastoreConfig{Host: "localhost", Port: 5432, DBName: "gis", DBUser: "golang", DBPass: "golang", Name: "gismanager_data"},
        Source:    gismanager.SourceConfig{Path: "./testdata"},
        logger:    gismanager.GetLogger(),
      }
      
    • get Supported GIS Files:
      files, _ := gismanager.GetGISFiles(manager.Source.Path)
      for _, file := range files {
        fmt.Println(file)
      }
      
      • output:
        <full_path>/testdata/neighborhood_names_gis.geojson
        <full_path>/testdata/nested/nyc_wi-fi_hotspot_locations.geojson
        <full_path>/testdata/sample.gpkg
        
    • read files and get layers Schema:
        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)
              }
            }
          }
        }
      
      • output sample:
        neighborhood_names_gis
        
        {Name:geom Type:POINT}
        
        {Name:stacked Type:String}
        
        {Name:name Type:String}
        
        {Name:annoline1 Type:String}
        
        {Name:annoline3 Type:String}
        
        {Name:objectid Type:String}
        
        {Name:annoangle Type:String}
        
        {Name:annoline2 Type:String}
        
        {Name:borough Type:String}
        ...
        
    • add your gis data to your database:
        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,
                }
                newLayer, postgisErr := gLayer.LayerToPostgis(targetSource, manager, true)
                if postgisErr != nil {
                  panic(postgisErr)
                }
                logger.Infof("Layer: %s added to you database", newLayer.Name())
              }
            }
        }
      
      • output:
        INFO[14-10-2018 17:28:37] Layer: neighborhood_names_gis added to you database 
        INFO[14-10-2018 17:28:38] Layer: nyc_wi_fi_hotspot_locations added to you database 
        INFO[14-10-2018 17:28:38] Layer: hwy_patrol added to you database
        
    • update the previous code to publish your postgis layers to geoserver
       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 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")
               }
             }
      
           }
         }
       }
      
      • output:
       INFO[14-10-2018 17:37:07] url:http://localhost:8080/geoserver/rest/workspaces/golang  Status=404  
       ERRO[14-10-2018 17:37:07] No such workspace: 'golang' found            
       INFO[14-10-2018 17:37:07] url:http://localhost:8080/geoserver/rest/workspaces  Status=201  
       INFO[14-10-2018 17:37:07] url:http://localhost:8080/geoserver/rest/workspaces/golang/datastores/gis?quietOnNotFound=true  Status=404  
       INFO[14-10-2018 17:37:07] url:http://localhost:8080/geoserver/rest/workspaces/golang/datastores  Status=201  
       ERRO[14-10-2018 17:37:07] {"featureType":{"name":"neighborhood_names_gis","nativeName":"neighborhood_names_gis"}} 
       INFO[14-10-2018 17:37:07] url:http://localhost:8080/geoserver/rest/workspaces/golang/datastores/gis/featuretypes  Status=201  
       INFO[14-10-2018 17:37:07] published                                    
       INFO[14-10-2018 17:37:08] url:http://localhost:8080/geoserver/rest/workspaces/golang  Status=200  
       INFO[14-10-2018 17:37:08] url:http://localhost:8080/geoserver/rest/workspaces/golang/datastores/gis?quietOnNotFound=true  Status=200  
       ERRO[14-10-2018 17:37:08] {"featureType":{"name":"nyc_wi_fi_hotspot_locations","nativeName":"nyc_wi_fi_hotspot_locations"}} 
       INFO[14-10-2018 17:37:08] url:http://localhost:8080/geoserver/rest/workspaces/golang/datastores/gis/featuretypes  Status=201  
       INFO[14-10-2018 17:37:08] published                                    
       INFO[14-10-2018 17:37:08] url:http://localhost:8080/geoserver/rest/workspaces/golang  Status=200  
       INFO[14-10-2018 17:37:08] url:http://localhost:8080/geoserver/rest/workspaces/golang/datastores/gis?quietOnNotFound=true  Status=200  
       ERRO[14-10-2018 17:37:08] {"featureType":{"name":"hwy_patrol","nativeName":"hwy_patrol"}} 
       INFO[14-10-2018 17:37:08] url:http://localhost:8080/geoserver/rest/workspaces/golang/datastores/gis/featuretypes  Status=201  
       INFO[14-10-2018 17:37:08] published 
      
      • done check you geoserver or via geoserver rest api url http://localhost:8080/geoserver/rest/layers.json :
         {
           "layers": {
             "layer": [..., {
               "name": "golang:hwy_patrol",
               "href": "http:\/\/localhost:8080\/geoserver\/rest\/layers\/golang%3Ahwy_patrol.json"
             }, {
               "name": "golang:neighborhood_names_gis",
               "href": "http:\/\/localhost:8080\/geoserver\/rest\/layers\/golang%3Aneighborhood_names_gis.json"
             }, {
               "name": "golang:nyc_wi_fi_hotspot_locations",
               "href": "http:\/\/localhost:8080\/geoserver\/rest\/layers\/golang%3Anyc_wi_fi_hotspot_locations.json"
             }]
           }
         }
        

Todo:

  • backup postgis as geopackage

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DBIsAlive

func DBIsAlive(dbType string, connectionStr string) (err error)

DBIsAlive check if database alive

func GetGISFiles

func GetGISFiles(root string) ([]string, error)

GetGISFiles retrun List of All GIS Files in this path

func GetLogger

func GetLogger() (logger *logrus.Logger)

GetLogger return logger

Types

type DatastoreConfig

type DatastoreConfig 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"`
}

DatastoreConfig configuration

func (*DatastoreConfig) BuildConnectionString

func (ds *DatastoreConfig) BuildConnectionString() string

BuildConnectionString return GDAL postgres connection as string

func (*DatastoreConfig) PostgresConnectionString

func (ds *DatastoreConfig) PostgresConnectionString() string

PostgresConnectionString return postgres connection as string

type GdalLayer

type GdalLayer struct {
	*gdal.Layer
}

GdalLayer Layer

func (*GdalLayer) GetFeatures

func (layer *GdalLayer) GetFeatures() (features []*gdal.Feature)

GetFeatures return layer features

func (*GdalLayer) GetGeomtryName

func (layer *GdalLayer) GetGeomtryName() (geometryName string)

GetGeomtryName Get Geometry Name point/line/....etc

func (*GdalLayer) GetLayerSchema

func (layer *GdalLayer) GetLayerSchema() (fields []*LayerField)

GetLayerSchema return slice of layer fields

func (*GdalLayer) LayerToPostgis

func (layer *GdalLayer) LayerToPostgis(targetSource *gdal.DataSource, manager *ManagerConfig, overwrite bool) (newLayer *GdalLayer, err error)

LayerToPostgis add Layer to Postgis

type GeoserverConfig

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

GeoserverConfig geoserver configuration

type LayerField

type LayerField struct {
	Name string
	Type string
}

LayerField Layer Field

type ManagerConfig

type ManagerConfig struct {
	Geoserver GeoserverConfig `yaml:"geoserver"`
	Datastore DatastoreConfig `yaml:"datastore"`
	Source    SourceConfig    `yaml:"source"`
	// contains filtered or unexported fields
}

ManagerConfig is the configuration Object

func FromConfig

func FromConfig(configFile string) (config *ManagerConfig, err error)

FromConfig load GIS Manager config from yaml file

func (*ManagerConfig) GetDriver

func (manager *ManagerConfig) GetDriver(path string) (driver gdal.OGRDriver, err error)

GetDriver return the proper driver based on file path/database connection

func (*ManagerConfig) GetGeoserverCatalog

func (manager *ManagerConfig) GetGeoserverCatalog() *gsconfig.GeoServer

GetGeoserverCatalog return geoserver Catalog instance to deal with geoserver

func (*ManagerConfig) OpenSource

func (manager *ManagerConfig) OpenSource(path string, access int) (source *gdal.DataSource, ok bool)

OpenSource open data source from a given Path and access permission 0/1

func (*ManagerConfig) PublishGeoserverLayer

func (manager *ManagerConfig) PublishGeoserverLayer(layer *GdalLayer) (ok bool, err error)

PublishGeoserverLayer Publish Layer to Geoserver instance

type SourceConfig

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

SourceConfig Data Source/Dir configuration

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL