Commit 835d5009 authored by Andreas Hocevar's avatar Andreas Hocevar
Browse files

Merge pull request #111 from proj4js/smart-defs

Use proj4.defs through the function, not the object
parents 74aeff0d 446f1a4c
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@ proj4(firstProjection).inverse([242075.00535055372, 750123.32090043]);

## Named Projections

If you prefer to define a projection as a string and refence it that way, you may use the proj4.defs method which can be called 2 ways, with a name and projection:
If you prefer to define a projection as a string and reference it that way, you may use the proj4.defs method which can be called 2 ways, with a name and projection:

```js
proj4.defs('WGS84', "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees");
@@ -95,8 +95,8 @@ proj4('EPSG:4326');

instead of writing out the whole proj definition, by default proj4 has the following projections predefined:

- 'EPSG:4326', which has the following alias
    - 'WGS84'
- 'EPSG:4326'
- 'EPSG:4269'
- 'EPSG:3857', which has the following aliases
    - 'EPSG:3785'
@@ -104,7 +104,13 @@ instead of writing out the whole proj definition, by default proj4 has the follo
    - 'EPSG:900913'
    - 'EPSG:102113'

defined projections can also be accessed as properties of the proj4.defs function (`proj4.defs['EPSG:4326']`).
defined projections can also be accessed through the proj4.defs function (`proj4.defs('EPSG:4326')`).

proj4.defs can also be used to define a named alias:

```javascript
proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
``` 

##Developing
to set up build tools make sure you have node and grunt-cli installed and then run `npm install`
+13 −6
Original line number Diff line number Diff line
@@ -6,12 +6,17 @@ function defs(name) {
  /*global console*/
  var that = this;
  if (arguments.length === 2) {
    if (arguments[1][0] === '+') {
    var def = arguments[1];
    if (typeof def === 'string') {
      if (def[0] === '+') {
        defs[name] = parseProj(arguments[1]);
      }
      else {
        defs[name] = wkt(arguments[1]);
      }
    } else {
      defs[name] = def;
    }
  }
  else if (arguments.length === 1) {
    if (Array.isArray(name)) {
@@ -25,7 +30,9 @@ function defs(name) {
      });
    }
    else if (typeof name === 'string') {

      if (name in defs) {
        return defs[name];
      }
    }
    else if ('EPSG' in name) {
      defs['EPSG:' + name.EPSG] = name;
+1 −1
Original line number Diff line number Diff line
module.exports = function(defs) {
  defs('WGS84', "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees");
  defs('EPSG:4326', "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees");
  defs('EPSG:4269', "+title=NAD83 (long/lat) +proj=longlat +a=6378137.0 +b=6356752.31414036 +ellps=GRS80 +datum=NAD83 +units=degrees");
  defs('EPSG:3857', "+title=WGS 84 / Pseudo-Mercator +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs");

  defs.WGS84 = defs['EPSG:4326'];
  defs['EPSG:3785'] = defs['EPSG:3857']; // maintain backward compat, official code is 3857
  defs.GOOGLE = defs['EPSG:3857'];
  defs['EPSG:900913'] = defs['EPSG:3857'];
+9 −2
Original line number Diff line number Diff line
@@ -159,13 +159,20 @@ function startTests(chai, proj4, testPoints) {
        });
      });
    });
    describe('defs', function() {
      assert.equal(proj4.defs('testmerc'), proj4.defs['testmerc']);
      proj4.defs('foo', '+proj=merc +lon_0=5.937 +lat_ts=45.027 +ellps=sphere +datum=none');
      assert.typeOf(proj4.defs['foo'], 'object');
      proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
      assert.strictEqual(proj4.defs['urn:x-ogc:def:crs:EPSG:4326'], proj4.defs['EPSG:4326']);
    });
    describe('errors', function() {
      it('should throw an error for an unknown ref', function() {
        assert.throws(function() {
          new proj4.Proj('fake one');
        }, 'fake one', 'should work');
      });
    })
    });
    describe('utility', function() {
      it('should have MGRS available in the proj4.util namespace', function() {
        assert.typeOf(proj4.mgrs, "object", "MGRS available in the proj4.util namespace");
@@ -205,7 +212,7 @@ function startTests(chai, proj4, testPoints) {
        it("MGRS reference with 3-digit accuracy correct.", function() {
          assert.equal(point.toMGRS(3), "25XEN041865", "MGRS reference with 3-digit accuracy correct.");
        });
      })
      });
    });
  });
}