Mercurial > gemma
comparison pkg/imports/sr.go @ 973:b6fec8f85599
Generate TINs and octrees in sounding result importer.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 18 Oct 2018 13:09:49 +0200 |
parents | 17a03a84b0e8 |
children | 7a89313f0ead |
comparison
equal
deleted
inserted
replaced
972:17a03a84b0e8 | 973:b6fec8f85599 |
---|---|
2 | 2 |
3 import ( | 3 import ( |
4 "archive/zip" | 4 "archive/zip" |
5 "bufio" | 5 "bufio" |
6 "context" | 6 "context" |
7 "crypto/sha1" | |
7 "database/sql" | 8 "database/sql" |
9 "encoding/hex" | |
8 "encoding/json" | 10 "encoding/json" |
9 "errors" | 11 "errors" |
10 "fmt" | 12 "fmt" |
11 "io" | 13 "io" |
12 "log" | 14 "log" |
65 CASE WHEN ST_Y(ST_Centroid(point_cloud::geometry)) > 0 THEN | 67 CASE WHEN ST_Y(ST_Centroid(point_cloud::geometry)) > 0 THEN |
66 32600 | 68 32600 |
67 ELSE | 69 ELSE |
68 32700 | 70 32700 |
69 END + floor((ST_X(ST_Centroid(point_cloud::geometry))+180)/6)::int + 1` | 71 END + floor((ST_X(ST_Centroid(point_cloud::geometry))+180)/6)::int + 1` |
72 | |
73 insertOctreeSQL = ` | |
74 INSERT INTO waterway.octrees ( | |
75 sounding_result_id, | |
76 checksum, | |
77 octree_index | |
78 ) VALUES ( | |
79 $1, | |
80 $2, | |
81 $3 | |
82 )` | |
70 ) | 83 ) |
71 | 84 |
72 func (srd *SoundingResultDate) UnmarshalJSON(data []byte) error { | 85 func (srd *SoundingResultDate) UnmarshalJSON(data []byte) error { |
73 var s string | 86 var s string |
74 if err := json.Unmarshal(data, &s); err != nil { | 87 if err := json.Unmarshal(data, &s); err != nil { |
267 polygon, err := loadBoundary(z) | 280 polygon, err := loadBoundary(z) |
268 if err != nil { | 281 if err != nil { |
269 return err | 282 return err |
270 } | 283 } |
271 | 284 |
272 tx, err := conn.BeginTx(context.Background(), nil) | 285 ctx := context.Background() |
286 | |
287 tx, err := conn.BeginTx(ctx, nil) | |
273 if err != nil { | 288 if err != nil { |
274 return err | 289 return err |
275 } | 290 } |
276 defer tx.Rollback() | 291 defer tx.Rollback() |
277 | 292 |
278 var id int64 | 293 var id int64 |
279 var epsg uint32 | 294 var epsg uint32 |
280 | 295 start := time.Now() |
281 if err := tx.QueryRow(insertPointsSQL, | 296 |
297 err = tx.QueryRow(insertPointsSQL, | |
282 m.Bottleneck, | 298 m.Bottleneck, |
283 m.Date.Time, | 299 m.Date.Time, |
284 m.DepthReference, | 300 m.DepthReference, |
285 xyz.AsWKB(), | 301 xyz.AsWKB(), |
286 polygon.AsWBK(), | 302 polygon.AsWBK(), |
287 m.EPSG, | 303 m.EPSG, |
288 ).Scan(&id, &epsg); err != nil { | 304 ).Scan(&id, &epsg) |
305 xyz, polygon = nil, nil // not need from now on. | |
306 log.Printf("storing points took %s\n", time.Since(start)) | |
307 if err != nil { | |
289 return err | 308 return err |
290 } | 309 } |
291 | 310 |
292 log.Printf("EPSG: %d\n", epsg) | 311 log.Printf("EPSG: %d\n", epsg) |
293 | 312 |
294 // TODO: Build octree | 313 start = time.Now() |
314 tin, err := octree.GenerateTinByID(conn, ctx, id, epsg) | |
315 log.Printf("triangulation took %s\n", time.Since(start)) | |
316 if err != nil { | |
317 return err | |
318 } | |
319 | |
320 if tin == nil { | |
321 return errors.New("cannot load TIN from database") | |
322 } | |
323 | |
324 builder := octree.NewBuilder(tin) | |
325 start = time.Now() | |
326 builder.Build() | |
327 octreeIndex, err := builder.Bytes() | |
328 builder, tin = nil, nil // not needed from now on | |
329 log.Printf("building octree took %s\n", time.Since(start)) | |
330 if err != nil { | |
331 return err | |
332 } | |
333 | |
334 h := sha1.New() | |
335 h.Write(octreeIndex) | |
336 checksum := hex.EncodeToString(h.Sum(nil)) | |
337 | |
338 start = time.Now() | |
339 _, err = tx.Exec(insertOctreeSQL, id, checksum, octreeIndex) | |
340 log.Printf("storing octree index took %s\n", time.Since(start)) | |
341 if err != nil { | |
342 return err | |
343 } | |
344 | |
345 index, err := octree.Deserialize(octreeIndex) | |
346 if err != nil { | |
347 return err | |
348 } | |
349 | |
295 // TODO: Generate iso-lines | 350 // TODO: Generate iso-lines |
296 | 351 |
352 _ = index | |
353 | |
297 return tx.Commit() | 354 return tx.Commit() |
298 } | 355 } |