OpenLayers OpenLayers

Ticket #1210: 1210.2.patch

File 1210.2.patch, 10.8 kB (added by tschaub, 1 year ago)

reworked a bit with tests

  • apidoc_config/Menu.txt

    old new  
    168168      File: Box  (no auto-title, OpenLayers/Marker/Box.js) 
    169169      }  # Group: Marker 
    170170 
     171   File: Projection  (no auto-title, OpenLayers/Projection.js) 
     172 
    171173   Group: Popup  { 
    172174 
    173175      File: Popup  (OpenLayers/Popup.js) 
  • doc_config/Menu.txt

    old new  
    168168      File: Box  (no auto-title, OpenLayers/Marker/Box.js) 
    169169      }  # Group: Marker 
    170170 
     171   File: Projection  (no auto-title, OpenLayers/Projection.js) 
     172 
    171173   Group: Popup  { 
    172174 
    173175      File: Popup  (OpenLayers/Popup.js) 
  • tests/Layer/test_SphericalMercator.html

    old new  
    11<html> 
    22<head> 
    3     <!-- this gmaps key generated for http://openlayers.org/dev/ --> 
    43    <script src="../../lib/OpenLayers.js"></script> 
    54    <script type="text/javascript"> 
    6     function test_SphericalMercator_forwardProject(t) { 
     5    function test_SphericalMercator_forwardMercator(t) { 
    76        t.plan(12); 
    87        var arctic = OpenLayers.Layer.SphericalMercator.forwardMercator(0, 85); 
    98        var antarctic = OpenLayers.Layer.SphericalMercator.forwardMercator(0, -85); 
     
    3231        t.eq(sw.lon, -20037508.34, "SW lon is correct"); 
    3332    }  
    3433     
    35     function test_sphericalMercator_inverseProject(t) { 
     34    function test_sphericalMercator_inverseMercator(t) { 
    3635        t.plan(4); 
    3736        var sw =  OpenLayers.Layer.SphericalMercator.inverseMercator(-20037508.34,  -20037508.34); 
    3837        var ne =  OpenLayers.Layer.SphericalMercator.inverseMercator(20037508.34,  20037508.34); 
     
    4241        t.eq(sw.lat, -85.05112877980659, "Southwest lat correct"); 
    4342        t.eq(ne.lat, 85.05112877980660, "Northeast lat correct"); 
    4443    } 
     44 
     45    function strToFixed(str, dig) {  
     46        if(dig == undefined) {  
     47            dig = 5;  
     48        }  
     49        return str.replace(/(\d+\.\d+)/g, function(match) {  
     50            return parseFloat(match).toFixed(dig);  
     51        });  
     52    }  
     53  
     54    function test_SphericalMercator_projectForward(t) {  
     55        t.plan(1); 
     56        var point = new OpenLayers.Geometry.Point(10, 20);  
     57        OpenLayers.Layer.SphericalMercator.projectForward(point);  
     58 
     59        t.eq(strToFixed(point.toString()),  
     60             strToFixed("POINT(1113194.9077777779 2273030.9266712805)"),  
     61             "point transforms from EPSG:4326 to Spherical Mercator");  
     62    }  
     63          
     64    function test_SphericalMercator_to4326(t) {  
     65        t.plan(1); 
     66        var point = new OpenLayers.Geometry.Point(1113195, 2273031);  
     67 
     68        OpenLayers.Layer.SphericalMercator.projectInverse(point);  
     69          
     70        t.eq(strToFixed(point.toString()),  
     71             strToFixed("POINT(10.000000828446318 20.000000618997227)"),  
     72             "point transforms from EPSG:4326 to Spherical Mercator");  
     73    } 
    4574     
     75    function test_SphericalMercator_addTransform(t) { 
     76        // this class should add two methods to the 
     77        // OpenLayers.Projection.transforms object 
     78        t.plan(4); 
     79        var wgs84 = OpenLayers.Projection.transforms["EPSG:4326"]; 
     80        t.ok(wgs84 instanceof Object, "EPSG:4326 exists in table"); 
     81         
     82        var smerc = OpenLayers.Projection.transforms["EPSG:900913"]; 
     83        t.ok(smerc instanceof Object, "EPSG:900913 exists in table"); 
     84         
     85        t.ok(wgs84["EPSG:900913"] === OpenLayers.Layer.SphericalMercator.projectForward, 
     86             "from EPSG:4326 to EPSG:900913 correctly defined"); 
     87        t.ok(smerc["EPSG:4326"] === OpenLayers.Layer.SphericalMercator.projectInverse, 
     88             "from EPSG:900913 to EPSG:4326 correctly defined"); 
     89    } 
     90     
    4691  </script>  
    4792</head> 
    4893<body> 
  • lib/OpenLayers/Projection.js

    old new  
    66 * @requires OpenLayers/Util.js 
    77 *  
    88 * Class: OpenLayers.Projection 
    9  * Class for coordinate transformations between coordinate systems. 
     9 * Class for coordinate transforms between coordinate systems. 
    1010 *     Depends on the proj4js library. If proj4js is not available,  
    1111 *     then this is just an empty stub. 
    1212 */ 
     
    102102});      
    103103 
    104104/** 
     105 * Property: transforms 
     106 * Transforms is an object, with from properties, each of which may 
     107 * have a to property. This allows you to define projections without  
     108 * requiring support for proj4js to be included. 
     109 * 
     110 * This object has keys which correspond to a 'source' projection object.  The 
     111 * keys should be strings, corresponding to the projection.getCode() value. 
     112 * Each source projection object should have a set of destination projection 
     113 * keys included in the object.  
     114 *  
     115 * Each value in the destination object should be a transformation function, 
     116 * where the function is expected to be passed an object with a .x and a .y 
     117 * property.  The function should return the object, with the .x and .y 
     118 * transformed according to the transformation function. 
     119 * 
     120 * Note - Properties on this object should not be set directly.  To add a 
     121 *     transform method to this object, use the <addTransform> method.  For an 
     122 *     example of usage, see the OpenLayers.Layer.SphericalMercator file. 
     123 */ 
     124OpenLayers.Projection.transforms = {}; 
     125 
     126/** 
     127 * APIMethod: addTransform 
     128 * Set a custom transform method between two projections.  Use this method in 
     129 *     cases where the proj4js lib is not available or where custom projections 
     130 *     need to be handled. 
     131 * 
     132 * Parameters: 
     133 * from - {String} The code for the source projection 
     134 * to - {String} the code for the destination projection 
     135 * method - {Function} A function that takes a point as an argument and 
     136 *     transforms that point from the source to the destination projection 
     137 *     in place.  The original point should be modified. 
     138 */ 
     139OpenLayers.Projection.addTransform = function(from, to, method) { 
     140    if(!OpenLayers.Projection.transforms[from]) { 
     141        OpenLayers.Projection.transforms[from] = {}; 
     142    } 
     143    OpenLayers.Projection.transforms[from][to] = method; 
     144}; 
     145 
     146/** 
    105147 * APIMethod: transform 
    106  * Read data from a string, and return an object whose type depends on the 
    107  * subclass.  
     148 * Transform a point coordinate from one projection to another.  Note that 
     149 *     the input point is transformed in place. 
    108150 *  
    109151 * Parameters: 
    110  * point - {object} input horizontal coodinate 
    111  * sourceProj - {OpenLayers.Projection} source map coordinate system 
    112  * destProj - {OpenLayers.Projection} destination map coordinate system 
     152 * point - {{OpenLayers.Geometry.Point> | Object} An object with x and y 
     153 *     properties representing coordinates in those dimensions. 
     154 * sourceProj - {OpenLayers.Projection} Source map coordinate system 
     155 * destProj - {OpenLayers.Projection} Destination map coordinate system 
    113156 * 
    114157 * Returns: 
    115  * point - {object} trasnformed coordinate 
     158 * point - {object} A transformed coordinate.  The original point is modified. 
    116159 */ 
    117160OpenLayers.Projection.transform = function(point, source, dest) { 
    118161    if (source.proj && dest.proj) { 
    119162        point = Proj4js.transform(source.proj, dest.proj, point); 
     163    } else if (source && dest &&  
     164               OpenLayers.Projection.transforms[source.getCode()] &&  
     165               OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) { 
     166        OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point);  
    120167    } 
    121168    return point; 
    122169}; 
  • lib/OpenLayers/Layer/SphericalMercator.js

    old new  
    11/** @requires OpenLayers/Layer.js 
     2 * @requires OpenLayers/Projection.js 
    23 * 
    34 * Class: OpenLayers.Layer.SphericalMercator 
    45 * A mixin for layers that wraps up the pieces neccesary to have a coordinate 
     
    101102        lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2); 
    102103         
    103104        return new OpenLayers.LonLat(lon, lat); 
     105    }, 
     106 
     107    /** 
     108     * Method: projectForward  
     109     * Given an object with x and y properties in EPSG:4326, modify the x,y 
     110     * properties on the object to be the Spherical Mercator projected 
     111     * coordinates. 
     112     * 
     113     * Parameters: 
     114     * point - {Object} An object with x and y properties.  
     115     *  
     116     * Returns: 
     117     * {Object} The point, with the x and y properties transformed to spherical 
     118     * mercator. 
     119     */ 
     120    projectForward: function(point) { 
     121        var lonlat = OpenLayers.Layer.SphericalMercator.forwardMercator(point.x, point.y); 
     122        point.x = lonlat.lon; 
     123        point.y = lonlat.lat; 
     124        return point; 
     125    }, 
     126     
     127    /** 
     128     * Method: projectForward  
     129     * Given an object with x and y properties in Spherical Mercator, modify 
     130     * the x,y properties on the object to be the unprojected coordinates. 
     131     * 
     132     * Parameters: 
     133     * point - {Object} An object with x and y properties.  
     134     *  
     135     * Returns: 
     136     * {Object} The point, with the x and y properties transformed from 
     137     * spherical mercator to unprojected coordinates.. 
     138     */ 
     139    projectInverse: function(point) { 
     140        var lonlat = OpenLayers.Layer.SphericalMercator.inverseMercator(point.x, point.y); 
     141        point.x = lonlat.lon; 
     142        point.y = lonlat.lat; 
     143        return point; 
    104144    } 
    105145 
    106146}; 
     147 
     148/** 
     149 * Note: Two transforms declared 
     150 * Transforms from EPSG:4326 to EPSG:900913 and from EPSG:900913 to EPSG:4326 
     151 *     are set by this class. 
     152 */ 
     153OpenLayers.Projection.addTransform("EPSG:4326", "EPSG:900913", 
     154    OpenLayers.Layer.SphericalMercator.projectForward); 
     155OpenLayers.Projection.addTransform("EPSG:900913", "EPSG:4326", 
     156    OpenLayers.Layer.SphericalMercator.projectInverse); 
  • lib/OpenLayers.js

    old new  
    7878            "Rico/Color.js", 
    7979            "OpenLayers/Ajax.js", 
    8080            "OpenLayers/Events.js", 
     81            "OpenLayers/Projection.js", 
    8182            "OpenLayers/Map.js", 
    8283            "OpenLayers/Layer.js", 
    8384            "OpenLayers/Icon.js", 
     
    175176            "OpenLayers/Layer/WFS.js", 
    176177            "OpenLayers/Control/MouseToolbar.js", 
    177178            "OpenLayers/Control/NavToolbar.js", 
    178             "OpenLayers/Control/EditingToolbar.js", 
    179             "OpenLayers/Projection.js" 
     179            "OpenLayers/Control/EditingToolbar.js" 
    180180        ); // etc. 
    181181 
    182182