Mercurial > gemma
comparison pkg/models/cross.go @ 658:288c496eca26
Cross sections: Simplified joining segments code a bit and moved timing into controller.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 14 Sep 2018 18:02:53 +0200 |
parents | 7aeacd7f150b |
children | bd215c4325ce |
comparison
equal
deleted
inserted
replaced
657:40d3b697ea15 | 658:288c496eca26 |
---|---|
4 "bytes" | 4 "bytes" |
5 "encoding/binary" | 5 "encoding/binary" |
6 "encoding/json" | 6 "encoding/json" |
7 "errors" | 7 "errors" |
8 "fmt" | 8 "fmt" |
9 "log" | |
10 "math" | 9 "math" |
11 "sort" | 10 "sort" |
12 "time" | 11 "time" |
13 ) | 12 ) |
14 | 13 |
286 | 285 |
287 if len(ml) < 2 { | 286 if len(ml) < 2 { |
288 return ml | 287 return ml |
289 } | 288 } |
290 | 289 |
291 start := time.Now() | |
292 | |
293 type value struct { | 290 type value struct { |
294 coords GeoJSONLineCoordinatesZ | 291 coords GeoJSONLineCoordinatesZ |
295 order int | 292 order int |
296 } | 293 } |
297 | 294 |
307 heads[coords[0].quant()] = &value{coords, i} | 304 heads[coords[0].quant()] = &value{coords, i} |
308 } | 305 } |
309 } | 306 } |
310 | 307 |
311 again: | 308 again: |
312 for { | 309 for k, v1 := range heads { |
313 for k, v1 := range heads { | 310 key := v1.coords[len(v1.coords)-1].quant() |
314 key := v1.coords[len(v1.coords)-1].quant() | 311 if k == key { // cycle detected |
315 if k == key { // cycle detected | 312 continue |
316 continue | 313 } |
317 } | 314 if v2 := heads[key]; v2 != nil { |
318 if v2 := heads[key]; v2 != nil { | 315 delete(heads, key) |
319 delete(heads, key) | 316 v1.coords = v1.coords.join(v2.coords) |
320 v1.coords = v1.coords.join(v2.coords) | 317 goto again |
321 continue again | 318 } |
322 } | |
323 } | |
324 break | |
325 } | 319 } |
326 | 320 |
327 // To make it deterministic sort in input order. | 321 // To make it deterministic sort in input order. |
328 tmp := make([]*value, len(heads)) | 322 tmp := make([]*value, len(heads)) |
329 var i int | 323 var i int |
336 out := make(GeoJSONMultiLineCoordinatesZ, len(tmp)) | 330 out := make(GeoJSONMultiLineCoordinatesZ, len(tmp)) |
337 for i, v := range tmp { | 331 for i, v := range tmp { |
338 out[i] = v.coords | 332 out[i] = v.coords |
339 } | 333 } |
340 | 334 |
341 log.Printf("joining took: %s\n", time.Since(start)) | |
342 log.Printf("segments before/after: %d %d\n", len(ml), len(out)) | |
343 | |
344 return out | 335 return out |
345 } | 336 } |
346 | 337 |
347 /* | 338 /* |
348 func (ml GeoJSONMultiLineCoordinatesZ) Join() GeoJSONMultiLineCoordinatesZ { | 339 func (ml GeoJSONMultiLineCoordinatesZ) Join() GeoJSONMultiLineCoordinatesZ { |
349 | 340 |
350 if len(ml) < 2 { | 341 if len(ml) < 2 { |
351 return ml | 342 return ml |
352 } | 343 } |
353 | |
354 start := time.Now() | |
355 | 344 |
356 lists := make(GeoJSONMultiLineCoordinatesZ, len(ml)) | 345 lists := make(GeoJSONMultiLineCoordinatesZ, len(ml)) |
357 copy(lists, ml) | 346 copy(lists, ml) |
358 | 347 |
359 next: | 348 next: |
382 } | 371 } |
383 } | 372 } |
384 j++ | 373 j++ |
385 } | 374 } |
386 | 375 |
387 log.Printf("joining took: %s\n", time.Since(start)) | |
388 log.Printf("segments before/after: %d %d\n", len(ml), len(lists)) | |
389 | |
390 return lists | 376 return lists |
391 } | 377 } |
392 */ | 378 */ |