Module: NSWTopo::Labels

Extended by:
Forwardable
Includes:
Log, Vector
Defined in:
lib/nswtopo/layer/labels.rb,
lib/nswtopo/layer/labels/barrier.rb

Defined Under Namespace

Modules: LabelFeatures Classes: Barrier, Label

Constant Summary collapse

CENTRELINE_FRACTION =
0.35
DEFAULT_SAMPLE =
5
INSET =
1
LABEL_ATTRIBUTES =
%w[
  coexist
  curved
  font-family
  font-size
  font-style
  font-variant
  font-weight
  format
  knockout
  letter-spacing
  line-height
  margin
  max-angle
  max-turn
  min-radius
  optional
  orientation
  position
  sample
  separation
  separation-all
  separation-along
  shield
  upcase
  word-spacing
]
LABEL_TRANSFORMS =
%w[
  buffer
  fallback
  keep-largest
  minimum-area
  minimum-hole
  minimum-length
  offset
  reduce
  remove
  remove-holes
  smooth
  trim
]
LABEL_PARAMS =
LABEL_ATTRIBUTES + LABEL_TRANSFORMS + SVG_ATTRIBUTES
DEFAULTS =
YAML.load <<~YAML
  knockout: true
  preserve: true
  stroke: none
  fill: black
  font-style: italic
  font-family: Arial, Helvetica, sans-serif
  font-size: 1.8
  line-height: 110%
  margin: 1.0
  max-turn: 60
  min-radius: 0
  max-angle: #{StraightSkeleton::DEFAULT_ROUNDING_ANGLE}
  sample: #{DEFAULT_SAMPLE}
YAML
DEBUG_PARAMS =
YAML.load <<~YAML
  debug:
    dupe: ~
    fill: none
    opacity: 0.5
    knockout: false
  debug feature:
    stroke: hsl(260,100%,50%)
    stroke-width: 0.2
    symbol:
      circle:
        r: 0.3
        stroke: none
        fill: hsl(260,100%,50%)
  debug candidate:
    stroke: hsl(300,100%,50%)
    stroke-width: 0.2
YAML

Constants included from Vector

Vector::FONT_SCALED_ATTRIBUTES, Vector::MARGIN, Vector::SVG_ATTRIBUTES

Constants included from Log

NSWTopo::Log::FAILURE, NSWTopo::Log::NEUTRAL, NSWTopo::Log::SUCCESS, NSWTopo::Log::UPDATE

Instance Method Summary collapse

Methods included from Vector

#categorise, #create, #features, #filename, #labeling_features, #params_for, #render, #svg_path_data, #to_s

Methods included from Log

#log_abort, #log_neutral, #log_success, #log_update, #log_warn

Instance Method Details

#add(layer) ⇒ Object



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
# File 'lib/nswtopo/layer/labels.rb', line 109

def add(layer)
  label_params = layer.params.fetch("labels", {})
  label_params.except(*LABEL_PARAMS).select do |key, value|
    Hash === value
  end.transform_keys do |categories|
    Array(categories).map do |category|
      [layer.name, category].join(?\s)
    end
  end.then do |params|
    { layer.name => label_params }.merge(params)
  end.transform_values do |params|
    params.slice(*LABEL_PARAMS)
  end.transform_values do |params|
    # handle legacy format for separation, separation-all, separation-along
    params.each.with_object("separation" => Hash[]) do |(key, value), hash|
      case [key, value]
      in ["separation",    Hash] then hash["separation"].merge! value
      in ["separation",       *] then hash["separation"].merge! "self"  => value
      in ["separation-all",   *] then hash["separation"].merge! "other" => value
      in ["separation-along", *] then hash["separation"].merge! "along" => value
      else hash[key] = value
      end
    end
  end.then do |category_params|
    @params.merge! category_params
  end

  feature_count = feature_total = 0
  layer.labeling_features.tap do |features|
    feature_total = features.length
  end.map(&:multi).group_by do |feature|
    Set[layer.name, *feature["category"]]
  end.each do |categories, features|
    transforms = params_for(categories).slice(*LABEL_TRANSFORMS)
    attributes, point_attributes, line_attributes = [nil, "point", "line"].map do |extra_category|
      categories | Set[*extra_category]
    end.map do |categories|
      params_for(categories).slice(*LABEL_ATTRIBUTES).merge("categories" => categories)
    end

    features.map do |feature|
      log_update "collecting labels: %s: feature %i of %i" % [layer.name, feature_count += 1, feature_total]
      text = feature["label"]
      text = case
      when REXML::Element === text then text
      when attributes["format"] then attributes["format"] % text
      else Array(text).map(&:to_s).map(&:strip).join(?\s)
      end
      dual = feature["dual"]
      text.upcase! if String === text && attributes["upcase"]
      dual.upcase! if String === dual && attributes["upcase"]

      transforms.inject([feature]) do |features, (transform, (arg, *args))|
        next features unless arg
        opts, args = args.partition do |arg|
          Hash === arg
        end
        opts = opts.inject({}, &:merge).transform_keys(&:to_sym)
        features.flat_map do |feature|
          case transform
          when "reduce"
            case arg
            when "skeleton"
              feature.respond_to?(arg) ? feature.send(arg) : feature
            when "centrelines"
              feature.respond_to?(arg) ? feature.send(arg, **opts) : feature
            when "centrepoints"
              interval = Float(opts.delete(:interval) || DEFAULT_SAMPLE)
              feature.respond_to?(arg) ? feature.send(arg, interval: interval, **opts) : feature
            when "centres"
              interval = Float(opts.delete(:interval) || DEFAULT_SAMPLE)
              feature.respond_to?(arg) ? feature.send(arg, interval: interval, **opts) : feature
            when "centroids"
              feature.respond_to?(arg) ? feature.send(arg) : feature
            when "samples"
              interval = Float(opts.delete(:interval) || DEFAULT_SAMPLE)
              feature.respond_to?(arg) ? feature.send(arg, interval) : feature
            else
              raise "unrecognised label transform: reduce: %s" % arg
            end

          when "fallback"
            case arg
            when "samples"
              next feature unless feature.respond_to? arg
              interval = Float(opts.delete(:interval) || DEFAULT_SAMPLE)
              [feature, *feature.send(arg, interval)]
            else
              raise "unrecognised label transform: fallback: %s" % arg
            end

          when "offset", "buffer"
            next feature unless feature.respond_to? transform
            margins = [arg, *args].map { |value| Float(value) }
            feature.send transform, *margins, **opts

          when "smooth"
            next feature unless feature.respond_to? transform
            margin = Float(arg)
            max_turn = attributes["max-turn"] * Math::PI / 180
            feature.send transform, margin, cutoff_angle: max_turn, **opts

          when "minimum-area"
            area = Float(arg)
            case feature
            when GeoJSON::MultiLineString
              feature.coordinates = feature.coordinates.reject do |linestring|
                linestring.first == linestring.last && linestring.signed_area.abs < area
              end
            when GeoJSON::MultiPolygon
              feature.coordinates = feature.coordinates.reject do |rings|
                rings.sum(&:signed_area) < area
              end
            end
            feature.empty? ? [] : feature

          when "minimum-length"
            next feature unless GeoJSON::MultiLineString === feature
            distance = Float(arg)
            feature.coordinates = feature.coordinates.reject do |linestring|
              linestring.path_length < distance
            end
            feature.empty? ? [] : feature

          when "minimum-hole", "remove-holes"
            area = Float(arg).abs unless true == arg
            feature.coordinates = feature.coordinates.map do |rings|
              rings.reject do |ring|
                area ? (-area...0) === ring.signed_area : ring.signed_area < 0
              end
            end if GeoJSON::MultiPolygon === feature
            feature

          when "remove"
            remove = [arg, *args].any? do |value|
              case value
              when true    then true
              when String  then text == value
              when Regexp  then text =~ value
              when Numeric then text == value.to_s
              end
            end
            remove ? [] : feature

          when "keep-largest"
            case feature
            when GeoJSON::MultiLineString
              feature.coordinates = [feature.explode.max_by(&:length).coordinates]
            when GeoJSON::MultiPolygon
              feature.coordinates = [feature.explode.max_by(&:area).coordinates]
            end
            feature

          when "trim"
            next feature unless GeoJSON::MultiLineString === feature
            distance = Float(arg)
            feature.coordinates = feature.coordinates.map do |linestring|
              linestring.trim distance
            end.reject(&:empty?)
            feature.empty? ? [] : feature
          end
        end
      rescue ArgumentError
        raise "invalid label transform: %s: %s" % [transform, [arg, *args].join(?,)]
      end.each do |feature|
        feature.properties = case feature
        when GeoJSON::MultiPoint      then point_attributes
        when GeoJSON::MultiLineString then line_attributes
        when GeoJSON::MultiPolygon    then line_attributes
        end
      end.then do |features|
        GeoJSON::Collection.new(projection: @map.neatline.projection, features: features).explode.extend(LabelFeatures)
      end.tap do |collection|
        collection.text, collection.dual, collection.layer_name = text, dual, layer.name
      end
    end.then do |collections|
      next collections unless label_params["collate"]
      collections.group_by(&:text).map do |text, collections|
        collections.inject(&:merge!)
      end
    end.each do |collection|
      label_features << collection
    end
  end
end

#barrier_segmentsObject



364
365
366
367
368
# File 'lib/nswtopo/layer/labels.rb', line 364

def barrier_segments
  @barrier_segments ||= barriers.flat_map(&:segments).then do |segments|
    RTree.load(segments, &:bounds)
  end
end

#barriersObject



94
95
96
# File 'lib/nswtopo/layer/labels.rb', line 94

def barriers
  @barriers ||= []
end

#drawing_featuresObject



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
# File 'lib/nswtopo/layer/labels.rb', line 717

def drawing_features
  debug_features = []
  candidates = label_candidates do |feature, category|
    debug_features << [feature, Set["debug", category]]
  end

  conflicts = candidates.map do |candidate|
    [candidate, candidate.conflicts.dup]
  end.to_h

  ordered, unlabeled = AVLTree.new, Hash.new(true)
  remaining = candidates.to_set.classify(&:label_index)

  Enumerator.produce do |label|
    if label
      removals = Set[label] | conflicts[label]
      if first = unlabeled[label.label_index]
        removals.merge remaining[label.label_index].select(&:barriers?)
        unlabeled[label.label_index] = false
      end

      removals.each do |candidate|
        remaining[candidate.label_index].delete candidate
        ordered.delete candidate
      end

      conflicts.values_at(*removals).inject(Set[], &:|).subtract(removals).each do |candidate|
        conflicts[candidate].subtract removals
      end.tap do |changed|
        changed.merge remaining[label.label_index] if first
      end
    else
      candidates
    end.each do |candidate|
      conflict_count = conflicts[candidate].each.with_object Set[] do |other, indices|
        indices << other.label_index
      end.delete(candidate.label_index).size
      conflict_count += candidate.barrier_count

      unsafe = candidate.conflicts.classify(&:label_index).any? do |label_index, conflicts|
        next false unless unlabeled[label_index]
        others = remaining[label_index].reject(&:optional?)
        others.any? && others.all?(conflicts)
      end

      ordinal = [
        candidate.fixed                  ? 0 : 1, # fixed grid-line labels
        candidate.optional?              ? 1 : 0, # non-optional candidates
        unsafe                           ? 1 : 0, # candidates which don't prevent another feature being labeled altogether
        unlabeled[candidate.label_index] ? 0 : 1, # candidates for unlabeled features
        conflict_count,                           # candidates with fewer conflicts
        candidate.priority                        # better quality candidates
      ]

      unless candidate.ordinal == ordinal
        ordered.delete candidate
        candidate.ordinal = ordinal
        ordered.insert candidate
      end
    end

    ordered.first or raise StopIteration
  end.to_set.tap do |labels|
    grouped = candidates.group_by(&:indices)
    5.times do
      labels.select(&:point?).each do |label|
        labels.delete label
        labels << grouped[label.indices].min_by do |candidate|
          [(labels & candidate.conflicts - Set[label]).count, candidate.priority]
        end
      end
    end
  end.flat_map do |label|
    label.elements.map.with_object(label["categories"]).entries
  end.tap do |result|
    next unless debug_features.any?
    @params = DEBUG_PARAMS.deep_merge @params
    result.concat debug_features
  end
end

#label_candidates(&debug) ⇒ Object



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
# File 'lib/nswtopo/layer/labels.rb', line 613

def label_candidates(&debug)
  label_features.flat_map.with_index do |collection, label_index|
    log_update "compositing %s: feature %i of %i" % [@name, label_index + 1, label_features.length]
    collection.each do |feature|
      font_size = feature.properties["font-size"]
      feature.properties.slice(*FONT_SCALED_ATTRIBUTES).each do |key, value|
        feature.properties[key] = value.to_i * font_size * 0.01 if /^\d+%$/ === value
      end
    end.flat_map do |feature|
      case feature
      when GeoJSON::Point, GeoJSON::LineString
        feature
      when GeoJSON::Polygon
        feature.coordinates.map do |ring|
          GeoJSON::LineString.new ring, feature.properties
        end
      end
    end.tap do |features|
      features.each.with_object("feature", &debug) if Config["debug"]
    end.flat_map.with_index do |feature, feature_index|
      case feature
      when GeoJSON::Point
        point_candidates(collection, label_index, feature_index, feature)
      when GeoJSON::LineString
        line_string_candidates(collection, label_index, feature_index, feature)
      end
    end.tap do |candidates|
      candidates.reject!(&:point?) unless candidates.all?(&:point?)
    end.sort.each.with_index do |candidate, index|
      candidate.priority = index
    end
  end.tap do |candidates|
    log_update "compositing %s: chosing label positions" % @name

    if Config["debug"]
      candidates.flat_map(&:hulls).each.with_object("candidate", &debug)
      candidates.clear
    end

    Enumerator.new do |yielder|
      # separation/self: minimum distance between a label and another label for the same feature
      candidates.group_by do |label|
        label.label_index
      end.values.each do |group|
        Label.overlaps(group) do |label|
          label.dig("separation", "self")
        end.inject(yielder, &:<<)
      end

      # separation/same: minimum distance between a label and another label with the same text
      candidates.group_by do |label|
        [label.layer_name, label.text]
      end.values.each do |group|
        Label.overlaps(group) do |label|
          label.dig("separation", "same")
        end.inject(yielder, &:<<)
      end

      candidates.group_by do |candidate|
        candidate.layer_name
      end.each do |layer_name, group|
        index = RTree.load(group, &:bounds)

        # separation/other: minimum distance between a label and another label from the same layer
        index.each do |bounds, label|
          next unless buffer = label.dig("separation", "other")
          index.search(bounds, buffer: buffer).with_object(label).select do |other, label|
            Label.overlaps? label, other, buffer: buffer
          end.inject(yielder, &:<<)
        end

        # separation/<layer>: minimum distance between a label and any label from <layer>
        candidates.each do |label|
          next unless buffer = label.dig("separation", layer_name)
          index.search(label.bounds, buffer: buffer).with_object(label).select do |other, label|
            Label.overlaps? label, other, buffer: buffer
          end.inject(yielder, &:<<)
        end
      end

      # separation/dual: minimum distance between any two dual labels
      candidates.select(&:dual).group_by do |label|
        [label.layer_name, Set[label.text, label.dual]]
      end.values.each do |group|
        Label.overlaps(group) do |label|
          label.dig("separation", "dual")
        end.inject(yielder, &:<<)
      end

      # separation/all: minimum distance between a label and *any* other label
      Label.overlaps(candidates) do |label|
        # default of zero prevents any two labels overlapping
        label.dig("separation", "all") || 0
      end.reject do |label1, label2|
        label1.coexists_with?(label2) ||
        label2.coexists_with?(label1)
      end.inject(yielder, &:<<)
    end.each do |label1, label2|
      label1.conflicts << label2
      label2.conflicts << label1
    end
  end
end

#label_featuresObject



98
99
100
# File 'lib/nswtopo/layer/labels.rb', line 98

def label_features
  @label_features ||= []
end

#labelling_hullObject



359
360
361
362
# File 'lib/nswtopo/layer/labels.rb', line 359

def labelling_hull
  # TODO: doesn't account for map insets, need to replace with generalised check for non-covex @map.neatline
  @labelling_hull ||= @map.neatline(mm: -INSET).coordinates.first.transpose.map(&:minmax).inject(&:product).values_at(0,2,3,1,0)
end

#line_string_candidates(collection, label_index, feature_index, feature) ⇒ Object



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
# File 'lib/nswtopo/layer/labels.rb', line 433

def line_string_candidates(collection, label_index, feature_index, feature)
  closed = feature.coordinates.first == feature.coordinates.last
  pairs = closed ? :ring : :segments
  data = feature.coordinates

  attributes  = feature.properties
  orientation = attributes["orientation"]
  max_turn    = attributes["max-turn"] * Math::PI / 180
  min_radius  = attributes["min-radius"]
  max_angle   = attributes["max-angle"] * Math::PI / 180
  curved      = attributes["curved"]
  sample      = attributes["sample"]
  font_size   = attributes["font-size"]

  text_length = case collection.text
  when REXML::Element then data.path_length
  when String then Font.glyph_length collection.text, attributes
  end

  points = data.segments.inject([]) do |memo, segment|
    distance = segment.distance
    case
    when REXML::Element === collection.text
      memo << segment[0]
    when curved && distance >= text_length
      memo << segment[0]
    else
      steps = (distance / sample).ceil
      memo += steps.times.map do |step|
        segment.along(step.to_f / steps)
      end
    end
  end
  points << data.last unless closed

  segments = points.send(pairs)
  vectors = segments.map(&:diff)
  distances = vectors.map(&:norm)

  cumulative = distances.inject([0]) do |memo, distance|
    memo << memo.last + distance
  end
  total = closed ? cumulative.pop : cumulative.last

  angles = vectors.map(&:normalised).send(pairs).map do |directions|
    Math.atan2 directions.inject(&:cross), directions.inject(&:dot)
  end
  closed ? angles.rotate!(-1) : angles.unshift(0).push(0)

  curvatures = segments.send(pairs).map do |(p0, p1), (_, p2)|
    sides = [[p0, p1], [p1, p2], [p2, p0]].map(&:distance)
    semiperimeter = 0.5 * sides.inject(&:+)
    diffs = sides.map { |side| semiperimeter - side }
    area_squared = [semiperimeter * diffs.inject(&:*), 0].max
    4 * Math::sqrt(area_squared) / sides.inject(&:*)
  end
  closed ? curvatures.rotate!(-1) : curvatures.unshift(0).push(0)

  dont_use = angles.zip(curvatures).map do |angle, curvature|
    angle.abs > max_angle || min_radius * curvature > 1
  end

  squared_angles = angles.map { |angle| angle * angle }

  barrier_overlaps = Hash.new do |overlaps, label_segment|
    bounds = label_segment.transpose.map(&:minmax)
    buffer = 0.5 * font_size
    overlaps[label_segment] = barrier_segments.search(bounds, buffer: buffer).select do |barrier_segment|
      barrier_segment.conflicts_with?(label_segment, buffer: buffer)
    end.inject Set[] do |barriers, segment|
      barriers.add segment.barrier
    end
  end

  Enumerator.new do |yielder|
    indices, distance, bad_indices, angle_integral = [0], 0, [], []
    loop do
      while distance < text_length
        break true if closed ? indices.many? && indices.last == indices.first : indices.last == points.length - 1
        unless indices.one?
          bad_indices << dont_use[indices.last]
          angle_integral << (angle_integral.last || 0) + angles[indices.last]
        end
        distance += distances[indices.last]
        indices << (indices.last + 1) % points.length
      end && break

      while distance >= text_length
        case
        when indices.length == 2 && curved
        when indices.length == 2 then yielder << indices.dup
        when distance - distances[indices.first] >= text_length
        when bad_indices.any?
        when angle_integral.max - angle_integral.min > max_turn
        else yielder << indices.dup
        end
        angle_integral.shift
        bad_indices.shift
        distance -= distances[indices.first]
        indices.shift
        break true if indices.first == (closed ? 0 : points.length - 1)
      end && break
    end if points.many?
  end.map do |indices|
    start, stop = cumulative.values_at(indices.first, indices.last)
    along = (start + 0.5 * (stop - start) % total) % total
    total_squared_curvature = squared_angles.values_at(*indices[1...-1]).inject(0, &:+)
    baseline = points.values_at(*indices).crop(text_length)

    barrier_count = baseline.segments.each.with_object Set[] do |segment, barriers|
      barriers.merge barrier_overlaps[segment]
    end.size
    priority = [total_squared_curvature, (total - 2 * along).abs / total.to_f]

    baseline.reverse! unless case orientation
    when "uphill", "anticlockwise" then true
    when "downhill", "clockwise" then false
    else baseline.values_at(0, -1).map(&:first).inject(&:<=)
    end

    hull = GeoJSON::LineString.new(baseline).multi.buffer(0.5 * font_size, splits: false).coordinates.flatten(1).convex_hull
    next unless labelling_hull.surrounds? hull

    path_id = [@name, collection.layer_name, "path", label_index, feature_index, indices.first, indices.last].join ?.
    path_element = REXML::Element.new("path")
    path_element.add_attributes "id" => path_id, "d" => svg_path_data(baseline), "pathLength" => VALUE % text_length
    text_element = REXML::Element.new("text")

    case collection.text
    when REXML::Element
      fixed = true
      text_element.add_element collection.text, "href" => "#%s" % path_id
    when String
      text_path = text_element.add_element "textPath", "href" => "#%s" % path_id, "textLength" => VALUE % text_length, "spacing" => "auto"
      text_path.add_element("tspan", "dy" => VALUE % (CENTRELINE_FRACTION * font_size)).add_text(collection.text)
    end
    Label.new collection, label_index, feature_index, barrier_count, priority, [hull], attributes, [text_element, path_element], along, fixed
  end.compact.reject do |candidate|
    candidate.optional? && candidate.barriers?
  end.then do |candidates|
    neighbours = Hash.new do |hash, candidate|
      hash[candidate] = Set[]
    end
    candidates.each.with_index do |candidate1, index1|
      index2 = index1
      loop do
        index2 = (index2 + 1) % candidates.length
        break if index2 == (closed ? index1 : 0)
        candidate2 = candidates[index2]
        offset = candidate2.along - candidate1.along
        break unless offset % total < sample || (closed && -offset % total < sample)
        neighbours[candidate2] << candidate1
        neighbours[candidate1] << candidate2
      end
    end
    removed = Set[]
    candidates.sort.each.with_object Array[] do |candidate, sampled|
      next if removed === candidate
      removed.merge neighbours[candidate]
      sampled << candidate
    end.tap do |candidates|
      next unless separation = attributes.dig("separation", "along")
      separation += text_length
      sorted = candidates.sort_by(&:along)
      sorted.each.with_index do |candidate1, index1|
        index2 = index1
        loop do
          index2 = (index2 + 1) % candidates.length
          break if index2 == (closed ? index1 : 0)
          candidate2 = sorted[index2]
          offset = candidate2.along - candidate1.along
          break unless offset % total < separation || (closed && -offset % total < separation)
          candidate2.conflicts << candidate1
          candidate1.conflicts << candidate2
        end
      end
    end
  end
end

#point_candidates(collection, label_index, feature_index, feature) ⇒ Object



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
# File 'lib/nswtopo/layer/labels.rb', line 370

def point_candidates(collection, label_index, feature_index, feature)
  attributes  = feature.properties
  margin      = attributes["margin"]
  line_height = attributes["line-height"]
  font_size   = attributes["font-size"]

  point = feature.coordinates
  lines = Font.in_two collection.text, attributes
  lines = [[collection.text, Font.glyph_length(collection.text, attributes)]] if lines.map(&:first).map(&:length).min == 1
  height = lines.map { font_size }.inject { |total| total + line_height }
  # if attributes["shield"]
  #   width += SHIELD_X * font_size
  #   height += SHIELD_Y * font_size
  # end

  [*attributes["position"] || "over"].map do |position|
    dx = position =~ /right$/ ? 1 : position =~ /left$/  ? -1 : 0
    dy = position =~ /^below/ ? 1 : position =~ /^above/ ? -1 : 0
    next dx, dy, dx * dy == 0 ? 1 : 0.6
  end.uniq.map.with_index do |(dx, dy, f), position_index|
    text_elements, hulls = lines.map.with_index do |(line, text_length), index|
      anchor = point.dup
      anchor[0] += dx * (f * margin + 0.5 * text_length)
      anchor[1] += dy * (f * margin + 0.5 * height)
      anchor[1] += (index - 0.5) * 0.5 * height unless lines.one?

      text_element = REXML::Element.new("text")
      text_element.add_attribute "transform", "translate(%s)" % POINT % anchor
      text_element.add_attribute "text-anchor", "middle"
      text_element.add_attribute "textLength", VALUE % text_length
      text_element.add_attribute "y", VALUE % (CENTRELINE_FRACTION * font_size)
      text_element.add_text line

      hull = [text_length, font_size].zip(anchor).map do |size, origin|
        [origin - 0.5 * size, origin + 0.5 * size]
      end.inject(&:product).values_at(0,2,3,1)

      next text_element, hull
    end.transpose

    next unless hulls.all? do |hull|
      labelling_hull.surrounds? hull
    end

    bounds = hulls.flatten(1).transpose.map(&:minmax)
    barrier_count = barrier_segments.search(bounds).with_object Set[] do |segment, barriers|
      next if barriers === segment.barrier
      hulls.any? do |hull|
        barriers << segment.barrier if segment.conflicts_with? hull
      end
    end.size
    priority = [position_index, feature_index]
    Label.new collection, label_index, feature_index, barrier_count, priority, hulls, attributes, text_elements
  end.compact.reject do |candidate|
    candidate.optional? && candidate.barriers?
  end.tap do |candidates|
    candidates.combination(2).each do |candidate1, candidate2|
      candidate1.conflicts << candidate2
      candidate2.conflicts << candidate1
    end
  end
end