/****************************************************************************** * $Id$ * * Project: MapServer * Purpose: Commandline App to build tile index for raster files. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2001, Frank Warmerdam, DM Solutions Group Inc * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "ogrsf_frmts/shape/shapefil.h" #include "gdal.h" #include "cpl_port.h" #include "cpl_conv.h" CPL_CVSID("$Id$"); /************************************************************************/ /* Usage() */ /************************************************************************/ static void Usage() { fprintf(stdout, "%s", "\n" "Usage: gdaltindex [-tileindex field_name] [-write_absolute_path] \n" " [-skip_different_projection] index_file [gdal_file]*\n" "\n" "eg.\n" " % gdaltindex doq_index.shp doq/*.tif\n" "\n" "NOTES:\n" " o The shapefile (index_file) will be created if it doesn't already exist.\n" " o The default tile index field is 'location'.\n" " o Raster filenames will be put in the file exactly as they are specified\n" " on the commandline unless the option -write_absolute_path is used.\n" " o If -skip_different_projection is specified, only files with same projection ref\n" " as files already inserted in the tileindex will be inserted.\n" " o Simple rectangular polygons are generated in the same\n" " coordinate system as the rasters.\n" ); exit(1); } /************************************************************************/ /* main() */ /************************************************************************/ int main(int argc, char *argv[]) { const char *index_filename = NULL; const char *tile_index = "location"; int i_arg, ti_field; SHPHandle hSHP; DBFHandle hDBF; int write_absolute_path = FALSE; char* current_path = NULL; int i; int nExistingFiles; int skip_different_projection = FALSE; char** existingFilesTab = NULL; int alreadyExistingProjectionRefValid = FALSE; char* alreadyExistingProjectionRef = NULL; /* Check that we are running against at least GDAL 1.4 */ /* Note to developers : if we use newer API, please change the requirement */ if (atoi(GDALVersionInfo("VERSION_NUM")) < 1400) { fprintf(stderr, "At least, GDAL >= 1.4.0 is required for this version of %s, " "which was compiled against GDAL %s\n", argv[0], GDAL_RELEASE_NAME); exit(1); } GDALAllRegister(); argc = GDALGeneralCmdLineProcessor( argc, &argv, 0 ); if( argc < 1 ) exit( -argc ); /* -------------------------------------------------------------------- */ /* Get commandline arguments other than the GDAL raster filenames. */ /* -------------------------------------------------------------------- */ for( i_arg = 1; i_arg < argc; i_arg++ ) { if( EQUAL(argv[i_arg], "--utility_version") ) { printf("%s was compiled against GDAL %s and is running against GDAL %s\n", argv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME")); return 0; } else if( strcmp(argv[i_arg],"-tileindex") == 0 ) { tile_index = argv[++i_arg]; } else if ( strcmp(argv[i_arg],"-write_absolute_path") == 0 ) { write_absolute_path = TRUE; } else if ( strcmp(argv[i_arg],"-skip_different_projection") == 0 ) { skip_different_projection = TRUE; } else if( argv[i_arg][0] == '-' ) Usage(); else if( index_filename == NULL ) { index_filename = argv[i_arg]; i_arg++; break; } } if( index_filename == NULL ) Usage(); /* -------------------------------------------------------------------- */ /* Open or create the target shapefile and DBF file. */ /* -------------------------------------------------------------------- */ hSHP = SHPOpen( index_filename, "r+" ); if( hSHP == NULL ) { printf( "Creating new index file...\n" ); hSHP = SHPCreate( index_filename, SHPT_POLYGON ); } if( hSHP == NULL ) { fprintf( stderr, "Unable to open/create shapefile `%s'.\n", index_filename ); exit(2); } hDBF = DBFOpen( index_filename, "r+" ); if( hDBF == NULL ) { hDBF = DBFCreate( index_filename ); if( hDBF == NULL ) { fprintf( stderr, "Unable to open/create DBF file `%s'.\n", index_filename ); exit(2); } DBFAddField( hDBF, tile_index, FTString, 255, 0 ); } for( ti_field = 0; ti_field < DBFGetFieldCount(hDBF); ti_field++ ) { char field_name[16]; DBFGetFieldInfo( hDBF, ti_field, field_name, NULL, NULL ); if( strcmp(field_name, tile_index) == 0 ) break; } if( ti_field == DBFGetFieldCount(hDBF) ) { fprintf( stderr, "Unable to find field `%s' in DBF file `%s'.\n", tile_index, index_filename ); exit(2); } /* Load in memory existing file names in SHP */ nExistingFiles = DBFGetRecordCount(hDBF); if (nExistingFiles) { existingFilesTab = (char**)CPLMalloc(nExistingFiles * sizeof(char*)); for(i=0;i