Add support for transformation functions without using proj4js, and make

the SphericalMercator mixin register two transformations to/from EPSG:900913,
EPSG:4326. Thanks to Tim for the feedback and review. (Closes #1210)

This allows us to transform points to/from SphericalMercator 
without proj4js support -- and if other projects need similar functionality, 
they can write their own custom transformation functions rather than 
modifying proj4js to support some custom projection.


git-svn-id: http://svn.openlayers.org/trunk/openlayers@5410 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2007-12-14 20:45:42 +00:00
parent 9dd5d0e6da
commit a170e3fdf6
6 changed files with 158 additions and 12 deletions
+54 -7
View File
@@ -6,7 +6,7 @@
* @requires OpenLayers/Util.js
*
* Class: OpenLayers.Projection
* Class for coordinate transformations between coordinate systems.
* Class for coordinate transforms between coordinate systems.
* Depends on the proj4js library. If proj4js is not available,
* then this is just an empty stub.
*/
@@ -101,22 +101,69 @@ OpenLayers.Projection = OpenLayers.Class({
CLASS_NAME: "OpenLayers.Projection"
});
/**
* Property: transforms
* Transforms is an object, with from properties, each of which may
* have a to property. This allows you to define projections without
* requiring support for proj4js to be included.
*
* This object has keys which correspond to a 'source' projection object. The
* keys should be strings, corresponding to the projection.getCode() value.
* Each source projection object should have a set of destination projection
* keys included in the object.
*
* Each value in the destination object should be a transformation function,
* where the function is expected to be passed an object with a .x and a .y
* property. The function should return the object, with the .x and .y
* transformed according to the transformation function.
*
* Note - Properties on this object should not be set directly. To add a
* transform method to this object, use the <addTransform> method. For an
* example of usage, see the OpenLayers.Layer.SphericalMercator file.
*/
OpenLayers.Projection.transforms = {};
/**
* APIMethod: addTransform
* Set a custom transform method between two projections. Use this method in
* cases where the proj4js lib is not available or where custom projections
* need to be handled.
*
* Parameters:
* from - {String} The code for the source projection
* to - {String} the code for the destination projection
* method - {Function} A function that takes a point as an argument and
* transforms that point from the source to the destination projection
* in place. The original point should be modified.
*/
OpenLayers.Projection.addTransform = function(from, to, method) {
if(!OpenLayers.Projection.transforms[from]) {
OpenLayers.Projection.transforms[from] = {};
}
OpenLayers.Projection.transforms[from][to] = method;
};
/**
* APIMethod: transform
* Read data from a string, and return an object whose type depends on the
* subclass.
* Transform a point coordinate from one projection to another. Note that
* the input point is transformed in place.
*
* Parameters:
* point - {object} input horizontal coodinate
* sourceProj - {OpenLayers.Projection} source map coordinate system
* destProj - {OpenLayers.Projection} destination map coordinate system
* point - {{OpenLayers.Geometry.Point> | Object} An object with x and y
* properties representing coordinates in those dimensions.
* sourceProj - {OpenLayers.Projection} Source map coordinate system
* destProj - {OpenLayers.Projection} Destination map coordinate system
*
* Returns:
* point - {object} trasnformed coordinate
* point - {object} A transformed coordinate. The original point is modified.
*/
OpenLayers.Projection.transform = function(point, source, dest) {
if (source.proj && dest.proj) {
point = Proj4js.transform(source.proj, dest.proj, point);
} else if (source && dest &&
OpenLayers.Projection.transforms[source.getCode()] &&
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) {
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point);
}
return point;
};