-
Notifications
You must be signed in to change notification settings - Fork 184
/
coordinates2politics.rb
executable file
·210 lines (164 loc) · 6.64 KB
/
coordinates2politics.rb
1
2
3
4
5
6
7
8
9
10
11
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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#***********************************************************************************
#
# All code (C) Pete Warden, 2011
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#***********************************************************************************
require 'rubygems' if RUBY_VERSION < '1.9'
require 'json'
cwd = File.expand_path(File.dirname(__FILE__))
require File.join(cwd, 'geodict_lib')
require File.join(cwd, 'dstk_config')
TypeToFriendly = {
'admin2' => 'country',
'admin4' => 'state',
'admin6' => 'county',
'admin5' => 'city',
'admin8' => 'city'
}
# Takes an array of coordinates as input, and looks up what political areas they lie
# within
def coordinates2politics(locations, callback=nil)
result = []
locations.each do |location|
lat = location[:latitude]
lon = location[:longitude]
lat_s = PGconn.escape(lat.to_s)
lon_s = PGconn.escape(lon.to_s)
is_valid = true
if !lat_s or lat_s.length == 0 or !lon_s or lon_s.length == 0
is_valid = false
end
if is_valid
point_string = 'setsrid(makepoint(' + lon_s + ', ' + lat_s +'), 4326)'
country_select = 'SELECT name,country_code FROM "world_countries_polygon" WHERE ST_DWithin('+point_string+', way, 0.1);'
country_hashes = select_as_hashes(country_select, DSTKConfig::REVERSE_GEO_DATABASE)
if !country_hashes or country_hashes.length == 0
is_valid = false
end
end
if !is_valid
output = nil
else
output = []
country_hashes.each do |country_hash|
country_name = country_hash['name']
country_code = country_hash['country_code'].downcase
output.push({
:name => country_name,
:code => country_code,
:type => 'admin2',
:friendly_type => 'country'
})
area_select = 'SELECT name,code,type FROM "admin_areas_polygon" WHERE ST_DWithin('+point_string+', way, 0.01);'
area_hashes = select_as_hashes(area_select, DSTKConfig::REVERSE_GEO_DATABASE)
if area_hashes
area_hashes.each do |area_hash|
area_name = area_hash['name']
area_code = area_hash['code'].downcase
area_type = area_hash['type']
if TypeToFriendly.has_key?(area_type)
friendly_type = TypeToFriendly[area_type]
else
friendly_type = area_type
end
output.push({
:name => area_name,
:code => area_code,
:type => area_type,
:friendly_type => friendly_type
})
end
end
# Look in the neighborhoods table if we're in the US
if country_code == 'usa'
neighborhood_select = 'SELECT name,city,state FROM "neighborhoods_polygon" WHERE ST_DWithin('+point_string+', way, 0.0001);'
neighborhood_hashes = select_as_hashes(neighborhood_select, DSTKConfig::REVERSE_GEO_DATABASE)
if neighborhood_hashes
neighborhood_hashes.each do |neighborhood_hash|
neighborhood_name = neighborhood_hash['name']
neighborhood_city = neighborhood_hash['city']
neighborhood_state = neighborhood_hash['state']
neighborhood_type = 'neighborhood'
friendly_type = 'neighborhood'
neighborhood_code = neighborhood_name+'|'+neighborhood_city+'|'+neighborhood_state
output.push({
:name => neighborhood_name,
:code => neighborhood_code,
:type => neighborhood_type,
:friendly_type => friendly_type
})
end
end
elsif country_code == 'eng' or country_code == 'sct' or country_code == 'wls'
distance = 0.01
uk_hashes = nil
while distance < 1.0 and !uk_hashes do
uk_select = 'SELECT postcode,country_code,nhs_region_code,nhs_code,county_code,district_code,ward_code,location'+
' FROM "uk_postcodes" WHERE ST_DWithin('+
point_string+
', location, '+
distance.to_s+
') ORDER BY ST_Distance('+
point_string+
', location) LIMIT 1;'
uk_hashes = select_as_hashes(uk_select, DSTKConfig::REVERSE_GEO_DATABASE)
distance *= 2
end
if uk_hashes
uk_hashes.each do |uk_hash|
postal_code = uk_hash['postcode']
nhs_code = uk_hash['nhs_region_code']+uk_hash['nhs_code']
ward_code = uk_hash['county_code']+uk_hash['district_code']+uk_hash['ward_code']
ward_select = 'SELECT * FROM uk_ward_names WHERE ward_code=\''+ward_code+'\';'
ward_hashes = select_as_hashes(ward_select, DSTKConfig::REVERSE_GEO_DATABASE)
ward_info = ward_hashes[0]
ward_name = ward_info['name']
output.push({
:name => postal_code,
:code => postal_code,
:type => 'postal_code',
:friendly_type => 'postal code'
})
output.push({
:name => nhs_code,
:code => nhs_code,
:type => 'uk_nhs_area',
:friendly_type => 'nhs area'
})
output.push({
:name => ward_name,
:code => ward_code,
:type => 'admin10',
:friendly_type => 'council ward'
})
end
end
end
end
end
result.push({
:location => location,
:politics => output
})
end
result
end
if __FILE__ == $0
locations = [ {:latitude => "37.769456", :longitude => "-122.429128"} ]
$stderr.puts "locations=#{JSON.pretty_generate(locations)}"
politics = coordinates2politics(locations)
$stderr.puts "politics=#{JSON.pretty_generate(politics)}"
end