LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/gml - gmlutils.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 116 81 69.8 %
Date: 2012-04-28 Functions: 8 8 100.0 %

       1                 : /******************************************************************************
       2                 :  * $Id: gmlutils.cpp 23638 2011-12-22 21:02:56Z rouault $
       3                 :  *
       4                 :  * Project:  GML Utils
       5                 :  * Purpose:  GML reader
       6                 :  * Author:   Even Rouault, <even dot rouault at mines dash paris dot org>
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2010, Even Rouault
      10                 :  *
      11                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      12                 :  * copy of this software and associated documentation files (the "Software"),
      13                 :  * to deal in the Software without restriction, including without limitation
      14                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15                 :  * and/or sell copies of the Software, and to permit persons to whom the
      16                 :  * Software is furnished to do so, subject to the following conditions:
      17                 :  * 
      18                 :  * The above copyright notice and this permission notice shall be included
      19                 :  * in all copies or substantial portions of the Software.
      20                 :  * 
      21                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      22                 :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
      24                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
      27                 :  * DEALINGS IN THE SOFTWARE.
      28                 :  ****************************************************************************/
      29                 : 
      30                 : #include "gmlutils.h"
      31                 : 
      32                 : #include "cpl_string.h"
      33                 : #include "ogr_api.h"
      34                 : #include "ogr_p.h"
      35                 : #include <string>
      36                 : 
      37                 : /************************************************************************/
      38                 : /*                GML_ExtractSrsNameFromGeometry()                      */
      39                 : /************************************************************************/
      40                 : 
      41            2104 : const char* GML_ExtractSrsNameFromGeometry(const CPLXMLNode* const * papsGeometry,
      42                 :                                      std::string& osWork,
      43                 :                                      int bConsiderEPSGAsURN)
      44                 : {
      45            2104 :     if (papsGeometry[0] != NULL && papsGeometry[1] == NULL)
      46                 :     {
      47            2104 :         const char* pszSRSName = CPLGetXMLValue((CPLXMLNode*)papsGeometry[0], "srsName", NULL);
      48            2104 :         if (pszSRSName)
      49                 :         {
      50             816 :             int nLen = strlen(pszSRSName);
      51                 : 
      52             816 :             if (strncmp(pszSRSName, "EPSG:", 5) == 0 &&
      53                 :                 bConsiderEPSGAsURN)
      54                 :             {
      55              72 :                 osWork.reserve(22 + nLen-5);
      56              72 :                 osWork.assign("urn:ogc:def:crs:EPSG::", 22);
      57              72 :                 osWork.append(pszSRSName+5, nLen-5);
      58              72 :                 return osWork.c_str();
      59                 :             }
      60             744 :             else if (strncmp(pszSRSName, "http://www.opengis.net/gml/srs/epsg.xml#", 40) == 0)
      61                 :             {
      62              82 :                 osWork.reserve(5 + nLen-40 );
      63              82 :                 osWork.assign("EPSG:", 5);
      64              82 :                 osWork.append(pszSRSName+40, nLen-40);
      65              82 :                 return osWork.c_str();
      66                 :             }
      67                 :             else
      68                 :             {
      69             662 :                 return pszSRSName;
      70                 :             }
      71                 :         }
      72                 :     }
      73            1288 :     return NULL;
      74                 : }
      75                 : 
      76                 : /************************************************************************/
      77                 : /*                       GML_IsSRSLatLongOrder()                        */
      78                 : /************************************************************************/
      79                 : 
      80             658 : int GML_IsSRSLatLongOrder(const char* pszSRSName)
      81                 : {
      82             658 :     if (pszSRSName == NULL)
      83              22 :         return FALSE;
      84                 : 
      85             636 :     if (strncmp(pszSRSName, "urn:", 4) == 0)
      86                 :     {
      87             404 :         if (strstr(pszSRSName, ":4326") != NULL)
      88                 :         {
      89                 :             /* Shortcut ... */
      90              58 :             return TRUE;
      91                 :         }
      92                 :         else
      93                 :         {
      94             346 :             OGRSpatialReference oSRS;
      95             346 :             if (oSRS.importFromURN(pszSRSName) == OGRERR_NONE)
      96                 :             {
      97             346 :                 if (oSRS.EPSGTreatsAsLatLong())
      98             308 :                     return TRUE;
      99               0 :             }
     100                 :         }
     101                 :     }
     102             270 :     return FALSE;
     103                 : }
     104                 : 
     105                 : 
     106                 : /************************************************************************/
     107                 : /*                GML_BuildOGRGeometryFromList_CreateCache()            */
     108                 : /************************************************************************/
     109                 : 
     110                 : class SRSCache
     111             532 : {
     112                 : public:
     113                 :     std::string osLastSRSName;
     114                 :     int   bAxisInvertLastSRSName;
     115                 : };
     116                 : 
     117             266 : void* GML_BuildOGRGeometryFromList_CreateCache()
     118                 : {
     119             266 :     return new SRSCache();
     120                 : }
     121                 : 
     122                 : /************************************************************************/
     123                 : /*                 GML_BuildOGRGeometryFromList_DestroyCache()          */
     124                 : /************************************************************************/
     125                 : 
     126             266 : void GML_BuildOGRGeometryFromList_DestroyCache(void* hCacheSRS)
     127                 : {
     128             266 :     delete (SRSCache*)hCacheSRS;
     129             266 : }
     130                 : 
     131                 : /************************************************************************/
     132                 : /*                 GML_BuildOGRGeometryFromList()                       */
     133                 : /************************************************************************/
     134                 : 
     135            1436 : OGRGeometry* GML_BuildOGRGeometryFromList(const CPLXMLNode* const * papsGeometry,
     136                 :                                           int bTryToMakeMultipolygons,
     137                 :                                           int bInvertAxisOrderIfLatLong,
     138                 :                                           const char* pszDefaultSRSName,
     139                 :                                           int bConsiderEPSGAsURN,
     140                 :                                           int bGetSecondaryGeometryOption,
     141                 :                                           void* hCacheSRS,
     142                 :                                           int bFaceHoleNegative)
     143                 : {
     144            1436 :     OGRGeometry* poGeom = NULL;
     145                 :     int i;
     146            1436 :     OGRGeometryCollection* poCollection = NULL;
     147            2856 :     for(i=0;papsGeometry[i] != NULL;i++)
     148                 :     {
     149                 :         OGRGeometry* poSubGeom = GML2OGRGeometry_XMLNode( papsGeometry[i],
     150                 :                                                           bGetSecondaryGeometryOption,
     151                 :                                                           0, FALSE, TRUE,
     152            1420 :                                                           bFaceHoleNegative );
     153            1420 :         if (poSubGeom)
     154                 :         {
     155            1420 :             if (poGeom == NULL)
     156            1420 :                 poGeom = poSubGeom;
     157                 :             else
     158                 :             {
     159               0 :                 if (poCollection == NULL)
     160                 :                 {
     161               0 :                     if (bTryToMakeMultipolygons &&
     162               0 :                         wkbFlatten(poGeom->getGeometryType()) == wkbPolygon &&
     163               0 :                         wkbFlatten(poSubGeom->getGeometryType()) == wkbPolygon)
     164                 :                     {
     165               0 :                         OGRGeometryCollection* poGeomColl = new OGRMultiPolygon();
     166               0 :                         poGeomColl->addGeometryDirectly(poGeom);
     167               0 :                         poGeomColl->addGeometryDirectly(poSubGeom);
     168               0 :                         poGeom = poGeomColl;
     169                 :                     }
     170               0 :                     else if (bTryToMakeMultipolygons &&
     171               0 :                                 wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon &&
     172               0 :                                 wkbFlatten(poSubGeom->getGeometryType()) == wkbPolygon)
     173                 :                     {
     174               0 :                         OGRGeometryCollection* poGeomColl = (OGRGeometryCollection* )poGeom;
     175               0 :                         poGeomColl->addGeometryDirectly(poSubGeom);
     176                 :                     }
     177               0 :                     else if (bTryToMakeMultipolygons &&
     178               0 :                                 wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon &&
     179               0 :                                 wkbFlatten(poSubGeom->getGeometryType()) == wkbMultiPolygon)
     180                 :                     {
     181               0 :                         OGRGeometryCollection* poGeomColl = (OGRGeometryCollection* )poGeom;
     182               0 :                         OGRGeometryCollection* poGeomColl2 = (OGRGeometryCollection* )poSubGeom;
     183               0 :                         int nCount = poGeomColl2->getNumGeometries();
     184                 :                         int i;
     185               0 :                         for(i=0;i<nCount;i++)
     186                 :                         {
     187               0 :                             poGeomColl->addGeometry(poGeomColl2->getGeometryRef(i));
     188                 :                         }
     189               0 :                         delete poSubGeom;
     190                 :                     }
     191               0 :                     else if (bTryToMakeMultipolygons &&
     192               0 :                                 wkbFlatten(poGeom->getGeometryType()) == wkbMultiPolygon)
     193                 :                     {
     194               0 :                         delete poGeom;
     195               0 :                         delete poSubGeom;
     196                 :                         return GML_BuildOGRGeometryFromList(papsGeometry, FALSE,
     197                 :                                                             bInvertAxisOrderIfLatLong,
     198                 :                                                             pszDefaultSRSName,
     199                 :                                                             bConsiderEPSGAsURN,
     200                 :                                                             bGetSecondaryGeometryOption,
     201               0 :                                                             hCacheSRS);
     202                 :                     }
     203                 :                     else
     204                 :                     {
     205               0 :                         poCollection = new OGRGeometryCollection();
     206               0 :                         poCollection->addGeometryDirectly(poGeom);
     207               0 :                         poGeom = poCollection;
     208                 :                     }
     209                 :                 }
     210               0 :                 if (poCollection != NULL)
     211                 :                 {
     212               0 :                     poCollection->addGeometryDirectly(poSubGeom);
     213                 :                 }
     214                 :             }
     215                 :         }
     216                 :     }
     217                 : 
     218            1436 :     if ( poGeom != NULL && bInvertAxisOrderIfLatLong )
     219                 :     {
     220            1378 :         std::string osWork;
     221                 :         const char* pszSRSName = GML_ExtractSrsNameFromGeometry(papsGeometry, osWork,
     222            1378 :                                                           bConsiderEPSGAsURN);
     223            1378 :         const char* pszNameLookup = pszSRSName ? pszSRSName : pszDefaultSRSName;
     224            1378 :         if (pszNameLookup != NULL)
     225                 :         {
     226             652 :             SRSCache* poSRSCache = (SRSCache*)hCacheSRS;
     227                 :             int bSwap;
     228             652 :             if (strcmp(poSRSCache->osLastSRSName.c_str(), pszNameLookup) == 0)
     229                 :             {
     230             558 :                 bSwap = poSRSCache->bAxisInvertLastSRSName;
     231                 :             }
     232                 :             else
     233                 :             {
     234              94 :                 bSwap = GML_IsSRSLatLongOrder(pszNameLookup);
     235              94 :                 poSRSCache->osLastSRSName = pszNameLookup;
     236              94 :                 poSRSCache->bAxisInvertLastSRSName= bSwap;
     237                 :             }
     238             652 :             if (bSwap)
     239             120 :                 poGeom->swapXY();
     240            1378 :         }
     241                 :     }
     242                 : 
     243            1436 :     return poGeom;
     244                 : }
     245                 : 
     246                 : /************************************************************************/
     247                 : /*                           GML_GetSRSName()                           */
     248                 : /************************************************************************/
     249                 : 
     250              40 : char* GML_GetSRSName(const OGRSpatialReference* poSRS, int bLongSRS, int *pbCoordSwap)
     251                 : {
     252              40 :     *pbCoordSwap = FALSE;
     253              40 :     if (poSRS == NULL)
     254               0 :         return CPLStrdup("");
     255                 : 
     256              40 :     const char* pszAuthName = NULL;
     257              40 :     const char* pszAuthCode = NULL;
     258              40 :     const char* pszTarget = NULL;
     259                 : 
     260              40 :     if (poSRS->IsProjected())
     261              22 :         pszTarget = "PROJCS";
     262                 :     else
     263              18 :         pszTarget = "GEOGCS";
     264                 : 
     265                 :     char szSrsName[50];
     266              40 :     szSrsName[0] = 0;
     267                 : 
     268              40 :     pszAuthName = poSRS->GetAuthorityName( pszTarget );
     269              40 :     if( NULL != pszAuthName )
     270                 :     {
     271              18 :         if( EQUAL( pszAuthName, "EPSG" ) )
     272                 :         {
     273              18 :             pszAuthCode = poSRS->GetAuthorityCode( pszTarget );
     274              18 :             if( NULL != pszAuthCode && strlen(pszAuthCode) < 10 )
     275                 :             {
     276              18 :                 if (bLongSRS && !((OGRSpatialReference*)poSRS)->EPSGTreatsAsLatLong())
     277                 :                 {
     278              18 :                     OGRSpatialReference oSRS;
     279              18 :                     if (oSRS.importFromEPSGA(atoi(pszAuthCode)) == OGRERR_NONE)
     280                 :                     {
     281              18 :                         if (oSRS.EPSGTreatsAsLatLong())
     282              18 :                             *pbCoordSwap = TRUE;
     283              18 :                     }
     284                 :                 }
     285                 : 
     286              18 :                 if (bLongSRS)
     287                 :                 {
     288                 :                     sprintf( szSrsName, " srsName=\"urn:ogc:def:crs:%s::%s\"",
     289              18 :                         pszAuthName, pszAuthCode );
     290                 :                 }
     291                 :                 else
     292                 :                 {
     293                 :                     sprintf( szSrsName, " srsName=\"%s:%s\"",
     294               0 :                             pszAuthName, pszAuthCode );
     295                 :                 }
     296                 :             }
     297                 :         }
     298                 :     }
     299                 : 
     300              40 :     return CPLStrdup(szSrsName);
     301                 : }

Generated by: LCOV version 1.7