Module: NSWTopo::Grid

Includes:
VectorRender
Defined in:
lib/nswtopo/layer/grid.rb

Constant Summary collapse

CREATE =
%w[interval border]
INSET =
1.5
DEFAULTS =
YAML.load "interval: 1000.0\nstroke: black\nstroke-width: 0.1\nedge:\n  fill: none\n  preserve: true\nlabels:\n  font-family: Arial Narrow, sans-serif\n  font-size: 2.3\n  stroke: none\n  fill: black\n  orientation: uphill\n"

Constants included from VectorRender

VectorRender::FONT_SCALED_ATTRIBUTES, VectorRender::MARGIN, VectorRender::SVG_ATTRIBUTES

Instance Method Summary collapse

Methods included from VectorRender

#categorise, #create, #drawing_features, #features, #filename, #params_for, #render

Instance Method Details

#get_featuresObject



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

def get_features
  @params["edge"]["stroke-width"] ||= 2 * @params["stroke-width"]
  Projection.utm_zones(@map.neatline).flat_map do |zone|
    utm, utm_geometry = Projection.utm(zone), Projection.utm_geometry(zone)
    map_geometry = @map.neatline(**MARGIN).reproject_to_wgs84

    eastings, northings = @map.neatline.reproject_to(utm).bounds.map do |min, max|
      (min / @interval).floor..(max / @interval).ceil
    end.map do |counts|
      counts.map { |count| count * @interval }
    end

    grid = eastings.map do |easting|
      [easting].product northings.reverse
    end

    eastings, northings = [grid, grid.transpose].map.with_index do |lines, index|
      lines.inject GeoJSON::Collection.new(projection: utm) do |collection, line|
        coord = line[0][index]
        label = [coord / 100000, (coord / 1000) % 100]
        label << coord % 1000 unless @interval % 1000 == 0
        collection.add_linestring line, "label" => label, "ends" => [0, 1], "category" => index.zero? ? "easting" : "northing"
      end.reproject_to_wgs84.clip(utm_geometry).clip(map_geometry).explode.each do |linestring|
        linestring["ends"].delete 0 if linestring.coordinates.first.x % 6 < 0.00001
        linestring["ends"].delete 1 if linestring.coordinates.last.x % 6 < 0.00001
      end
    end

    boundary_points = GeoJSON.multilinestring(grid.transpose, projection: utm).reproject_to_wgs84.clip(utm_geometry).coordinates.map(&:first)
    boundary = GeoJSON.linestring boundary_points, properties: { "category" => "boundary" }

    [eastings, northings, boundary]
  end.tap do |collections|
    next unless @border
    @map.neatline.reproject_to_wgs84.map! do |border|
      border.with_properties("category" => "edge")
    end.tap do |border|
      collections << border
    end
  end.inject(&:merge)
end

#label_element(labels, label_params) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nswtopo/layer/grid.rb', line 63

def label_element(labels, label_params)
  font_size = label_params["font-size"]
  parts = labels.zip(["%d\u00a0", "%02d", "\u00a0%03d"]).map do |part, format|
    format % part
  end.zip([80, 100, 80])

  text_path = REXML::Element.new("textPath")
  parts.each.with_index do |(text, percent), index|
    tspan = text_path.add_element "tspan", "font-size" => "#{percent}%"
    tspan.add_attributes "dy" => VALUE % (Labels::CENTRELINE_FRACTION * font_size) if index.zero?
    tspan.add_text text
  end

  text_length = parts.sum do |text, percent|
    Font.glyph_length text, label_params.merge("font-size" => font_size * percent / 100.0)
  end
  text_path.add_attribute "textLength", VALUE % text_length
  [text_length, text_path]
end

#labeling_featuresObject



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

def labeling_features
  return [] if @params["unlabeled"]
  label_params = @params["labels"]
  font_size = label_params["font-size"]
  offset = -0.85 * font_size
  inset = INSET + font_size * 0.5 * Math::sin(@map.rotation.abs * Math::PI / 180)
  inset_geometry = @map.neatline(mm: -inset).map!(&:remove_holes)

  eastings, northings = features.select do |linestring|
    linestring["label"]
  end.partition do |gridline|
    gridline["category"] == "easting"
  end

  flip_eastings = eastings.partition do |easting|
    Math::atan2(*easting.coordinates.values_at(0, -1).inject(&:-)) * 180.0 / Math::PI > @map.rotation
  end.map(&:length).inject(&:>)

  eastings.each do |easting|
    easting["ends"].map! { |index| 1 - index }
  end.map!(&:reverse) if flip_eastings

  eastings.concat(northings).inject(GeoJSON::Collection.new(projection: @map.neatline.projection)) do |collection, gridline|
    collection << gridline.offset(offset, splits: false)
  end.clip(inset_geometry).explode.flat_map do |gridline|
    label, ends = gridline.values_at "label", "ends"
    i[itself reverse].values_at(*ends).map do |order|
      text_length, text_path = label_element(label, label_params)
      p0, p1 = gridline.coordinates.send(order).take(2)
      fraction = text_length / (p1 - p0).norm
      p01 = p1 * fraction + p0 * (1 - fraction)
      coordinates = [p0, p01].send(order)
      GeoJSON::LineString[coordinates, "label" => text_path]
    end
  end
end

#to_sObject



120
121
122
# File 'lib/nswtopo/layer/grid.rb', line 120

def to_s
  @name
end