Source: api/ProjectionManager.js

Retour à la documentation

define(["dojo/_base/declare", "dojo/dom-construct", "dojo/_base/array", "dojo/_base/window",
        "esri/geometry/jsonUtils", "dojo/_base/lang", "esri/SpatialReference", "spw/libs/proj4js/proj4js"],
        function (declare, domConstruct, array, win, jsonUtils, lang, SpatialReference, Proj4js) {
    var ProjectionManager = null;

    if (Array.isArray == null) { // IE 8
        Array.isArray = function(arr) {
            return (arr instanceof Array);
        }
    }

    ProjectionManager = declare("spw.api.ProjectionManager", null, /** @lends spw.api.ProjectionManager.prototype */{

        /**
         * Projection correspondant au WGS84
         */
        wgs84: "WGS84",

        /**
         * Projection correspondant au Lambert72
         */
        lambert: '+proj=lcc +lat_1=51.16666723333333 +lat_2=49.8333339 +lat_0=90 +lon_0=4.367486666666666 +x_0=150000.013 +y_0=5400088.438 +ellps=intl +towgs84=-106.869,52.2978,-103.724,0.3366,-0.457,1.8422,-1.2747 +units=m +no_defs',

        /**
         * Le constructeur ne doit pas être appelé directement. ProjectionManager est une classe statique et doit être accédée via la méthode {@link spw.api.ProjectionManager.getInstance getInstance}.
         * @classdesc Classe utilitaire permettant d'effectuer des transformations de coordonnées dans différents systèmes de projection.
         * @constructs
         */
        constructor: function() {
            this.inherited(arguments);
        },

        /** Projette des coordonnées lambert en coordonnées GPS (WGS84).
        * @public
        * @param x coordonnée x en lambert
        * @param y coordonnée y en lambert
        * @return objProjectedXY objet contenant les coordonnées x et y projetées
        */
        projectLambertToGoogle: function(x, y) {
            return Proj4js(this.lambert,this.wgs84,{x:x,y:y});
        },

        /** Projette des coordonnées GPS (WGS84) en coordonnées lambert.
        * @public
        * @param x coordonnée x en lambert
        * @param y coordonnée y en lambert
        * @return objProjectedXY objet contenant les coordonnées x et y projetées
        */
        projectGoogleToLambert: function(lat, lon) {
            return Proj4js(this.wgs84,this.lambert,{x:lat,y:lon});
        },

        /** Projette des coordonnées Lambert en WGS84.
         * @public
         * @param x coordonnée x en lambert
         * @param y coordonnée y en lambert
         * @return objProjectedXY objet contenant les coordonnées x et y projetées
         */
        projectLambertToWgs84: function(x, y) {
            var coord = Proj4js(this.lambert, this.wgs84, [x,y]);
            return {x: coord[0], y: coord[1]};
        },

        /** Projette des coordonnées GPS (WGS84) en coordonnées lambert.
         * @public
         * @param x coordonnée x en lambert
         * @param y coordonnée y en lambert
         * @return objProjectedXY objet contenant les coordonnées x et y projetées
         */
        projectWgs84ToLambert: function(lat, lon) {
            var coord = Proj4js(this.wgs84, this.lambert, [lat,lon]);
            return {x: coord[0], y: coord[1]};
        },

        transform: function(){
            if(arguments && arguments.length === 3)
                return this._transformGeometry.apply(this, arguments);
            else if(arguments && arguments.length === 4)
                return this._transformPoint.apply(this, arguments);
        },

        _transformPoint: function(source, dest, x, y) {
            return this.projectPoint(source, dest, x, y);
        },

        _transformGeometry: function(source, dest, geometry){
            if(!geometry || source == dest) return geometry;
            if(geometry.declaredClass == "esri.geometry.Point"){
                var oGeom = geometry.toJson();
                var nCoord = this._transformPoint(source, dest, oGeom.x, oGeom.y);
                lang.mixin(oGeom, nCoord, {
                    spatialReference: new SpatialReference({wkid: dest})
                });
                return jsonUtils.fromJson(oGeom);
            } else if(geometry.declaredClass == "esri.geometry.Multipoint"){
                var nGeom = jsonUtils.fromJson(geometry.toJson());
                array.forEach(nGeom.points, lang.hitch(this, function(p, i){
                    p = nGeom.getPoint(i);
                    lang.mixin(p, this._transformPoint(source, dest, p.x, p.y), {
                        spatialReference: new SpatialReference({wkid: dest})
                    });

                    nGeom.setSpatialReference(new SpatialReference({wkid: dest}));
                    nGeom.setPoint(i, p);
                }));
                return nGeom;
            } else if(geometry.declaredClass == "esri.geometry.Polyline"){
                var nGeom = jsonUtils.fromJson(geometry.toJson());
                array.forEach(nGeom.paths, lang.hitch(this, function(path, idx){
                    array.forEach(path, lang.hitch(this, function(p, i){
                        p = nGeom.getPoint(idx, i);
                        lang.mixin(p, this._transformPoint(source, dest, p.x, p.y), {
                            spatialReference: new SpatialReference({wkid: dest})
                        });

                        nGeom.setSpatialReference(new SpatialReference({wkid: dest}));
                        nGeom.setPoint(idx, i, p);
                    }));
                }));
                return nGeom;
            } else if(geometry.declaredClass == "esri.geometry.Polygon"){
                var nGeom = jsonUtils.fromJson(geometry.toJson());
                array.forEach(nGeom.rings, lang.hitch(this, function(ring, idx){
                    array.forEach(ring, lang.hitch(this, function(p, i){
                        p = nGeom.getPoint(idx, i);
                        lang.mixin(p, this._transformPoint(source, dest, p.x, p.y));

                        nGeom.setSpatialReference(new SpatialReference({wkid: dest}));
                        nGeom.setPoint(idx, i, p);
                    }));
                }));
                return nGeom;
            } else if (geometry.declaredClass == "esri.geometry.Extent") {
                var nGeom = jsonUtils.fromJson(geometry.toJson());
                var props = [['xmin', 'ymin'], ['xmax', 'ymax']];

                props.forEach(lang.hitch(this, function(pt) {
                    var newPt = this._transformPoint(source, dest, nGeom[pt[0]], nGeom[pt[1]]);

                    nGeom[pt[0]] = newPt.x;
                    nGeom[pt[1]] = newPt.y;
                }));

                nGeom.setSpatialReference(new SpatialReference({wkid: dest}));
                return nGeom;
            }
            return geometry;
        },

        /** Projette des coordonnées d'une référence spatiale vers une autre.
         * @public
         * @param source projection dans laquelle sont les points x et y
         * @param destination projection vers laquelle projeter les points x et y
         * @param x coordonnée x
         * @param y coordonnée y
         * @return objProjectedXY objet contenant les coordonnées x et y projetées
         */
        projectPoint: function(source,destination,x, y) {
            var projSource = this._getDefinitionIfSRID(source);
            var projDestiation = this._getDefinitionIfSRID(destination);

            if(projSource !== null && projDestiation !== null){
                return Proj4js(projSource,projDestiation,{x:x,y:y});
            }
            else{
                return null;
            }
        },

        _getDefinitionIfSRID:function(proj){
            if(!isNaN(proj)){
                if(typeof(ProjectionManager.definitions[proj]) === "undefined"){
                    return null;
                }
                return ProjectionManager.definitions[proj];
            }
            else{
                return proj;
            }
        }
    });

    var _INSTANCE = null;
    /**
     * Permet de récupérer l'instance du ProjectionManager
     * @method getInstance
     * @memberof spw.api.ProjectionManager
     * @returns spw.api.ProjectionManager
     */
    ProjectionManager.getInstance = function(){
        if(_INSTANCE == null){
            _INSTANCE = new ProjectionManager();
        }
        return _INSTANCE;
    };

    ProjectionManager.definitions={
        31370:"+proj=lcc +lat_1=51.16666723333333 +lat_2=49.8333339 +lat_0=90 +lon_0=4.367486666666666 +x_0=150000.01256 +y_0=5400088.4378 +ellps=intl +towgs84=-106.869,52.2978,-103.724,0.3366,-0.457,1.8422,-1.2747 +units=m +no_defs",
        4326:"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs",
        3812:"+proj=lcc +lat_1=49.83333333333334 +lat_2=51.16666666666666 +lat_0=50.797815 +lon_0=4.359215833333333 +x_0=649328 +y_0=665262 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs",
        102199:"+proj=lcc +lat_1=49.83333333333334 +lat_2=51.16666666666666 +lat_0=50.797815 +lon_0=4.359215833333333 +x_0=649328 +y_0=665262 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs",
        3857: "+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 +wktext +no_defs",
        900913: "+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 +wktext +no_defs",
        102100: "+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 +wktext +no_defs",
        3034:"+proj=lcc +lat_1=35 +lat_2=65 +lat_0=52 +lon_0=10 +x_0=4000000 +y_0=2800000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
    };

    return ProjectionManager;

});