OpenLayers OpenLayers

Ticket #857: proj.patch

File proj.patch, 11.6 kB (added by tschaub, 1 year ago)

suggested structure for adding coordinate transformation functionality

  • lib/OpenLayers/Projection.js

    old new  
     1/** 
     2 * Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. 
     3 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt  
     4 * for the full text of the license. 
     5 */ 
     6 
     7 
     8/** 
     9 * Namespace: Projection 
     10 * Contains methods for transforming coordinates between spatial reference 
     11 *     systems.   
     12 */ 
     13OpenLayers.Projection = { 
     14     
     15    /** 
     16     * Property: alias 
     17     * {Object} Aliases can be added for a spatial reference system by adding a 
     18     *     name-value pair to the alias object.  This object represents the 
     19     *     many to one mapping between aliases and spatial references.  Note 
     20     *     that all SRS keys are assumed to be case insensitive.  Use all caps 
     21     *     for alias keys here. 
     22     */ 
     23    alias: { 
     24        "WGS84": "EPSG:4326" 
     25    }, 
     26     
     27    /** 
     28     * APIFunction: transform 
     29     * Transform coordinates of the passed in feature, geometry, array of 
     30     *     features, or array of geometries from on spatial reference system 
     31     *     to another. 
     32     * 
     33     * Parameters: 
     34     * obj - {<OpenLayers.Geometry> | <OpenLayers.Vector.Feature> | 
     35     *     Array(<OpenLayers.Geometry>) | Array(OpenLayers.Vector.Feature)} 
     36     *     A geometry, feature, array of geometries, or array of features 
     37     *     to be transformed. 
     38     * to - {String} A string representing the input spatial reference system. 
     39     * from - {String} A string representing the output spatial reference 
     40     *     system. 
     41     * 
     42     * Returns: 
     43     * {<OpenLayers.Geometry> | <OpenLayers.Vector.Feature> | 
     44     *     Array(<OpenLayers.Geometry>) | Array(OpenLayers.Vector.Feature)} 
     45     *     A clone of the input object with transformed coordinates. 
     46     */ 
     47    transform: function(obj, from, to) { 
     48        from = from.toUpperCase(); 
     49        if(this.alias[from]) { 
     50            from = this.alias[from].toUpperCase(); 
     51        } 
     52        to = to.toUpperCase(); 
     53        if(this.alias[to]) { 
     54            to = this.alias[to].toUpperCase(); 
     55        } 
     56        var clone; 
     57        if(obj instanceof Array) { 
     58            // deal with arrays of features or geometries 
     59            var item; 
     60            clone = new Array(); 
     61            for(var i=0; i<obj.length; ++i) { 
     62                item = obj[i]; 
     63                clone.push(OpenLayers.Projection.transform(item, from, to)); 
     64            } 
     65        } 
     66        if(obj.CLASS_NAME) { 
     67            if(obj.CLASS_NAME.indexOf("OpenLayers.Geometry") == 0) { 
     68                // transform a geometry 
     69                if(obj instanceof OpenLayers.Geometry.Point) { 
     70                    clone = obj.clone(); 
     71                    OpenLayers.Projection[from].inverse(clone); 
     72                    OpenLayers.Projection[to].forward(clone); 
     73                } else { 
     74                    var component; 
     75                    clone = eval("new " + obj.CLASS_NAME + "()"); 
     76                    for(var j=0; j<obj.components.length; ++j) { 
     77                        component = obj.components[j]; 
     78                        clone.addComponent( 
     79                            OpenLayers.Projection.transform(component, from, to) 
     80                        ); 
     81                    } 
     82                } 
     83            } else if(obj.CLASS_NAME.indexOf("OpenLayers.Feature.Vector") == 0) { 
     84                // transform a vector feature 
     85                clone = new OpenLayers.Feature.Vector(null, 
     86                                                      obj.attributes, 
     87                                                      obj.style); 
     88                clone.geometry = OpenLayers.Projection.transform(obj.geometry, 
     89                                                                 from, 
     90                                                                 to); 
     91            } 
     92        } 
     93        return clone; 
     94    }, 
     95     
     96    /** 
     97     * Various constants used by the forward and inverse transoforms. 
     98     *     To be organized. 
     99     */ 
     100    rad2deg: 180 / Math.PI, 
     101    deg2rad:  Math.PI / 180, 
     102    halfPi: Math.PI / 2, 
     103    rMajor: 6378137.0, 
     104    rMinor: 6356752.31424518, 
     105    eccent: Math.sqrt(1.0 - Math.pow(6356752.31424518 / 6378137.0, 2)) 
     106     
     107}; 
     108 
  • lib/OpenLayers/Projection/EPSG54004.js

    old new  
     1/** 
     2 * Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. 
     3 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt  
     4 * for the full text of the license. 
     5 */ 
     6 
     7/** 
     8 * Namespace: OpenLayers.Projection["EPSG:54004"] 
     9 * Contains functions to transform points to and from the spatial reference 
     10 *     system represented by the EPSG code 54004. 
     11 * 
     12 * Proj4 text: 
     13 *     +proj=merc +lon_0=0 +k=1.000000 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 
     14 *     +units=m +no_defs 
     15 */ 
     16OpenLayers.Projection["EPSG:54004"] = { 
     17     
     18    /** 
     19     * APIFunction: forward 
     20     * Transform a point from Geographic/WGS84 to the spatial reference system 
     21     *     represented here (EPSG:54004).  The x & y properties of the passed 
     22     *     in point are modified. 
     23     * 
     24     * Parameters: 
     25     * point - {<OpenLayers.Geometry.Point>} The point to be transformed.  This 
     26     *     point should have coordinates in Geographic/WGS84.  The passed in 
     27     *     point will be modified. 
     28     * 
     29     * 
     30     * Returns: 
     31     * {<OpenLayers.Geometry.Point>} The passed in point with transformed 
     32     *     coordinates. 
     33     */ 
     34    forward: function(point) { 
     35        var x = point.x; 
     36        var y = point.y; 
     37         
     38        // convert to radians 
     39        if(y <= 90.0 && y >= -90.0 && x <= 180.0 && x >= -180.0) { 
     40            y *= OpenLayers.Projection.deg2rad; 
     41            x *= OpenLayers.Projection.deg2rad; 
     42            // force latitude away from the poles 
     43            y = Math.min(OpenLayers.Projection.halfPi - 1.0e-9, y); 
     44            y = Math.max(-OpenLayers.Projection.halfPi + 1.0e-9, y); 
     45        } else { 
     46            // OpenLayers.Console.error("nice error message"); 
     47            return null; 
     48        } 
     49 
     50        // force latitude away from the poles 
     51        y = Math.min(OpenLayers.Projection.halfPi - 1.0e-9, y); 
     52        y = Math.max(-OpenLayers.Projection.halfPi + 1.0e-9, y); 
     53 
     54        var con = OpenLayers.Projection.eccent * Math.sin(y); 
     55        var com = .5 * OpenLayers.Projection.eccent; 
     56        con = Math.pow(((1.0 - con) / (1.0 + con)), com); 
     57        var ts = (Math.tan(.5 * (OpenLayers.Projection.halfPi - y))/con); 
     58        var x = OpenLayers.Projection.rMajor * x; 
     59        var y = -OpenLayers.Projection.rMajor * Math.log(ts); 
     60         
     61        // modify the point 
     62        point.x = x; 
     63        point.y = y; 
     64        return point; 
     65    }, 
     66 
     67    /** 
     68     * APIFunction: inverse 
     69     * Transform a point from the spatial reference system represented here 
     70     *     (EPSG:54004) to Geographic/WGS84 .  The x & y properties of the 
     71     *     passed in point are modified. 
     72     * 
     73     * Parameters: 
     74     * point - {<OpenLayers.Geometry.Point>} The point to be transformed.  This 
     75     *     point should have coordinates in EPSG:54004.  The passed in 
     76     *     point will be modified. 
     77     * 
     78     * 
     79     * Returns: 
     80     * {<OpenLayers.Geometry.Point>} The passed in point with transformed 
     81     *     coordinates. 
     82     */ 
     83    inverse: function(point) { 
     84 
     85        // calculate latitude 
     86        var temp = OpenLayers.Projection.rMinor / OpenLayers.Projection.rMajor; 
     87        var ts = Math.exp(-point.y / OpenLayers.Projection.rMajor); 
     88 
     89        // phi2z: compute the latitude angle, phi2, for the inverse 
     90        // of the Lambert Conformal Conic and Polar Stereographic projections. 
     91        var y = null; 
     92        var eccnth = .5 * OpenLayers.Projection.eccent; 
     93        var con, dphi; 
     94        var phi = OpenLayers.Projection.halfPi - 2 * Math.atan(ts); 
     95        for(var i=0; i<=15; i++) { 
     96            con = OpenLayers.Projection.eccent * Math.sin(phi); 
     97            dphi = OpenLayers.Projection.halfPi - 2 * Math.atan(ts * 
     98                        (Math.pow(((1.0 - con)/(1.0 + con)),eccnth))) - phi; 
     99            phi += dphi; 
     100            if (Math.abs(dphi) <= .0000000001) { 
     101                y = phi; 
     102                break; 
     103            } 
     104        } 
     105        if(y == null) { 
     106            throw "Convergence error - phi2z"; 
     107        } 
     108 
     109        // calculate longitude 
     110        var x = point.x / OpenLayers.Projection.rMajor; 
     111        if(Math.abs(x) >= Math.PI) { 
     112            var sign = (x < 0) ? -1 : 1; 
     113            x = x - (sign * 2 * Math.PI); 
     114        } 
     115 
     116        // convert to degrees 
     117        x *= OpenLayers.Projection.rad2deg; 
     118        y *= OpenLayers.Projection.rad2deg; 
     119         
     120        // modify the point 
     121        point.x = x; 
     122        point.y = y; 
     123        return point; 
     124    } 
     125}; 
  • lib/OpenLayers/Projection/EPSG4326.js

    old new  
     1/** 
     2 * Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license. 
     3 * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt  
     4 * for the full text of the license. 
     5 */ 
     6 
     7/** 
     8 * Namespace: OpenLayers.Projection["EPSG:4326"] 
     9 * Contains functions to transform points to and from the spatial reference 
     10 *     system represented by the EPSG code 4326. 
     11 * 
     12 * Proj4 text: 
     13 *    +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs 
     14 */ 
     15OpenLayers.Projection["EPSG:4326"] = { 
     16     
     17    /** 
     18     * APIFunction: forward 
     19     * Transform a point from Geographic/WGS84 to the spatial reference system 
     20     *     represented here (EPSG:4326).  The x & y properties of the passed 
     21     *     in point are modified. 
     22     * 
     23     * Parameters: 
     24     * point - {<OpenLayers.Geometry.Point>} The point to be transformed.  This 
     25     *     point should have coordinates in Geographic/WGS84.  The passed in 
     26     *     point will be modified. 
     27     * 
     28     * 
     29     * Returns: 
     30     * {<OpenLayers.Geometry.Point>} The passed in point with transformed 
     31     *     coordinates. 
     32     */ 
     33    forward: function(point) { 
     34        return point; 
     35    }, 
     36 
     37    /** 
     38     * APIFunction: inverse 
     39     * Transform a point from the spatial reference system represented here 
     40     *     (EPSG:4326) to Geographic/WGS84 .  The x & y properties of the 
     41     *     passed in point are modified. 
     42     * 
     43     * Parameters: 
     44     * point - {<OpenLayers.Geometry.Point>} The point to be transformed.  This 
     45     *     point should have coordinates in EPSG:4326.  The passed in 
     46     *     point will be modified. 
     47     * 
     48     * 
     49     * Returns: 
     50     * {<OpenLayers.Geometry.Point>} The passed in point with transformed 
     51     *     coordinates. 
     52     */ 
     53    inverse: function(point) { 
     54        return point; 
     55    } 
     56}; 
  • lib/OpenLayers.js

    old new  
    167167            "OpenLayers/Layer/WFS.js", 
    168168            "OpenLayers/Control/MouseToolbar.js", 
    169169            "OpenLayers/Control/NavToolbar.js", 
    170             "OpenLayers/Control/EditingToolbar.js" 
     170            "OpenLayers/Control/EditingToolbar.js", 
     171            "OpenLayers/Projection.js", 
     172            "OpenLayers/Projection/EPSG4326.js", 
     173            "OpenLayers/Projection/EPSG54004.js"             
    171174        ); // etc. 
    172175 
    173176