Module: CartoCSSHelper::Heuristic

Includes:
Configuration
Defined in:
lib/cartocss_helper/heuristic.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configuration

#default_renderer, #find_style_file_location, #get_cartocss_project_name, #get_data_filename, #get_max_z, #get_min_z, #get_overpass_instance_url, #get_path_to_cartocss_project_folder, #get_path_to_folder_for_branch_specific_cache, #get_path_to_folder_for_cache, #get_path_to_folder_for_history_api_cache, #get_path_to_folder_for_notes_api_cache, #get_path_to_folder_for_output, #get_path_to_folder_for_overpass_cache, #get_style_file_location, #get_style_specific_data, #mapnik_reference_version_override, #path_to_kosmtik, #project_file_location, #renderer, #set_known_alternative_overpass_url, #set_mapnik_reference_version_override, #set_overpass_instance_url, #set_path_to_cartocss_project_folder, #set_path_to_folder_for_cache, #set_path_to_folder_for_output, #set_path_to_kosmtik, #set_renderer, #set_style_specific_data

Class Method Details

.get_generic_tag_valueObject



101
102
103
# File 'lib/cartocss_helper/heuristic.rb', line 101

def self.get_generic_tag_value
  return '*'
end

Instance Method Details

#get_tagsObject



8
9
10
11
12
13
14
15
# File 'lib/cartocss_helper/heuristic.rb', line 8

def get_tags
  tags = get_tags_from_mss
  tags.merge(get_tags_from_yaml)
  tags.merge(get_tags_from_osm2pqsql)
  # unrecovable = Set.new [['area', 'yes'], ['area', 'no']] #TODO - how this should be handled?
  # tags.merge(unrecovable)
  return tags.to_a.sort
end

#get_tags_from_mssObject



17
18
19
20
21
22
23
24
# File 'lib/cartocss_helper/heuristic.rb', line 17

def get_tags_from_mss
  tags = Set.new
  filenames = Dir[Configuration.get_path_to_cartocss_project_folder + '*.mss']
  filenames.each do |filename|
    tags.merge(get_tags_from_mss_file(filename))
  end
  return tags
end

#get_tags_from_mss_file(style_filename) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cartocss_helper/heuristic.rb', line 44

def get_tags_from_mss_file(style_filename)
  possible_key_values = get_tags_from_osm2pqsql
  tags = Set.new
  # puts style_filename
  style_file = open(style_filename)
  style = style_file.read
  matched = style.scan(/\[feature = '(man_made|[^_]+)_([^']+)'\]/)
  matched.each do |element|
    key = element[0]
    if possible_key_values.include?([key, get_generic_tag_value])
      tags.add([key, element[1]])
    end
  end
  matched = style.scan(/(\w+) = '(\w+)'/)
  matched.each do |element|
    key = element[0]
    next unless key != 'feature'
    if possible_key_values.include?([key, get_generic_tag_value])
      tags.add([key, element[1]])
    end
  end
  return tags
end

#get_tags_from_osm2pqsqlObject



35
36
37
38
39
40
41
42
# File 'lib/cartocss_helper/heuristic.rb', line 35

def get_tags_from_osm2pqsql
  tags = Set.new
  filenames = Dir[Configuration.get_path_to_cartocss_project_folder + '*.style']
  filenames.each do |filename|
    tags.merge(get_tags_from_osm2pqsql_file(filename))
  end
  return tags
end

#get_tags_from_osm2pqsql_file(style_filename) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cartocss_helper/heuristic.rb', line 105

def get_tags_from_osm2pqsql_file(style_filename)
  tags = Set.new
  # puts style_filename
  style_file = open(style_filename)
  style = style_file.read
  matched = style.scan(/^(node,way|node|way)\s*([^ ]+)\s*text\s*($|linear|polygon|nocache)/)
  matched.each do |element|
    tags.add([element[1], get_generic_tag_value])
  end
  return tags
end

#get_tags_from_yamlObject



26
27
28
29
30
31
32
33
# File 'lib/cartocss_helper/heuristic.rb', line 26

def get_tags_from_yaml
  tags = Set.new
  filenames = Dir[Configuration.get_path_to_cartocss_project_folder + '*.yaml']
  filenames.each do |filename|
    tags.merge(get_tags_from_yaml_file(filename))
  end
  return tags
end

#get_tags_from_yaml_file(yaml_filename) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cartocss_helper/heuristic.rb', line 68

def get_tags_from_yaml_file(yaml_filename)
  possible_key_values = get_tags_from_osm2pqsql
  tags = Set.new
  yaml_file = open(yaml_filename)
  yaml = yaml_file.read
  # (.*\.|)     #WHERE p.highway = 'turning_circle'
  # ("|)        #natural and other SQL keywords
  # ([^"() ]+)  #tag
  # repeat
  # =
  # '([^'()]+)' #quoted value
  matched = yaml.scan(/(.*\.|)("|)([^"() ]+)("|) = '([^'()]+)'/)
  matched.each do |element|
    key = element[2]
    value = element[4]
    if possible_key_values.include?([key, get_generic_tag_value])
      tags.add([key, value])
    end
  end
  matched = yaml.scan(/("|)([^"() ]+)("|) (NOT |)IN \(([^()]*)\)/)
  matched.each do |element|
    tag = element[1]
    key = tag.gsub(/.*\./, '')
    values = element[4]
    values_matched = values.scan(/'([^']+)'/)
    next unless possible_key_values.include?([key, get_generic_tag_value])
    values_matched.each do |value|
      tags.add([key, value[0]])
    end
  end
  return tags
end