nominatim_reverse.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2014 Daniel 'grindhold' Brendle
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Authors:
  18. * Daniel 'grindhold' Brendle <grindhold@skarphed.org>
  19. */
  20. package gominatim
  21. import (
  22. "encoding/json"
  23. "errors"
  24. "fmt"
  25. "io/ioutil"
  26. "net/http"
  27. "net/url"
  28. )
  29. type reverseAPIResult struct {
  30. ReverseResult
  31. Error string `json:"error"`
  32. }
  33. type ReverseResult struct {
  34. PlaceId string `json:"place_id"`
  35. License string `json:"license"`
  36. OsmType string `json:"osm_type"`
  37. OsmId string `json:"osm_id"`
  38. Lat string `json:"lat"`
  39. Lon string `json:"lon"`
  40. DisplayName string `json:"display_name"`
  41. Address Address `json:"address"`
  42. }
  43. type ReverseQuery struct {
  44. JsonCallback interface{}
  45. AcceptLanguage string
  46. OsmType string
  47. OsmId string
  48. Lat string
  49. Lon string
  50. Zoom int
  51. AddressDetails bool
  52. Email string
  53. }
  54. func (r *ReverseQuery) buildQuery() (string, error) {
  55. if server == "" {
  56. return "", errors.New("Server is not set. Set via gominatim.SetServer(srv string)")
  57. }
  58. s := server
  59. s = s + "/reverse?format=json"
  60. if r.AcceptLanguage != "" {
  61. s = s + "&accept-language=" + r.AcceptLanguage
  62. }
  63. if r.OsmType != "" {
  64. if r.OsmType != "N" && r.OsmType != "W" && r.OsmType != "R" {
  65. return "", errors.New("OsmType must be 'N', 'W' or 'R'")
  66. }
  67. s = s + "&osm_type=" + r.OsmType
  68. }
  69. if r.OsmId != "" {
  70. s = s + "&osm_id=" + r.OsmType
  71. }
  72. if r.Lat == "" {
  73. return "", errors.New("Cannot search without a latitude. Set field Lat")
  74. }
  75. s = s + "&lat=" + r.Lat
  76. if r.Lon == "" {
  77. return "", errors.New("Cannot search without a longitude. Set field Lon")
  78. }
  79. s = s + "&lon=" + r.Lon
  80. if r.Zoom > 18 || r.Zoom < 0 {
  81. return "", errors.New(fmt.Sprintf("Zoom must be within 0 and 18. %d is out of range", r.Zoom))
  82. }
  83. s = s + fmt.Sprintf("&zoom=%d", r.Zoom)
  84. if r.AddressDetails {
  85. s = s + "&addressdetails=1"
  86. } else {
  87. s = s + "&addressdetails=0"
  88. }
  89. if r.Email != "" {
  90. s = s + "&email=" + url.QueryEscape(r.Email)
  91. }
  92. return s, nil
  93. }
  94. func (r *ReverseQuery) Get() (*ReverseResult, error) {
  95. querystring, err := r.buildQuery()
  96. if err != nil {
  97. return nil, err
  98. }
  99. resp, err := http.Get(querystring)
  100. if err != nil {
  101. return nil, err
  102. }
  103. defer resp.Body.Close()
  104. body, err := ioutil.ReadAll(resp.Body)
  105. if err != nil {
  106. return nil, err
  107. }
  108. result := new(reverseAPIResult)
  109. err = json.Unmarshal(body, &result)
  110. if err != nil {
  111. return nil, err
  112. }
  113. if result.Error != "" {
  114. return nil, errors.New(result.Error)
  115. }
  116. return &result.ReverseResult, nil
  117. }