forked from lyaunzbe/ptolemy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Skycatch/dev
[INFRA-744] Update Ptolemy
- Loading branch information
Showing
9 changed files
with
226 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,6 @@ node_modules | |
|
||
# Users Environment Variables | ||
.lock-wscript | ||
|
||
# ESLint Config Files | ||
.eslintrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.