Skip to content
This repository has been archived by the owner on Oct 24, 2022. It is now read-only.

Commit

Permalink
Squash/merge: Add GeoJSON support to the forward, reverse and grid me…
Browse files Browse the repository at this point in the history
…thods

commit 2024c60
Author: vicchi <gary@vicchi.org>
Date:   Wed Oct 5 16:24:44 2016 +0100

    Squash/merge: Add GeoJSON support to the forward, reverse and grid methods

    commit 450e1ce
    Author: vicchi <gary@vicchi.org>
    Date:   Wed Oct 5 16:19:58 2016 +0100

        Add GeoJSON support to the forward, reverse and grid methods
  • Loading branch information
vicchi committed Oct 5, 2016
1 parent bb51969 commit ac3f467
Show file tree
Hide file tree
Showing 9 changed files with 559 additions and 54 deletions.
5 changes: 5 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ module.exports = function(grunt) {
}
}
},
version: {
project: {
src: ['bower.json', 'src/js/Bootstrap.js', 'package.json']
}
},
watch: {
options: {
livereload: true,
Expand Down
126 changes: 123 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ Object literal containing the parameters to be passed to the `forward` method.
| ----- | ---- | ------- | ----------- |
| `addr` | `String` | None | A 3 word address |
| `lang` | `String` | `en` | A supported 3 word address language as an [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) 2 letter code |
| `format` | `String` | `json` | Return data format, can be `json` or `geojson` |

#### `callbackHandler`

Object literal containing the `onSuccess` and `onFailure` functions; see [callbacks](#callbacks) for more information.

The `onSuccess` callback's JSON will look similar to the following:
When JSON output is requested, the `onSuccess` callback's response will look similar to the following:

```JSON
{
Expand Down Expand Up @@ -114,6 +115,44 @@ The `onSuccess` callback's JSON will look similar to the following:
}
```

When GeoJSON output is requested, the `onSuccess` callback's response will look similar to the following:

```JSON
{
"crs": {
"type": "link",
"properties": {
"href": "http://spatialreference.org/ref/epsg/4326/ogcwkt/",
"type": "ogcwkt"
}
},
"bbox": [
-0.203607,
51.521238,
-0.203564,
51.521265
],
"geometry": {
"coordinates": [
-0.203586,
51.521251
],
"type": "Point"
},
"type": "Feature",
"properties": {
"words": "index.home.raft",
"language": "en",
"map": "http://w3w.co/index.home.raft",
"status": {
"status": 200,
"reason": "OK"
},
"thanks": "Thanks from all of us at index.home.raft for using a what3words API"
}
}
```

#### Forward Geocoding Example

```JavaScript
Expand Down Expand Up @@ -157,12 +196,13 @@ Object literal containing the parameters to be passed to the `reverse` method.
| ----- | ---- | ------- | ----------- |
| `coords` | `String`,`Array` | None | Coordinates either as a comma separated string or as an array in long, lat order |
| `lang` | `String` | `en` | A supported 3 word address language as an [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) 2 letter code |
| `format` | `String` | `json` | Return data format, can be `json` or `geojson` |

#### `callbackHandler`

Object literal containing the `onSuccess` and `onFailure` functions; see [callbacks](#callbacks) for more information.

The `onSuccess` callback's JSON will look similar to the following:
When JSON output is requested, the `onSuccess` callback's response will look similar to the following:

```JSON
{
Expand Down Expand Up @@ -198,6 +238,44 @@ The `onSuccess` callback's JSON will look similar to the following:
}
```

When GeoJSON output is requested, the `onSuccess` callback's response will look similar to the following:

```JSON
{
"crs": {
"type": "link",
"properties": {
"href": "http://spatialreference.org/ref/epsg/4326/ogcwkt/",
"type": "ogcwkt"
}
},
"bbox": [
-0.203607,
51.521238,
-0.203564,
51.521265
],
"geometry": {
"coordinates": [
-0.203586,
51.521251
],
"type": "Point"
},
"type": "Feature",
"properties": {
"words": "index.home.raft",
"language": "en",
"map": "http://w3w.co/index.home.raft",
"status": {
"status": 200,
"reason": "OK"
},
"thanks": "Thanks from all of us at index.home.raft for using a what3words API"
}
}
```

#### Reverse Geocoding Example

```JavaScript
Expand Down Expand Up @@ -538,12 +616,14 @@ Object literal containing the parameters to be passed to the `grid` method.
| Param | Type | Default | Description |
| ----- | ---- | ------- | ----------- |
| `bbox` | `String`, `Array` | None | Bounding box, specified by the northeast and southwest corner coordinates, for which the grid should be returned. The bounding box can be specified either as a comma separated string or as an array of 4 elements in lat, long order|
| `format` | `String` | `json` | Return data format, can be `json` or `geojson` |


#### `callbackHandler`

Object literal containing the `onSuccess` and `onFailure` functions; see [callbacks](#callbacks) for more information.

The `onSuccess` callback's JSON will look similar to the following:
When JSON output is requested, the `onSuccess` callback's response will look similar to the following:

```JSON
{
Expand Down Expand Up @@ -578,6 +658,34 @@ The `onSuccess` callback's JSON will look similar to the following:
}
```

When GeoJSON output is requested, the `onSuccess` callback's response will look similar to the following:

```JSON
{
"coordinates": [
[
[
0.11612600000001,
52.208009918068
],
[
0.11753999999999,
52.208009918068
]
],
...
],
"type": "MultiLineString",
"properties": {
"status": {
"status": 200,
"reason": "OK"
},
"thanks": "Thanks from all of us at index.home.raft for using a what3words API"
}
}
```

#### Grid Example

```JavaScript
Expand Down Expand Up @@ -701,3 +809,15 @@ var callback = {

w3w.languages(callback);
```

# Revision History

* `v3.1.0` 05/10/16 - Add support for GeoJSON returns for the `forward`, `reverse` and `grid` methods
* `v3.0.5` 18/8/16 - Fix bug in language handling. Add additional system tests
* `v3.0.4` 18/07/16 - Fix bug in handling options
* `v3.0.3` 27/6/16 - Fix Bower installation bug and rationalise version numbers
* `v3.0.2` 26/6/16 - Minor README tweaks
* `v3.0.1` 24/6/16 - Update Travis build with encrypted test API key
* `v3.0.0` 24/6/16 - Rewrite and restructure; add system tests and Travis builds
* `v2.0.0` 24/7/15 - Use v2 of the what3words RESTful API under the hood
* `v1.0.0` 27/1/15 - Initial release
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "w3w-javascript-wrapper",
"version": "3.0.5",
"version": "3.1.0",
"homepage": "https://github.com/what3words/w3w-javascript-wrapper",
"authors": [
"Will Henderson <will@what3words.com>",
Expand Down
71 changes: 64 additions & 7 deletions dist/W3W.Geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';

var W3W = {
version: '3.0.5'
version: '3.0.6'
};

if (typeof module === 'object' && typeof module.exports === 'object') {
Expand Down Expand Up @@ -120,10 +120,11 @@ W3W.Geocoder = function(options) {
throw new Error('Missing what3words API key');
}
this.options = {
lang: 'en'
lang: 'en',
format: 'json'
};
this.options = W3W.Utils.mergeOptions(this.options, options);
if( this.options.hasOwnProperty('base_url')) {
if (this.options.hasOwnProperty('base_url')) {
this.base_url = this.options.base_url;
delete this.options.base_url;
}
Expand All @@ -138,8 +139,9 @@ W3W.Geocoder = function(options) {
};

// var params = {
// addr: 'index.home.raft',
// lang: 'en'
// addr: '3-word-address',
// lang: 'en',
// format: 'json|geojson'
// };
W3W.Geocoder.prototype.forward = function(params, callback) {
if (typeof params === 'undefined' || typeof params !== 'object') {
Expand All @@ -157,6 +159,15 @@ W3W.Geocoder.prototype.forward = function(params, callback) {
if (params.hasOwnProperty('lang') && typeof params.lang !== 'string') {
throw new Error('params.lang must be a string');
}

if (params.hasOwnProperty('format')) {
if (typeof params.format !== 'string') {
throw new Error('params.format must be a string');
}
else if (params.format !== 'json' && params.format !== 'geojson') {
throw new Error('params.format must have a value of "json" or "geojson"');
}
}
}

if (typeof callback === 'undefined') {
Expand All @@ -174,7 +185,8 @@ W3W.Geocoder.prototype.forward = function(params, callback) {
// var params = {
// coords: [lat, long],
// coords: 'lat,long',
// lang: 'en'
// lang: 'en',
// format: 'json|geojson'
// };
W3W.Geocoder.prototype.reverse = function(params, callback) {
if (typeof params === 'undefined' || typeof params !== 'object') {
Expand All @@ -195,6 +207,15 @@ W3W.Geocoder.prototype.reverse = function(params, callback) {
if (params.hasOwnProperty('lang') && typeof params.lang !== 'string') {
throw new Error('params.lang must be a string');
}

if (params.hasOwnProperty('format')) {
if (typeof params.format !== 'string') {
throw new Error('params.format must be a string');
}
else if (params.format !== 'json' && params.format !== 'geojson') {
throw new Error('params.format must have a value of "json" or "geojson"');
}
}
}

if (typeof callback === 'undefined') {
Expand All @@ -211,7 +232,7 @@ W3W.Geocoder.prototype.reverse = function(params, callback) {

// var params = {
// lang: 'en',
// addr: 'index.home.r'
// addr: '3-word-address'
// focus: [lat, lng],
// focus: 'lat,lng',
// clip: {
Expand Down Expand Up @@ -262,6 +283,15 @@ W3W.Geocoder.prototype.autosuggest = function(params, callback) {
throw new Error('params.lang must be a string');
}

if (params.hasOwnProperty('format')) {
if (typeof params.format !== 'string') {
throw new Error('params.format must be a string');
}
else if (params.format !== 'json') {
throw new Error('params.format must have a value of "json"');
}
}

if (params.hasOwnProperty('clip')) {
if (!params.clip.hasOwnProperty('type')) {
throw new Error('Invalid clipping policy type for params.clip');
Expand Down Expand Up @@ -341,6 +371,11 @@ W3W.Geocoder.prototype.autosuggest = function(params, callback) {
W3W.Xhr.handleRequest(url, callback);
};

// var params = {
// addr: '3-word-address',
// lang: 'en',
// focus: '[lat, lng]
// };
W3W.Geocoder.prototype.standardblend = function(params, callback) {
if (typeof params === 'undefined' || typeof params !== 'object') {
throw new Error('Missing or invalid params object');
Expand All @@ -367,6 +402,15 @@ W3W.Geocoder.prototype.standardblend = function(params, callback) {
else if (typeof params.lang !== 'string') {
throw new Error('params.lang must be a string');
}

if (params.hasOwnProperty('format')) {
if (typeof params.format !== 'string') {
throw new Error('params.format must be a string');
}
else if (params.format !== 'json') {
throw new Error('params.format must have a value of "json"');
}
}
}

if (typeof callback === 'undefined') {
Expand All @@ -381,6 +425,10 @@ W3W.Geocoder.prototype.standardblend = function(params, callback) {
W3W.Xhr.handleRequest(url, callback);
};

// var params = {
// bbox: [nelat, nelng, swlat, swlng],
// format: 'json|geojson'
// };
W3W.Geocoder.prototype.grid = function(params, callback) {
if (typeof params === 'undefined' || typeof params !== 'object') {
throw new Error('Missing or invalid params object');
Expand All @@ -395,6 +443,15 @@ W3W.Geocoder.prototype.grid = function(params, callback) {
if (null === params.bbox) {
throw new Error('Invalid format coordinates for params.bbox');
}

if (params.hasOwnProperty('format')) {
if (typeof params.format !== 'string') {
throw new Error('params.format must be a string');
}
else if (params.format !== 'json' && params.format !== 'geojson') {
throw new Error('params.format must have a value of "json" or "geojson"');
}
}
}

if (typeof callback === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion dist/W3W.Geocoder.min.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "w3w-javascript-wrapper",
"version": "3.0.5",
"version": "3.1.0",
"description": "Javascript wrapper for the what3words API",
"main": "dist/W3W.Geocoder.js",
"scripts": {
Expand All @@ -26,10 +26,10 @@
"email": "will@what3words.com"
},
{
"name": "David Piesse"
"name": "David Piesse"
},
{
"name": "Oliver Svenson"
"name": "Oliver Svenson"
},
{
"name": "Arnaud Ferrand",
Expand All @@ -49,6 +49,7 @@
"grunt-contrib-jshint": "^0.11.2",
"grunt-contrib-uglify": "^0.9.1",
"grunt-contrib-watch": "^0.6.1",
"grunt-version": "^1.1.1",
"jasmine-expect": "^2.0.2",
"load-grunt-tasks": "^3.5.0"
},
Expand Down
Loading

0 comments on commit ac3f467

Please sign in to comment.