pihmLIBS/addFID.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <iomanip>
00004 #include "shapefil.h"
00005 
00006 using namespace std;
00007 
00008 void addFID(const char *dbfFileName){
00009         DBFHandle dbf = DBFOpen(dbfFileName, "rb");
00010         cout<<"Copying Existing Records...";
00011         int temp; 
00012         int fieldCount = DBFGetFieldCount(dbf);
00013         int recordCount = DBFGetRecordCount(dbf);
00014 
00015         DBFHandle newdbf = DBFCreate("temp.dbf");
00016         
00017         int width[1], decimals[1];
00018         char fieldName[20];
00019         DBFFieldType fieldType;
00020         for (int i=0; i<fieldCount; i++){
00021                 fieldType = DBFGetFieldInfo(dbf, i, fieldName, width, decimals);
00022                 temp = DBFAddField(newdbf, fieldName, fieldType, width[0], decimals[0]);
00023         }
00024 
00025         temp = DBFAddField(newdbf, "FID", FTInteger, 6, 0);
00026 
00027         int iValue;
00028         double fValue;
00029         const char * cValue;
00030         for (int i=0; i<recordCount; i++){
00031                 for (int j=0; j<fieldCount; j++){
00032                         fieldType = DBFGetFieldInfo(dbf, j, fieldName, width, decimals);
00033                         if(fieldType == FTString){
00034                                 cValue = DBFReadStringAttribute(dbf, i, j);
00035                                 temp = DBFWriteStringAttribute(newdbf, i, j, cValue);
00036                         }
00037                         else if(fieldType == FTInteger){
00038                                 iValue = DBFReadIntegerAttribute(dbf, i, j);
00039                                 temp = DBFWriteIntegerAttribute(newdbf, i, j, iValue);
00040                         }
00041                         else if(fieldType == FTDouble){
00042                                 fValue = DBFReadDoubleAttribute(dbf, i, j);
00043                                 temp = DBFWriteDoubleAttribute(newdbf, i, j, fValue);
00044                         }
00045                 }
00046                 temp = DBFWriteIntegerAttribute(newdbf, i, fieldCount, i+1);
00047         }
00048         cout<<"Done!\n\n";
00049         cout<<"Adding New Field FID...";
00050         cout<<"Done!\n\n";
00051         DBFClose(dbf);
00052         DBFClose(newdbf);
00053 
00054         cout<<"Writing to the .dbf File...";
00055         char buffer[100];
00056         ifstream infile;
00057         ofstream outfile;
00058         infile.open("temp.dbf",ios::binary|ios::in);
00059         outfile.open(dbfFileName,ios::binary|ios::out);
00060         int j=1;
00061         //cout<<"\nWriting to the .dbf file...";
00062         while(infile){
00063                 infile.read(buffer,100);
00064                 infile.seekg(j*100);j++;
00065                 outfile.write(buffer,100);
00066         }
00067         cout<<"Done!\n\n";
00068 }

Generated on Sun Aug 5 17:33:59 2007 for PIHMgis by  doxygen 1.5.2