Skip to content

Commit

Permalink
Merge pull request #1 from Skycatch/dev
Browse files Browse the repository at this point in the history
[INFRA-744] Update Ptolemy
  • Loading branch information
xasos authored Jul 20, 2016
2 parents 1c92c4c + 7047549 commit 7a239be
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 106 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ node_modules

# Users Environment Variables
.lock-wscript

# ESLint Config Files
.eslintrc
6 changes: 1 addition & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
language: node_js
node_js:
- "0.12"
- "0.11"
- "0.10"
- "iojs"
- "iojs-v1.0.4"
- "4.4.4"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Ben L.
Copyright (c) 2015-2016 Skycatch, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
58 changes: 41 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# ptolemy
[![Build Status](http://img.shields.io/travis/lyaunzbe/ptolemy.svg?style=flat-square)](https://travis-ci.org/lyaunzbe/ptolemy)
[![Build Status](http://img.shields.io/travis/Skycatch/ptolemy.svg?style=flat-square)](https://travis-ci.org/Skycatch/ptolemy)
[![Build Status](http://img.shields.io/npm/v/ptolemy.svg?style=flat-square)](https://www.npmjs.org/package/ptolemy)

![ptolemy](http://i.imgur.com/OEqohGJ.png)

A simple way to retrieve geographic projection information, in a variety of formats, from an [EPSG SRID](http://en.wikipedia.org/wiki/SRID). Uses the [epsg.io](http://epsig.io/about/) website.
A simple way to retrieve geographic projection information, in a variety of formats, from an [EPSG SRID](http://en.wikipedia.org/wiki/SRID). Uses the [EPSG.io](http://epsig.io/about/) database.

The following formats for projections are supported:

The following formats for projections are supported (**Disclaimer**: Not all SRIDs will support every format.):

* prettywkt (Well Known Text as HTML)
* wkt (OGC WKT)
* esriwkt (ESRI WKT)
* proj4
* wkt
* prettywkt (human-readable)
* esriwkt
* gml
* js (Proj4js - always be careful when using eval)
* usgs
* geoserver
* mapfile (MapServer - MAPfile)
* mapnik
* [proj4js](http://proj4js.org/) (returns js code - always be careful when using eval)
* sql (PostGIS)

Install
-------
Expand All @@ -23,25 +26,46 @@ Install
$ npm install ptolemy
```

Usage
Example
-----

```js
var ptolemy = require('ptolemy');

ptolemy.get('4326', 'ogcwkt', function (err, resp) {
console.log(resp);
ptolemy.get('2004', 'proj4')
.then((res) => {
console.log(res);
})
.catch((e) => {
throw e;
});

// Result
'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]'
{
"epsg": 2004,
"name": "Montserrat 1958 / British West Indies Grid",
"proj4": "+proj=tmerc +lat_0=0 +lon_0=-62 +k=0.9995000000000001 +x_0=400000 +y_0=0 +ellps=clrk80 +towgs84=174,359,365,0,0,0,0 +units=m +no_defs"
}
```

Development + Testing
-------
```sh
# Develop library locally
$ npm install
$ npm link
# In project that requires Ptolemy
$ npm link ptolemy # Now points to locally cloned Ptolemy

# Test
$ npm test
```

Credits
---------
[http://epsg.io/](http://epsg.io/)
[EPSG.io](http://epsg.io/)

Copyright
---------
License
-------

(c) 2015 Ben Lyaunzon Licensed under the MIT license.
[MIT License](LICENSE)
63 changes: 1 addition & 62 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1 @@
var needle = require('needle');

var BASE_URI = 'http://epsg.io/',
Ptolemy = {},
formatWhiteList = [
'proj4',
'wkt',
'prettywkt',
'esriwkt',
'gml',
'mapnik',
'proj4js'
];
/**
* CURRENTLY SUPPORTED PROJECTION FORMATS:
* proj4
* ogcwkt
* prettywkt (human-readable)
* esriwkt
* gml
* mapnik
* proj4js (always be careful when using eval)
*/

function validateEPSG (srid) {
if (srid.match("[0-9]+") && srid.length < 7) {
return true;
} else {
return false;
}
}

/**
* Provide an EPSG SRID and the requested projection format you want to retrieve.
*
* @param {string or number} epsg The EPSG SRID
* @param {string} info The projection info being requested
* @return {function} callback Returns projection info requested
*/
Ptolemy.get = function(epsg, format, callback) {
epsg = epsg.toString();
if (validateEPSG(epsg)) {
if (formatWhiteList.indexOf(format) > -1) {
var requestURI = BASE_URI + epsg + '.' + format;
needle.get(requestURI, {timeout:4000}, function(error, response) {
if (!error && response && response.statusCode == 200){
callback(null, response.body);
} else if (response && response.statusCode) {
callback(response.statusCode + ' : ' + response.body);
} else {
callback(error);
}
});
} else {
callback('Please make sure you are requesting one of the supported formats.');
}
} else {
callback('Please make sure you are providing a valid EPSG SRID');
}
};

module.exports = Ptolemy;
module.exports = require('./lib/ptolemy');
41 changes: 41 additions & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

class ExtendableError extends Error {
constructor(message) {
super();
this.message = message;
this.stack = (new Error(message)).stack;
this.name = this.constructor.name;
}
}

class InvalidSRIDError extends ExtendableError {
constructor(m) {
super(m);
}
}

class InvalidFormatError extends ExtendableError {
constructor(m) {
super(m);
}
}

class StatusCodeError extends ExtendableError {
constructor(m) {
super(m);
}
}

class NameError extends ExtendableError {
constructor(m) {
super(m);
}
}

module.exports = {
InvalidSRIDError: InvalidSRIDError,
InvalidFormatError: InvalidFormatError,
StatusCodeError: StatusCodeError,
NameError: NameError
};
108 changes: 108 additions & 0 deletions lib/ptolemy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const needle = require('needle');
const Bluebird = require('bluebird');
const InvalidFormatError = require('./error').InvalidFormatError;
const InvalidSRIDError = require('./error').InvalidSRIDError;
const StatusCodeError = require('./error').StatusCodeError;
const NameError = require('./error').NameError;

// Promisify Needle.get()
const promisfiedGet = Bluebird.promisify(needle.get);

const BASE_URL = 'http://epsg.io/';

function Ptolemy() {}

/**
* CURRENTLY SUPPORTED PROJECTION FORMATS:
* prettywkt (Well Known Text as HTML)
* wkt (OGC WKT)
* esriwkt (ESRI WKT)
* gml (OGC GML)
* xml
* proj4
* js (Proj4js - always be careful when using eval)
* usgs
* geoserver
* mapfile (MapServer - MAPfile)
* mapnik
* sql (PostGIS)
*/
var formatWhiteList = [
'prettywkt',
'wkt',
'esriwkt',
'proj4',
'js',
'usgs',
'geoserver',
'mapfile',
'mapnik',
'sql',
];

// UTIL FUNCTIONS
function isValidEPSG(srid) {
if (srid.match('[0-9]+') && srid.length < 7) {
return true;
} else {
return false;
}
}

/**
* Provide an EPSG SRID and the requested projection format you want to retrieve.
*
* @param {string or number} epsg The EPSG SRID
* @param {string} info The projection info being requested
* @return {function} returnObj Returns projection info requested
*/
Ptolemy.prototype.get = function(epsg, format) {
format = format.toLowerCase();
epsg = epsg.toString();
var returnObj = {};
returnObj.epsg = epsg;

if (!isValidEPSG(epsg)) {
return Bluebird.reject(new InvalidSRIDError('Invalid EPSG SRID.'));
}

if (!(formatWhiteList.indexOf(format) > -1)) {
return Bluebird.reject(new InvalidFormatError('Invalid format.'));
}

var requestURL = BASE_URL + epsg + '.' + format;
// Note: We parse the SRID's XML to get the Site Name (avoids scraping)
var nameURL = BASE_URL + epsg + '.xml';
var opts = {
timeout: 4000
};

return promisfiedGet(requestURL, opts)
.then((res) => {
if (res.statusCode !== 200) {
return Bluebird.reject(new StatusCodeError(res.statusCode + ': ' + res.statusMessage + '. ' + res.body));
}
returnObj[format] = res.body;
return promisfiedGet(nameURL, {
timeout: 4000
})
.then((res) => {
// Parse XML for name
if (res.body['gml:ProjectedCRS']) {
returnObj.name = res.body['gml:ProjectedCRS']['gml:srsName'];
}
else if (res.body['gml:GeographicCRS']) {
returnObj.name = res.body['gml:GeographicCRS']['gml:srsName'];
}
else {
return Bluebird.reject(new NameError('Projection doesn\'t support requested format.'));
}

return returnObj;
});
}).catch((e) => {
throw e;
});
};

module.exports = new Ptolemy();
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "ptolemy",
"version": "0.0.3",
"version": "1.0.0",
"description": "Retrieve geographic projection information from an EPSG SRID",
"main": "index.js",
"scripts": {
"test": "mocha test"
},
"repository": {
"type": "git",
"url": "https://github.com/lyaunzbe/ptolemy.git"
"url": "https://github.com/Skycatch/ptolemy.git"
},
"keywords": [
"srs",
Expand All @@ -20,14 +20,16 @@
"datum",
"crs"
],
"author": "Ben Lyaunzon",
"author": "Skycatch, Inc.",
"license": "MIT",
"bugs": {
"url": "https://github.com/lyaunzbe/ptolemy/issues"
"url": "https://github.com/Skycatch/ptolemy/issues"
},
"homepage": "https://github.com/lyaunzbe/ptolemy",
"homepage": "https://github.com/Skycatch/ptolemy",
"dependencies": {
"needle": "~0.8.2"
"bluebird": "^3.4.1",
"needle": "^1.0.0",
"xml2js": "^0.4.16"
},
"devDependencies": {
"should": "~5.2.0",
Expand Down
Loading

0 comments on commit 7a239be

Please sign in to comment.