Class: NSWTopo::Labels::Label

Inherits:
ConvexHulls show all
Defined in:
lib/nswtopo/layer/labels/label.rb

Constant Summary

Constants included from StraightSkeleton

StraightSkeleton::DEFAULT_ROUNDING_ANGLE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ConvexHulls

#each, overlap?

Methods inherited from GeoJSON::MultiLineString

#buffer, #dissolve_points, #freeze!, #nodes, #offset, #path_length, #samples, #smooth, #subdivide, #to_multipolygon, #to_polygon, #trim

Constructor Details

#initialize(baselines, feature, priority, elements, along: nil, fixed: nil, &block) ⇒ Label

Returns a new instance of Label.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/nswtopo/layer/labels/label.rb', line 8

def initialize(baselines, feature, priority, elements, along: nil, fixed: nil, &block)
  super baselines, 0.5 * feature["font-size"] do
    @categories, @text, @dual, @layer_name, @label_index, @feature_index = feature.values_at(:categories, :text, :dual, :layer_name, :label_index, :feature_index)
    @indices = [@label_index, @feature_index]
    @priority, @elements, @along, @fixed = priority, elements, along, fixed
    @hull = dissolve_points.convex_hull
    @optional, @coexist, knockout = feature.values_at("optional", "coexist", "knockout")
    @barrier_count = each.with_object(Label.knockout(knockout)).map(&block).inject(&:merge).size
    @ordinal = [@barrier_count, @priority]
    @separation = feature.fetch("separation", {})
  end
end

Instance Attribute Details

#alongObject (readonly)

Returns the value of attribute along.



22
23
24
# File 'lib/nswtopo/layer/labels/label.rb', line 22

def along
  @along
end

#barrier_countObject (readonly)

Returns the value of attribute barrier_count.



22
23
24
# File 'lib/nswtopo/layer/labels/label.rb', line 22

def barrier_count
  @barrier_count
end

#categoriesObject (readonly)

Returns the value of attribute categories.



21
22
23
# File 'lib/nswtopo/layer/labels/label.rb', line 21

def categories
  @categories
end

#dualObject (readonly)

Returns the value of attribute dual.



21
22
23
# File 'lib/nswtopo/layer/labels/label.rb', line 21

def dual
  @dual
end

#elementsObject (readonly)

Returns the value of attribute elements.



22
23
24
# File 'lib/nswtopo/layer/labels/label.rb', line 22

def elements
  @elements
end

#feature_indexObject (readonly)

Returns the value of attribute feature_index.



21
22
23
# File 'lib/nswtopo/layer/labels/label.rb', line 21

def feature_index
  @feature_index
end

#fixedObject (readonly)

Returns the value of attribute fixed.



22
23
24
# File 'lib/nswtopo/layer/labels/label.rb', line 22

def fixed
  @fixed
end

#hullObject (readonly)

Returns the value of attribute hull.



22
23
24
# File 'lib/nswtopo/layer/labels/label.rb', line 22

def hull
  @hull
end

#indicesObject (readonly)

Returns the value of attribute indices.



21
22
23
# File 'lib/nswtopo/layer/labels/label.rb', line 21

def indices
  @indices
end

#label_indexObject (readonly)

Returns the value of attribute label_index.



21
22
23
# File 'lib/nswtopo/layer/labels/label.rb', line 21

def label_index
  @label_index
end

#layer_nameObject (readonly)

Returns the value of attribute layer_name.



21
22
23
# File 'lib/nswtopo/layer/labels/label.rb', line 21

def layer_name
  @layer_name
end

#ordinalObject (readonly)

Returns the value of attribute ordinal.



22
23
24
# File 'lib/nswtopo/layer/labels/label.rb', line 22

def ordinal
  @ordinal
end

#priorityObject (readonly)

Returns the value of attribute priority.



22
23
24
# File 'lib/nswtopo/layer/labels/label.rb', line 22

def priority
  @priority
end

#separationObject (readonly)

Returns the value of attribute separation.



22
23
24
# File 'lib/nswtopo/layer/labels/label.rb', line 22

def separation
  @separation
end

#textObject (readonly)

Returns the value of attribute text.



21
22
23
# File 'lib/nswtopo/layer/labels/label.rb', line 21

def text
  @text
end

Class Method Details

.knockout(value) ⇒ Object



4
5
6
# File 'lib/nswtopo/layer/labels/label.rb', line 4

def self.knockout(value)
  Float(Numeric === value ? value : value ? Config.fetch("knockout", 0.3) : 0)
end

.overlaps(labels, group = labels, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nswtopo/layer/labels/label.rb', line 44

def self.overlaps(labels, group = labels, &block)
  return Set[] unless group.any?(&block)
  index = RTree.load(labels.flat_map(&:explode), &:bounds)
  group.each.with_object Set[] do |label, overlaps|
    next unless buffer = yield(label)
    index.search(label.bounds, buffer).each do |other|
      next if label == other.source
      next if overlaps === [label, other.source]
      next if overlaps === [other.source, label]
      next unless label.length < 3 || ConvexHulls.overlap?(label.hull, other, buffer)
      next unless label.any? do |hull|
        ConvexHulls.overlap?(hull, other, buffer)
      end
      overlaps << [label, other.source]
    end
  end
end

Instance Method Details

#<=>(other) ⇒ Object



40
41
42
# File 'lib/nswtopo/layer/labels/label.rb', line 40

def <=>(other)
  self.ordinal <=> other.ordinal
end

#barriers?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/nswtopo/layer/labels/label.rb', line 28

def barriers?
  @barrier_count > 0
end

#coexists_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/nswtopo/layer/labels/label.rb', line 36

def coexists_with?(other)
  Array(@coexist).include? other.layer_name
end

#optional?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/nswtopo/layer/labels/label.rb', line 32

def optional?
  @optional
end

#point?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/nswtopo/layer/labels/label.rb', line 24

def point?
  @along.nil?
end