pihmLIBS/addSOrder.cpp

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <iomanip>
00004 #include "shapefil.h"
00005 #include "Point.h"
00006 
00007 using namespace std;
00008 
00009 void addSOrder(const char *dbfFileName, const char *shpFileName){
00010         DBFHandle dbf = DBFOpen(dbfFileName, "rb");
00011         SHPHandle shp = SHPOpen(shpFileName, "rb");
00012 
00013         cout<<"Checking Required Fields...";
00014         int fromField = DBFGetFieldIndex(dbf, "FROM_NODE");
00015         if (fromField==-1){ cout<<"Error: FROM_NODE field does NOT exist\nCan not proceed\n"; return; }
00016         int toField = DBFGetFieldIndex(dbf, "TO_NODE");
00017         if (toField==-1){ cout<<"Error: TO_NODE field does NOT exist\nCan not proceed\n"; return; }
00018         cout<<"Done!\n\n";
00019 
00020         DBFHandle newdbf = DBFCreate("temp.dbf");
00021         int temp;
00022 
00023         int fieldCount = DBFGetFieldCount(dbf);
00024         int recordCount = DBFGetRecordCount(dbf);
00025 
00026         /*********************************************************/
00027         /*                              Copying Existing Record                                  */
00028         /*********************************************************/
00029         cout<<"Copying Existing Records...";
00030         int width[1], decimals[1];
00031         char fieldName[20];
00032         DBFFieldType fieldType;
00033         for (int i=0; i<fieldCount; i++){
00034                 fieldType = DBFGetFieldInfo(dbf, i, fieldName, width, decimals);
00035                 temp = DBFAddField(newdbf, fieldName, fieldType, width[0], decimals[0]);
00036                 //cout<<temp<<"\n";
00037         }
00038 
00039 
00040         int SOrderField = DBFAddField(newdbf, "S_Order", FTInteger, 6, 0); //cout<<"SOrderField= "<<SOrderField<<"\n";
00041 
00042         //temp = DBFAddField(newdbf, "FID", FTInteger, 6, 0);
00043 
00044 
00045         int iValue;
00046         double fValue;
00047         const char * cValue;
00048         for (int i=0; i<recordCount; i++){
00049                 for (int j=0; j<fieldCount; j++){
00050                         fieldType = DBFGetFieldInfo(dbf, j, fieldName, width, decimals);
00051                         if(fieldType == FTString){
00052                                 cValue = DBFReadStringAttribute(dbf, i, j);
00053                                 temp = DBFWriteStringAttribute(newdbf, i, j, cValue);
00054                         }
00055                         else if(fieldType == FTInteger){
00056                                 iValue = DBFReadIntegerAttribute(dbf, i, j);
00057                                 temp = DBFWriteIntegerAttribute(newdbf, i, j, iValue);
00058                         }
00059                         else if(fieldType == FTDouble){
00060                                 fValue = DBFReadDoubleAttribute(dbf, i, j);
00061                                 temp = DBFWriteDoubleAttribute(newdbf, i, j, fValue);
00062                         }
00063                 }
00064                 //temp = DBFWriteIntegerAttribute(newdbf, i, fieldCount, i+1);
00065         }
00066         cout<<"Done!\n\n";
00067         /*********************************************************/
00068         cout<<"Adding New field \"S_Order\" at Field #"<<SOrderField<<"...Done!\n\n";
00069 
00070         cout<<"Computing Stream Order...";
00071         int (*toArray)[5] = new int[2*recordCount][5];
00072         int *count = new int [2*recordCount];
00073         for (int i=0; i<2*recordCount; i++)
00074                 count[i]=0;
00075         //count[2*recordCount]={0};
00076 
00077         for(int i=0; i<recordCount; i++){
00078                 temp = DBFReadIntegerAttribute(dbf, i, toField);
00079                 //cout<<"temp = "<<temp<<"\n";
00080                 //cout<<"count= "<<count[temp]<<"\n";
00081                 if(DBFReadIntegerAttribute(dbf,toArray[temp][count[temp]-1],fromField) != DBFReadIntegerAttribute(dbf,i,fromField))
00082                         toArray[temp][count[temp]++]=i;
00083         }
00084 
00085         int UPDATE = 1;
00086         int loopCount = 0;
00087         int max1, max2, from, to, so;
00088         while(UPDATE == 1){
00089                 UPDATE = 0;
00090                 loopCount++;
00091                 //cout<<"loop Count= "<<loopCount<<"\n";
00092 
00093                 for(int i=0; i<recordCount; i++){
00094                         int temp = DBFReadIntegerAttribute(newdbf, i, fromField);
00095                         if(count[temp]==0){
00096                                 //
00097                                 //cout <<"i= "<<i<<" SO =1\n";getchar();
00098                                 DBFWriteIntegerAttribute(newdbf, i, SOrderField, 1);
00099                                 //cout<<"Wrote = "<<DBFReadIntegerAttribute(newdbf, i, SOrderField)<<"\n";
00100                         }
00101                         else if(count[temp] == 1){
00102                                 temp = DBFReadIntegerAttribute(newdbf, i, fromField);
00103                                 //cout<<temp<<"\t";
00104                                 temp = toArray[temp][0];
00105                                 //cout<<temp<<"\n";//getchar();
00106                                 //cout <<"i= "<<i<<" SO= "<<DBFReadIntegerAttribute(newdbf, temp, SOrderField);getchar();
00107                                 if(DBFReadIntegerAttribute(newdbf, temp, SOrderField) != DBFReadIntegerAttribute(newdbf, i, SOrderField))
00108                                         UPDATE = 1;
00109                                 DBFWriteIntegerAttribute(newdbf, i, SOrderField, DBFReadIntegerAttribute(newdbf, temp, SOrderField));
00110                         }
00111                         else{
00112                                 from = DBFReadIntegerAttribute(newdbf, i, fromField);
00113                                 max1=0; max2=0;
00114                                 //cout<<"i = "<<i<<"\n";
00115                                 for(int j=0; j<count[temp]; j++){
00116                                         to = toArray[from][j]; //cout<<"  to = "<<to<<"\t";
00117                                         so = DBFReadIntegerAttribute(newdbf, to, SOrderField);// cout<<"  so = "<<so<<"\n";
00118                                         if(so>=max1){
00119                                                 max2=max1;
00120                                                 max1=so;
00121                                         }
00122                                 }
00123                                 if(max1==max2){
00124                                         if(DBFReadIntegerAttribute(newdbf, i, SOrderField) != max1+1)
00125                                                 UPDATE = 1;
00126                                         DBFWriteIntegerAttribute(newdbf, i, SOrderField, max1+1);
00127                                 }
00128                                 else{
00129                                         if(DBFReadIntegerAttribute(newdbf, i, SOrderField) != max1)
00130                                                 UPDATE = 1;
00131                                         DBFWriteIntegerAttribute(newdbf, i, SOrderField, max1);
00132                                 }
00133                         }
00134 
00135                                 //if(DBFReadIntegerAttribute(newdbf, i, SOrderField) != DBFReadIntegerAttribute(newdbf, i, DBFGetFieldIndex(newdbf, "SO_VALUE"))){
00136                                         //{getchar();getchar();}
00137                 }
00138         }
00139         cout<<"Done!\n\n";
00140 
00141         DBFClose(dbf);
00142         SHPClose(shp);
00143         DBFClose(newdbf);
00144 
00145         cout<<"Writing to the .dbf File...";
00146         char buffer[100];
00147         ifstream infile;
00148         ofstream outfile;
00149         infile.open("temp.dbf",ios::binary|ios::in);
00150         outfile.open(dbfFileName,ios::binary|ios::out);
00151         int j=1;
00152         //cout<<"\nWriting to the .dbf file...";
00153         while(infile){
00154                 infile.read(buffer,100);
00155                 infile.seekg(j*100);j++;
00156                 outfile.write(buffer,100);
00157         }
00158         cout<<"Done!\n\n";
00159 }

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