changeset 636:7a867e48b3c2

Cross sections: Enforce minimal length of coordinates components and coordinates in input linestrings.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 13 Sep 2018 04:11:36 +0200
parents 729c2692fea0
children e72229d54c42
files pkg/models/cross.go
diffstat 1 files changed, 21 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/models/cross.go	Wed Sep 12 19:55:00 2018 +0200
+++ b/pkg/models/cross.go	Thu Sep 13 04:11:36 2018 +0200
@@ -26,10 +26,12 @@
 		Date       GeoJSONDate `json:"date"`
 	}
 
+	GeoJSONLineCoordinates []GeoJSONCoordinate
+
 	CrossSectionInput struct {
 		Type        GeoJSONFeature              `json:"type"`
 		Geometry    GeoJSONLineString           `json:"geometry"`
-		Coordinates []GeoJSONCoordinate         `json:"coordinates"`
+		Coordinates GeoJSONLineCoordinates      `json:"coordinates"`
 		Properties  CrossSectionInputProperties `json:"properties"`
 	}
 )
@@ -37,8 +39,22 @@
 var (
 	errNoGeoJSONFeature        = errors.New("Not a valid GeoJSON feature")
 	errNoGeoJSONLineStringType = errors.New("Not a valid GeoJSON linestring type")
+	errTooLessComponents       = errors.New("Too less components in coordinate")
+	errTooLessCoordinates      = errors.New("Too less coordinates")
 )
 
+func (lc *GeoJSONLineCoordinates) UnmarshalJSON(data []byte) error {
+	var coords []GeoJSONCoordinate
+	if err := json.Unmarshal(data, &coords); err != nil {
+		return err
+	}
+	if len(coords) < 2 {
+		return errTooLessCoordinates
+	}
+	*lc = GeoJSONLineCoordinates(coords)
+	return nil
+}
+
 func (d *GeoJSONDate) UnmarshalJSON(data []byte) error {
 	var s string
 	if err := json.Unmarshal(data, &s); err != nil {
@@ -53,10 +69,13 @@
 }
 
 func (c *GeoJSONCoordinate) UnmarshalJSON(data []byte) error {
-	pos := make([]float64, 2)
+	var pos []float64
 	if err := json.Unmarshal(data, &pos); err != nil {
 		return err
 	}
+	if len(pos) < 2 {
+		return errTooLessComponents
+	}
 	*c = GeoJSONCoordinate{Lon: pos[0], Lat: pos[1]}
 	return nil
 }