changeset 1626:92da44ba610c

WFS downloader: Parse into raw feature collections.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 19 Dec 2018 15:07:14 +0100
parents de4e4dcb8f87
children b10aa02d7819
files cmd/wfs/dump.go cmd/wfs/main.go pkg/wfs/download.go pkg/wfs/rawfeaturecollection.go
diffstat 4 files changed, 43 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/cmd/wfs/dump.go	Wed Dec 19 12:52:33 2018 +0100
+++ b/cmd/wfs/dump.go	Wed Dec 19 15:07:14 2018 +0100
@@ -15,6 +15,7 @@
 
 import (
 	"bufio"
+	"errors"
 	"fmt"
 	"io"
 	"os"
@@ -22,9 +23,38 @@
 	"gemma.intevation.de/gemma/pkg/wfs"
 )
 
+func parseFeatures(urls []string, defaultCRS string) error {
+
+	return wfs.DownloadURLs(urls, func(r io.Reader) error {
+		rfc, err := wfs.ParseRawFeatureCollection(r)
+		if err != nil {
+			return err
+		}
+		var crsName string
+		if rfc.Features == nil {
+			return errors.New("no features given")
+		}
+		if rfc.CRS != nil {
+			crsName = rfc.CRS.Properties.Name
+		} else {
+			crsName = defaultCRS
+		}
+		fmt.Printf("CRS: %s\n", crsName)
+		types := map[string]struct{}{}
+		for _, feature := range rfc.Features {
+			types[feature.Geometry.Type] = struct{}{}
+		}
+		fmt.Printf("found types in %d features:\n", len(rfc.Features))
+		for typ := range types {
+			fmt.Printf("\t%s\n", typ)
+		}
+		return nil
+	})
+}
+
 func dumpURLs(urls []string) error {
 	out := bufio.NewWriter(os.Stdout)
-	if err := wfs.downloadURLs(urls, func(r io.Reader) error {
+	if err := wfs.DownloadURLs(urls, func(r io.Reader) error {
 		_, err := io.Copy(out, r)
 		return err
 	}); err != nil {
--- a/cmd/wfs/main.go	Wed Dec 19 12:52:33 2018 +0100
+++ b/cmd/wfs/main.go	Wed Dec 19 15:07:14 2018 +0100
@@ -46,6 +46,11 @@
 			continue
 		}
 
+		feature := caps.FindFeatureType(*featureType)
+		if feature == nil {
+			log.Fatalf("Unknown feature type '%s'\n", *featureType)
+		}
+
 		urls, err := wfs.GetFeaturesGET(
 			caps, *featureType, "application/json", *sortBy)
 		check(err)
@@ -54,5 +59,7 @@
 		if *dumpFeatures {
 			check(dumpURLs(urls))
 		}
+
+		parseFeatures(urls, feature.DefaultCRS)
 	}
 }
--- a/pkg/wfs/download.go	Wed Dec 19 12:52:33 2018 +0100
+++ b/pkg/wfs/download.go	Wed Dec 19 15:07:14 2018 +0100
@@ -242,6 +242,10 @@
 	if err != nil {
 		return err
 	}
+	if resp.StatusCode < 200 || resp.StatusCode > 299 {
+		return fmt.Errorf("Invalid HTTP status code: %d (%s)",
+			resp.StatusCode, resp.Status)
+	}
 	defer resp.Body.Close()
 	return handler(resp.Body)
 }
--- a/pkg/wfs/rawfeaturecollection.go	Wed Dec 19 12:52:33 2018 +0100
+++ b/pkg/wfs/rawfeaturecollection.go	Wed Dec 19 15:07:14 2018 +0100
@@ -19,7 +19,7 @@
 )
 
 type RawFeatureCollection struct {
-	CRS struct {
+	CRS *struct {
 		Properties struct {
 			Name string `json:"name"`
 		} `json:"properties"`