Mercurial > gemma
view pkg/imports/stsh.go @ 4314:c3b5cf2f200a
shape upload stretch import: Converted multi polygon to WKB.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 03 Sep 2019 17:12:07 +0200 |
parents | 5da02dcc51f6 |
children | 3d6a2c6b436c |
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) 2019 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 ( "archive/zip" "context" "database/sql" "errors" "fmt" "log" "os" "path" "path/filepath" "strings" shp "github.com/jonas-p/go-shp" "gemma.intevation.de/gemma/pkg/common" ) type StretchShape struct { Dir string `json:"dir"` } const STSHJobKind JobKind = "stsh" type stshJobCreator struct{} func init() { RegisterJobCreator(STSHJobKind, stshJobCreator{}) } func (stshJobCreator) Description() string { return "stretch from shape" } func (stshJobCreator) AutoAccept() bool { return false } func (stshJobCreator) Create() Job { return new(StretchShape) } func (stshJobCreator) Depends() [2][]string { return [2][]string{ {"stretches", "stretch_countries"}, {}, } } func (stshJobCreator) StageDone( ctx context.Context, tx *sql.Tx, id int64, ) error { // TODO: Implement me! return nil } func (stsh *StretchShape) CleanUp() error { return os.RemoveAll(stsh.Dir) } func (stsh *StretchShape) Do( ctx context.Context, importID int64, conn *sql.Conn, feedback Feedback, ) (interface{}, error) { //start := time.Now() zpath := filepath.Join(stsh.Dir, "stretch.zip") z, err := zip.OpenReader(zpath) if err != nil { return nil, err } defer z.Close() shpF := common.FindInZIP(z, ".shp") if shpF == nil { return nil, errors.New("no SHP file found in ZIP") } prefix := strings.TrimSuffix(shpF.Name, path.Ext(shpF.Name)) dbfF := common.FindInZIP(z, prefix+".dbf") if dbfF == nil { return nil, fmt.Errorf("no DBF file found for %s", shpF.Name) } shpR, err := shpF.Open() if err != nil { return nil, err } dbfR, err := dbfF.Open() if err != nil { shpR.Close() return nil, err } sr := shp.SequentialReaderFromExt(shpR, dbfR) defer sr.Close() fields := sr.Fields() for sr.Next() { _, p := sr.Shape() if p == nil { feedback.Warn("Invalid NULL geometry found.") continue } poly, err := shapeToPolygon(p) if err != nil { feedback.Warn("Invalid geometry found: %v.", err) continue } // Convert to a multi polygon. mp := poly.MultiPolygonGeom() wkb := mp.AsWKB() _ = wkb for k, f := range fields { name := f.String() val := sr.Attribute(k) log.Printf("info: \t%s = %v\n", name, val) } // TODO: Implement me! } if err := sr.Err(); err != nil { return nil, err } return nil, nil }