Class: CartoCSSHelper::DataFileGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/cartocss_helper/data_file_handling.rb

Instance Method Summary collapse

Constructor Details

#initialize(tags, type, lat, lon, size) ⇒ DataFileGenerator

Returns a new instance of DataFileGenerator.



45
46
47
48
49
50
51
# File 'lib/cartocss_helper/data_file_handling.rb', line 45

def initialize(tags, type, lat, lon, size)
  @lat = lat
  @lon = lon
  @tags = tags
  @type = type
  @size = size
end

Instance Method Details

#add_node(lat, lon, tags, id) ⇒ Object



112
113
114
115
116
117
# File 'lib/cartocss_helper/data_file_handling.rb', line 112

def add_node(lat, lon, tags, id)
  @data_file.write "\n"
  @data_file.write "  <node id='#{id}' visible='true' lat='#{lat}' lon='#{lon}'>"
  add_tags(tags)
  @data_file.write '</node>'
end

#add_tags(tags) ⇒ Object



130
131
132
133
134
135
# File 'lib/cartocss_helper/data_file_handling.rb', line 130

def add_tags(tags)
  tags.each do |tag|
    @data_file.write "\n"
    @data_file.write "    <tag k='#{tag[0]}' v='#{tag[1]}' />"
  end
end

#add_way(tags, nodes, id) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/cartocss_helper/data_file_handling.rb', line 119

def add_way(tags, nodes, id)
  @data_file.write "\n"
  @data_file.write "  <way id='#{id}' visible='true'>"
  nodes.each do |node|
    @data_file.write "\n"
    @data_file.write "    <nd ref='#{node}' />"
  end
  add_tags(tags)
  @data_file.write "\n  </way>"
end

#close_fileObject



57
58
59
# File 'lib/cartocss_helper/data_file_handling.rb', line 57

def close_file
  @data_file.close
end

#finish_fileObject



66
67
68
69
# File 'lib/cartocss_helper/data_file_handling.rb', line 66

def finish_file
  generate_sufix
  close_file
end

#generateObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cartocss_helper/data_file_handling.rb', line 71

def generate
  prepare_file
  if @type == 'node'
    generate_node_topology(@lat, @lon, @tags)
  elsif @type == 'way'
    generate_way_topology(@lat, @lon, @tags)
  elsif @type == 'closed_way'
    generate_closed_way_topology(@lat, @lon, @tags)
  else
    raise 'this type of element does not exists'
  end
  finish_file
end

#generate_closed_way_topology(lat, lon, tags) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/cartocss_helper/data_file_handling.rb', line 95

def generate_closed_way_topology(lat, lon, tags)
  delta = @size / 3
  add_node lat - delta, lon - delta, [], 1
  add_node lat - delta, lon + delta, [], 2
  add_node lat + delta, lon + delta, [], 3
  add_node lat + delta, lon - delta, [], 4
  add_way tags, [1, 2, 3, 4, 1], 5
end

#generate_node_topology(lat, lon, tags) ⇒ Object



85
86
87
# File 'lib/cartocss_helper/data_file_handling.rb', line 85

def generate_node_topology(lat, lon, tags)
  add_node lat, lon, tags, 2387
end

#generate_prefixObject



104
105
106
# File 'lib/cartocss_helper/data_file_handling.rb', line 104

def generate_prefix
  @data_file.write "<?xml version='1.0' encoding='UTF-8'?>\n<osm version='0.6' generator='script'>"
end

#generate_sufixObject



108
109
110
# File 'lib/cartocss_helper/data_file_handling.rb', line 108

def generate_sufix
  @data_file.write "\n</osm>"
end

#generate_way_topology(lat, lon, tags) ⇒ Object



89
90
91
92
93
# File 'lib/cartocss_helper/data_file_handling.rb', line 89

def generate_way_topology(lat, lon, tags)
  add_node lat, lon - @size / 3, [], 1
  add_node lat, lon + @size / 3, [], 2
  add_way tags, [1, 2], 3
end

#open_fileObject



53
54
55
# File 'lib/cartocss_helper/data_file_handling.rb', line 53

def open_file
  @data_file = open(Configuration.get_data_filename, 'w')
end

#prepare_fileObject



61
62
63
64
# File 'lib/cartocss_helper/data_file_handling.rb', line 61

def prepare_file
  open_file
  generate_prefix
end