Module: NSWTopo::Overlay

Includes:
Vector
Defined in:
lib/nswtopo/layer/overlay.rb

Constant Summary collapse

CREATE =
%w[simplify tolerance]
TOLERANCE =
0.4
GPX_STYLES =
YAML.load <<~YAML
  stroke: black
  stroke-width: 0.4
YAML

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, #labeling_features, #params_for, #render, #svg_path_data

Instance Method Details

#get_featuresObject



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

def get_features
  GPS.new(@path).tap do |gps|
    @simplify = true if GPS::GPX === gps
    @tolerance ||= [@map.to_mm(5), TOLERANCE].max if @simplify
  end.collection.reproject_to(@map.neatline.projection).explode.each do |feature|
    styles, folder, name = feature.values_at "styles", "folder", "name"
    styles ||= GPX_STYLES

    case feature
    when GeoJSON::LineString
      styles["stroke-linejoin"] = "round"
      if @tolerance
        simplified = feature.coordinates.douglas_peucker(@tolerance)
        smoothed = simplified.sample_at(2*@tolerance).each_cons(2).map do |segment|
          segment.along(0.5)
        end.push(simplified.last).prepend(simplified.first)
        feature.coordinates = smoothed
      end
    when GeoJSON::Polygon
      styles["stroke-linejoin"] = "miter"
    end

    categories = [folder, name].compact.reject(&:empty?).map(&method(:categorise))
    keys = styles.keys - params_for(categories.to_set).keys
    styles = styles.slice *keys

    feature.clear
    feature["category"] = categories << feature.object_id
    @params[categories.join(?\s)] = styles if styles.any?
  end
end

#to_sObject



44
45
46
47
48
49
50
51
# File 'lib/nswtopo/layer/overlay.rb', line 44

def to_s
  counts = %i[linestrings polygons].map do |type|
    features.send type
  end.reject(&:empty?).map(&:length).zip(%w[line polygon]).map do |count, word|
    "%s %s%s" % [count, word, (?s if count > 1)]
  end.join(", ")
  "%s: %s" % [@name, counts]
end