[fdo-commits] r2620 - trunk/Providers/GDAL/Src/Provider

svn_fdo at osgeo.org svn_fdo at osgeo.org
Thu Mar 1 16:47:01 EST 2007


Author: warmerdam
Date: 2007-03-01 16:47:01 -0500 (Thu, 01 Mar 2007)
New Revision: 2620

Modified:
   trunk/Providers/GDAL/Src/Provider/FdoRfpClassData.cpp
   trunk/Providers/GDAL/Src/Provider/FdoRfpConnection.cpp
   trunk/Providers/GDAL/Src/Provider/FdoRfpConnection.h
   trunk/Providers/GDAL/Src/Provider/FdoRfpGeoRasterExtractor.cpp
   trunk/Providers/GDAL/Src/Provider/FdoRfpRasterUtil.cpp
Log:
Capture spatial references, and raster extents properly per ticket #15.


Modified: trunk/Providers/GDAL/Src/Provider/FdoRfpClassData.cpp
===================================================================
--- trunk/Providers/GDAL/Src/Provider/FdoRfpClassData.cpp	2007-03-01 21:41:40 UTC (rev 2619)
+++ trunk/Providers/GDAL/Src/Provider/FdoRfpClassData.cpp	2007-03-01 21:47:01 UTC (rev 2620)
@@ -57,7 +57,6 @@
         return;
 
     // Initialize class member variables to Nil
-    m_coord = "";
     m_bFirstRaster = true;
 
     // Retrieve the properties from the class definition
@@ -80,13 +79,6 @@
         throw FdoException::Create(NlsMsgGet1(GRFP_46_CLASS_INVALID, "Feature class '%1$ls' is invalid.", fcName));
     }
 
-    // Retrieve the spatial context association from the Raster Property
-    FdoStringP scName = propraster->GetSpatialContextAssociation();
-
-    // the first one must be the default one or be set by the config file
-    FdoPtr<FdoRfpSpatialContext> defaultContext = contexts->GetItem(0);
-    FdoStringP defaultCoord = defaultContext->GetCoordinateSystem();
-
     // Retrieve the Raster Property Schema Mapping definition
     FdoGrfpRasterDefinitionP rasterDef = classMapping->GetRasterDefinition();
     FdoGrfpRasterLocationsP locations = rasterDef->GetLocations();
@@ -108,38 +100,14 @@
             _buildUpGeoRastersFromCatalogue(conn, featureCatalogue, coordSystems);
     }
 	
-    // First decide whether the coordinate system is already set for the class
-    // If the Association is set, verify that the SC exists in the SC Collection 
-    // de-serialized from the Configuration file. If the SC Name is already set
-    // then the correlation information specified by each raster file will not 
-    // be used to determine teh SC in FDO
-    bool bSCExists = false;
-    if (scName != L"") {
-        FdoPtr<FdoRfpSpatialContext> context;
-        for (int i = 0; i<contexts->GetCount(); i++)
-        {
-            context = contexts->GetItem(i);
-            if (STREQUAL(context->GetCoordinateSystem(), scName))
-            {
-                bSCExists = true;
-                break;
-            }
-        }
+    // Retrieve the spatial context association from the Raster Property
+    FdoStringP m_coord = propraster->GetSpatialContextAssociation();
 
-        if (!bSCExists) {
-            throw FdoException::Create(
-                NlsMsgGet2(
-                    GRFP_108_CLASS_SPATIAL_CONTEXT_NOT_DEFINED, 
-                    "Spatial Context '%1$ls' defined for Class '%2$ls' was not found.",
-                    (FdoString*)scName, fcName));
-        }
-    }
-    else {
-        // If the Association is not set, the SC name will be determined from
-        // the correlation information from the raster images themselves.
-        // Ensure every feature class has only one single CS name
+    if (m_coord == L"") {
         if (coordSystems->GetCount() == 0) {
-            m_coord = defaultCoord;            
+            FdoPtr<FdoRfpSpatialContext> defaultContext = 
+                conn->GetDefaultSpatialContext();
+            m_coord = defaultContext->GetName();
         }
         else if (coordSystems->GetCount() > 1) { 
             throw FdoException::Create(
@@ -152,54 +120,25 @@
             m_coord = FdoStringElementP(coordSystems->GetItem(0))->GetString();            
         }
 
-        // we must decide whether the coordinate system already exists in the providers
-        // spatial context collection
-        FdoPtr<FdoRfpSpatialContext> context;
-        for (int i = 0; i<contexts->GetCount(); i++)
-        {
-            context = contexts->GetItem(i);
-            if (STREQUAL(context->GetCoordinateSystem(), m_coord))
-            {
-                bSCExists = true;
-                break;
-            }
-        }
+        // Set the Class Definition's Spatial Context Association 
+        propraster->SetSpatialContextAssociation(m_coord);
+    }
 
-        if (bSCExists) // the coordinate system exists
-        {
-            // Retrieve the SC name
-            scName = context->GetName();
-            FdoPtr<FdoByteArray> extentArr = context->GetExtent();
+    // Retrieve the context, so we can merge in our extents.
+    FdoPtr<FdoRfpSpatialContext> context = contexts->GetItem(contexts->IndexOf(m_coord));
     		
-            // Calculate the new extent of the existing spatial context
-            FdoRfpRect extent = FdoRfpUtil::CreateRectFromGeometryAgf(extentArr);
-            extent = extent.Union(m_extent);
+    // Calculate the new extent of the existing spatial context
+    try {
+        FdoPtr<FdoByteArray> extentArr = context->GetExtent();
+        FdoRfpRect extent = FdoRfpUtil::CreateRectFromGeometryAgf(extentArr);
+        extent = extent.Union(m_extent);
 
-            // Set the new extent
-            context->SetExtent(FdoRfpUtil::CreateGeometryAgfFromRect(extent));
-        }
-        else // it's a new coordinate system
-        {
-            // spatial context name is set by coordinate system name
-            // since coordinate system name is unique, the name of spatial context will be also unique.
-            scName = m_coord;
-
-            // Create a new SC
-            context = new FdoRfpSpatialContext();
-
-            // Set the SC properties
-            context->SetName(scName);
-            context->SetCoordinateSystem(m_coord);
-            context->SetExtent(FdoRfpUtil::CreateGeometryAgfFromRect(m_extent));
-            context->SetExtentType(FdoSpatialContextExtentType_Static);
-
-            // Add the SC to the list of SC exposed by the RFP Provider
-            contexts->Add(context);
-        }
-    	
-        // Set the Class Definition's Spatial Context Association 
-        propraster->SetSpatialContextAssociation(scName);
+        // Set the new extent
+        context->SetExtent(FdoRfpUtil::CreateGeometryAgfFromRect(extent));
     }
+    catch( ... ) {
+        context->SetExtent(FdoRfpUtil::CreateGeometryAgfFromRect(m_extent));
+    }
 }
 
 // Extract all rasters from specified location, return value is the associated coordinate system name
@@ -269,8 +208,13 @@
             //
             // first, get the coordinate system names associated to the rasters in the catalogue
             FdoStringP coord;
-            if (bHasGeoInfo && geoRef->HasCoordSystem() && FdoRfpRasterUtil::IsCoordinateSystemValid(geoRef->GetCoorSystem())) 
-                coord = geoRef->GetCoorSystem(); // it is a valid coordinate system
+            if (bHasGeoInfo && geoRef->HasCoordSystem() 
+                && FdoRfpRasterUtil::IsCoordinateSystemValid(geoRef->GetCoorSystem())) 
+            { 
+                FdoPtr<FdoRfpSpatialContext> context = 
+                    conn->GetSpatialContextByWkt( geoRef->GetCoorSystem() );
+                coord = context->GetName();
+            }
 
             // Add the coordinate system names to the collection of Spatial Contexts to be returned to the caller
             // It is the callers responsibility to determine what constitutes an error if more than one spatial context

Modified: trunk/Providers/GDAL/Src/Provider/FdoRfpConnection.cpp
===================================================================
--- trunk/Providers/GDAL/Src/Provider/FdoRfpConnection.cpp	2007-03-01 21:41:40 UTC (rev 2619)
+++ trunk/Providers/GDAL/Src/Provider/FdoRfpConnection.cpp	2007-03-01 21:47:01 UTC (rev 2620)
@@ -46,6 +46,7 @@
 #include "FdoRfpGlobals.h"
 #include <GdalFile/Override/FdoGrfpOverrides.h>
 #include <gdal.h>
+#include <ogr_srs_api.h>
 #include <FdoCommonConnStringParser.h>
 
 // external access to connection for client services
@@ -234,6 +235,7 @@
     defaultSC->SetExtentType(FdoSpatialContextExtentType_Static);
     defaultSC->SetXYTolerance(FdoGrfpGlobals::DefaultSpatialContextXYTolerance);
     defaultSC->SetZTolerance(FdoGrfpGlobals::DefaultSpatialContextZTolerance);
+    defaultSC->SetCoordinateSystem(FdoGrfpGlobals::DefaultSpatialContextName);
     defaultSC->SetCoordinateSystemWkt(FdoGrfpGlobals::DefaultSpatialContextWKTName);
     m_spatialContexts->Add(defaultSC);
 }
@@ -347,10 +349,6 @@
     // build up spatial contexts
     if (m_spatialContexts == NULL)
         m_spatialContexts = new FdoRfpSpatialContextCollection();
-    if (m_spatialContexts->GetCount() == 0)
-        _buildUpDefaultSpatialContext();
-    //Set the first spatial context as the active one
-    m_activeSpatialContext = FdoRfpSpatialContextP(m_spatialContexts->GetItem(0))->GetName();
 
     //Build up feature schema collection
     if (m_featureSchemas == NULL)
@@ -380,6 +378,13 @@
     //Build up the data for each feature class
     _buildUpSchemaDatas();
 
+    // Create default spatial context if we have none.
+    if (m_spatialContexts->GetCount() == 0)
+        _buildUpDefaultSpatialContext();
+
+    //Set the first spatial context as the active one
+    m_activeSpatialContext = FdoRfpSpatialContextP(m_spatialContexts->GetItem(0))->GetName();
+
     //All done, no exceptions
     m_state = FdoConnectionState_Open;
 
@@ -560,12 +565,22 @@
 
 //Get active spatial context
 FdoPtr<FdoRfpSpatialContext> FdoRfpConnection::GetActiveSpatialContext()
-{
+{ 
     //Ensure the connection is established
     _validateOpen();
+
     return m_spatialContexts->GetItem(m_activeSpatialContext);
 }
 
+//Get default spatial context
+FdoPtr<FdoRfpSpatialContext> FdoRfpConnection::GetDefaultSpatialContext()
+{
+    if( m_spatialContexts->GetCount() == 0 )
+        _buildUpDefaultSpatialContext();
+
+    return m_spatialContexts->GetItem(0);
+}
+
 //Activate spatial context
 void FdoRfpConnection::ActivateSpatialContext(FdoString* contextName)
 {
@@ -589,6 +604,73 @@
         m_spatialContexts->Add(spatialContext);
 }
 
+// Find or create spatial context by WKT.
+FdoPtr<FdoRfpSpatialContext> FdoRfpConnection::GetSpatialContextByWkt( FdoString *wkt )
+{
+    FdoInt32 i;
+    FdoPtr<FdoRfpSpatialContext> context;
+
+    // Do we have an existing matching SC?
+    for( i = 0; i < m_spatialContexts->GetCount(); i++ )
+    {
+        context = m_spatialContexts->GetItem(i);
+        
+        if( STREQUAL(context->GetCoordinateSystemWkt(),wkt) )
+            return context;
+    }
+
+    // Pick a unique name for this SC.  We try to use the basename from
+    // the WKT, but we will append an index if necessary to make it unique.
+    FdoStringP wkt2( wkt );
+    FdoStringP basename, name;
+    OGRSpatialReferenceH hSRS = OSRNewSpatialReference( wkt2 );
+
+    if( hSRS == NULL )
+        basename = "unnamed";
+    else if( OSRIsGeographic(hSRS) )
+        basename = OSRGetAttrValue( hSRS, "GEOGCS", 0 );
+    else if( OSRIsProjected(hSRS) )
+        basename = OSRGetAttrValue( hSRS, "PROJCS", 0 );
+    else if( OSRIsLocal(hSRS) )
+        basename = OSRGetAttrValue( hSRS, "LOCAL_CS", 0 );
+    else
+        basename = "unnamed";
+
+    for( i = 0; ; i++ )
+    {
+        if( i == 0 )
+            name = basename;
+        else
+        {
+            char id[12];
+
+            sprintf( id, "%d", i );
+            name = basename;
+            name += id;
+        }
+
+        if( m_spatialContexts->IndexOf( name ) == -1 )
+            break;
+    }
+    
+    // Create a new SC
+    context = new FdoRfpSpatialContext();
+
+    
+    // Set the SC properties
+    context->SetName(name);
+    context->SetCoordinateSystem(wkt);
+    context->SetCoordinateSystemWkt(wkt);
+    //context->SetExtent(FdoRfpUtil::CreateGeometryAgfFromRect(m_extent));
+    context->SetExtentType(FdoSpatialContextExtentType_Dynamic);
+
+    // Add the SC to the list of SC exposed by the RFP Provider
+    m_spatialContexts->Add(context);
+    
+    return context;
+}
+
+
 // Destroy a spatial context
 void FdoRfpConnection::DestroySpatialContext(FdoString* contextName)
 {

Modified: trunk/Providers/GDAL/Src/Provider/FdoRfpConnection.h
===================================================================
--- trunk/Providers/GDAL/Src/Provider/FdoRfpConnection.h	2007-03-01 21:41:40 UTC (rev 2619)
+++ trunk/Providers/GDAL/Src/Provider/FdoRfpConnection.h	2007-03-01 21:47:01 UTC (rev 2620)
@@ -219,6 +219,9 @@
     //Get active spatial context
     FdoPtr<FdoRfpSpatialContext> GetActiveSpatialContext();
 
+    //Get default spatial context
+    FdoPtr<FdoRfpSpatialContext> GetDefaultSpatialContext();
+
     //Activate spatial context
     void ActivateSpatialContext(FdoString* contextName);
 
@@ -228,6 +231,9 @@
     // Destroy a spatial context
     void DestroySpatialContext(FdoString* contextName);
 
+    // Fetch a spatial context by WKT, creating if it does not exist. 
+    FdoPtr<FdoRfpSpatialContext> GetSpatialContextByWkt(FdoString* wkt);
+
     // Fetch the dataset cache
     FdoRfpDatasetCache *GetDatasetCache();
 

Modified: trunk/Providers/GDAL/Src/Provider/FdoRfpGeoRasterExtractor.cpp
===================================================================
--- trunk/Providers/GDAL/Src/Provider/FdoRfpGeoRasterExtractor.cpp	2007-03-01 21:41:40 UTC (rev 2619)
+++ trunk/Providers/GDAL/Src/Provider/FdoRfpGeoRasterExtractor.cpp	2007-03-01 21:47:01 UTC (rev 2620)
@@ -196,8 +196,12 @@
 
         // coordinate system
         FdoStringP coord;
-        if (geoRef->HasCoordSystem() && FdoRfpRasterUtil::IsCoordinateSystemValid(geoRef->GetCoorSystem())) // there is a coordinate system with the image
-            coord = geoRef->GetCoorSystem();
+        if (geoRef->HasCoordSystem() )
+        {
+            FdoPtr<FdoRfpSpatialContext> context = 
+                conn->GetSpatialContextByWkt( geoRef->GetCoorSystem() );
+            coord = context->GetName();
+        }
 
         // Add the coordinate system names to the collection of Spatial Contexts to be returned to the caller
         // It is the callers responsibility to determine what constitutes an error if more than one spatial context

Modified: trunk/Providers/GDAL/Src/Provider/FdoRfpRasterUtil.cpp
===================================================================
--- trunk/Providers/GDAL/Src/Provider/FdoRfpRasterUtil.cpp	2007-03-01 21:41:40 UTC (rev 2619)
+++ trunk/Providers/GDAL/Src/Provider/FdoRfpRasterUtil.cpp	2007-03-01 21:47:01 UTC (rev 2620)
@@ -189,11 +189,7 @@
     const char *pszWKT = GDALGetProjectionRef( hDS );
     if ( pszWKT != NULL && strlen(pszWKT) > 0 )
     {
-        // I think we should just be assigning a coordinate
-        // system name here, not the whole WKT definition.
-
-        // Commented out to avoid "multiple coordinate system" problems.
-        //geoRef->SetCoordSystem(pszWKT);
+        geoRef->SetCoordSystem(pszWKT);
     }
 
     return true;



More information about the fdo-commits mailing list