Skip to content
This repository has been archived by the owner on Aug 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from PDOK/local-gpkg
Browse files Browse the repository at this point in the history
add support for local geopackage
  • Loading branch information
Danen, Gerben authored and kad-gitea-bot committed Oct 22, 2019
2 parents 79b91cc + 24966ae commit 562f61e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.dll
*.so
*.dylib
main

# Test binary, build with `go test -c`
*.test
Expand Down
34 changes: 28 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
# gpkg-to-featureinfo-texthtml
Generate HTML feature-info templates for Mapserver from GPKG

## Build
To build the application, make sure you have GoLang (v1.11+) installed.
`go build`

## Test
`go test main.go`
To test the application, make sure you have GoLang (v1.11+) installed.
`go test`

## Build
`go build main.go`
## Usage with sources
You can use either an URL where a Geopackage can be downloaded or use a local Geopackage.

Example with an URL:
`go run main.go -gpkg-url http://csu338.cs.kadaster.nl:8080/geopackages/afvalwater2016/1/afvalwater.gpkg`

Example with a local file:
`go run main.go -gpkg-path /home/user/downloads/afvalwater.gpkg`

## Usage with binary (Linux)
You can use either an URL where a Geopackage can be downloaded or use a local Geopackage.

Example with an URL:
`gpkg-to-featureinfo-texthtml -gpkg-url http://csu338.cs.kadaster.nl:8080/geopackages/afvalwater2016/1/afvalwater.gpkg`

## Usage
`go run main.go -gpkgurl http://csu338.cs.kadaster.nl:8080/geopackages/afvalwater2016/1/afvalwater.gpkg`
Example with a local file:
`gpkg-to-featureinfo-texthtml -gpkg-path /home/user/downloads/afvalwater.gpkg`

## Usage with binary (Windows)
You can use either an URL where a Geopackage can be downloaded or use a local Geopackage.

`main.exe -gpkgurl http://csu338.cs.kadaster.nl:8080/geopackages/afvalwater2016/1/afvalwater.gpkg`
Example with an URL:
`gpkg-to-featureinfo-texthtml.exe -gpkg-url http://csu338.cs.kadaster.nl:8080/geopackages/afvalwater2016/1/afvalwater.gpkg`

Example with a local file:
`gpkg-to-featureinfo-texthtml.exe -gpkg-path /home/user/downloads/afvalwater.gpkg`
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module gpkg-to-featureinfo-texthtml

require github.com/mattn/go-sqlite3 v1.10.0

go 1.13
48 changes: 31 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@ import (
)

func main() {
gpkgURLParam := flag.String("gpkgurl", "", "URL pointing to a geopackage (https://example.com/geopackage.gpkg).")
checkParameters(gpkgURLParam)
gpkgURLParam := flag.String("gpkg-url", "", "URL pointing to a geopackage (https://example.com/geopackage.gpkg)")
gpkgPathParam := flag.String("gpkg-path", "", "Path pointing to a geopackage (./geopackage.gpkg)")

gpkgFile := createTmpFile()
defer os.Remove(gpkgFile.Name())
if errDownloadGeopackage := downloadGeopackage(gpkgFile, *gpkgURLParam); errDownloadGeopackage != nil {
log.Fatal(errDownloadGeopackage)
checkParameters(gpkgURLParam, gpkgPathParam)

var gpkgFile *os.File
if *gpkgURLParam != "" {
gpkgFile = createTmpFile()
defer os.Remove(gpkgFile.Name())
if errDownloadGeopackage := downloadGeopackage(gpkgFile, *gpkgURLParam); errDownloadGeopackage != nil {
log.Fatal(errDownloadGeopackage)
}
} else {
var err error
gpkgFile, err = os.Open(*gpkgPathParam)
if err != nil {
log.Fatal("Error opening file", gpkgPathParam)
}
}
log.Println("Geopackage downloaded")

geopackage := openGeopackage(gpkgFile)
defer geopackage.Close()
geomColumns := getGeometryColumnsFromGeopackage(geopackage)
Expand All @@ -37,12 +48,13 @@ func main() {
}

// Check if parameters are provided
func checkParameters(gpkgURLParam *string) {
func checkParameters(gpkgURLParam *string, gpkgPathParam *string) {
flag.Parse()
if *gpkgURLParam == "" {
log.Fatal("gpkgUrl is required. Run with -h for help.")
if *gpkgURLParam == "" && *gpkgPathParam == "" {
log.Fatal("gpkgUrl or gpkgPath is required. Run with -h for help.")
} else if *gpkgURLParam != "" && *gpkgPathParam != "" {
log.Fatal("either gpkgUrl or gpkgPath is required. Run with -h for help.")
}
log.Printf("Starting download for: %s", *gpkgURLParam)
}

// Create a temporary file
Expand All @@ -57,13 +69,15 @@ func createTmpFile() *os.File {

// Download a Geopackage and store it in a file
func downloadGeopackage(gpkgFile *os.File, url string) error {
log.Printf("Starting download for: %s", url)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
defer gpkgFile.Close()
_, err = io.Copy(gpkgFile, resp.Body)
log.Println("Geopackage downloaded")
return err
}

Expand Down Expand Up @@ -165,7 +179,7 @@ func generateHTMLForLayer(layer string, columns []string, geomColumns []string)
}
}
}
buf.WriteString("</tr><tr>")
buf.WriteString("\t\t\t</tr>\n\t\t\t<tr>\n")
columnRowTemplate, err := template.New("column").Parse(htmlColumnRow)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -206,8 +220,8 @@ func writeHTMLfile(layer string, htmlBuffer *bytes.Buffer) {
}
}

const htmlStart = "<!-- MapServer Template --><html><head><title>GetFeatureInfo output</title></head><style type=\"text/css\">table.featureInfo, table.featureInfo td, table.featureInfo th { border: 1px solid #ddd; border-collapse: collapse; margin: 0; padding: 0; font-size: 90%; padding: .2em .1em; } table.featureInfo th { padding: .2em .2em; font-weight: bold; background: #eee; } table.featureInfo td { background: #fff; } table.featureInfo tr.odd td { background: #eee; } table.featureInfo caption { text-align: left; font-size: 100%; font-weight: bold; padding: .2em .2em; }</style><body><table class=\"featureInfo\">"
const htmlLayer = "<caption class=\"featureInfo\">{{.layer}}</caption><tr>"
const htmlColumnHead = "<th>{{.column}}</th>"
const htmlColumnRow = "<td>[{{.column}}]</td>"
const htmlEnd = "</tr></table><br /></body></html><!-- Generated by PDOK ( https://www.pdok.nl/ ) -->"
const htmlStart = "<!-- MapServer Template -->\n<html>\n\t<head>\n\t\t<title>GetFeatureInfo output</title>\n\t</head>\n\t<style type=\"text/css\">table.featureInfo, table.featureInfo td, table.featureInfo th { border: 1px solid #ddd; border-collapse: collapse; margin: 0; padding: 0; font-size: 90%; padding: .2em .1em; } table.featureInfo th { padding: .2em .2em; font-weight: bold; background: #eee; } table.featureInfo td { background: #fff; } table.featureInfo tr.odd td { background: #eee; } table.featureInfo caption { text-align: left; font-size: 100%; font-weight: bold; padding: .2em .2em; }</style>\n\t<body>\n\t\t<table class=\"featureInfo\">\n"
const htmlLayer = "\t\t\t<caption class=\"featureInfo\">{{.layer}}</caption>\n\t\t\t<tr>\n"
const htmlColumnHead = "\t\t\t\t<th>{{.column}}</th>\n"
const htmlColumnRow = "\t\t\t\t<td>[{{.column}}]</td>\n"
const htmlEnd = "\t\t\t</tr>\n\t\t</table>\n\t</body>\n</html>\n<!-- Generated by PDOK ( https://www.pdok.nl/ ) -->"
16 changes: 12 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ import (
)

func Test_checkParameters(t *testing.T) {
var testArg *string
var testArgUrl *string
temp := "-gpkgurl http://csu338.cs.kadaster.nl:8080/geopackages/afvalwater2016/1/afvalwater.gpkg"
testArg = &temp
checkParameters(testArg)
testArgUrl = &temp
var testArgPath *string
temp2 := ""
testArgPath = &temp2
checkParameters(testArgUrl, testArgPath)
temp = ""
temp2 = "-gpkgpath ./test.gpkg"
testArgUrl = &temp
testArgPath = &temp2
checkParameters(testArgUrl, testArgPath)
}

func Test_createTmpFile(t *testing.T) {
Expand All @@ -30,7 +38,7 @@ func Test_downloadGeopackage(t *testing.T) {
}

func Test_generateHTMLForLayer(t *testing.T) {
const expectedResult = "<!-- MapServer Template --><html><head><title>GetFeatureInfo output</title></head><style type=\"text/css\">table.featureInfo, table.featureInfo td, table.featureInfo th { border: 1px solid #ddd; border-collapse: collapse; margin: 0; padding: 0; font-size: 90%; padding: .2em .1em; } table.featureInfo th { padding: .2em .2em; font-weight: bold; background: #eee; } table.featureInfo td { background: #fff; } table.featureInfo tr.odd td { background: #eee; } table.featureInfo caption { text-align: left; font-size: 100%; font-weight: bold; padding: .2em .2em; }</style><body><table class=\"featureInfo\"><caption class=\"featureInfo\">testLayer</caption><tr><th>testColumn1</th><th>testColumn2</th></tr><tr><td>[testColumn1]</td><td>[testColumn2]</td></tr></table><br /></body></html><!-- Generated by PDOK ( https://www.pdok.nl/ ) -->"
const expectedResult = "<!-- MapServer Template -->\n<html>\n\t<head>\n\t\t<title>GetFeatureInfo output</title>\n\t</head>\n\t<style type=\"text/css\">table.featureInfo, table.featureInfo td, table.featureInfo th { border: 1px solid #ddd; border-collapse: collapse; margin: 0; padding: 0; font-size: 90%; padding: .2em .1em; } table.featureInfo th { padding: .2em .2em; font-weight: bold; background: #eee; } table.featureInfo td { background: #fff; } table.featureInfo tr.odd td { background: #eee; } table.featureInfo caption { text-align: left; font-size: 100%; font-weight: bold; padding: .2em .2em; }</style>\n\t<body>\n\t\t<table class=\"featureInfo\">\n\t\t\t<caption class=\"featureInfo\">testLayer</caption>\n\t\t\t<tr>\n\t\t\t\t<th>testColumn1</th>\n\t\t\t\t<th>testColumn2</th>\n\t\t\t</tr>\n\t\t\t<tr>\n\t\t\t\t<td>[testColumn1]</td>\n\t\t\t\t<td>[testColumn2]</td>\n\t\t\t</tr>\n\t\t</table>\n\t</body>\n</html>\n<!-- Generated by PDOK ( https://www.pdok.nl/ ) -->"
testColumn := []string{"testColumn1", "testColumn2", "geom", "shape_len"}
geomColumn := []string{"geo"}
htmlBuffer := generateHTMLForLayer("testLayer", testColumn, geomColumn)
Expand Down

0 comments on commit 562f61e

Please sign in to comment.