-
Notifications
You must be signed in to change notification settings - Fork 176
/
latlong.pl
65 lines (59 loc) · 1.97 KB
/
latlong.pl
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
:- use_module(library(http/http_open)).
:- use_module(library(http/json)).
:- use_module(library(listing)).
:- dynamic(country_location/2).
latlong_from_google(Name,
loc( FA,
box(Bound_NELat, Bound_NELong, Bound_SWLat, Bound_SWLong),
pair(Loc_Lat, Loc_Long),
box(View_NELat, View_NELong, View_SWLat, View_SWLong)
)) :-
atom_codes(AName , Name),
www_form_encode(AName , FEName),
atom_concat('http://maps.googleapis.com/maps/api/geocode/json?address=',
FEName , PreReq),
atom_concat(PreReq, '&sensor=false', Req),
http_open(Req, Stream, []),
% read_stream_to_codes(In , Codes),
json_read(Stream , Term),
close(Stream),
json([results=Results,status='OK']) = Term,
[json([address_components=_AC, formatted_address=FA, geometry=GEO,types=_TYPES])] = Results,
json(
[ =(bounds,
json(
[ northeast=json([lat= Bound_NELat, lng=Bound_NELong]),
southwest=json([lat= Bound_SWLat, lng=Bound_SWLong])
])),
location=json([lat= Loc_Lat, lng=Loc_Long]),
location_type=_,
=(viewport,
json(
[ northeast=json([lat= View_NELat, lng=View_NELong]),
southwest=json([lat= View_SWLat, lng=View_SWLong])
]))
]) = GEO.
% portray_clause(GEO),nl.
write_loc(loc( FA,
box(Bound_NELat, Bound_NELong, Bound_SWLat, Bound_SWLong),
pair(Loc_Lat, Loc_Long),
box(View_NELat, View_NELong, View_SWLat, View_SWLong)
)) :-
format('~w,~f,~f,~f,~f,~f,~f,~f,~f,~f,~f~n',
[FA, Bound_NELat, Bound_NELong, Bound_SWLat, Bound_SWLong,
Loc_Lat, Loc_Long,
View_NELat, View_NELong, View_SWLat, View_SWLong]).
assert_capital_locations :-
retractall(country_location(_,_)),
open('countrynames.csv' , read , Stream),
repeat,
read_line_to_codes(Stream , Name),
( Name = end_of_file ->
true
;
latlong_from_google(Name, Loc),
asserta(country_location(Name, Loc)),
write_loc(Loc),
fail
),
close(Stream).