示例#1
0
/* msDebugCleanup()
**
** Called by msCleanup to remove info related to this thread.
*/
void msDebugCleanup()
{
  /* make sure file is closed */
  msCloseErrorFile();

#ifdef USE_THREAD
  {
    int  thread_id = msGetThreadId();
    debugInfoObj *link;

    msAcquireLock( TLOCK_DEBUGOBJ );

    /* find link for this thread */

    for( link = debuginfo_list;
         link != NULL && link->thread_id != thread_id
         && link->next != NULL && link->next->thread_id != thread_id;
         link = link->next ) {}

    if( link->thread_id == thread_id ) {
      /* presumably link is at head of list.  */
      if( debuginfo_list == link )
        debuginfo_list = link->next;

      free( link );
    } else if( link->next != NULL && link->next->thread_id == thread_id ) {
      debugInfoObj *next_link = link->next;
      link->next = link->next->next;
      free( next_link );
    }
    msReleaseLock( TLOCK_DEBUGOBJ );
  }
#endif

}
示例#2
0
int
msPluginLayerInitializeVirtualTable(layerObj *layer)
{
    VTFactoryItemObj *pVTFI;

    msAcquireLock(TLOCK_LAYER_VTABLE);

    pVTFI = lookupVTFItem(&gVirtualTableFactory, layer->plugin_library);
    if ( ! pVTFI) {
        pVTFI = loadCustomLayerDLL(layer, layer->plugin_library);
        if ( ! pVTFI) {
            msReleaseLock(TLOCK_LAYER_VTABLE);
            return MS_FAILURE;
        }
        if (insertNewVTFItem(&gVirtualTableFactory, pVTFI) != MS_SUCCESS) {
            destroyVTFItem(&pVTFI);
            msReleaseLock(TLOCK_LAYER_VTABLE);
            return MS_FAILURE;
        }
    }
    msReleaseLock(TLOCK_LAYER_VTABLE);

    copyVirtualTable(layer->vtable, &pVTFI->vtable);
    return MS_SUCCESS;
}
示例#3
0
void msSetPROJ_LIB( const char *proj_lib, const char *pszRelToPath )

{
#ifdef USE_PROJ
    static int finder_installed = 0;
    char *extended_path = NULL;

    /* Handle relative path if applicable */
    if( proj_lib && pszRelToPath
        && proj_lib[0] != '/'
        && proj_lib[0] != '\\'
        && !(proj_lib[0] != '\0' && proj_lib[1] == ':') )
    {
        struct stat stat_buf;
        extended_path = (char*) msSmallMalloc(strlen(pszRelToPath)
                                              + strlen(proj_lib) + 10);
        sprintf( extended_path, "%s/%s", pszRelToPath, proj_lib );

#ifndef S_ISDIR
#  define S_ISDIR(x) ((x) & S_IFDIR)
#endif            
            
        if( stat( extended_path, &stat_buf ) == 0 
            && S_ISDIR(stat_buf.st_mode) )
            proj_lib = extended_path;
    }


    msAcquireLock( TLOCK_PROJ );

    if( finder_installed == 0 && proj_lib != NULL)
    {
        finder_installed = 1;
        pj_set_finder( msProjFinder );
    }
    
    if (proj_lib == NULL) pj_set_finder(NULL);
    
    if( ms_proj_lib != NULL )
    {
        free( ms_proj_lib );
        ms_proj_lib = NULL;
    }

    if( last_filename != NULL )
    {
        free( last_filename );
        last_filename = NULL;
    }

    if( proj_lib != NULL )
        ms_proj_lib = msStrdup( proj_lib );
    
    msReleaseLock( TLOCK_PROJ );

    if ( extended_path )
        msFree( extended_path );
#endif
}
示例#4
0
static void msContourOGRCloseConnection(void *conn_handle)
{
  OGRDataSourceH hDS = (OGRDataSourceH) conn_handle;

  msAcquireLock(TLOCK_OGR);
  OGR_DS_Destroy(hDS);
  msReleaseLock(TLOCK_OGR);
}
示例#5
0
debugInfoObj *msGetDebugInfoObj()
{
    debugInfoObj *link;
    int          thread_id;
    debugInfoObj *ret_obj;
    
    msAcquireLock( TLOCK_DEBUGOBJ );
    
    thread_id = msGetThreadId();

    /* find link for this thread */
    
    for( link = debuginfo_list; 
         link != NULL && link->thread_id != thread_id
             && link->next != NULL && link->next->thread_id != thread_id;
         link = link->next ) {}

    /* If the target thread link is already at the head of the list were ok */
    if( debuginfo_list != NULL && debuginfo_list->thread_id == thread_id )
    {
    }

    /* We don't have one ... initialize one. */
    else if( link == NULL || link->next == NULL )
    {
        debugInfoObj *new_link;

        new_link = (debugInfoObj *) malloc(sizeof(debugInfoObj));
        if (new_link != NULL) 
        {
            new_link->next = debuginfo_list;
            new_link->thread_id = thread_id;
            new_link->global_debug_level = MS_DEBUGLEVEL_ERRORSONLY;
            new_link->debug_mode = MS_DEBUGMODE_OFF;
            new_link->errorfile = NULL;
            new_link->fp = NULL;
        } else
            msSetError(MS_MEMERR, "Out of memory allocating %u bytes.\n", "msGetDebugInfoObj()", sizeof(debugInfoObj));

        debuginfo_list = new_link;
    }

    /* If the link is not already at the head of the list, promote it */
    else if( link != NULL && link->next != NULL )
    {
        debugInfoObj *target = link->next;

        link->next = link->next->next;
        target->next = debuginfo_list;
        debuginfo_list = target;
    }

    ret_obj = debuginfo_list;

    msReleaseLock( TLOCK_DEBUGOBJ ); 

    return ret_obj;
}
示例#6
0
/**********************************************************************
 *                          msHTTPCleanup()
 *
 **********************************************************************/
void msHTTPCleanup()
{
  msAcquireLock(TLOCK_OWS);
  if (gbCurlInitialized)
    curl_global_cleanup();

  gbCurlInitialized = MS_FALSE;
  msReleaseLock(TLOCK_OWS);
}
示例#7
0
int msContourLayerOpen(layerObj *layer)
{
  char *decrypted_path;
  char szPath[MS_MAXPATHLEN];  
  contourLayerInfo *clinfo;

  if (layer->debug)
    msDebug("Entering msContourLayerOpen().\n");

  /* If we don't have info, initialize an empty one now */
  if (layer->layerinfo == NULL)
    msContourLayerInfoInitialize(layer);

  clinfo = (contourLayerInfo *) layer->layerinfo;

  GDALAllRegister();

  /* Open the original Dataset */
  msTryBuildPath3(szPath, layer->map->mappath, layer->map->shapepath, layer->data);
  decrypted_path = msDecryptStringTokens(layer->map, szPath);

  msAcquireLock(TLOCK_GDAL);
  if (decrypted_path) {
    clinfo->hOrigDS = GDALOpen(decrypted_path, GA_ReadOnly);
    msFree(decrypted_path);
  } else
    clinfo->hOrigDS = NULL;

  msReleaseLock(TLOCK_GDAL);

  if (clinfo->hOrigDS == NULL) {
    msSetError(MS_IMGERR,
               "Unable to open GDAL dataset.",
               "msContourLayerOpen()");
    return MS_FAILURE;
  }
  
  /* Open the raster source */
  if (msContourLayerReadRaster(layer, layer->map->extent) != MS_SUCCESS)
    return MS_FAILURE;

  /* Generate Contour Dataset */
  if (msContourLayerGenerateContour(layer) != MS_SUCCESS)
    return MS_FAILURE;

  if (clinfo->hDS) {
    GDALClose(clinfo->hDS);
    clinfo->hDS = NULL;  
    free(clinfo->buffer);
  }

  /* Open our virtual ogr layer */
  if (clinfo->hOGRDS && (msLayerOpen(&clinfo->ogrLayer) != MS_SUCCESS))
    return MS_FAILURE;
  
  return MS_SUCCESS;
}
示例#8
0
文件: mapio.c 项目: codeforeurope/gim
static msIOContextGroup *msIO_GetContextGroup()

{
    int nThreadId = msGetThreadId();
    msIOContextGroup *prev = NULL, *group = io_context_list;

    if( group != NULL && group->thread_id == nThreadId )
        return group;

/* -------------------------------------------------------------------- */
/*      Search for group for this thread                                */
/* -------------------------------------------------------------------- */
    msAcquireLock( TLOCK_IOCONTEXT );
    msIO_Initialize();

    group = io_context_list;
    while( group != NULL && group->thread_id != nThreadId )
    {
        prev = group;
        group = group->next;
    }

/* -------------------------------------------------------------------- */
/*      If we found it, make sure it is pushed to the front of the      */
/*      link for faster finding next time, and return it.               */
/* -------------------------------------------------------------------- */
    if( group != NULL )
    {
        if( prev != NULL )
        {
            prev->next = group->next;
            group->next = io_context_list;
            io_context_list = group;
        }

        msReleaseLock( TLOCK_IOCONTEXT );
        return group;
    }

/* -------------------------------------------------------------------- */
/*      Create a new context group for this thread.                     */
/* -------------------------------------------------------------------- */
    group = (msIOContextGroup *) calloc(sizeof(msIOContextGroup),1);
    
    group->stdin_context = default_contexts.stdin_context;
    group->stdout_context = default_contexts.stdout_context;
    group->stderr_context = default_contexts.stderr_context;
    group->thread_id = nThreadId;

    group->next = io_context_list;
    io_context_list = group;
    
    msReleaseLock( TLOCK_IOCONTEXT );

    return group;
}
示例#9
0
/* msResetErrorList()
**
** Clear the list of error objects.
*/
void msResetErrorList()
{
    errorObj *ms_error, *this_error;
    ms_error = msGetErrorObj();

    this_error = ms_error->next;
    while( this_error != NULL)
    {
        errorObj *next_error;

        next_error = this_error->next;
        msFree(this_error);
        this_error = next_error;
    }

    ms_error->next = NULL;
    ms_error->code = MS_NOERR;
    ms_error->routine[0] = '\0';
    ms_error->message[0] = '\0';

    /* -------------------------------------------------------------------- */
    /*      Cleanup our entry in the thread list.  This is mainly           */
    /*      imprortant when msCleanup() calls msResetErrorList().           */
    /* -------------------------------------------------------------------- */
#ifdef USE_THREAD
    {
        int  thread_id = msGetThreadId();
        te_info_t *link;

        msAcquireLock( TLOCK_ERROROBJ );

        /* find link for this thread */

        for( link = error_list;
                link != NULL && link->thread_id != thread_id
                && link->next != NULL && link->next->thread_id != thread_id;
                link = link->next ) {}

        if( link->thread_id == thread_id )
        {
            /* presumably link is at head of list.  */
            if( error_list == link )
                error_list = link->next;

            free( link );
        }
        else if( link->next != NULL && link->next->thread_id == thread_id )
        {
            te_info_t *next_link = link->next;
            link->next = link->next->next;
            free( next_link );
        }
        msReleaseLock( TLOCK_ERROROBJ );
    }
#endif
}
示例#10
0
int msLoadSymbolSet(symbolSetObj *symbolset, mapObj *map)
{
  int retval = MS_FAILURE;

  msAcquireLock( TLOCK_PARSER );
  retval = loadSymbolSet( symbolset, map );
  msReleaseLock( TLOCK_PARSER );

  return retval;
}
示例#11
0
文件: mappool.c 项目: EOX-A/mapserver
void msConnPoolFinalCleanup()

{
  /* this really needs to be commented out before commiting.  */
  /* msDebug( "msConnPoolFinalCleanup()\n" ); */

  msAcquireLock( TLOCK_POOL );
  while( connectionCount > 0 )
    msConnPoolClose( 0 );
  msReleaseLock( TLOCK_POOL );
}
示例#12
0
errorObj *msGetErrorObj()
{
    te_info_t *link;
    int        thread_id;
    errorObj   *ret_obj;

    msAcquireLock( TLOCK_ERROROBJ );

    thread_id = msGetThreadId();

    /* find link for this thread */

    for( link = error_list;
            link != NULL && link->thread_id != thread_id
            && link->next != NULL && link->next->thread_id != thread_id;
            link = link->next ) {}

    /* If the target thread link is already at the head of the list were ok */
    if( error_list != NULL && error_list->thread_id == thread_id )
    {
    }

    /* We don't have one ... initialize one. */
    else if( link == NULL || link->next == NULL )
    {
        te_info_t *new_link;
        errorObj   error_obj = { MS_NOERR, "", "", 0 };

        new_link = (te_info_t *) malloc(sizeof(te_info_t));
        new_link->next = error_list;
        new_link->thread_id = thread_id;
        new_link->ms_error = error_obj;

        error_list = new_link;
    }

    /* If the link is not already at the head of the list, promote it */
    else if( link != NULL && link->next != NULL )
    {
        te_info_t *target = link->next;

        link->next = link->next->next;
        target->next = error_list;
        error_list = target;
    }

    ret_obj = &(error_list->ms_error);

    msReleaseLock( TLOCK_ERROROBJ );

    return ret_obj;
}
示例#13
0
void msGDALInitialize( void )

{
  if( !bGDALInitialized ) {
    msAcquireLock( TLOCK_GDAL );

    GDALAllRegister();
    CPLPushErrorHandler( CPLQuietErrorHandler );
    msReleaseLock( TLOCK_GDAL );

    bGDALInitialized = 1;
  }
}
示例#14
0
void msGDALCleanup( void )

{
    if( bGDALInitialized )
    {
        int iRepeat = 5;
        msAcquireLock( TLOCK_GDAL );

#if GDAL_RELEASE_DATE > 20101207
        {
            /* 
            ** Cleanup any unreferenced but open datasets as will tend
            ** to exist due to deferred close requests.  We are careful
            ** to only close one file at a time before refecting the
            ** list as closing some datasets may cause others to be 
            ** closed (subdatasets in a VRT for instance).
            */
            GDALDatasetH *pahDSList = NULL;
            int nDSCount = 0;
            int bDidSomething;

            do {
                int i;
                GDALGetOpenDatasets( &pahDSList, &nDSCount );
                bDidSomething = FALSE;
                for( i = 0; i < nDSCount && !bDidSomething; i++ )
                {
                    if( GDALReferenceDataset( pahDSList[i] ) == 1 )
                    {
                        GDALClose( pahDSList[i] );
                        bDidSomething = TRUE;
                    }
                    else
                        GDALDereferenceDataset( pahDSList[i] );
                }
            } while( bDidSomething );
        }
#endif

        while( iRepeat-- )
            CPLPopErrorHandler();

#if GDAL_RELEASE_DATE > 20021001
        GDALDestroyDriverManager();
#endif

        msReleaseLock( TLOCK_GDAL );

        bGDALInitialized = 0;
    }
}
示例#15
0
void *msConnPoolRequest( layerObj *layer )

{
    int  i;
    const char* close_connection;

    if( layer->connection == NULL )
        return NULL;

    /* check if we must always create a new connection */
    close_connection = msLayerGetProcessingKey( layer, "CLOSE_CONNECTION" );
    if( close_connection && strcasecmp(close_connection,"ALWAYS") == 0 )
        return NULL;

    msAcquireLock( TLOCK_POOL );
    for( i = 0; i < connectionCount; i++ )
    {
        connectionObj *conn = connections + i;

        if( layer->connectiontype == conn->connectiontype
            && strcasecmp( layer->connection, conn->connection ) == 0 
            && (conn->ref_count == 0 || conn->thread_id == msGetThreadId())
            && conn->lifespan != MS_LIFE_SINGLE)
        {
            void *conn_handle = NULL;

            conn->ref_count++;
            conn->thread_id = msGetThreadId();
            conn->last_used = time(NULL);

            if( layer->debug )
            {
                msDebug( "msConnPoolRequest(%s,%s) -> got %p\n",
                         layer->name, layer->connection, conn->conn_handle );
                conn->debug = layer->debug;
            }

            conn_handle = conn->conn_handle;

            msReleaseLock( TLOCK_POOL );
            return conn_handle;
        }
    }

    msReleaseLock( TLOCK_POOL );

    return NULL;
}
示例#16
0
void msConnPoolRelease( layerObj *layer, void *conn_handle )

{
    int  i;

    if( layer->debug )
        msDebug( "msConnPoolRelease(%s,%s,%p)\n",
                 layer->name, layer->connection, conn_handle );

    if( layer->connection == NULL )
        return;

    msAcquireLock( TLOCK_POOL );
    for( i = 0; i < connectionCount; i++ )
    {
        connectionObj *conn = connections + i;

        if( layer->connectiontype == conn->connectiontype
            && strcasecmp( layer->connection, conn->connection ) == 0 
            && conn->conn_handle == conn_handle )
        {
            conn->ref_count--;
            conn->last_used = time(NULL);

            if( conn->ref_count == 0 )
                conn->thread_id = 0;

            if( conn->ref_count == 0 && (conn->lifespan == MS_LIFE_ZEROREF || conn->lifespan == MS_LIFE_SINGLE) )
                msConnPoolClose( i );

            msReleaseLock( TLOCK_POOL );
            return;
        }
    }

    msReleaseLock( TLOCK_POOL );

    msDebug( "%s: Unable to find handle for layer '%s'.\n",
             "msConnPoolRelease()",
             layer->name );

    msSetError( MS_MISCERR, 
                "Unable to find handle for layer '%s'.",
                "msConnPoolRelease()",
                layer->name );
}
示例#17
0
/* msPluginFreeVirtualTableFactory()
** Called by msCleanup() to free the virtual table factory
*/
void
msPluginFreeVirtualTableFactory()
{
    int i;
    msAcquireLock(TLOCK_LAYER_VTABLE);

    for (i=0; i<gVirtualTableFactory.size; i++) {
        if (gVirtualTableFactory.vtItems[i])
            destroyVTFItem(&(gVirtualTableFactory.vtItems[i]));
    }
    free(gVirtualTableFactory.vtItems);
    gVirtualTableFactory.vtItems = NULL;
    gVirtualTableFactory.size = 0;
    gVirtualTableFactory.first_free = 0;

    msReleaseLock(TLOCK_LAYER_VTABLE);
}
示例#18
0
文件: mappool.c 项目: EOX-A/mapserver
void msConnPoolCloseUnreferenced()

{
  int  i;

  /* this really needs to be commented out before commiting.  */
  /* msDebug( "msConnPoolCloseUnreferenced()\n" ); */

  msAcquireLock( TLOCK_POOL );
  for( i = connectionCount - 1; i >= 0; i-- ) {
    connectionObj *conn = connections + i;

    if( conn->ref_count == 0 ) {
      /* for now we don't assume the locks are re-entrant, so release */
      /* it so msConnPoolClose() can get it.  */
      msConnPoolClose( i );
    }
  }
  msReleaseLock( TLOCK_POOL );
}
示例#19
0
int msHTTPInit()
{
  /* curl_global_init() should only be called once (no matter how
   * many threads or libcurl sessions that'll be used) by every
   * application that uses libcurl.
   */

  msAcquireLock(TLOCK_OWS);
  if (!gbCurlInitialized &&
      curl_global_init(CURL_GLOBAL_ALL) != 0) {
    msReleaseLock(TLOCK_OWS);
    msSetError(MS_HTTPERR, "Libcurl initialization failed.",
               "msHTTPInit()");
    return MS_FAILURE;
  }

  gbCurlInitialized = MS_TRUE;

  msReleaseLock(TLOCK_OWS);
  return MS_SUCCESS;
}
示例#20
0
int msTimeSetup() {
  if(!ms_time_inited) {
    msAcquireLock(TLOCK_TIME);
    if(!ms_time_inited) {
      int i;
      for(i=0;i<MS_NUMTIMEFORMATS;i++) {
        ms_timeFormats[i].regex = msSmallMalloc(sizeof(ms_regex_t));
        if(0!=ms_regcomp(ms_timeFormats[i].regex, ms_timeFormats[i].pattern, MS_REG_EXTENDED|MS_REG_NOSUB)) {
          msSetError(MS_REGEXERR, "Failed to compile expression (%s).", "msTimeSetup()", ms_timeFormats[i].pattern);
          return MS_FAILURE;
          /* TODO: free already inited regexes */
        }
      }
      ms_limited_pattern = (int *)msSmallMalloc(sizeof(int)*MS_NUMTIMEFORMATS);
      ms_num_limited_pattern = 0;
      ms_time_inited = 1;
    }
    msReleaseLock(TLOCK_TIME);
  }
  return MS_SUCCESS;
}
示例#21
0
int msUVRASTERLayerGetExtent(layerObj *layer, rectObj *extent)

{
  char szPath[MS_MAXPATHLEN];
  mapObj *map = layer->map;
  double adfGeoTransform[6];
  int nXSize, nYSize;
  GDALDatasetH hDS;
  shapefileObj *tileshpfile;
  int tilelayerindex = -1;
  CPLErr eErr = CE_Failure;
  char *decrypted_path;

  if( (!layer->data || strlen(layer->data) == 0)
      && layer->tileindex == NULL) {
    /* should we be issuing a specific error about not supporting
       extents for tileindexed raster layers? */
    return MS_FAILURE;
  }

  if( map == NULL )
    return MS_FAILURE;

  /* If the layer use a tileindex, return the extent of the tileindex shapefile/referenced layer */
  if (layer->tileindex) {
    tilelayerindex = msGetLayerIndex(map, layer->tileindex);
    if(tilelayerindex != -1) /* does the tileindex reference another layer */
      return msLayerGetExtent(GET_LAYER(map, tilelayerindex), extent);
    else {
      tileshpfile = (shapefileObj *) malloc(sizeof(shapefileObj));
      MS_CHECK_ALLOC(tileshpfile, sizeof(shapefileObj), MS_FAILURE);

      if(msShapefileOpen(tileshpfile, "rb", msBuildPath3(szPath, map->mappath, map->shapepath, layer->tileindex), MS_TRUE) == -1)
        if(msShapefileOpen(tileshpfile, "rb", msBuildPath(szPath, map->mappath, layer->tileindex), MS_TRUE) == -1)
          return MS_FAILURE;

      *extent = tileshpfile->bounds;
      msShapefileClose(tileshpfile);
      free(tileshpfile);
      return MS_SUCCESS;
    }
  }

  msTryBuildPath3(szPath, map->mappath, map->shapepath, layer->data);
  decrypted_path = msDecryptStringTokens( map, szPath );

  msAcquireLock( TLOCK_GDAL );
  if( decrypted_path ) {
    hDS = GDALOpen(decrypted_path, GA_ReadOnly );
    msFree( decrypted_path );
  } else
    hDS = NULL;

  if( hDS != NULL ) {
    nXSize = GDALGetRasterXSize( hDS );
    nYSize = GDALGetRasterYSize( hDS );
    eErr = GDALGetGeoTransform( hDS, adfGeoTransform );

    GDALClose( hDS );
  }

  msReleaseLock( TLOCK_GDAL );

  if( hDS == NULL || eErr != CE_None ) {
    return MS_FAILURE;
  }

  /* If this appears to be an ungeoreferenced raster than flip it for
     mapservers purposes. */
  if( adfGeoTransform[5] == 1.0 && adfGeoTransform[3] == 0.0 ) {
    adfGeoTransform[5] = -1.0;
    adfGeoTransform[3] = nYSize;
  }

  extent->minx = adfGeoTransform[0];
  extent->maxy = adfGeoTransform[3];

  extent->maxx = adfGeoTransform[0] + nXSize * adfGeoTransform[1];
  extent->miny = adfGeoTransform[3] + nYSize * adfGeoTransform[5];

  return MS_SUCCESS;
}
示例#22
0
int msProjectPoint(projectionObj *in, projectionObj *out, pointObj *point)
{
#ifdef USE_PROJ
  projUV p;
  int	 error;

  if( in && in->gt.need_geotransform )
  {
      double x_out, y_out;

      x_out = in->gt.geotransform[0]
          + in->gt.geotransform[1] * point->x 
          + in->gt.geotransform[2] * point->y;
      y_out = in->gt.geotransform[3]
          + in->gt.geotransform[4] * point->x 
          + in->gt.geotransform[5] * point->y;

      point->x = x_out;
      point->y = y_out;
  }

/* -------------------------------------------------------------------- */
/*      If the source and destination are simple and equal, then do     */
/*      nothing.                                                        */
/* -------------------------------------------------------------------- */
  if( in && in->numargs == 1 && out && out->numargs == 1
      && strcmp(in->args[0],out->args[0]) == 0 )
  {
      /* do nothing, no transformation required */
  }

/* -------------------------------------------------------------------- */
/*      If we have a fully defined input coordinate system and          */
/*      output coordinate system, then we will use pj_transform.        */
/* -------------------------------------------------------------------- */
  else if( in && in->proj && out && out->proj )
  {
      double	z = 0.0;

      if( pj_is_latlong(in->proj) )
      {
          point->x *= DEG_TO_RAD;
          point->y *= DEG_TO_RAD;
      }

      msAcquireLock( TLOCK_PROJ );
      error = pj_transform( in->proj, out->proj, 1, 0, 
                            &(point->x), &(point->y), &z );
      msReleaseLock( TLOCK_PROJ );

      if( error || point->x == HUGE_VAL || point->y == HUGE_VAL )
          return MS_FAILURE;

      if( pj_is_latlong(out->proj) )
      {
          point->x *= RAD_TO_DEG;
          point->y *= RAD_TO_DEG;
      }
  }

/* -------------------------------------------------------------------- */
/*      Otherwise we fallback to using pj_fwd() or pj_inv() and         */
/*      assuming that the NULL projectionObj is supposed to be          */
/*      lat/long in the same datum as the other projectionObj.  This    */
/*      is essentially a backwards compatibility mode.                  */
/* -------------------------------------------------------------------- */
  else
  {
      /* nothing to do if the other coordinate system is also lat/long */
      if( in == NULL && out != NULL && pj_is_latlong(out->proj) )
          return MS_SUCCESS;
      if( out == NULL && in != NULL && pj_is_latlong(in->proj) )
          return MS_SUCCESS;

      p.u = point->x;
      p.v = point->y;

      if(in==NULL || in->proj==NULL) { /* input coordinates are lat/lon */
          p.u *= DEG_TO_RAD; /* convert to radians */
          p.v *= DEG_TO_RAD;  
          p = pj_fwd(p, out->proj);
      } else {
          if(out==NULL || out->proj==NULL) { /* output coordinates are lat/lon */
              p = pj_inv(p, in->proj);
              p.u *= RAD_TO_DEG; /* convert to decimal degrees */
              p.v *= RAD_TO_DEG;
          } else { /* need to go from one projection to another */
              p = pj_inv(p, in->proj);
              p = pj_fwd(p, out->proj);
          }
      }

      if( p.u == HUGE_VAL || p.v == HUGE_VAL )
          return MS_FAILURE;

      point->x = p.u;
      point->y = p.v;
  }

  if( out && out->gt.need_geotransform )
  {
      double x_out, y_out;

      x_out = out->gt.invgeotransform[0]
          + out->gt.invgeotransform[1] * point->x 
          + out->gt.invgeotransform[2] * point->y;
      y_out = out->gt.invgeotransform[3]
          + out->gt.invgeotransform[4] * point->x 
          + out->gt.invgeotransform[5] * point->y;

      point->x = x_out;
      point->y = y_out;
  }

  return(MS_SUCCESS);
#else
  msSetError(MS_PROJERR, "Projection support is not available.", "msProjectPoint()");
  return(MS_FAILURE);
#endif
}
示例#23
0
文件: mappool.c 项目: EOX-A/mapserver
void msConnPoolRegister( layerObj *layer,
                         void *conn_handle,
                         void (*close_func)( void * ) )

{
  const char *close_connection = NULL;
  connectionObj *conn = NULL;

  if( layer->debug )
    msDebug( "msConnPoolRegister(%s,%s,%p)\n",
             layer->name, layer->connection, conn_handle );

  /* -------------------------------------------------------------------- */
  /*      We can't meaningful keep a connection with no connection or     */
  /*      connection type string on the layer.                            */
  /* -------------------------------------------------------------------- */
  if( layer->connection == NULL ) {
    if( layer->tileindex != NULL
        && layer->connectiontype == MS_OGR ) {
      /* this is ok, no need to make a fuss */
    } else {
      msDebug( "%s: Missing CONNECTION on layer %s.\n",
               "msConnPoolRegister()",
               layer->name );

      msSetError( MS_MISCERR,
                  "Missing CONNECTION on layer %s.",
                  "msConnPoolRegister()",
                  layer->name );
    }
    return;
  }

  /* -------------------------------------------------------------------- */
  /*      Grow the array of connection information objects if needed.     */
  /* -------------------------------------------------------------------- */
  msAcquireLock( TLOCK_POOL );

  if( connectionCount == connectionMax ) {
    connectionMax += 10;
    connections = (connectionObj *)
                  realloc(connections,
                          sizeof(connectionObj) * connectionMax );
    if( connections == NULL ) {
      msSetError(MS_MEMERR, NULL, "msConnPoolRegister()");
      msReleaseLock( TLOCK_POOL );
      return;
    }
  }

  /* -------------------------------------------------------------------- */
  /*      Set the new connection information.                             */
  /* -------------------------------------------------------------------- */
  conn = connections + connectionCount;

  connectionCount++;

  conn->connectiontype = layer->connectiontype;
  conn->connection = msStrdup( layer->connection );
  conn->close = close_func;
  conn->ref_count = 1;
  conn->thread_id = msGetThreadId();
  conn->last_used = time(NULL);
  conn->conn_handle = conn_handle;
  conn->debug = layer->debug;

  /* -------------------------------------------------------------------- */
  /*      Categorize the connection handling information.                 */
  /* -------------------------------------------------------------------- */
  close_connection =
    msLayerGetProcessingKey( layer, "CLOSE_CONNECTION" );

  if( close_connection == NULL )
    close_connection = "NORMAL";

  if( strcasecmp(close_connection,"NORMAL") == 0 )
    conn->lifespan = MS_LIFE_ZEROREF;
  else if( strcasecmp(close_connection,"DEFER") == 0 )
    conn->lifespan = MS_LIFE_FOREVER;
  else if( strcasecmp(close_connection,"ALWAYS") == 0 )
    conn->lifespan = MS_LIFE_SINGLE;
  else {
    msDebug("msConnPoolRegister(): "
            "Unrecognised CLOSE_CONNECTION value '%s'\n",
            close_connection );

    msSetError( MS_MISCERR, "Unrecognised CLOSE_CONNECTION value '%s'",
                "msConnPoolRegister()",
                close_connection );
    conn->lifespan = MS_LIFE_ZEROREF;
  }

  msReleaseLock( TLOCK_POOL );
}
示例#24
0
int updateMap(mapservObj *mapserv, mapObj *map) {
  int i, j;

  if(!msLookupHashTable(&(map->web.validation), "immutable")) {
    /* check for any %variable% substitutions here, also do any map_ changes, we do this here so WMS/WFS  */
    /* services can take advantage of these "vendor specific" extensions */
    for(i=0; i<mapserv->request->NumParams; i++) {
      /*
       ** a few CGI variables should be skipped altogether
       **
       ** qstring: there is separate per layer validation for attribute queries and the substitution checks
       **          below conflict with that so we avoid it here
       */
      if(strncasecmp(mapserv->request->ParamNames[i],"qstring",7) == 0) continue;

      /* check to see if there are any additions to the mapfile */
      if(strncasecmp(mapserv->request->ParamNames[i],"map_",4) == 0 || strncasecmp(mapserv->request->ParamNames[i],"map.",4) == 0) {
        msAcquireLock( TLOCK_PARSER );
        if(msUpdateMapFromURL(map, mapserv->request->ParamNames[i], mapserv->request->ParamValues[i]) != MS_SUCCESS) {
          msReleaseLock( TLOCK_PARSER );
          return MS_FAILURE;
        }
        msReleaseLock( TLOCK_PARSER );

        continue;
      }

      if(strncasecmp(mapserv->request->ParamNames[i],"classgroup",10) == 0) { /* #4207 */
        for(j=0; j<map->numlayers; j++) {
          setClassGroup(GET_LAYER(map, j), mapserv->request->ParamValues[i]);
        }
        continue;
      }
    }

    msApplySubstitutions(map, mapserv->request->ParamNames, mapserv->request->ParamValues, mapserv->request->NumParams);
    msApplyDefaultSubstitutions(map);

    /* check to see if a ogc map context is passed as argument. if there */
    /* is one load it */

    for(i=0; i<mapserv->request->NumParams; i++) {
      if(strcasecmp(mapserv->request->ParamNames[i],"context") == 0) {
        if(mapserv->request->ParamValues[i] && strlen(mapserv->request->ParamValues[i]) > 0) {
          if(strncasecmp(mapserv->request->ParamValues[i],"http",4) == 0) {
            if(msGetConfigOption(map, "CGI_CONTEXT_URL"))
              msLoadMapContextURL(map, mapserv->request->ParamValues[i], MS_FALSE);
          } else
            msLoadMapContext(map, mapserv->request->ParamValues[i], MS_FALSE);
        }
      }
    }
  }

  /*
   * RFC-42 HTTP Cookie Forwarding
   * Here we set the http_cookie_data metadata to handle the
   * HTTP Cookie Forwarding. The content of this metadata is the cookie
   * content. In the future, this metadata will probably be replaced
   * by an object that is part of the mapObject that would contain
   * information on the application status (such as cookie).
   */
  if( mapserv->request->httpcookiedata != NULL ) {
    msInsertHashTable( &(map->web.metadata), "http_cookie_data",
                       mapserv->request->httpcookiedata );
  }

  return MS_SUCCESS;
}
示例#25
0
int msSaveImageGDAL( mapObj *map, imageObj *image, char *filename )

{
  int  bFileIsTemporary = MS_FALSE;
  GDALDatasetH hMemDS, hOutputDS;
  GDALDriverH  hMemDriver, hOutputDriver;
  int          nBands = 1;
  int          iLine;
  GByte       *pabyAlphaLine = NULL;
  char        **papszOptions = NULL;
  outputFormatObj *format = image->format;
  rasterBufferObj rb;
  GDALDataType eDataType = GDT_Byte;
  int bUseXmp = MS_FALSE;

  msGDALInitialize();
  memset(&rb,0,sizeof(rasterBufferObj));

#ifdef USE_EXEMPI
  if( map != NULL ) {
    bUseXmp = msXmpPresent(map);
  }
#endif


  /* -------------------------------------------------------------------- */
  /*      Identify the proposed output driver.                            */
  /* -------------------------------------------------------------------- */
  msAcquireLock( TLOCK_GDAL );
  hOutputDriver = GDALGetDriverByName( format->driver+5 );
  if( hOutputDriver == NULL ) {
    msReleaseLock( TLOCK_GDAL );
    msSetError( MS_MISCERR, "Failed to find %s driver.",
                "msSaveImageGDAL()", format->driver+5 );
    return MS_FAILURE;
  }

  /* -------------------------------------------------------------------- */
  /*      We will need to write the output to a temporary file and        */
  /*      then stream to stdout if no filename is passed.  If the         */
  /*      driver supports virtualio then we hold the temporary file in    */
  /*      memory, otherwise we try to put it in a reasonable temporary    */
  /*      file location.                                                  */
  /* -------------------------------------------------------------------- */
  if( filename == NULL ) {
    const char *pszExtension = format->extension;
    if( pszExtension == NULL )
      pszExtension = "img.tmp";

    if( bUseXmp == MS_FALSE && GDALGetMetadataItem( hOutputDriver, GDAL_DCAP_VIRTUALIO, NULL )
        != NULL ) {
      CleanVSIDir( "/vsimem/msout" );
      filename = msTmpFile(map, NULL, "/vsimem/msout/", pszExtension );
    }

    if( filename == NULL && map != NULL)
      filename = msTmpFile(map, map->mappath,NULL,pszExtension);
    else if( filename == NULL ) {
      filename = msTmpFile(map, NULL, NULL, pszExtension );
    }

    bFileIsTemporary = MS_TRUE;
  }

  /* -------------------------------------------------------------------- */
  /*      Establish the characteristics of our memory, and final          */
  /*      dataset.                                                        */
  /* -------------------------------------------------------------------- */

  if( format->imagemode == MS_IMAGEMODE_RGB ) {
    nBands = 3;
    assert( MS_RENDERER_PLUGIN(format) && format->vtable->supports_pixel_buffer );
    format->vtable->getRasterBufferHandle(image,&rb);
  } else if( format->imagemode == MS_IMAGEMODE_RGBA ) {
    pabyAlphaLine = (GByte *) calloc(image->width,1);
    if (pabyAlphaLine == NULL) {
      msReleaseLock( TLOCK_GDAL );
      msSetError( MS_MEMERR, "Out of memory allocating %u bytes.\n", "msSaveImageGDAL()", image->width);
      return MS_FAILURE;
    }
    nBands = 4;
    assert( MS_RENDERER_PLUGIN(format) && format->vtable->supports_pixel_buffer );
    format->vtable->getRasterBufferHandle(image,&rb);
  } else if( format->imagemode == MS_IMAGEMODE_INT16 ) {
    nBands = format->bands;
    eDataType = GDT_Int16;
  } else if( format->imagemode == MS_IMAGEMODE_FLOAT32 ) {
    nBands = format->bands;
    eDataType = GDT_Float32;
  } else if( format->imagemode == MS_IMAGEMODE_BYTE ) {
    nBands = format->bands;
    eDataType = GDT_Byte;
  } else {
#ifdef USE_GD
    assert( format->imagemode == MS_IMAGEMODE_PC256
            && format->renderer == MS_RENDER_WITH_GD );
#else
    {
      msReleaseLock( TLOCK_GDAL );
      msSetError( MS_MEMERR, "GD not compiled in. This is a bug.", "msSaveImageGDAL()");
      return MS_FAILURE;
    }
#endif

  }

  /* -------------------------------------------------------------------- */
  /*      Create a memory dataset which we can use as a source for a      */
  /*      CreateCopy().                                                   */
  /* -------------------------------------------------------------------- */
  hMemDriver = GDALGetDriverByName( "MEM" );
  if( hMemDriver == NULL ) {
    msReleaseLock( TLOCK_GDAL );
    msSetError( MS_MISCERR, "Failed to find MEM driver.",
                "msSaveImageGDAL()" );
    return MS_FAILURE;
  }

  hMemDS = GDALCreate( hMemDriver, "msSaveImageGDAL_temp",
                       image->width, image->height, nBands,
                       eDataType, NULL );
  if( hMemDS == NULL ) {
    msReleaseLock( TLOCK_GDAL );
    msSetError( MS_MISCERR, "Failed to create MEM dataset.",
                "msSaveImageGDAL()" );
    return MS_FAILURE;
  }

  /* -------------------------------------------------------------------- */
  /*      Copy the gd image into the memory dataset.                      */
  /* -------------------------------------------------------------------- */
  for( iLine = 0; iLine < image->height; iLine++ ) {
    int iBand;

    for( iBand = 0; iBand < nBands; iBand++ ) {
      GDALRasterBandH hBand = GDALGetRasterBand( hMemDS, iBand+1 );

      if( format->imagemode == MS_IMAGEMODE_INT16 ) {
        GDALRasterIO( hBand, GF_Write, 0, iLine, image->width, 1,
                      image->img.raw_16bit + iLine * image->width
                      + iBand * image->width * image->height,
                      image->width, 1, GDT_Int16, 2, 0 );

      } else if( format->imagemode == MS_IMAGEMODE_FLOAT32 ) {
        GDALRasterIO( hBand, GF_Write, 0, iLine, image->width, 1,
                      image->img.raw_float + iLine * image->width
                      + iBand * image->width * image->height,
                      image->width, 1, GDT_Float32, 4, 0 );
      } else if( format->imagemode == MS_IMAGEMODE_BYTE ) {
        GDALRasterIO( hBand, GF_Write, 0, iLine, image->width, 1,
                      image->img.raw_byte + iLine * image->width
                      + iBand * image->width * image->height,
                      image->width, 1, GDT_Byte, 1, 0 );
      }
#ifdef USE_GD
      else if(format->renderer == MS_RENDER_WITH_GD) {
        gdImagePtr img = (gdImagePtr)image->img.plugin;
        GDALRasterIO( hBand, GF_Write, 0, iLine, image->width, 1,
                      img->pixels[iLine],
                      image->width, 1, GDT_Byte, 0, 0 );
      }
#endif
      else {
        GByte *pabyData;
        unsigned char *pixptr = NULL;
        assert( rb.type == MS_BUFFER_BYTE_RGBA );
        switch(iBand) {
          case 0:
            pixptr = rb.data.rgba.r;
            break;
          case 1:
            pixptr = rb.data.rgba.g;
            break;
          case 2:
            pixptr = rb.data.rgba.b;
            break;
          case 3:
            pixptr = rb.data.rgba.a;
            break;
        }
        assert(pixptr);
        if( pixptr == NULL ) {
          msReleaseLock( TLOCK_GDAL );
          msSetError( MS_MISCERR, "Missing RGB or A buffer.\n",
                      "msSaveImageGDAL()" );
          return MS_FAILURE;
        }

        pabyData = (GByte *)(pixptr + iLine*rb.data.rgba.row_step);

        if( rb.data.rgba.a == NULL || iBand == 3 ) {
          GDALRasterIO( hBand, GF_Write, 0, iLine, image->width, 1,
                        pabyData, image->width, 1, GDT_Byte,
                        rb.data.rgba.pixel_step, 0 );
        } else { /* We need to un-pre-multiple RGB by alpha. */
          GByte *pabyUPM = (GByte*) malloc(image->width);
          GByte *pabyAlpha= (GByte *)(rb.data.rgba.a + iLine*rb.data.rgba.row_step);
          int i;

          for( i = 0; i < image->width; i++ ) {
            int alpha = pabyAlpha[i*rb.data.rgba.pixel_step];

            if( alpha == 0 )
              pabyUPM[i] = 0;
            else {
              int result = (pabyData[i*rb.data.rgba.pixel_step] * 255) / alpha;

              if( result > 255 )
                result = 255;

              pabyUPM[i] = result;
            }
          }

          GDALRasterIO( hBand, GF_Write, 0, iLine, image->width, 1,
                        pabyUPM, image->width, 1, GDT_Byte, 1, 0 );
          free( pabyUPM );
        }
      }
    }
  }

  if( pabyAlphaLine != NULL )
    free( pabyAlphaLine );

  /* -------------------------------------------------------------------- */
  /*      Attach the palette if appropriate.                              */
  /* -------------------------------------------------------------------- */
#ifdef USE_GD
  if( format->renderer == MS_RENDER_WITH_GD ) {
    GDALColorEntry sEntry;
    int  iColor;
    GDALColorTableH hCT;
    gdImagePtr img = (gdImagePtr)image->img.plugin;
    hCT = GDALCreateColorTable( GPI_RGB );

    for( iColor = 0; iColor < img->colorsTotal; iColor++ ) {
      sEntry.c1 = img->red[iColor];
      sEntry.c2 = img->green[iColor];
      sEntry.c3 = img->blue[iColor];

      if( iColor == gdImageGetTransparent( img ) )
        sEntry.c4 = 0;
      else if( iColor == 0
               && gdImageGetTransparent( img ) == -1
               && format->transparent )
        sEntry.c4 = 0;
      else
        sEntry.c4 = 255;

      GDALSetColorEntry( hCT, iColor, &sEntry );
    }

    GDALSetRasterColorTable( GDALGetRasterBand( hMemDS, 1 ), hCT );

    GDALDestroyColorTable( hCT );
  } else
#endif
    if( format->imagemode == MS_IMAGEMODE_RGB ) {
      GDALSetRasterColorInterpretation(
        GDALGetRasterBand( hMemDS, 1 ), GCI_RedBand );
      GDALSetRasterColorInterpretation(
        GDALGetRasterBand( hMemDS, 2 ), GCI_GreenBand );
      GDALSetRasterColorInterpretation(
        GDALGetRasterBand( hMemDS, 3 ), GCI_BlueBand );
    } else if( format->imagemode == MS_IMAGEMODE_RGBA ) {
      GDALSetRasterColorInterpretation(
        GDALGetRasterBand( hMemDS, 1 ), GCI_RedBand );
      GDALSetRasterColorInterpretation(
        GDALGetRasterBand( hMemDS, 2 ), GCI_GreenBand );
      GDALSetRasterColorInterpretation(
        GDALGetRasterBand( hMemDS, 3 ), GCI_BlueBand );
      GDALSetRasterColorInterpretation(
        GDALGetRasterBand( hMemDS, 4 ), GCI_AlphaBand );
    }

  /* -------------------------------------------------------------------- */
  /*      Assign the projection and coordinate system to the memory       */
  /*      dataset.                                                        */
  /* -------------------------------------------------------------------- */

  if( map != NULL ) {
    char *pszWKT;

    GDALSetGeoTransform( hMemDS, map->gt.geotransform );

    pszWKT = msProjectionObj2OGCWKT( &(map->projection) );
    if( pszWKT != NULL ) {
      GDALSetProjection( hMemDS, pszWKT );
      msFree( pszWKT );
    }
  }

  /* -------------------------------------------------------------------- */
  /*      Possibly assign a nodata value.                                 */
  /* -------------------------------------------------------------------- */
  if( msGetOutputFormatOption(format,"NULLVALUE",NULL) != NULL ) {
    int iBand;
    const char *nullvalue = msGetOutputFormatOption(format,
                            "NULLVALUE",NULL);

    for( iBand = 0; iBand < nBands; iBand++ ) {
      GDALRasterBandH hBand = GDALGetRasterBand( hMemDS, iBand+1 );
      GDALSetRasterNoDataValue( hBand, atof(nullvalue) );
    }
  }

  /* -------------------------------------------------------------------- */
  /*  Try to save resolution in the output file.                          */
  /* -------------------------------------------------------------------- */
  if( image->resolution > 0 ) {
    char res[30];

    sprintf( res, "%lf", image->resolution );
    GDALSetMetadataItem( hMemDS, "TIFFTAG_XRESOLUTION", res, NULL );
    GDALSetMetadataItem( hMemDS, "TIFFTAG_YRESOLUTION", res, NULL );
    GDALSetMetadataItem( hMemDS, "TIFFTAG_RESOLUTIONUNIT", "2", NULL );
  }

  /* -------------------------------------------------------------------- */
  /*      Create a disk image in the selected output format from the      */
  /*      memory image.                                                   */
  /* -------------------------------------------------------------------- */
  papszOptions = (char**)calloc(sizeof(char *),(format->numformatoptions+1));
  if (papszOptions == NULL) {
    msReleaseLock( TLOCK_GDAL );
    msSetError( MS_MEMERR, "Out of memory allocating %u bytes.\n", "msSaveImageGDAL()",
                (unsigned int)(sizeof(char *)*(format->numformatoptions+1)));
    return MS_FAILURE;
  }

  memcpy( papszOptions, format->formatoptions,
          sizeof(char *) * format->numformatoptions );

  hOutputDS = GDALCreateCopy( hOutputDriver, filename, hMemDS, FALSE,
                              papszOptions, NULL, NULL );

  free( papszOptions );

  if( hOutputDS == NULL ) {
    GDALClose( hMemDS );
    msReleaseLock( TLOCK_GDAL );
    msSetError( MS_MISCERR, "Failed to create output %s file.\n%s",
                "msSaveImageGDAL()", format->driver+5,
                CPLGetLastErrorMsg() );
    return MS_FAILURE;
  }

  /* closing the memory DS also frees all associated resources. */
  GDALClose( hMemDS );

  GDALClose( hOutputDS );
  msReleaseLock( TLOCK_GDAL );


  /* -------------------------------------------------------------------- */
  /*      Are we writing license info into the image?                     */
  /*      If so, add it to the temp file on disk now.                     */
  /* -------------------------------------------------------------------- */
#ifdef USE_EXEMPI
  if ( bUseXmp == MS_TRUE ) {
    if( msXmpWrite(map, filename) == MS_FAILURE ) {
      /* Something bad happened. */
      msSetError( MS_MISCERR, "XMP write to %s failed.\n",
                  "msSaveImageGDAL()", filename);
      return MS_FAILURE;
    }
  }
#endif

  /* -------------------------------------------------------------------- */
  /*      Is this supposed to be a temporary file?  If so, stream to      */
  /*      stdout and delete the file.                                     */
  /* -------------------------------------------------------------------- */
  if( bFileIsTemporary ) {
    FILE *fp;
    unsigned char block[4000];
    int bytes_read;

    if( msIO_needBinaryStdout() == MS_FAILURE )
      return MS_FAILURE;

    /* We aren't sure how far back GDAL exports the VSI*L API, so
       we only use it if we suspect we need it.  But we do need it if
       holding temporary file in memory. */
    fp = VSIFOpenL( filename, "rb" );
    if( fp == NULL ) {
      msSetError( MS_MISCERR,
                  "Failed to open %s for streaming to stdout.",
                  "msSaveImageGDAL()", filename );
      return MS_FAILURE;
    }

    while( (bytes_read = VSIFReadL(block, 1, sizeof(block), fp)) > 0 )
      msIO_fwrite( block, 1, bytes_read, stdout );

    VSIFCloseL( fp );

    VSIUnlink( filename );
    CleanVSIDir( "/vsimem/msout" );

    free( filename );
  }

  return MS_SUCCESS;
}
示例#26
0
int msContourLayerOpen(layerObj *layer)
{
  char *decrypted_path;
  char szPath[MS_MAXPATHLEN];
  contourLayerInfo *clinfo;

  if (layer->debug)
    msDebug("Entering msContourLayerOpen().\n");

  /* If we don't have info, initialize an empty one now */
  if (layer->layerinfo == NULL)
    msContourLayerInfoInitialize(layer);

  clinfo = (contourLayerInfo *) layer->layerinfo;
  if (layer->data == NULL && layer->tileindex == NULL ) {
    msSetError(MS_MISCERR,
               "Layer %s has neither DATA nor TILEINDEX defined.",
               "msContourLayerOpen()",
               layer->name);
    return MS_FAILURE;
  }

  if( layer->tileindex != NULL )
  {
      char szTilename[MS_MAXPATHLEN];
      int status;
      int tilelayerindex, tileitemindex, tilesrsindex;
      rectObj searchrect;
      layerObj* tlp;
      shapeObj tshp;
      char tilesrsname[1];

      msInitShape(&tshp);
      searchrect = layer->map->extent;

      status = msDrawRasterSetupTileLayer(layer->map, layer,
                                 &searchrect, MS_FALSE,
                                 &tilelayerindex,
                                 &tileitemindex,
                                 &tilesrsindex,
                                 &tlp);
      if( status == MS_FAILURE )
      {
          return MS_FAILURE;
      }

      status = msDrawRasterIterateTileIndex(layer, tlp, &tshp,
                                            tileitemindex, -1,
                                            szTilename, sizeof(szTilename),
                                            tilesrsname, sizeof(tilesrsname));
      if( status == MS_FAILURE || status == MS_DONE ) {
          if( status == MS_DONE )
          {
              if (layer->debug)
                msDebug("No raster matching filter.\n");
          }
          msDrawRasterCleanupTileLayer(tlp, tilelayerindex);
          return MS_FAILURE;
      }

      msDrawRasterCleanupTileLayer(tlp, tilelayerindex);

      msDrawRasterBuildRasterPath(layer->map, layer, szTilename, szPath);
      decrypted_path = msStrdup(szPath);

      /* Cancel the time filter that might have been set on ours in case of */
      /* a inline tileindex */
      msFreeExpression(&layer->filter);
      msInitExpression(&layer->filter);
  }
  else
  {
      msTryBuildPath3(szPath, layer->map->mappath, layer->map->shapepath, layer->data);
      decrypted_path = msDecryptStringTokens(layer->map, szPath);
  }
  
  GDALAllRegister();

  /* Open the original Dataset */

  msAcquireLock(TLOCK_GDAL);
  if (decrypted_path) {
    clinfo->hOrigDS = GDALOpen(decrypted_path, GA_ReadOnly);
    msFree(decrypted_path);
  } else
    clinfo->hOrigDS = NULL;

  msReleaseLock(TLOCK_GDAL);

  if (clinfo->hOrigDS == NULL) {
    msSetError(MS_IMGERR,
               "Unable to open GDAL dataset.",
               "msContourLayerOpen()");
    return MS_FAILURE;
  }
  
  /* Open the raster source */
  if (msContourLayerReadRaster(layer, layer->map->extent) != MS_SUCCESS)
    return MS_FAILURE;

  /* Generate Contour Dataset */
  if (msContourLayerGenerateContour(layer) != MS_SUCCESS)
    return MS_FAILURE;

  if (clinfo->hDS) {
    GDALClose(clinfo->hDS);
    clinfo->hDS = NULL;  
    free(clinfo->buffer);
  }

  /* Open our virtual ogr layer */
  if (clinfo->hOGRDS && (msLayerOpen(&clinfo->ogrLayer) != MS_SUCCESS))
    return MS_FAILURE;
  
  return MS_SUCCESS;
}