OGRErr OGRShapeLayer::Repack() { if( !bUpdateAccess ) { CPLError( CE_Failure, CPLE_AppDefined, "The REPACK operation is not permitted on a read-only shapefile." ); return OGRERR_FAILURE; } if( hDBF == NULL ) { CPLError( CE_Failure, CPLE_NotSupported, "Attempt to repack a shapefile with no .dbf file not supported."); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* Build a list of records to be dropped. */ /* -------------------------------------------------------------------- */ int *panRecordsToDelete = (int *) CPLMalloc(sizeof(int)*(nTotalShapeCount+1)); int nDeleteCount = 0; int iShape = 0; OGRErr eErr = OGRERR_NONE; for( iShape = 0; iShape < nTotalShapeCount; iShape++ ) { if( DBFIsRecordDeleted( hDBF, iShape ) ) panRecordsToDelete[nDeleteCount++] = iShape; } panRecordsToDelete[nDeleteCount] = -1; /* -------------------------------------------------------------------- */ /* If there are no records marked for deletion, we take no */ /* action. */ /* -------------------------------------------------------------------- */ if( nDeleteCount == 0 ) { CPLFree( panRecordsToDelete ); return OGRERR_NONE; } /* -------------------------------------------------------------------- */ /* Find existing filenames with exact case (see #3293). */ /* -------------------------------------------------------------------- */ CPLString osDirname(CPLGetPath(pszFullName)); CPLString osBasename(CPLGetBasename(pszFullName)); CPLString osDBFName, osSHPName, osSHXName; char **papszCandidates = CPLReadDir( osDirname ); int i = 0; while(papszCandidates != NULL && papszCandidates[i] != NULL) { CPLString osCandidateBasename = CPLGetBasename(papszCandidates[i]); CPLString osCandidateExtension = CPLGetExtension(papszCandidates[i]); if (osCandidateBasename.compare(osBasename) == 0) { if (EQUAL(osCandidateExtension, "dbf")) osDBFName = CPLFormFilename(osDirname, papszCandidates[i], NULL); else if (EQUAL(osCandidateExtension, "shp")) osSHPName = CPLFormFilename(osDirname, papszCandidates[i], NULL); else if (EQUAL(osCandidateExtension, "shx")) osSHXName = CPLFormFilename(osDirname, papszCandidates[i], NULL); } i++; } CSLDestroy(papszCandidates); papszCandidates = NULL; if (osDBFName.size() == 0) { /* Should not happen, really */ CPLFree( panRecordsToDelete ); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* Cleanup any existing spatial index. It will become */ /* meaningless when the fids change. */ /* -------------------------------------------------------------------- */ if( CheckForQIX() ) DropSpatialIndex(); /* -------------------------------------------------------------------- */ /* Create a new dbf file, matching the old. */ /* -------------------------------------------------------------------- */ DBFHandle hNewDBF = NULL; CPLString oTempFile(CPLFormFilename(osDirname, osBasename, NULL)); oTempFile += "_packed.dbf"; hNewDBF = DBFCloneEmpty( hDBF, oTempFile ); if( hNewDBF == NULL ) { CPLFree( panRecordsToDelete ); CPLError( CE_Failure, CPLE_OpenFailed, "Failed to create temp file %s.", oTempFile.c_str() ); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* Copy over all records that are not deleted. */ /* -------------------------------------------------------------------- */ int iDestShape = 0; int iNextDeletedShape = 0; for( iShape = 0; iShape < nTotalShapeCount && eErr == OGRERR_NONE; iShape++ ) { if( panRecordsToDelete[iNextDeletedShape] == iShape ) iNextDeletedShape++; else { void *pTuple = (void *) DBFReadTuple( hDBF, iShape ); if( pTuple == NULL ) eErr = OGRERR_FAILURE; else if( !DBFWriteTuple( hNewDBF, iDestShape++, pTuple ) ) eErr = OGRERR_FAILURE; } } if( eErr != OGRERR_NONE ) { CPLFree( panRecordsToDelete ); VSIUnlink( oTempFile ); return eErr; } /* -------------------------------------------------------------------- */ /* Cleanup the old .dbf and rename the new one. */ /* -------------------------------------------------------------------- */ DBFClose( hDBF ); DBFClose( hNewDBF ); hDBF = hNewDBF = NULL; VSIUnlink( osDBFName ); if( VSIRename( oTempFile, osDBFName ) != 0 ) { CPLDebug( "Shape", "Can not rename DBF file: %s", VSIStrerror( errno ) ); CPLFree( panRecordsToDelete ); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* Now create a shapefile matching the old one. */ /* -------------------------------------------------------------------- */ if( hSHP != NULL ) { SHPHandle hNewSHP = NULL; if (osSHPName.size() == 0 || osSHXName.size() == 0) { /* Should not happen, really */ CPLFree( panRecordsToDelete ); return OGRERR_FAILURE; } oTempFile = CPLFormFilename(osDirname, osBasename, NULL); oTempFile += "_packed.shp"; hNewSHP = SHPCreate( oTempFile, hSHP->nShapeType ); if( hNewSHP == NULL ) { CPLFree( panRecordsToDelete ); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* Copy over all records that are not deleted. */ /* -------------------------------------------------------------------- */ iNextDeletedShape = 0; for( iShape = 0; iShape < nTotalShapeCount && eErr == OGRERR_NONE; iShape++ ) { if( panRecordsToDelete[iNextDeletedShape] == iShape ) iNextDeletedShape++; else { SHPObject *hObject; hObject = SHPReadObject( hSHP, iShape ); if( hObject == NULL ) eErr = OGRERR_FAILURE; else if( SHPWriteObject( hNewSHP, -1, hObject ) == -1 ) eErr = OGRERR_FAILURE; if( hObject ) SHPDestroyObject( hObject ); } } if( eErr != OGRERR_NONE ) { CPLFree( panRecordsToDelete ); VSIUnlink( CPLResetExtension( oTempFile, "shp" ) ); VSIUnlink( CPLResetExtension( oTempFile, "shx" ) ); return eErr; } /* -------------------------------------------------------------------- */ /* Cleanup the old .shp/.shx and rename the new one. */ /* -------------------------------------------------------------------- */ SHPClose( hSHP ); SHPClose( hNewSHP ); hSHP = hNewSHP = NULL; VSIUnlink( osSHPName ); VSIUnlink( osSHXName ); oTempFile = CPLResetExtension( oTempFile, "shp" ); if( VSIRename( oTempFile, osSHPName ) != 0 ) { CPLDebug( "Shape", "Can not rename SHP file: %s", VSIStrerror( errno ) ); CPLFree( panRecordsToDelete ); return OGRERR_FAILURE; } oTempFile = CPLResetExtension( oTempFile, "shx" ); if( VSIRename( oTempFile, osSHXName ) != 0 ) { CPLDebug( "Shape", "Can not rename SHX file: %s", VSIStrerror( errno ) ); CPLFree( panRecordsToDelete ); return OGRERR_FAILURE; } } CPLFree( panRecordsToDelete ); panRecordsToDelete = NULL; /* -------------------------------------------------------------------- */ /* Reopen the shapefile */ /* */ /* We do not need to reimplement OGRShapeDataSource::OpenFile() here */ /* with the fully featured error checking. */ /* If all operations above succeeded, then all necessery files are */ /* in the right place and accessible. */ /* -------------------------------------------------------------------- */ CPLAssert( NULL == hSHP ); CPLAssert( NULL == hDBF && NULL == hNewDBF ); CPLPushErrorHandler( CPLQuietErrorHandler ); const char* pszAccess = NULL; if( bUpdateAccess ) pszAccess = "r+"; else pszAccess = "r"; hSHP = SHPOpen ( CPLResetExtension( pszFullName, "shp" ) , pszAccess ); hDBF = DBFOpen ( CPLResetExtension( pszFullName, "dbf" ) , pszAccess ); CPLPopErrorHandler(); if( NULL == hSHP || NULL == hDBF ) { CPLString osMsg(CPLGetLastErrorMsg()); CPLError( CE_Failure, CPLE_OpenFailed, "%s", osMsg.c_str() ); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* Update total shape count. */ /* -------------------------------------------------------------------- */ nTotalShapeCount = hDBF->nRecords; return OGRERR_NONE; }
int main( int argc, char ** argv ) { SHPHandle old_SHP, new_SHP; DBFHandle old_DBF, new_DBF; int nShapeType, nEntities, nVertices, nParts, *panParts, i, iPart; double *padVertices, adBounds[4]; const char *pszPlus; DBFFieldType idfld_type; int idfld, nflds; char kv[257] = ""; char idfldName[120] = ""; char fldName[120] = ""; char shpFileName[120] = ""; char dbfFileName[120] = ""; double apeture[4]; char *DBFRow = NULL; int Cpan[2] = { 0,0 }; int byRing = 1; PT Centrd; SHPObject *psCShape, *cent_pt; if( argc < 3 ) { printf( "shpcentrd shp_file new_shp_file\n" ); exit( 1 ); } old_SHP = SHPOpen (argv[1], "rb" ); old_DBF = DBFOpen (argv[1], "rb"); if( old_SHP == NULL || old_DBF == NULL ) { printf( "Unable to open old files:%s\n", argv[1] ); exit( 1 ); } SHPGetInfo( old_SHP, &nEntities, &nShapeType, NULL, NULL ); new_SHP = SHPCreate ( argv[2], SHPT_POINT ); new_DBF = DBFCloneEmpty (old_DBF, argv[2]); if( new_SHP == NULL || new_DBF == NULL ) { printf( "Unable to create new files:%s\n", argv[2] ); exit( 1 ); } DBFRow = (char *) malloc ( (old_DBF->nRecordLength) + 15 ); #ifdef DEBUG printf ("ShpCentrd using shpgeo \n"); #endif for( i = 0; i < nEntities; i++ ) { int res ; psCShape = SHPReadObject( old_SHP, i ); if ( byRing == 1 ) { int ring; for ( ring = 0; ring < psCShape->nParts; ring ++ ) { SHPObject *psO; psO = SHPClone ( psCShape, ring, ring + 1 ); Centrd = SHPCentrd_2d ( psO ); cent_pt = SHPCreateSimpleObject ( SHPT_POINT, 1, (double*) &(Centrd.x), (double*) &(Centrd.y), NULL ); SHPWriteObject ( new_SHP, -1, cent_pt ); memcpy ( DBFRow, DBFReadTuple ( old_DBF, i ), old_DBF->nRecordLength ); DBFWriteTuple ( new_DBF, new_DBF->nRecords, DBFRow ); SHPDestroyObject ( cent_pt ); SHPDestroyObject ( psO ); } } else { Centrd = SHPCentrd_2d ( psCShape ); cent_pt = SHPCreateSimpleObject ( SHPT_POINT, 1, (double*) &(Centrd.x), (double*) &(Centrd.y), NULL ); SHPWriteObject ( new_SHP, -1, cent_pt ); memcpy ( DBFRow, DBFReadTuple ( old_DBF, i ), old_DBF->nRecordLength ); DBFWriteTuple ( new_DBF, new_DBF->nRecords, DBFRow ); SHPDestroyObject ( cent_pt ); } } SHPClose( old_SHP ); SHPClose( new_SHP ); DBFClose( old_DBF ); DBFClose( new_DBF ); printf ("\n"); }
int main (int argc, char *argv[]) { SHPHandle inSHP, outSHP; DBFHandle inDBF, outDBF; int len; int i; char **fieldNames; char **strOrder = 0; struct DataStruct *index; int width; int decimals; SHPObject *feat; void *tuple; if (argc < 4) { printf("USAGE: shpsort <infile> <outfile> <field[;...]> [<(ASCENDING|DESCENDING)[;...]>]\n"); exit(EXIT_FAILURE); } inSHP = SHPOpen (argv[1], "rb"); if (!inSHP) { fputs("Couldn't open shapefile for reading!\n", stderr); exit(EXIT_FAILURE); } SHPGetInfo(inSHP, &nShapes, &shpType, NULL, NULL); /* If we can open the inSHP, open its DBF */ inDBF = DBFOpen (argv[1], "rb"); if (!inDBF) { fputs("Couldn't open dbf file for reading!\n", stderr); exit(EXIT_FAILURE); } /* Parse fields and validate existence */ fieldNames = split(argv[3], ";"); if (!fieldNames) { fputs("ERROR: parsing field names!\n", stderr); exit(EXIT_FAILURE); } for (nFields = 0; fieldNames[nFields] ; nFields++) { continue; } fldIdx = malloc(sizeof *fldIdx * nFields); if (!fldIdx) { fputs("malloc failed!\n", stderr); exit(EXIT_FAILURE); } for (i = 0; i < nFields; i++) { len = (int)strlen(fieldNames[i]); while(len > 0) { --len; fieldNames[i][len] = (char)toupper((unsigned char)fieldNames[i][len]); } fldIdx[i] = DBFGetFieldIndex(inDBF, fieldNames[i]); if (fldIdx[i] < 0) { /* try "SHAPE" */ if (strcmp(fieldNames[i], "SHAPE") == 0) { fldIdx[i] = -1; } else if (strcmp(fieldNames[i], "FID") == 0) { fldIdx[i] = -2; } else { fprintf(stderr, "ERROR: field '%s' not found!\n", fieldNames[i]); exit(EXIT_FAILURE); } } } /* set up field type array */ fldType = malloc(sizeof *fldType * nFields); if (!fldType) { fputs("malloc failed!\n", stderr); exit(EXIT_FAILURE); } for (i = 0; i < nFields; i++) { if (fldIdx[i] < 0) { fldType[i] = fldIdx[i]; } else { fldType[i] = DBFGetFieldInfo(inDBF, fldIdx[i], NULL, &width, &decimals); if (fldType[i] == FTInvalid) { fputs("Unrecognized field type in dBASE file!\n", stderr); exit(EXIT_FAILURE); } } } /* set up field order array */ fldOrder = malloc(sizeof *fldOrder * nFields); if (!fldOrder) { fputs("malloc failed!\n", stderr); exit(EXIT_FAILURE); } for (i = 0; i < nFields; i++) { /* default to ascending order */ fldOrder[i] = ASCENDING; } if (argc > 4) { strOrder = split(argv[4], ";"); if (!strOrder) { fputs("ERROR: parsing fields ordering!\n", stderr); exit(EXIT_FAILURE); } for (i = 0; i < nFields && strOrder[i]; i++) { if (strcmp(strOrder[i], "DESCENDING") == 0) { fldOrder[i] = DESCENDING; } } } /* build the index */ index = build_index (inSHP, inDBF); /* Create output shapefile */ outSHP = SHPCreate(argv[2], shpType); if (!outSHP) { fprintf(stderr, "%s:%d: couldn't create output shapefile!\n", __FILE__, __LINE__); exit(EXIT_FAILURE); } /* Create output dbf */ outDBF = DBFCloneEmpty(inDBF, argv[2]); if (!outDBF) { fprintf(stderr, "%s:%d: couldn't create output dBASE file!\n", __FILE__, __LINE__); exit(EXIT_FAILURE); } /* Copy projection file, if any */ copy_related(argv[1], argv[2], ".shp", ".prj"); /* Copy metadata file, if any */ copy_related(argv[1], argv[2], ".shp", ".shp.xml"); /* Write out sorted results */ for (i = 0; i < nShapes; i++) { feat = SHPReadObject(inSHP, index[i].record); if (SHPWriteObject(outSHP, -1, feat) < 0) { fprintf(stderr, "%s:%d: error writing shapefile!\n", __FILE__, __LINE__); exit(EXIT_FAILURE); } tuple = (void *) DBFReadTuple(inDBF, index[i].record); if (DBFWriteTuple(outDBF, i, tuple) < 0) { fprintf(stderr, "%s:%d: error writing dBASE file!\n", __FILE__, __LINE__); exit(EXIT_FAILURE); } } SHPClose(inSHP); SHPClose(outSHP); DBFClose(inDBF); DBFClose(outDBF); return EXIT_SUCCESS; }
int main( int argc, char ** argv ) { SHPHandle old_SHP, new_SHP; DBFHandle old_DBF, new_DBF; int nShapeType, nEntities, nVertices, nParts, *panParts, i, iPart, j; double *padVertices, adBounds[4]; const char *pszPlus; DBFFieldType idfld_type; SHPObject *psCShape; FILE *ifp = NULL; int idfld, nflds; char kv[257] = ""; char idfldName[120] = ""; char fldName[120] = ""; char shpFileName[120] = ""; char dbfFileName[120] = ""; char prjFileName[120] = ""; char parg[1024]; double apeture[4]; int inarg, outarg; char *DBFRow = NULL; /* for testing only char *in_args[] = { "init=nad83:1002", "units=us-ft" }; char *out_args[] = { "proj=utm", "zone=16", "units=m" }; */ char *in_args[16]; char *out_args[16]; int in_argc = 0 , out_argc = 0, outf_arg; char *arglst; projPJ orig_prj, new_prj; va_list myargs, moargs; if( argc < 4) { printf( "shpproj shp_file new_shp ( -i=in_proj_file | -i=\"in_params\" | -i=geographic ) ( -o=out_info_file | -o=\"out_params\" | -o=geographic ) \n" ); exit( 1 ); } old_SHP = SHPOpen( argv[1], "rb" ); old_DBF = DBFOpen( argv[1], "rb" ); if( old_SHP == NULL || old_DBF == NULL ) { printf( "Unable to open old files:%s\n", argv[1] ); exit( 1 ); } outf_arg = 2; inarg = 0; outarg = 0; for ( i = 3; i < argc; i++ ) { if ( !strncmp ("-i=", argv[i], 3 )) inarg = i; if ( !strncmp ("-o=", argv[i], 3 )) outarg = i; } /* if shapefile has a prj component then use that else try for a file then read args as list */ if( inarg == 0 ) { strcpy( prjFileName, argv[1] ); ifp = fopen( asFileName ( prjFileName, "prj" ),"rt"); } else { ifp = fopen( asFileName ( argv[inarg] + 3, "prj" ),"rt"); } i = 0; if ( ifp ) { if( inarg == 0 ) printf ("using default file proj params from <- %s\n", asFileName ( prjFileName, "prj" ) ); else printf ("using file proj params from <- %s\n", asFileName ( argv[inarg] + 3, "prj" ) ); while( fscanf( ifp, "%s", parg) != EOF ) { in_args[i] = malloc ( strlen(parg)+1 ); strcpy ( in_args[i], parg); i++; } in_argc = i; fclose (ifp); } else { if ( inarg > 0 ) { arglst = argv[inarg] + 3; j = 0; i = 0; while ( j < strlen (arglst) ) { in_argc += sscanf ( arglst + j, "%s", parg); in_args[i] = malloc( strlen (parg)+1024); strcpy (in_args[i], parg); i++; j += strlen (parg) +1; if ( arglst[j] + 1 == 0 ) j = strlen (argv[inarg]); } } } i = 0; if ( outarg > 0 ) ifp = fopen( asFileName ( argv[outarg] + 3, "prj" ),"rt"); if ( ifp ) { while( fscanf( ifp, "%s", parg) != EOF ) { out_args[i] = malloc ( strlen(parg)); strcpy ( out_args[i], parg); i++; } out_argc = i; fclose (ifp); } else { if ( outarg > 0 ) { arglst = argv[outarg] + 3; j = 0; i = 0; while ( j < strlen (arglst) ) { out_argc += sscanf ( arglst + j, "%s", parg); out_args[i] = malloc( strlen (parg)+1); strcpy (out_args[i], parg); i++; j += strlen (parg) +1; if ( arglst[j] + 1 == 0 ) j = strlen (argv[outarg]); } } } if ( !strcmp( argv[inarg], "-i=geographic" )) in_argc = 0; if ( !strcmp( argv[outarg], "-o=geographic" )) out_argc = 0; orig_prj = SHPSetProjection ( in_argc, in_args ); new_prj = SHPSetProjection ( out_argc, out_args ); if ( !(( (!in_argc) || orig_prj) && ( (!out_argc) || new_prj) )) { fprintf (stderr, "unable to process projection, exiting...\n"); exit(1); } SHPGetInfo( old_SHP, &nEntities, &nShapeType, NULL, NULL); new_SHP = SHPCreate ( argv[outf_arg], nShapeType ); new_DBF = DBFCloneEmpty (old_DBF, argv[outf_arg]); if( new_SHP == NULL || new_DBF == NULL ) { printf( "Unable to create new files:%s\n", argv[outf_arg] ); exit( 1 ); } DBFRow = (char *) malloc ( (old_DBF->nRecordLength) + 15 ); for( i = 0; i < nEntities; i++ ) { int j; psCShape = SHPReadObject ( old_SHP, i ); SHPProject (psCShape, orig_prj, new_prj ); SHPWriteObject ( new_SHP, -1, psCShape ); SHPDestroyObject ( psCShape ); memcpy ( DBFRow, DBFReadTuple ( old_DBF, i ), old_DBF->nRecordLength ); DBFWriteTuple ( new_DBF, new_DBF->nRecords, DBFRow ); } SHPFreeProjection ( orig_prj ); SHPFreeProjection ( new_prj ); /* store projection params into prj file */ ifp = fopen( asFileName ( argv[outf_arg], "prj" ),"wt"); if ( ifp ) { if ( out_argc == 0 ) { fprintf( ifp, "proj=geographic\n" ); } else { for ( i = 0; i < out_argc; i++ ) fprintf( ifp, "%s\n", out_args[i]); } fclose (ifp); } SHPClose( old_SHP ); SHPClose( new_SHP ); DBFClose( old_DBF ); DBFClose( new_DBF ); printf ("\n"); }