File: map_geocode_nominatim.tcl

package info (click to toggle)
tcllib 1.21%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 69,456 kB
  • sloc: tcl: 266,493; ansic: 14,259; sh: 2,936; xml: 1,766; yacc: 1,145; pascal: 881; makefile: 112; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (91 lines) | stat: -rw-r--r-- 2,296 bytes parent folder | download | duplicates (5)
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
## -*- tcl -*-
# ### ### ### ######### ######### #########

## Geocoding (finding geo coordinates from location names and keywords)
## Reverse geocoding (putting names on coordinate sets)
## Both based on the nominatim interface

## See https://wiki.openstreetmap.org/wiki/Nominatim for details

# ### ### ### ######### ######### #########
## Requisites

package require Tcl 8.5
package require http
package require json
package require uri
package require snit

# ### ### ### ######### ######### #########
## Implementation

snit::type map::geocode::nominatim {
    # ### ### ### ######### ######### #########
    ## API

    proc callbackdefault {result} {
        # FIXME Is there a good default here?
        puts "callback: $result"
    }

    proc errordefault {err} {
        bgerror "nominatim error: $err"
    }

    option -baseurl "http://nominatim.openstreetmap.org/search"
    option -callback callbackdefault
    option -error errordefault
    

    # No special constructor, so far

    # ::nominatim::search
    # Queries the location server. Returns a list of dicts, each item having
    # - place_id
    # - licence
    # - osm_type
    # - osm_id
    # - boundingbox
    # - lat
    # - lon
    # - display_name
    # - class
    # - type
    # - icon 
    # Most interesting should be display_name, lat, lon and boundingbox
    method search {query} {
        set query [http::formatQuery q $query format json]
        http::geturl [uri::join {*}[uri::split $options(-baseurl)] query $query] \
            -command [mymethod Done] -timeout 60000
    }

    method Error {context err} {
        uplevel \#0 [list {*}$options(-error) "$context: $err"]
	return
    }

    # Private method
    method Done {htok} {
        if { [http::ncode $htok] != 200 } {
	    $self Error "HTTP" [http::code $htok]
            return
        }
	if { [catch {
	    set res [::json::json2dict [encoding convertfrom utf-8 [::http::data $htok]]]
	} _ err] } {
            $self Error "JSON" $err
            return
        }
        if { [catch {
	    uplevel \#0 [list {*}$options(-callback) $res]
	} _ err] } {
            $self Error "Callback" $err
        }
    }

    # ### ### ### ######### ######### #########
    ## State
    # none, so far
}

package provide map::geocode::nominatim 0.1