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
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
|
# File 'lib/jekyll_geocode.rb', line 12
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|
if !File.file?("#{data_source}/#{d[geo_name]}.json")
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
json = URI.encode("#{geo_service}#{d[geo_address]}#{geo_postcode_field}#{geo_city_field}#{geo_region_field}#{geo_country_field}&limit=1")
source = JSON.load(open(json))
if outputfile
source.each do |coordinates|
data = [ "title" => "#{d[geo_name]}", "url" => "#places-01", "data_set" => "01", "location" => { "latitude" => "#{coordinates["lat"]}","longitude" => "#{coordinates["lon"]}" } ]
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|
data_yml_simple = data_yml.gsub("---", "").gsub(regEx, '')
f.puts data_yml_simple
}
file_yaml = YAML.load(File.open(path_yaml))
file_yaml_uniq = file_yaml.uniq { |s| s.first }
File.open(path_yaml, "w") {|f| f.write(file_yaml_uniq.to_yaml) }
end
end
else
site.data[geo_name_field] = source
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
end
|