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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/jekyll_geocode.rb', line 16
def generate(site)
regEx = /^[\s]*$\n/
I18n.config.available_locales = :en
geo_service = 'https://nominatim.openstreetmap.org/?format=json&q='
config = site.config['jekyll_geocode']
if !config
return
end
if !config.kind_of?(Array)
filename = site.config['jekyll_geocode']['file-name']
filepath = site.config['jekyll_geocode']['file-path']
outputfile = site.config['jekyll_geocode']['outputfile']
geo_name = site.config['jekyll_geocode']['name']
geo_address = site.config['jekyll_geocode']['address']
geo_postcode = site.config['jekyll_geocode']['postcode']
geo_city = site.config['jekyll_geocode']['city']
geo_region = site.config['jekyll_geocode']['region']
geo_country = site.config['jekyll_geocode']['country']
end
if !filepath
data_source = (site.config['data_source'])
else
data_source = (filepath)
end
path_yaml = "#{data_source}/#{outputfile}"
members = YAML.load_file("#{data_source}/#{filename}")
members.each do |d|
geo_name_field = d[geo_name].downcase.tr(" ", "-")
if d[geo_postcode]
geo_postcode_field = ", #{d[geo_postcode]}"
end
if d[geo_city]
geo_city_field = ", #{d[geo_city]}"
end
if d[geo_region]
geo_region_field = ", #{d[geo_region]}"
end
if d[geo_country]
geo_country_field = ", #{d[geo_country]}"
end
geo_coord = "#{d[geo_address]}#{geo_postcode_field}#{geo_city_field}#{geo_region_field}#{geo_country_field}"
geo_request = "#{geo_service}#{geo_coord}&limit=1"
p geo_request
if outputfile
geo_cache = true
if File.file?(path_yaml)
file_yaml = YAML.load(File.open(path_yaml))
end
if file_yaml
file_yaml.each do |coordinates|
if coordinates['address'] == geo_coord
geo_cache = false
end
end
else
geo_cache = true
end
if geo_cache == true
p "geocode is requesting #{geo_coord}"
request_service(geo_request).each do |coordinates|
data = [ "title" => "#{d[geo_name]}", "url" => "##{d[geo_name]}", "data_set" => "01", "location" => { "latitude" => "#{coordinates["lat"]}","longitude" => "#{coordinates["lon"]}" }, "address" => "#{geo_coord}" ]
data_yml = data.to_yaml
if !File.file?(path_yaml)
File.open(path_yaml, "w") {|f| f.write(data_yml) }
end
if File.file?(path_yaml)
File.open(path_yaml, 'a') { |f|
f.puts data_yml.gsub("---", "").gsub(regEx, '')
}
file_yaml = YAML.load(File.open(path_yaml)).uniq { |s| s.first }
File.open(path_yaml, "w") {|f| f.write(file_yaml.to_yaml) }
end
end
end
end
if !outputfile && !File.file?("#{data_source}/#{d[geo_name]}.json")
site.data[geo_name_field] = request_service(geo_request)
if site.config['jekyll_geocode']['cache']
path_json = "#{data_source}/#{geo_name_field}.json"
open(path_json, 'wb') do |file|
file << JSON.generate(site.data[geo_name_field])
end
end
end
end
end
|