Skip to content

Commit

Permalink
handle zipped shapefile
Browse files Browse the repository at this point in the history
  • Loading branch information
hisham waleed karam committed Oct 20, 2018
1 parent 1a0878a commit 7b41022
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 13 deletions.
Binary file added testdata/faults.zip
Binary file not shown.
Binary file added testdata/faults_empty.zip
Binary file not shown.
76 changes: 64 additions & 12 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package gismanager

import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

"github.com/mholt/archiver"

//postgres Driver
_ "github.com/lib/pq"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -49,27 +53,27 @@ func GetGISFiles(root string) ([]string, error) {
return files, statErr
}
if !fileInfo.IsDir() {
files = append(files, path.Join(root, fileInfo.Name()))
extension := strings.ToLower(filepath.Ext(fileInfo.Name()))
if isSupported(extension) {
finalPath, preProcessErr := preprocessFile(root, "")
if preProcessErr != nil {
return files, preProcessErr
}
files = append(files, finalPath)
return files, nil
}
return files, nil
}
dirInfo, err := ioutil.ReadDir(root)
if err != nil {
return files, err
}
for _, file := range dirInfo {
if file.IsDir() {
subFiles, subErr := GetGISFiles(path.Join(root, file.Name()))
if subErr == nil {
files = append(files, subFiles...)
}
} else {
extension := strings.ToLower(filepath.Ext(file.Name()))
if isSupported(extension) {
files = append(files, path.Join(root, file.Name()))
}
subFiles, subErr := GetGISFiles(path.Join(root, file.Name()))
if subErr == nil {
files = append(files, subFiles...)
}
}

return files, nil
}

Expand All @@ -87,3 +91,51 @@ func DBIsAlive(dbType string, connectionStr string) (err error) {
}
return
}
func zippedShapeFile(zippedPath string, destPath string) (err error) {
fileInfo, statErr := os.Stat(zippedPath)
if statErr != nil || os.IsNotExist(statErr) {
err = statErr
return
}
if fileInfo.IsDir() {
err = errors.New("zippedPath must be file not a directory")
return
}
err = archiver.Zip.Open(zippedPath, destPath)
return
}
func preprocessFile(filePath string, tempPath string) (finalPath string, err error) {
logger := GetLogger()
ext := strings.ToLower(filepath.Ext(filePath))
switch ext {
case ".zip":
newDir, tempDirErr := ioutil.TempDir(tempPath, "zipped_shapeFile")
fmt.Println(newDir)
if tempDirErr != nil {
logger.Error(tempDirErr)
err = tempDirErr
break
}
unzipErr := zippedShapeFile(filePath, newDir)
if unzipErr != nil {
logger.Error(unzipErr)
err = unzipErr
break
}
files, filesErr := GetGISFiles(newDir)
if filesErr != nil {
logger.Error(filesErr)
err = filesErr
break
}
if len(files) == 0 {
err = errors.New("cannot find gis files")
break
}
finalPath = files[0]
break
default:
finalPath = filePath
}
return
}
26 changes: 25 additions & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gismanager

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,11 +12,34 @@ func TestIsSupported(t *testing.T) {
assert.True(t, ok)
fail := isSupported(".tiff")
assert.False(t, fail)
zippedTest := isSupported(".zip")
assert.True(t, zippedTest)

}
func TestZippedShapeFile(t *testing.T) {
newDir, _ := ioutil.TempDir("", "zipped_shapeFile")
unzipErr := zippedShapeFile("./testdata/faults.zip", newDir)
assert.Nil(t, unzipErr)
dummyErr := zippedShapeFile("./testdata/faults_ss.zip", newDir)
assert.NotNil(t, dummyErr)
dirErr := zippedShapeFile("./testdata/", newDir)
assert.NotNil(t, dirErr)
}
func TestPreprocessFile(t *testing.T) {
finalPath, preProcessErr := preprocessFile("./testdata/faults.zip", "")
assert.NotNil(t, finalPath)
assert.NotEqual(t, "", finalPath)
assert.Nil(t, preProcessErr)
errFinalPath, err := preprocessFile("./testdata/dummy.zip", "")
assert.Equal(t, "", errFinalPath)
assert.NotNil(t, err)
emptyFinalPath, emptyErr := preprocessFile("./testdata/faults_empty.zip", "")
assert.Equal(t, "", emptyFinalPath)
assert.NotNil(t, emptyErr)
}
func TestGetGISFiles(t *testing.T) {
files, err := GetGISFiles("./testdata")
assert.Equal(t, 3, len(files))
assert.Equal(t, 4, len(files))
assert.Nil(t, err)
noDir, NoDirerr := GetGISFiles("./testdata/sample.gpkg")
assert.Equal(t, 1, len(noDir))
Expand Down

0 comments on commit 7b41022

Please sign in to comment.