lua-resty-opencage-geocoder

Opencage 正反向地理编码 API 的 Lua 客户端

$ opm get nmdguerreiro/lua-resty-opencage-geocoder

lua-resty-opencage-geocoder

此 Lua 模块提供了一个简单的 OpenCage 正反向地理编码 API 客户端,可用于 Openresty

安装

要安装此模块,请使用 OpenResty 包管理器运行以下命令:

    opm get nmdguerreiro/lua-resty-opencage-geocoder

示例用法

请参阅 nginx 配置文件示例。它包含了如何调用 OpenCage API 的示例。注意:在启动 nginx 之前,您需要在配置文件中替换您的 API 密钥。

重新启动 nginx 后,您应该能够获得一些结果。

    $ curl localhost:8080
    
    
    Result: {"timestamp":{"created_http":"Sat, 24 Feb 2018 19:21:44 GMT","created_unix":1519500104},"documentation":"https:\/\/geocoder.opencagedata.com\/api","thanks":"For using an OpenCage Data API","stay_informed":{"blog":"https:\/\/blog.opencagedata.com","twitter":"https:\/\/twitter.com\/opencagedata"},"results":[{"geometry":{"lng":13.3777025,"lat":52.5162767},"components":{"_type":"attraction","ISO_3166-1_alpha-2":"DE","suburb":"Mitte","state":"Berlin","road":"Pariser Platz","political_union":"European Union","house_number":"1","city":"Berlin","city_district":"Mitte","country":"Germany","postcode":"10117","country_code":"de","attraction":"Brandenburg Gate"},"confidence":9,"bounds":{"southwest":{"lng":13.37758,"lat":52.5161167},"northeast":{"lng":13.377825,"lat":52.5164327}},"formatted":"Brandenburg Gate, Pariser Platz 1, 10117 Berlin, Germany"},{"geometry":{"lng":-91.554716,"lat":34.515457},"components":{"_type":"road","state":"Arkansas","town":"Stuttgart","state_code":"AR","county":"Arkansas County","postcode":"72160","country":"United States of America","road":"Brandenburg Gate","country_code":"us","ISO_3166-1_alpha-2":"US"},"confidence":9,"bounds":{"southwest":{"lng":-91.5575726,"lat":34.515457},"northeast":{"lng":-91.554716,"lat":34.5155269}},"formatted":"Brandenburg Gate, Stuttgart, AR 72160, United States of America"}],"licenses":[{"url":"http:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/","name":"CC-BY-SA"},{"url":"http:\/\/opendatacommons.org\/licenses\/odbl\/summary\/","name":"ODbL"}],"total_results":2,"rate":{"limit":2500,"reset":1519516800,"remaining":2481},"status":{"message":"OK","code":200}}

正向地理编码

要执行正向地理编码请求,您只需实例化客户端并进行 geocode 调用,如下所示:

    local geocoder = require "opencage.geocoder"
    
    local gc = geocoder.new({
      key = "REPLACE WITH YOUR KEY"
    })
    
    local res, status, err = gc:geocode("Brandenburg Gate")
    
    gc.close()
    

请记住关闭客户端,以便在您完成后关闭任何底层连接。

反向地理编码

类似地,要发出反向地理编码请求,您只需实例化客户端并进行 reverse_geocode 调用,如下所示:

    local geocoder = require "opencage.geocoder"
    
    local lat, long = 52.5162767, 13.3777025
    
    local gc = geocoder.new({
      key = "REPLACE WITH YOUR KEY"
    })
    
    local res, status, err = gc:reverse_geocode(lat, long)
    
    gc.close()

同样,请记住关闭客户端,以便在您完成后关闭任何底层连接。

错误处理

geocodereverse_geocode 的调用返回三个值:

  • 表示 OpenCage API 返回的 JSON 的表格。

  • 状态码。

  • 错误消息(如果适用)。

为了方便起见,状态码在客户端对象本身可用,并且定义如下(与 API 指南 保持一致):

    gc.status_ok = 200
    gc.status_invalid_request = 400
    gc.status_quota_exceeded = 402
    gc.status_invalid_key = 403
    gc.status_timeout = 408
    gc.status_request_too_long = 410
    gc.status_rate_exceeded = 429
    gc.status_internal_server_error = 503

因此,您可以像这样处理错误:

    local res, status, err = gc:geocode("Brandenburg Gate", params)
    
    if (status = gc.status_invalid_key) then
        ngx.log(ngx.ERR, "It seems we forgot to set our API key correctly :-)")
    end

参数

您可以提供任何其他参数来帮助改善您的结果,如 API 指南 中所述。例如:

    local geocoder = require "opencage.geocoder"
    
    local gc = geocoder.new({
      key = "REPLACE WITH YOUR KEY"
    })
    
    params = { abbrv = 1 }
    local res, status, err = gc:geocode("Brandenburg Gate", params)
    
    gc.close()

连接设置

此模块依赖于 lua-resty-http,它允许您配置连接和请求超时。要设置超时,请使用 timeout 参数。

    local gc = geocoder.new({
      key = "REPLACE WITH YOUR KEY",
      timeout = 5000, -- maximum timeout in milliseconds
    })

许可证

MIT

作者

Nuno Guerreiro

许可证

mit

版本