4554a508472a3afa896d3122a459af1eaf246695
[geeqie.git] / plugins / geocode-parameters / geocode-parameters.awk
1 #
2 # This file is used by the Search option "search on geo-position".
3 # It is used to decode the results of internet or other searches
4 # to extract a geo-position from a text string.
5 #
6 # To include other searches, follow the examples below and
7 # store the file in:
8 # ~/.config/geeqie/applications/geocode-parameters.awk
9 # Ensure the returned value is either in the format:
10 # 89.123 179.123
11 # or
12 # Error: $0
13 #
14
15 function check_parameters(latitude, longitude)
16     {
17     # Ensure the parameters are numbers
18     if ((latitude == (latitude+0)) && (longitude == (longitude+0)))
19         {
20         if (latitude >= -90 && latitude <= 90 &&
21                         longitude >= -180 && longitude <= 180)
22             {
23             return latitude " " longitude
24             }
25         else
26             {
27             return "Error: " latitude " " longitude
28             }
29         }
30     else
31         {
32         return "Error: " latitude " " longitude
33         }
34     }
35
36 # This awk file is accessed by the decode_geo_parameters() function
37 # in search.c. The call is of the format:
38 # echo "string_to_be_searched" | awk -f geocode-parameters.awk
39 #
40 # Search the input string for known formats.
41 {
42 if (index($0, "http://www.geonames.org/maps/google_"))
43     {
44     # This is a drag-and-drop or copy-paste from a geonames.org search
45     # in the format e.g.
46     # http://www.geonames.org/maps/google_51.513_-0.092.html
47
48     gsub(/http:\/\/www.geonames.org\/maps\/google_/, "")
49     gsub(/.html/, "")
50     gsub(/_/, " ")
51     print check_parameters($1, $2)
52     }
53
54 else if (index($0, "https://www.openstreetmap.org/search?query="))
55     {
56     # This is a copy-paste from an openstreetmap.org search
57     # in the format e.g.
58     # https://www.openstreetmap.org/search?query=51.4878%2C-0.1353#map=11/51.4880/-0.1356
59
60     gsub(/https:\/\/www.openstreetmap.org\/search\?query=/, "")
61     gsub(/#map=.*/, "")
62     gsub(/%2C/, " ")
63     print check_parameters($1, $2)
64     }
65
66 else if (index($0, "https://www.openstreetmap.org/#map="))
67     {
68     # This is a copy-paste from an openstreetmap.org search
69     # in the format e.g.
70     # https://www.openstreetmap.org/#map=5/18.271/16.084
71
72     gsub(/https:\/\/www.openstreetmap.org\/#map=[^\/]*/,"")
73     gsub(/\//," ")
74     print check_parameters($1, $2)
75     }
76
77 else if (index($0, "https://www.google.com/maps/"))
78     {
79     # This is a copy-paste from a google.com maps search
80     # in the format e.g.
81     # https://www.google.com/maps/place/London,+UK/@51.5283064,-0.3824815,10z/data=....
82
83     gsub(/https:\/\/www.google.com\/maps.*@/,"")
84     sub(/,/," ")
85     gsub(/,.*/,"")
86     print check_parameters($1, $2)
87     }
88
89 else if (index($0,".html"))
90     {
91     # This is an unknown html address
92
93     print "Error: " $0
94     }
95
96 else if (index($0,"http"))
97     {
98     # This is an unknown html address
99
100     print "Error: " $0
101     }
102
103 else if (index($0, ","))
104     {
105     # This is assumed to be a simple lat/long of the format:
106     # 89.123,179.123
107
108     split($0, latlong, ",")
109     print check_parameters(latlong[1], latlong[2])
110     }
111
112 else
113     {
114     # This is assumed to be a simple lat/long of the format:
115     # 89.123 179.123
116
117     split($0, latlong, " ")
118     print check_parameters(latlong[1], latlong[2])
119     }
120 }