项目作者: nmdguerreiro

项目描述 :
A simple client for the OpenCage forward/reverse geocoding API
高级语言: Lua
项目地址: git://github.com/nmdguerreiro/lua-opencage-geocoder.git
创建时间: 2018-02-25T20:26:15Z
项目社区:https://github.com/nmdguerreiro/lua-opencage-geocoder

开源协议:

下载


lua-opencage-geocoder

This Lua module provides a simple client for the OpenCage forward/reverse geocoding API.
If you’d like to call the geocoding API from an OpenResty installation, check out the sister library.

Installation

To install this module, run the following command using the luarocks package manager:

  1. luarocks install lua-opencage-geocoder

Forward geocoding

To perform a forward geocoding request, all you need to do is instantiate the client and make the geocode call as is shown below:

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

Reverse geocoding

Similarly, to issue a reverse geocoding request, all you need to do is instantiate the client and make the reverse_geocode call as is shown below:

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

Error handling

Calls to geocode and reverse_geocode return three values:

  • The table that represents the JSON returned by the OpenCage API.
  • A status code
  • An error message, if applicable

For convenience, the status codes are available on the client object itself and are defined as per below (and in-line with the API guide):

  1. gc.status_ok = 200
  2. gc.status_invalid_request = 400
  3. gc.status_quota_exceeded = 402
  4. gc.status_invalid_key = 403
  5. gc.status_timeout = 408
  6. gc.status_request_too_long = 410
  7. gc.status_rate_exceeded = 429
  8. gc.status_internal_server_error = 503

Parameters

You can supply any additional parameters to help improve your results, as is described in the API guide. For example:

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

Dependencies

This library depends on the following luarocks packages:

  • luasocket
  • cjson
  • net-url
  • busted (for running tests locally)

If you’re running tests locally, you’ll also need to install docker.

Licence

MIT