From ca85cd759c65f08542f80a7d3a388a95c62b97f7 Mon Sep 17 00:00:00 2001 From: hisham waleed karam Date: Sun, 30 Sep 2018 13:54:47 +0200 Subject: [PATCH] add Layer Schema --- .gitignore | 2 +- cmd/layerSchema/layerSchema.go | 37 ++++++++++++++++++++++++ layer.go | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 cmd/layerSchema/layerSchema.go diff --git a/.gitignore b/.gitignore index 6bdf0fb..ad16b87 100644 --- a/.gitignore +++ b/.gitignore @@ -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 \ No newline at end of file +local_config.yaml \ No newline at end of file diff --git a/cmd/layerSchema/layerSchema.go b/cmd/layerSchema/layerSchema.go new file mode 100644 index 0000000..d52d294 --- /dev/null +++ b/cmd/layerSchema/layerSchema.go @@ -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) + } + } + } +} diff --git a/layer.go b/layer.go index ad18dfe..78d2faf 100644 --- a/layer.go +++ b/layer.go @@ -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 { @@ -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 +}