osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/SpatialReference

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003  * Copyright 2008-2010 Pelican Mapping
00004  * http://osgearth.org
00005  *
00006  * osgEarth is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 #ifndef OSGEARTH_SPATIAL_REFERENCE_H
00020 #define OSGEARTH_SPATIAL_REFERENCE_H 1
00021 
00022 #include <osgEarth/Common>
00023 #include <osgEarth/Units>
00024 #include <osg/Referenced>
00025 #include <osg/CoordinateSystemNode>
00026 #include <osg/Vec3>
00027 #include <OpenThreads/ReentrantMutex>
00028 
00029 namespace osgEarth
00030 {
00031     //Definitions for the mercator extent
00032     const double MERC_MINX = -2.00375e+007;
00033     const double MERC_MINY = -2.00375e+007;
00034     const double MERC_MAXX = 2.00375e+007;
00035     const double MERC_MAXY = 2.00375e+007;
00036     const double MERC_WIDTH = MERC_MAXX - MERC_MINX;
00037     const double MERC_HEIGHT = MERC_MAXY - MERC_MINY;
00038 
00039 
00040     class OSGEARTH_EXPORT GeoLocator;
00041 
00046     class OSGEARTH_EXPORT SpatialReference : public osg::Referenced
00047     {
00048     public:
00054         static SpatialReference* create( const std::string& init );
00055 
00060         static SpatialReference* create( osg::CoordinateSystemNode* csn );
00061 
00070         static SpatialReference* createFromHandle( void* ogrHandle, bool xferOwnership =false );
00071 
00072     public:
00076         virtual bool transform(
00077             double x, double y, double z,
00078             const SpatialReference* to_srs,
00079             double& out_x, double& out_y, double& out_z,
00080             void* context =0L ) const;
00081 
00082         bool transform(
00083             const osg::Vec3d&       input,
00084             const SpatialReference* to_srs,
00085             osg::Vec3d&             output,
00086             void*                   context =0L) const
00087         {
00088             return transform(input.x(), input.y(), input.z(), to_srs, output.x(), output.y(), output.z(), context);
00089         }
00090 
00091         bool transform2D(
00092             double x, double y,
00093             const SpatialReference* to_srs,
00094             double& out_x, double& out_y,
00095             void* context =0L ) const
00096         {
00097             double dummyZ;
00098             return transform(x, y, dummyZ, to_srs, out_x, out_y, dummyZ, context);
00099         }
00100 
00104         virtual bool transformPoints(
00105             const SpatialReference* to_srs, 
00106             double* x, double* y, double* z,
00107             unsigned int numPoints,
00108             void* context =0L,
00109             bool ignore_errors =false) const;
00110 
00114         virtual bool transformPoints(
00115             const SpatialReference*  to_srs,
00116             std::vector<osg::Vec3d>& points,
00117             void* context =0L,
00118             bool ignore_errors =false) const;
00119 
00123         bool transformToECEF(
00124             const osg::Vec3d& input,
00125             osg::Vec3d& output ) const;
00126 
00131         bool transformToECEF(
00132             std::vector<osg::Vec3d>& points,
00133             bool                     ignore_errors =false) const;
00134 
00139         bool transformFromECEF(
00140             const osg::Vec3d& input,
00141             osg::Vec3d& output ) const;
00142 
00147         bool transformFromECEF(
00148             std::vector<osg::Vec3d>& points,
00149             bool                     ignoreErrors =false) const;
00150 
00158         virtual bool transformExtent(
00159             const SpatialReference* to_srs,
00160             double& in_out_xmin, double& in_out_ymin,
00161             double& in_out_xmax, double& in_out_ymax,
00162             void* context =0L ) const;
00163 
00164         virtual bool transformExtentPoints(
00165             const SpatialReference* to_srs,
00166             double in_xmin, double in_ymin,
00167             double in_xmax, double in_ymax,
00168             double* x, double* y,
00169             unsigned int numx, unsigned int numy,
00170             void* context = 0L, bool ignore_errors = false ) const;
00171         
00173         virtual bool isGeographic() const;
00174 
00176         virtual bool isProjected() const;
00177 
00179         bool isMercator() const;
00180 
00182         bool isNorthPolar() const;
00183         bool isSouthPolar() const;
00184 
00187         virtual bool isUserDefined() const;
00188 
00192         virtual bool isContiguous() const;
00193 
00195         virtual bool isCube() const;
00196 
00198         virtual bool isLTP() const { return _is_ltp; }
00199 
00201         const std::string& getName() const;
00202 
00204         const osg::EllipsoidModel* getEllipsoid() const;
00205 
00207         const std::string& getWKT() const;
00208 
00210         const std::string& getInitType() const;
00211 
00213         const std::string& getInitString() const;
00214 
00216         const std::string& getDatumName() const;
00217 
00219         virtual bool isEquivalentTo( const SpatialReference* rhs ) const;
00220 
00222         const SpatialReference* getGeographicSRS() const;
00223 
00227         SpatialReference* createTangentPlaneSRS( const osg::Vec3d& refPos ) const;
00228 
00230         SpatialReference* createTransMercFromLongitude( const Angular& lon ) const;
00231 
00235         SpatialReference* createUTMFromLongitude( const Angular& lon ) const;
00236 
00238         osg::CoordinateSystemNode* createCoordinateSystemNode() const;
00239 
00241         bool populateCoordinateSystemNode( osg::CoordinateSystemNode* csn ) const;
00242 
00253         virtual GeoLocator* createLocator(
00254             double xmin, double ymin, double xmax, double ymax,
00255             bool plate_carre =false ) const;
00256 
00257 
00258 
00259     protected:
00260         virtual ~SpatialReference();
00261 
00262     protected:
00263         SpatialReference( void* handle, const std::string& type, const std::string& init_str, const std::string& name );
00264         SpatialReference( void* handle, bool ownsHandle =true );
00265         void init();
00266 
00267         bool _initialized;
00268         void* _handle;
00269         bool _owns_handle;
00270         bool _is_geographic;
00271         bool _is_mercator;
00272         bool _is_north_polar, _is_south_polar;
00273         bool _is_cube;
00274         bool _is_contiguous;
00275         bool _is_user_defined;
00276         bool _is_ltp;
00277         std::string _name;
00278         std::string _wkt;
00279         std::string _proj4;
00280         std::string _init_type;
00281         std::string _init_str;
00282         std::string _init_str_lc;
00283         std::string _datum;
00284         osg::ref_ptr<osg::EllipsoidModel> _ellipsoid;
00285         osg::ref_ptr<SpatialReference> _geo_srs;
00286 
00287         typedef std::map<std::string,void*> TransformHandleCache;
00288         TransformHandleCache _transformHandleCache;
00289 
00290         // user can override these methods in a subclass to perform custom functionality; must
00291         // call the superclass version.
00292         virtual void _init();
00293         virtual bool _isEquivalentTo( const SpatialReference* srs ) const;
00294 
00295         virtual bool preTransform(double& x, double& y, double& z, void* context) const { return true; }
00296         virtual bool postTransform(double& x, double& y, double& z, void* context) const { return true;}
00297         
00298 
00299 
00300         typedef std::map< std::string, osg::ref_ptr<SpatialReference> > SpatialReferenceCache;
00301         static SpatialReferenceCache& getSpatialReferenceCache();
00302 
00303     private:
00304         static SpatialReference* createFromWKT(
00305             const std::string& wkt, const std::string& alias, const std::string& name ="" );
00306 
00307         static SpatialReference* createFromPROJ4(
00308             const std::string& proj4, const std::string& alias, const std::string& name ="" );
00309 
00310         static SpatialReference* createCube();
00311 
00312 
00313         SpatialReference* validate();
00314     };
00315 }
00316 
00317 
00318 #endif // OSGEARTH_SPATIAL_REFERENCE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines