Mercurial > gemma
view pkg/imports/polygon.go @ 1241:a45fa8943254
Sounding result import: Forgot to return the token temp file to the importer. Doh!
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 20 Nov 2018 18:05:19 +0100 |
parents | a244b18cb916 |
children | 3bf1f0de0763 |
line wrap: on
line source
// This is Free Software under GNU Affero General Public License v >= 3.0 // without warranty, see README.md and license for details. // // SPDX-License-Identifier: AGPL-3.0-or-later // License-Filename: LICENSES/AGPL-3.0.txt // // Copyright (C) 2018 by via donau // – Österreichische Wasserstraßen-Gesellschaft mbH // Software engineering by Intevation GmbH // // Author(s): // * Sascha L. Teichmann <sascha.teichmann@intevation.de> package imports import ( "bytes" "encoding/binary" "fmt" "math" shp "github.com/jonas-p/go-shp" ) type ( Point struct { X float64 Y float64 } LineString []Point Polygon []LineString ) const ( wkbNDR byte = 1 wkbPolygon uint32 = 3 ) func shapeToPolygon(s shp.Shape) (Polygon, error) { switch p := s.(type) { case *shp.Polygon: return toPolygon(p.NumParts, p.Parts, p.Points), nil case *shp.PolygonZ: return toPolygon(p.NumParts, p.Parts, p.Points), nil case *shp.PolygonM: return toPolygon(p.NumParts, p.Parts, p.Points), nil } return nil, fmt.Errorf("Unsupported shape type %T", s) } func toPolygon(numParts int32, parts []int32, points []shp.Point) Polygon { out := make(Polygon, numParts) pos := 0 for i := range out { ps := parts[i] line := make(LineString, ps) for j := int32(0); j < ps; j, pos = j+1, pos+1 { p := &points[pos] line[j] = Point{p.X, p.Y} } out[i] = line } return out } func (p Polygon) AsWBK() []byte { if p == nil { return nil } // pre-calculate size to avoid reallocations. size := 1 + 4 + 4 for _, ring := range p { size += 4 + len(ring)*2*8 } buf := bytes.NewBuffer(make([]byte, 0, size)) binary.Write(buf, binary.LittleEndian, wkbNDR) binary.Write(buf, binary.LittleEndian, wkbPolygon) binary.Write(buf, binary.LittleEndian, uint32(len(p))) for _, ring := range p { binary.Write(buf, binary.LittleEndian, uint32(len(ring))) for _, v := range ring { binary.Write(buf, binary.LittleEndian, math.Float64bits(v.X)) binary.Write(buf, binary.LittleEndian, math.Float64bits(v.Y)) } } return buf.Bytes() }