pihmLIBS/calDownSegment.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 calDownSegment(const char* dbfFileName){
00009         int temp;
00010 
00011         DBFHandle dbf = DBFOpen(dbfFileName, "rb");
00012         cout<<"Checking Requirent Field";
00013         int FId= DBFGetFieldIndex(dbf, "FID");
00014         if( FId == -1){ cout<<"Error: FID field does NOT exist\nCan not proceed\n"; return; }
00015         int FROM_NODE= DBFGetFieldIndex(dbf, "FROM_NODE");
00016         if( FId == -1){ cout<<"Error: FROM_NODE field does NOT exist\nCan not proceed\n"; return; }
00017         int TO_NODE = DBFGetFieldIndex(dbf, "TO_NODE");
00018         if( FId == -1){ cout<<"Error: TO_NODE field does NOT exist\nCan not proceed\n"; return; }
00019 
00020         DBFHandle newdbf = DBFCreate("temp.dbf");
00021 
00022         int fieldCount = DBFGetFieldCount(dbf); 
00023         int recordCount = DBFGetRecordCount(dbf); 
00024         
00025         /*********************************************************/
00026         /*                              Copying Existing Record                                  */
00027         /*********************************************************/
00028         cout<<"Copying Existing Records...";
00029         int width[1], decimals[1];
00030         char fieldName[20];
00031         DBFFieldType fieldType;
00032         for (int i=0; i<fieldCount; i++){
00033                 fieldType = DBFGetFieldInfo(dbf, i, fieldName, width, decimals);
00034                 temp = DBFAddField(newdbf, fieldName, fieldType, width[0], decimals[0]);
00035                 //cout<<temp<<"\n";
00036         }
00037         
00038         int DSegmt = DBFAddField(newdbf, "DownSegmt", FTInteger, 6, 0);
00039         
00040         //temp = DBFAddField(newdbf, "FID", FTInteger, 6, 0);
00041         
00042         int iValue;
00043         double fValue;
00044         const char * cValue;
00045         for (int i=0; i<recordCount; i++){
00046                 for (int j=0; j<fieldCount; j++){
00047                         fieldType = DBFGetFieldInfo(dbf, j, fieldName, width, decimals);
00048                         if(fieldType == FTString){
00049                                 cValue = DBFReadStringAttribute(dbf, i, j);
00050                                 temp = DBFWriteStringAttribute(newdbf, i, j, cValue);
00051                         }
00052                         else if(fieldType == FTInteger){
00053                                 iValue = DBFReadIntegerAttribute(dbf, i, j);
00054                                 temp = DBFWriteIntegerAttribute(newdbf, i, j, iValue);
00055                         }
00056                         else if(fieldType == FTDouble){
00057                                 fValue = DBFReadDoubleAttribute(dbf, i, j);
00058                                 temp = DBFWriteDoubleAttribute(newdbf, i, j, fValue);
00059                         }
00060                 }
00061                 //temp = DBFWriteIntegerAttribute(newdbf, i, fieldCount, i+1);
00062         }
00063         cout<<"Done!\n\n";
00064 
00065         cout<<"\nAdding  Field  DownSegmt...";
00066         cout<<"DONE!\n";
00067 
00068         //cout<<"field number= "<<temp<<"\n";
00069 
00070         cout<<"\nComputing DownSegmt Records...";
00071         // = DBFGetFieldIndex(newdbf, "DownSegmt"); //cout<<DSegmt<<"\n";
00072         //getchar();getchar();
00073         int j;
00074         for(int i=0; i<recordCount; i++){
00075                 //cout<<"\n"<<"i = "<<i<<"\n";
00076                 j=0;
00077                 while((DBFReadIntegerAttribute(dbf,i,TO_NODE) != DBFReadIntegerAttribute(dbf,j,FROM_NODE) && j<recordCount)){
00078                         //cout<<j<<" "<<DBFReadIntegerAttribute(dbf,i,TNode)<<" "<<DBFReadIntegerAttribute(dbf,j,FNode)<<"\n";
00079                         j++;
00080                         //cout<<"done\n";
00081                 }
00082                 if(j<recordCount){
00083                         //cout<<"here "<<fieldCount <<" " <<DBFReadIntegerAttribute(dbf, j, IDIndex)<<"\n";
00084                         temp = DBFWriteIntegerAttribute(newdbf, i, fieldCount, DBFReadIntegerAttribute(dbf, j, FId));
00085                         //cout<<"After push\n";
00086                 }
00087                 else{
00088                         temp = DBFWriteIntegerAttribute(newdbf, i, fieldCount, 0);
00089                 }
00090         }
00091         cout<<"DONE!\n";
00092         DBFClose(dbf);
00093         DBFClose(newdbf);
00094         //cout<<"here2";
00095 
00096         char buffer[100];
00097         ifstream infile;
00098         ofstream outfile;
00099         infile.open("temp.dbf",ios::binary|ios::in);
00100         outfile.open(dbfFileName,ios::binary|ios::out);
00101         j=1;
00102         cout<<"\nWriting to the .dbf file...";
00103         while(infile){
00104                 infile.read(buffer,100);
00105                 infile.seekg(j*100);j++;
00106                 outfile.write(buffer,100);
00107         }
00108         cout<<"DONE!\n\n";
00109 
00110 }

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