comparison pkg/imports/fd.go @ 5277:3bab0e19f08b

Merged multi-geoms branch.
author Sascha Wilde <sascha.wilde@intevation.de>
date Thu, 11 Jun 2020 10:09:28 +0200
parents 73563c4bba5b 569aaba23e20
children f2204f91d286
comparison
equal deleted inserted replaced
5276:6f56d17c0291 5277:3bab0e19f08b
15 15
16 import ( 16 import (
17 "context" 17 "context"
18 "database/sql" 18 "database/sql"
19 "encoding/json" 19 "encoding/json"
20 "errors"
21 "fmt" 20 "fmt"
22 "io" 21 "io"
23 "strings" 22 "strings"
24 "time" 23 "time"
25 24
45 // User is an optional username for Basic Auth. 44 // User is an optional username for Basic Auth.
46 User string `json:"user,omitempty"` 45 User string `json:"user,omitempty"`
47 // Password is an optional password for Basic Auth. 46 // Password is an optional password for Basic Auth.
48 Password string `json:"password,omitempty"` 47 Password string `json:"password,omitempty"`
49 } 48 }
50
51 var errContinue = errors.New("continue")
52 49
53 // Description gives a short info about relevant facts of this import. 50 // Description gives a short info about relevant facts of this import.
54 func (fd *FairwayDimension) Description() (string, error) { 51 func (fd *FairwayDimension) Description() (string, error) {
55 return strings.Join([]string{ 52 return strings.Join([]string{
56 fd.URL, 53 fd.URL,
366 dateInfo = start 363 dateInfo = start
367 } else { 364 } else {
368 dateInfo = (*props.HydroSorDat).Time 365 dateInfo = (*props.HydroSorDat).Time
369 } 366 }
370 367
371 // Store a feature in the database. 368 var polys multiPolygonSlice
372 storeFeature := func(p polygonSlice) error { 369
373 370 switch feature.Geometry.Type {
371 case "Polygon":
372 var p polygonSlice
373 if err := json.Unmarshal(*feature.Geometry.Coordinates, &p); err != nil {
374 return err
375 }
376 polys = multiPolygonSlice{p}
377
378 case "MultiPolygon":
379 if err := json.Unmarshal(*feature.Geometry.Coordinates, &polys); err != nil {
380 return err
381 }
382 default:
383 unsupported[feature.Geometry.Type]++
384 continue features
385 }
386
387 // Store the features.
388 storePolygons:
389 for _, p := range polys {
374 var fdid int64 390 var fdid int64
375 var lat, lon float64 391 var lat, lon float64
376 err = savepoint(func() error { 392 switch err := savepoint(func() error {
377 err := insertStmt.QueryRowContext( 393 return insertStmt.QueryRowContext(
378 ctx, 394 ctx,
379 p.asWKB(), 395 p.asWKB(),
380 epsg, 396 epsg,
381 fd.LOS, 397 fd.LOS,
382 fd.MinWidth, 398 fd.MinWidth,
383 fd.MaxWidth, 399 fd.MaxWidth,
384 fd.Depth, 400 fd.Depth,
385 dateInfo, 401 dateInfo,
386 fd.SourceOrganization, 402 fd.SourceOrganization,
387 ).Scan(&fdid, &lat, &lon) 403 ).Scan(&fdid, &lat, &lon)
388 return err 404 }); {
389 })
390 switch {
391 case err == sql.ErrNoRows: 405 case err == sql.ErrNoRows:
392 outside++ 406 outside++
393 // ignore -> filtered by responsibility area (stretches) 407 // ignore -> filtered by responsibility area (stretches)
394 return errContinue 408 continue storePolygons
395 case err != nil: 409 case err != nil:
396 feedback.Error(pgxutils.ReadableError{Err: err}.Error()) 410 feedback.Error(pgxutils.ReadableError{Err: err}.Error())
397 return errContinue 411 continue storePolygons
398 } 412 }
399 // Store for potential later removal. 413 // Store for potential later removal.
400 if err = track( 414 if err := track(
401 ctx, tx, importID, "waterway.fairway_dimensions", fdid, 415 ctx, tx, importID, "waterway.fairway_dimensions", fdid,
402 ); err != nil { 416 ); err != nil {
403 return err 417 return err
404 } 418 }
405 fds = append(fds, fdSummary{ID: fdid, Lat: lat, Lon: lon}) 419 fds = append(fds, fdSummary{ID: fdid, Lat: lat, Lon: lon})
406 420
407 features++ 421 features++
408
409 return nil
410 }
411
412 switch feature.Geometry.Type {
413 case "Polygon":
414 var p polygonSlice
415 if err := json.Unmarshal(*feature.Geometry.Coordinates, &p); err != nil {
416 return err
417 }
418 switch err := storeFeature(p); {
419 case err == errContinue:
420 continue features
421 case err != nil:
422 return err
423 }
424
425 case "MultiPolygon":
426 var mp multiPolygonSlice
427 if err := json.Unmarshal(*feature.Geometry.Coordinates, &mp); err != nil {
428 return err
429 }
430 for i := range mp {
431 switch err := storeFeature(mp[i]); {
432 case err == errContinue:
433 continue features
434 case err != nil:
435 return err
436 }
437 }
438 default:
439 unsupported[feature.Geometry.Type]++
440 } 422 }
441 } 423 }
442 return nil 424 return nil
443 }); err != nil { 425 }); err != nil {
444 return nil, err 426 return nil, err