Module: NSWTopo::Grid

Includes:
Vector
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 Vector

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

Instance Method Summary collapse

Methods included from Vector

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

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

def get_features
  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[0][0] % 6 < 0.00001
        linestring["ends"].delete 1 if linestring.coordinates[-1][0] % 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
    mm = -0.5 * @params["stroke-width"]
    @map.neatline(mm: mm).reproject_to_wgs84.tap do |border|
      border.properties.replace "category" => "edge"
      collections << border
    end
  end.inject(&:merge)
end

#label_element(labels, label_params) ⇒ Object



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

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



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

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)

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

  flip_eastings = eastings.partition do |easting|
    Math::atan2(*easting.coordinates.values_at(0, -1).inject(&:minus)) * 180.0 / Math::PI > @map.rotation
  end.map(&:length).inject(&:>)
  eastings.each do |easting|
    easting.coordinates.reverse!
    easting["ends"].map! { |index| 1 - index }
  end if flip_eastings

  gridlines.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)
      segment = gridline.coordinates.send(order).take(2)
      fraction = text_length / segment.distance
      coordinates = [segment[0], segment.along(fraction)].send(order)
      GeoJSON::LineString.new coordinates, "label" => text_path
    end
  end
end

#to_sObject



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

def to_s
  @name
end