Skip to content

Commit

Permalink
add Layer Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
hisham waleed karam committed Oct 2, 2018
1 parent b4ec969 commit ca85cd7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ _testmain.go
# use alternative ways to exclude files from git.
# For example, set up .git/info/exclude or use a global .gitignore.

loca_config.yaml
local_config.yaml
37 changes: 37 additions & 0 deletions cmd/layerSchema/layerSchema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"errors"
"flag"
"fmt"
"os"

"github.com/hishamkaram/gismanager"
)

func main() {
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)
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)
}
}
}
}
51 changes: 51 additions & 0 deletions layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ type GdalLayer struct {
*gdal.Layer
}

//LayerField Field
type LayerField struct {
Name string
Type string
}

//LayerToPostgis Layer to Postgis
func (layer *GdalLayer) LayerToPostgis(targetSource gdal.DataSource) (newLayer *GdalLayer) {
if layer.Layer != nil {
Expand All @@ -21,3 +27,48 @@ func (layer *GdalLayer) LayerToPostgis(targetSource gdal.DataSource) (newLayer *
}
return
}

//GetGeomtryName Get Geometry Name
func (layer *GdalLayer) GetGeomtryName() (geometryName string) {
geom := gdal.Create(layer.Layer.Type())
geometryName = geom.Name()
return
}

//GetLayerSchema Get Layer Schema
func (layer *GdalLayer) GetLayerSchema() (fields []*LayerField) {
if layer.Layer != nil {
layerDef := layer.Layer.Definition()
geomField := LayerField{
Name: layer.Layer.GeometryColumn(),
Type: layer.GetGeomtryName(),
}
fields = append(fields, &geomField)
for index := 0; index < layerDef.FieldCount(); index++ {
fieldDef := layerDef.FieldDefinition(index)
layerField := LayerField{
Name: fieldDef.Name(),
Type: fieldDef.Type().Name(),
}
fields = append(fields, &layerField)

}
}
return
}

//GetFeature Get Layer Features
func (layer *GdalLayer) GetFeature() (features []*gdal.Feature) {
if layer.Layer != nil {
count, ok := layer.Layer.FeatureCount(true)
if !ok {

} else {
for index := 0; index < count; index++ {
f := layer.Layer.Feature(index)
features = append(features, &f)
}
}
}
return
}

0 comments on commit ca85cd7

Please sign in to comment.