/
assign.go
968 lines (674 loc) · 24.6 KB
/
assign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
package georeference
// Eventually it would be good to abstract out all of the SFO Museum stuff from this
// but not today...
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/sfomuseum/go-sfomuseum-geo/alt"
"github.com/sfomuseum/go-sfomuseum-geo/geometry"
"github.com/sfomuseum/go-sfomuseum-geo/github"
sfom_writer "github.com/sfomuseum/go-sfomuseum-writer/v3"
"github.com/tidwall/gjson"
"github.com/whosonfirst/go-reader"
"github.com/whosonfirst/go-whosonfirst-export/v2"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
wof_reader "github.com/whosonfirst/go-whosonfirst-reader"
"github.com/whosonfirst/go-whosonfirst-uri"
"github.com/whosonfirst/go-writer-featurecollection/v3"
"github.com/whosonfirst/go-writer/v3"
"log"
"sync"
"time"
)
// AssignReferencesOptions defines a struct for reading/writing options when updating geo-related information in depictions.
// A depiction is assumed to be the record for an image or some other piece of media. A subject is assumed to be
// the record for an object.
type AssignReferencesOptions struct {
// A valid whosonfirst/go-reader.Reader instance for reading depiction features.
DepictionReader reader.Reader
// A valid whosonfirst/go-writer.Writer instance for writing depiction features.
DepictionWriter writer.Writer
// A valid whosonfirst/go-reader.Reader instance for reading subject features.
SubjectReader reader.Reader
// A valid whosonfirst/go-writer.Writer instance for writing subject features.
SubjectWriter writer.Writer
// A valid whosonfirst/go-reader.Reader instance for reading "parent" features.
WhosOnFirstReader reader.Reader
// Author is the name of a person to associate with commit messages if using a `githubapi://` writer
Author string
// SourceGeomSuffix is an additional suffix to append to 'src:geom' properties (default is "sfomuseum#geoference")
SourceGeomSuffix string
// DepictionWriterURI is the URI used to create `DepictionWriter`; it is a temporary necessity to be removed with the go-writer/v3 (clone) release
DepictionWriterURI string
// SubjectWriterURI is the URI used to create `SubjectWriter`; it is a temporary necessity to be removed with the go-writer/v3 (clone) release
SubjectWriterURI string
Logger *log.Logger
}
func AssignReferences(ctx context.Context, opts *AssignReferencesOptions, depiction_id int64, refs ...*Reference) ([]byte, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
src_geom := "sfomuseum#georeference"
if opts.SourceGeomSuffix != "" {
src_geom = fmt.Sprintf("%s-%s", src_geom, opts.SourceGeomSuffix)
}
depiction_body, err := wof_reader.LoadBytes(ctx, opts.DepictionReader, depiction_id)
if err != nil {
return nil, fmt.Errorf("Failed to create depiction reader, %w", err)
}
depiction_repo, err := properties.Repo(depiction_body)
if err != nil {
return nil, fmt.Errorf("Unabled to derive wof:repo, %w", err)
}
subject_id, err := properties.ParentId(depiction_body)
if err != nil {
return nil, fmt.Errorf("Failed to derive subject (parent) ID for depiction, %w", err)
}
// START OF to be removed once the go-writer/v4 (Clone) interface is complete
update_opts := &github.UpdateWriterURIOptions{
WhosOnFirstId: depiction_id,
Author: opts.Author,
Action: github.GeoreferenceAction,
}
depiction_wr_uri, err := github.UpdateWriterURI(ctx, update_opts, opts.DepictionWriterURI)
if err != nil {
return nil, fmt.Errorf("Failed to update depiction writer URI, %w", err)
}
depiction_writer, err := writer.NewWriter(ctx, depiction_wr_uri)
if err != nil {
return nil, fmt.Errorf("Failed to create new depiction writer, %w", err)
}
subject_wr_uri, err := github.UpdateWriterURI(ctx, update_opts, opts.SubjectWriterURI)
if err != nil {
return nil, fmt.Errorf("Failed to update subject writer URI, %w", err)
}
subject_writer, err := writer.NewWriter(ctx, subject_wr_uri)
if err != nil {
return nil, fmt.Errorf("Failed to create new subject writer, %w", err)
}
// END OF to be removed once the go-writer/v4 (Clone) interface is complete
// START OF hooks to capture updates/writes so we can parrot them back in the method response
// We're doing it this way because the code, as written, relies on sfomuseum/go-sfomuseum-writer
// which hides the format-and-export stages and modifies the document being written. To account
// for this we'll just keep local copies of those updates in *_buf and reference them at the end.
// Note that we are not doing this for the alternate geometry feature (alt_body) since are manually
// formatting, exporting and writing a byte slice in advance of better support for alternate
// geometries in the tooling.
// The buffers where we will write updated Feature information
var local_depiction_buf bytes.Buffer
var local_subject_buf bytes.Buffer
// The io.Writers where we will write updated Feature information
local_depiction_buf_writer := bufio.NewWriter(&local_depiction_buf)
local_subject_buf_writer := bufio.NewWriter(&local_subject_buf)
// Note that we are writing to a writer.FeatureCollectionWriter instead of a writer.IOWriter
// instance. This is because we end writing (potentially) multiple alternate geometries (as
// well as the depiction (image)) below. A FeatureCollectionWriter allows us to iterate over
// the results when we are constructing the final response body at the end of this function.
local_depiction_wr, err := featurecollection.NewFeatureCollectionWriterWithWriter(ctx, local_depiction_buf_writer)
if err != nil {
return nil, fmt.Errorf("Failed to create FeatureCollection writer, %w", err)
}
local_subject_wr, err := writer.NewIOWriterWithWriter(ctx, local_subject_buf_writer)
if err != nil {
return nil, fmt.Errorf("Failed to create IOWriter for subject, %w", err)
}
// The writer.MultiWriter where we will write updated Feature information
depiction_mw, err := writer.NewMultiWriter(ctx, depiction_writer, local_depiction_wr)
if err != nil {
return nil, fmt.Errorf("Failed to create multi writer for depiction, %w", err)
}
subject_mw, err := writer.NewMultiWriter(ctx, subject_writer, local_subject_wr)
if err != nil {
return nil, fmt.Errorf("Failed to create multi writer for subject, %w", err)
}
// END OF hooks to capture updates/writes so we can parrot them back in the method response
// TBD: use of https://github.com/whosonfirst/go-reader-cachereader for reading depictions
// Maybe check for non-nil opts.DepictionCache and update depiction_reader accordingly?
var depiction_reader reader.Reader
depiction_reader = opts.DepictionReader
// START OF update the depiction record
done_ch := make(chan bool)
err_ch := make(chan error)
alt_ch := make(chan *alt.WhosOnFirstAltFeature)
references_map := new(sync.Map)
updates_map := new(sync.Map)
// START OF create/update alt files for references
new_alt_features := make([]*alt.WhosOnFirstAltFeature, 0)
other_alt_features := make([]*alt.WhosOnFirstAltFeature, 0)
for _, r := range refs {
go func(ctx context.Context, r *Reference) {
defer func() {
done_ch <- true
}()
if len(r.Ids) == 0 {
return
}
prop_label := r.Property
alt_label := r.AltLabel
path := fmt.Sprintf("properties.%s", prop_label)
updates_map.Store(path, r.Ids)
count := len(r.Ids)
points := make([]orb.Point, count)
for idx, id := range r.Ids {
body, err := wof_reader.LoadBytes(ctx, opts.WhosOnFirstReader, id)
if err != nil {
err_ch <- fmt.Errorf("Failed to read body for %d, %w", id, err)
return
}
hiers := properties.Hierarchies(body)
for _, h := range hiers {
for _, h_id := range h {
references_map.Store(h_id, true)
}
}
pt, _, err := properties.Centroid(body)
if err != nil {
err_ch <- fmt.Errorf("Failed to derive centroid for %d, %w", id, err)
return
}
points[idx] = *pt
}
mp := orb.MultiPoint(points)
alt_geom := geojson.NewGeometry(mp)
alt_props := map[string]interface{}{
"wof:id": depiction_id,
"wof:repo": depiction_repo,
"src:alt_label": alt_label,
"src:geom": src_geom,
}
alt_props[prop_label] = r.Ids
alt_feature := &alt.WhosOnFirstAltFeature{
Type: "Feature",
Id: depiction_id,
Properties: alt_props,
Geometry: alt_geom,
}
alt_ch <- alt_feature
}(ctx, r)
}
remaining := len(refs)
for remaining > 0 {
select {
case <-ctx.Done():
break
case <-done_ch:
remaining -= 1
case err := <-err_ch:
return nil, err
case alt_f := <-alt_ch:
new_alt_features = append(new_alt_features, alt_f)
}
}
// START OF create/update alt files for references
depiction_updates := map[string]interface{}{
"properties.src:geom": src_geom,
}
references := make([]int64, 0)
refs_rsp := gjson.GetBytes(depiction_body, "properties.wof:references")
for _, r := range refs_rsp.Array() {
references_map.Store(r.Int(), true)
}
references_map.Range(func(k interface{}, v interface{}) bool {
id := k.(int64)
references = append(references, id)
return true
})
depiction_updates["properties.wof:references"] = references
updates_map.Range(func(k interface{}, v interface{}) bool {
path := k.(string)
depiction_updates[path] = v
return true
})
// START OF resolve alt files
// Create a lookup table of the new alt geom labels
lookup := make(map[string]bool)
for _, f := range new_alt_features {
label := f.Properties["src:alt_label"].(string)
lookup[label] = true
}
// Fetch the existing alt geom labels associated with this record
existing_alt, err := properties.AltGeometries(depiction_body)
if err != nil {
return nil, fmt.Errorf("Failed to determine existing alt files, %w", err)
}
// Determine whether there are any other alt files (not included in the set of new labels)
// First create a lookup table for alt files that need to be "removed"
to_remove := make(map[string]*Reference)
for _, r := range refs {
if len(r.Ids) == 0 {
to_remove[r.AltLabel] = r
}
}
// Now build the list of features (used to build alt files) to fetch
// Note how we are skipping features to remove
to_fetch := make([]string, 0)
for _, label := range existing_alt {
_, ok_lookup := lookup[label]
_, ok_remove := to_remove[label]
if !ok_lookup && !ok_remove {
to_fetch = append(to_fetch, label)
}
}
// Fetch any extra alt geometries, if necessary
if len(to_fetch) > 0 {
done_ch := make(chan bool)
err_ch := make(chan error)
alt_ch := make(chan *alt.WhosOnFirstAltFeature)
for _, label := range to_fetch {
go func(label string) {
defer func() {
done_ch <- true
}()
alt_uri_geom := &uri.AltGeom{
Source: label,
}
alt_uri_args := &uri.URIArgs{
IsAlternate: true,
AltGeom: alt_uri_geom,
}
alt_uri, err := uri.Id2RelPath(depiction_id, alt_uri_args)
if err != nil {
err_ch <- fmt.Errorf("Failed to derive rel path for alt file, %w", err)
return
}
r, err := depiction_reader.Read(ctx, alt_uri)
if err != nil {
err_ch <- fmt.Errorf("Failed to read %s, %w", alt_uri, err)
return
}
defer r.Close()
var f *alt.WhosOnFirstAltFeature
dec := json.NewDecoder(r)
err = dec.Decode(&f)
if err != nil {
err_ch <- fmt.Errorf("Failed to decode %s, %w", alt_uri, err)
return
}
alt_ch <- f
}(label)
remaining := len(to_fetch)
for remaining > 0 {
select {
case <-done_ch:
remaining -= 1
case err := <-err_ch:
return nil, err
case f := <-alt_ch:
other_alt_features = append(other_alt_features, f)
}
}
}
}
// Combine new and other alt features
alt_features := make([]*alt.WhosOnFirstAltFeature, 0)
for _, f := range new_alt_features {
alt_features = append(alt_features, f)
}
for _, f := range other_alt_features {
alt_features = append(alt_features, f)
}
// Use this new list to catalog alt geoms and derived a multipoint geometry
alt_geoms := make([]string, len(alt_features))
for idx, f := range alt_features {
label := f.Properties["src:alt_label"].(string)
alt_geoms[idx] = label
}
depiction_updates["properties.src:geom_alt"] = alt_geoms
// Derive a MultiPoint geometry
mp_geom, err := alt.DeriveMultiPointGeometry(ctx, alt_features...)
if err != nil {
return nil, fmt.Errorf("Failed to derive multi point geometry, %w", err)
}
mp_geojson_geom := geojson.NewGeometry(mp_geom)
depiction_updates["geometry"] = mp_geojson_geom
// Now save the new alt files
for _, f := range new_alt_features {
label := f.Properties["src:alt_label"].(string)
alt_uri_geom := &uri.AltGeom{
Source: label,
}
alt_uri_args := &uri.URIArgs{
IsAlternate: true,
AltGeom: alt_uri_geom,
}
alt_uri, err := uri.Id2RelPath(depiction_id, alt_uri_args)
if err != nil {
return nil, fmt.Errorf("Failed to derive rel path for alt file, %w", err)
}
enc_f, err := alt.FormatAltFeature(f)
if err != nil {
return nil, fmt.Errorf("Failed to format %s, %w", alt_uri, err)
}
r := bytes.NewReader(enc_f)
// Note how we're invoking depiction_wr directly because sfom_writer doesn't
// know how to work with alt files yet.
_, err = depiction_mw.Write(ctx, alt_uri, r)
if err != nil {
return nil, fmt.Errorf("Failed to write %s, %w", alt_uri, err)
}
}
// Now rewrite alt files that need to be "removed"
for _, ref := range to_remove {
alt_uri_geom := &uri.AltGeom{
Source: ref.AltLabel,
}
alt_uri_args := &uri.URIArgs{
IsAlternate: true,
AltGeom: alt_uri_geom,
}
alt_uri, err := uri.Id2RelPath(depiction_id, alt_uri_args)
if err != nil {
return nil, fmt.Errorf("Failed to derive rel path for alt file, %w", err)
}
// In advance of a generic "exists" method/package this will have to do...
_, err = depiction_reader.Read(ctx, alt_uri)
if err != nil {
continue
}
now := time.Now()
deprecated := now.Format("2006-01-02")
alt_props := map[string]interface{}{
"edtf:deprecated": deprecated,
"src:alt_label": ref.AltLabel,
"src:geom": "sfomuseum#georeference-flightcover",
"wof:id": depiction_id,
"wof:repo": "sfomuseum-data-media-collection",
}
pt := orb.Point{0.0, 0.0}
alt_geom := geojson.NewGeometry(pt)
alt_f := &alt.WhosOnFirstAltFeature{
Type: "Feature",
Id: depiction_id,
Properties: alt_props,
Geometry: alt_geom,
}
enc_f, err := alt.FormatAltFeature(alt_f)
if err != nil {
return nil, fmt.Errorf("Failed to format %s, %w", alt_uri, err)
}
r := bytes.NewReader(enc_f)
// Note how we're invoking depiction_wr directly because sfom_writer doesn't
// know how to work with alt files yet.
_, err = depiction_mw.Write(ctx, alt_uri, r)
if err != nil {
return nil, fmt.Errorf("Failed to write %s, %w", alt_uri, err)
}
}
// END OF resolve alt files
depiction_removals := make([]string, 0)
for _, r := range refs {
if len(r.Ids) == 0 {
path := fmt.Sprintf("properties.%s", r.Property)
depiction_removals = append(depiction_removals, path)
}
}
if len(depiction_removals) > 0 {
depiction_body, err = export.RemoveProperties(ctx, depiction_body, depiction_removals)
if err != nil {
return nil, fmt.Errorf("Failed to remove depiction properties, %w", err)
}
}
depiction_has_changed, new_body, err := export.AssignPropertiesIfChanged(ctx, depiction_body, depiction_updates)
if err != nil {
return nil, fmt.Errorf("Failed to assign depiction properties, %w", err)
}
if depiction_has_changed {
// _, err = sfom_writer.WriteBytes(ctx, depiction_wr, new_body)
_, err = sfom_writer.WriteBytes(ctx, depiction_mw, new_body)
if err != nil {
return nil, fmt.Errorf("Failed to write depiction update, %w", err)
}
}
// END OF update the depiction record
// START OF update the subject (parent) record
subject_body, err := wof_reader.LoadBytes(ctx, opts.SubjectReader, subject_id)
if err != nil {
return nil, fmt.Errorf("Failed to load subject (parent) for depiction, %w", err)
}
// Things to update in the subject (object) record
subject_updates := map[string]interface{}{
"properties.src:geom": depiction_updates["properties.src:geom"],
}
// Things to remove from the subject record - this is tightly integrated with the
// georef_properties_lookup table below
subject_removals := make([]string, 0)
// Lookup table for all the flightcover properties across all the images for an objects
georef_properties_lookup := new(sync.Map)
// Assign the reference pointers to the subject record
fq_from := fmt.Sprintf("properties.%s", PROPERTY_FLIGHTCOVER_FROM)
fq_to := fmt.Sprintf("properties.%s", PROPERTY_FLIGHTCOVER_TO)
fq_sent := fmt.Sprintf("properties.%s", PROPERTY_FLIGHTCOVER_SENT)
fq_received := fmt.Sprintf("properties.%s", PROPERTY_FLIGHTCOVER_RECEIVED)
georeferences_paths := []string{
fq_from,
fq_to,
fq_sent,
fq_received,
}
updates_map.Range(func(k interface{}, v interface{}) bool {
path := k.(string)
switch path {
case fq_from, fq_to, fq_sent, fq_received:
georef_ids_lookup := new(sync.Map)
ids := v.([]int64)
for _, id := range ids {
georef_ids_lookup.Store(id, true)
}
georef_properties_lookup.Store(path, georef_ids_lookup)
// subject record georeference properties are not updated until
// the combined properties for all the images are gathered (below)
default:
subject_updates[path] = v
}
return true
})
references_lookup := new(sync.Map)
for _, r := range references {
references_lookup.Store(r, true)
}
subject_refs := gjson.GetBytes(subject_body, "properties.wof:references")
for _, r := range subject_refs.Array() {
references_lookup.Store(r.Int(), true)
}
subject_references := make([]int64, 0)
references_lookup.Range(func(k interface{}, v interface{}) bool {
id := k.(int64)
subject_references = append(subject_references, id)
return true
})
subject_updates["properties.wof:references"] = subject_references
//
geoms_lookup := new(sync.Map)
georefs_lookup := new(sync.Map)
georefs_lookup.Store(depiction_id, true)
geoms_lookup.Store(depiction_id, true)
geotag_depictions_rsp := gjson.GetBytes(subject_body, "properties.geotag:depictions")
for _, r := range geotag_depictions_rsp.Array() {
id := r.Int()
geoms_lookup.Store(id, true)
}
// The problem here is that we haven't written/published the geom changes so
// they won't be picked up by the depiction_reader passed to geometry.DeriveMultiPointFromIds
// below. Even if we have (written the changes) they won't be able to be read
// if we are using different readers/writers (for example repo:// and stdout://)
georefs_depictions_rsp := gjson.GetBytes(subject_body, "properties.georeference:depictions")
for _, r := range georefs_depictions_rsp.Array() {
id := r.Int()
geoms_lookup.Store(id, true)
georefs_lookup.Store(id, true)
}
georeferences := make([]int64, 0)
georefs_lookup.Range(func(k interface{}, v interface{}) bool {
id := k.(int64)
georeferences = append(georeferences, id)
return true
})
subject_updates["properties.georeference:depictions"] = georeferences
// START OF derive geometry from depictions (media/image files)
geom_ids := make([]int64, 0)
geoms_lookup.Range(func(k interface{}, v interface{}) bool {
id := k.(int64)
// Do not try to fetch the geometry for depiction ID from depiction_reader
// because it hasn't been written/published yet and we will update things from
// memory below
if id != depiction_id {
geom_ids = append(geom_ids, id)
}
return true
})
if len(geom_ids) > 0 {
geom, err := geometry.DeriveMultiPointFromIds(ctx, depiction_reader, geom_ids...)
if err != nil {
return nil, fmt.Errorf("Failed to derive multipoint geometry for subject, %w", err)
}
// Now append the geometry for the depiction
subject_orb_geom := geom.Geometry()
subject_points := subject_orb_geom.(orb.MultiPoint)
depiction_geom := depiction_updates["geometry"].(*geojson.Geometry)
depiction_orb_geom := depiction_geom.Geometry()
depiction_points := depiction_orb_geom.(orb.MultiPoint)
for _, pt := range depiction_points {
subject_points = append(subject_points, pt)
}
new_mp := orb.MultiPoint(subject_points)
new_geom := geojson.NewGeometry(new_mp)
subject_updates["geometry"] = new_geom
} else {
// No other gemoetries so just append the geometry for the depiction
subject_updates["geometry"] = depiction_updates["geometry"]
}
// END OF derive geometry from depictions (media/image files)
// START OF denormalize all the georeferenced properties (flightcover, etc.) from all the images in to the object record
type image_ref struct {
path string
id int64
}
im_done_ch := make(chan bool)
im_err_ch := make(chan error)
im_ref_ch := make(chan image_ref)
im_remaining := 0
// Replace with georeferences array defined above?
images_rsp := gjson.GetBytes(subject_body, "properties.millsfield:images")
images_list := images_rsp.Array()
for _, r := range images_list {
image_id := r.Int()
// Remember these have been assigned above
if image_id == depiction_id {
continue
}
im_remaining += 1
go func(image_id int64) {
defer func() {
im_done_ch <- true
}()
image_body, err := wof_reader.LoadBytes(ctx, depiction_reader, image_id)
if err != nil {
im_err_ch <- fmt.Errorf("Failed to read %d, %w", image_id, err)
return
}
for _, path := range georeferences_paths {
rsp := gjson.GetBytes(image_body, path)
for _, r := range rsp.Array() {
im_ref_ch <- image_ref{path: path, id: r.Int()}
}
}
}(image_id)
}
// Wait...
for im_remaining > 0 {
select {
case <-im_done_ch:
im_remaining -= 1
case err := <-im_err_ch:
return nil, fmt.Errorf("Failed to denormalize georeference properties, %w", err)
case ref := <-im_ref_ch:
var georef_ids_lookup *sync.Map
ids_v, ok := georef_properties_lookup.Load(ref.path)
if !ok {
georef_ids_lookup = new(sync.Map)
} else {
georef_ids_lookup = ids_v.(*sync.Map)
}
georef_ids_lookup.Store(ref.id, true)
georef_properties_lookup.Store(ref.path, georef_ids_lookup)
}
}
// Once all the images have been reviewed for flightcover (georeference) properties
// figure out which ones need to be updated in or removed from the subject record
for _, path := range georeferences_paths {
ids_v, ok := georef_properties_lookup.Load(path)
if ok {
ids := make([]int64, 0)
georef_ids_lookup := ids_v.(*sync.Map)
georef_ids_lookup.Range(func(k interface{}, v interface{}) bool {
ids = append(ids, k.(int64))
return true
})
subject_updates[path] = ids
} else {
subject_removals = append(subject_removals, path)
}
}
// END OF denormalize all the georeferenced properties (flightcover, etc.) from all the images in to the object record
if len(subject_removals) > 0 {
subject_body, err = export.RemoveProperties(ctx, subject_body, subject_removals)
if err != nil {
return nil, fmt.Errorf("Failed to remove depiction properties, %w", err)
}
}
subject_has_changed, new_subject, err := export.AssignPropertiesIfChanged(ctx, subject_body, subject_updates)
if err != nil {
return nil, fmt.Errorf("Failed to assign subject properties, %w", err)
}
if subject_has_changed {
_, err = sfom_writer.WriteBytes(ctx, subject_mw, new_subject)
if err != nil {
return nil, fmt.Errorf("Failed to write subject update, %w", err)
}
}
// END OF update the subject (parent) record
// Close the depiction and subject writers - this is a no-op for many writer but
// required for things like the githubapi-tree:// and githubapi-pr:// writers.
err = depiction_mw.Close(ctx)
if err != nil {
return nil, fmt.Errorf("Failed to close depiction writer, %w", err)
}
err = subject_mw.Close(ctx)
if err != nil {
return nil, fmt.Errorf("Failed to close subject writer, %w", err)
}
//
local_depiction_buf_writer.Flush()
local_subject_buf_writer.Flush()
fc := geojson.NewFeatureCollection()
var new_subject_b []byte
if subject_has_changed {
new_subject_b = local_subject_buf.Bytes()
} else {
new_subject_b = subject_body
}
new_subject_f, err := geojson.UnmarshalFeature(new_subject_b)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal feature from depiction buffer, %w", err)
}
fc.Append(new_subject_f)
if depiction_has_changed {
new_depiction_b := local_depiction_buf.Bytes()
new_depiction_fc, err := geojson.UnmarshalFeatureCollection(new_depiction_b)
if err != nil {
return nil, fmt.Errorf("Failed to unmarshal feature from depiction buffer, %w '%s'", err, string(new_depiction_b))
}
for _, f := range new_depiction_fc.Features {
fc.Append(f)
}
}
fc_body, err := fc.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("Failed to marshal feature collection, %w", err)
}
return fc_body, nil
}