pihmLIBS/addToFromNode.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 addToFromNode(const char *dbfFileName, const char *shpFileName){
00010         DBFHandle dbf = DBFOpen(dbfFileName, "rb");
00011         SHPHandle shp = SHPOpen(shpFileName, "rb");
00012 
00013         DBFHandle newdbf = DBFCreate("temp.dbf");
00014         int temp;
00015 
00016         int fieldCount = DBFGetFieldCount(dbf);
00017         int recordCount = DBFGetRecordCount(dbf);
00018 
00019         /*********************************************************/
00020         /*                              Copying Existing Record                                  */
00021         /*********************************************************/
00022         cout<<"Copying Existing Records...";
00023         int width[1], decimals[1];
00024         char fieldName[20];
00025         DBFFieldType fieldType;
00026         for (int i=0; i<fieldCount; i++){
00027                 fieldType = DBFGetFieldInfo(dbf, i, fieldName, width, decimals);
00028                 temp = DBFAddField(newdbf, fieldName, fieldType, width[0], decimals[0]);
00029                 //cout<<temp<<"\n";
00030         }
00031 
00032         int fromField = DBFAddField(newdbf, "FROM_NODE", FTInteger, 6, 0); //cout<<"fromField= "<<fromField<<"\n";
00033         int toField = DBFAddField(newdbf, "TO_NODE", FTInteger, 6, 0); //cout<<"toField= "<<toField<<"\n";
00034         
00035         //temp = DBFAddField(newdbf, "FID", FTInteger, 6, 0);
00036         
00037         int iValue;
00038         double fValue;
00039         const char * cValue;
00040         for (int i=0; i<recordCount; i++){
00041                 for (int j=0; j<fieldCount; j++){
00042                         fieldType = DBFGetFieldInfo(dbf, j, fieldName, width, decimals);
00043                         if(fieldType == FTString){
00044                                 cValue = DBFReadStringAttribute(dbf, i, j);
00045                                 temp = DBFWriteStringAttribute(newdbf, i, j, cValue);
00046                         }
00047                         else if(fieldType == FTInteger){
00048                                 iValue = DBFReadIntegerAttribute(dbf, i, j);
00049                                 temp = DBFWriteIntegerAttribute(newdbf, i, j, iValue);
00050                         }
00051                         else if(fieldType == FTDouble){
00052                                 fValue = DBFReadDoubleAttribute(dbf, i, j);
00053                                 temp = DBFWriteDoubleAttribute(newdbf, i, j, fValue);
00054                         }
00055                 }
00056                 //temp = DBFWriteIntegerAttribute(newdbf, i, fieldCount, i+1);
00057         }
00058         cout<<"Done!\n\n";
00059         /*********************************************************/
00060         //cout<<"hello";
00061         cout<<"Adding New Field \"FROM_NODE\"...Done!\n\n";
00062         cout<<"Adding New Field \"TO_NODE\"...Done!\n\n";
00063         
00064         cout<<"Computing FROM-NODE and TO-NODE...";
00065         Point *nodes = new Point[recordCount*2];
00066         int ptCount = 0;
00067         
00068         for(int i=0; i<recordCount; i++){
00069                 SHPObject *obj = SHPReadObject(shp, i);
00070                 Point tempPt;
00071                 for (int k=0; k<2; k++){
00072                         tempPt.x=obj->padfX[k];
00073                         tempPt.y=obj->padfY[k];
00074                         int j = ptCount-1;
00075                         while(equalPoint(tempPt, nodes[j]) != 1 && j>=0)
00076                                 j--;
00077                         if(j==-1){
00078                                 nodes[ptCount].x=obj->padfX[k];
00079                                 nodes[ptCount].y=obj->padfY[k];
00080                                 ptCount++;
00081 
00082                                 if(k==0)
00083                                         temp = DBFWriteIntegerAttribute(newdbf,i,fromField,ptCount);
00084                                 else
00085                                         temp = DBFWriteIntegerAttribute(newdbf,i,toField,ptCount);
00086                         }
00087                         else{
00088                                 if(k==0)
00089                                         temp = DBFWriteIntegerAttribute(newdbf,i,fromField,j+1);
00090                                 else
00091                                         temp = DBFWriteIntegerAttribute(newdbf,i,toField,j+1);
00092                         }
00093                 }
00094         }
00095         cout<<"Done!\n\n";
00096         //cout<<"Number of nodes = "<<ptCount<<"\n";
00097         //cout<<"test";
00098         
00099         DBFClose(dbf);
00100         SHPClose(shp);
00101         DBFClose(newdbf);
00102 
00103         cout<<"Writing to the .dbf File...";
00104         char buffer[100];
00105         ifstream infile;
00106         ofstream outfile;
00107         infile.open("temp.dbf",ios::binary|ios::in);
00108         outfile.open(dbfFileName,ios::binary|ios::out);
00109         int j=1;
00110         //cout<<"\nWriting to the .dbf file...";
00111         while(infile){
00112                 infile.read(buffer,100);
00113                 infile.seekg(j*100);j++;
00114                 outfile.write(buffer,100);
00115         }
00116         cout<<"Done!\n\n";
00117 }

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