pihmLIBS/generatePolyFile.h

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <fstream>
00003 #include <iomanip>
00004 #include <math.h>
00005 #include "shapefil.h"
00006 
00007 //#include "shpopen.h"
00008 //#include "dbfopen.h"
00009 
00010 using namespace std;
00011 
00012 struct Point{
00013         double x,y;
00014         int serialNum;
00015 };
00016 
00017 struct Line{
00018         int pt1, pt2;
00019 };
00020 
00021 
00022 int searchPoint(Point* pointArray, double coord1,double coord2,int *temp, int pointCounter){
00023                 //std::cout<<"------- Inside searchPoint--------\n";
00024                 //getchar();getchar();
00025                 for(int i=0;i<pointCounter;i++){
00026                                 //std::cout<<"\n"<<pointArray[i].x<<"\t"<<pointArray[i].y<<"\n";
00027                         //if(pointArray[i].x == coord1 && pointArray[i].y == coord2){
00028                         if(fabs(pointArray[i].x - coord1)<0.000001 && fabs(pointArray[i].y - coord2)<0.000001){
00029 
00030                                 *temp=i;
00031                                 //std::cout<<"++++++++++++++++++++++++++ "<<i<<"\n"; 
00032                                 return 1;
00033                         }
00034                 }
00035                 return 0;
00036 }
00037 
00038 
00039 void generatePolyFile(const char* constFileName, const char* outputFileName, QString logFileName){
00040                 
00041                 //struct Map_info* vectMap;
00042                 
00043                 //G_gisinit("QGIS");
00044                 //int vectOpenResult = Vect_open_new(vectMap,"/root/Desktop/testData/Export_Output.shp",0);
00045                  
00046                 //SHPInfo* currentHandle = SHPOpen("/root/Desktop/testData/q20.shp","r+");
00047                 
00048                 SHPHandle thisObj = SHPOpen(constFileName,"rb");
00049                 ofstream out;
00050                 out.open(logFileName,ios::app);
00051                 out<<" Done!</p>";
00052                 //out.close();
00053                                 //SHPHandle thisObj = SHPOpen("/root/Desktop/share/Boundary_SplitLine.shp","r+");
00054                 int numEntities, shapeType;
00055                 double minBound[4], maxBound[4];
00056                 SHPGetInfo(thisObj, &numEntities, &shapeType, thisObj->adBoundsMin,thisObj->adBoundsMax);
00057 
00058                 //std::cout<<"============ SHAPE FILE DETAILS===============\n";
00059                 //std::cout<<"Number of Entities:"<<numEntities<<"\n"<<"Shapefile type:"<<shapeType<<"\n"<<"Minimum Bound"<<minBound<<"\n"<<"Maximum Bound"<<maxBound<<"\n";
00060 
00061 
00062                 //std::cout<<"---------- DEBUG DATA -------\n";
00063 
00064                 //std::cout<<currentHandle->adBoundsMin[0];
00065                 //std::cout<<currentHandle->adBoundsMax[0];
00066 
00067                 //std::cout<<"-------- END DEBUG DATA ------- \n";
00068 
00069                 //std::cout<<"successful shpopen call!\n";
00070                 //std::cout<<"THIS IS JUST FOR TESTING"<<"\n";  
00071                 
00072                 
00073                 
00074                 
00075 
00076                 out<<"<p><font size=2 color=black>Reading Nodes..."; 
00077                 Line* lineArray = new Line[numEntities];
00078 
00079                 Point* pointArray = new Point[2*numEntities];
00080 
00081                 int lineCounter=0;
00082                 int pointCounter=0;
00083                 int tempPt;
00084 
00085                 //std::cout<<"\n"<<"After declaration of arrays"<<"\n";
00086                 
00087                 SHPObject* thisObjHandle;
00088                 
00089                 for(int i=0;i<numEntities;i++){
00090                         thisObjHandle  = SHPReadObject(thisObj,i);
00091                 
00092                 //std::cout<<"\n"<<"Before call to searchPoint"<<"\n";
00093                 // Line Pt 1
00094                         if(pointCounter!=0 && searchPoint(pointArray,thisObjHandle->padfX[0],thisObjHandle->padfY[0],&tempPt, pointCounter)){ 
00095                                 lineArray[lineCounter].pt1=tempPt;
00096                         }
00097                         else{
00098                                 pointArray[pointCounter].x=thisObjHandle->padfX[0]; pointArray[pointCounter].y=thisObjHandle->padfY[0];
00099                                 lineArray[lineCounter].pt1=pointCounter;
00100                                 ++pointCounter;
00101                         }
00102                 // Line Pt 2
00103                         if(pointCounter!=0 && searchPoint(pointArray,thisObjHandle->padfX[1],thisObjHandle->padfY[1],&tempPt, pointCounter)){
00104                                 lineArray[lineCounter].pt2=tempPt;
00105                         }
00106                         else{
00107                                 pointArray[pointCounter].x=thisObjHandle->padfX[1]; pointArray[pointCounter].y=thisObjHandle->padfY[1];
00108                                 lineArray[lineCounter].pt2=pointCounter;
00109                                 ++pointCounter;
00110                         }
00111                         ++lineCounter;
00112                 
00113                 //std::cout<<"++++++++++++++++++++++++++++ LINE COUNTER:"<<lineCounter<<"\n";
00114                 //lineArray[lineCounter].pt1=
00115                 }
00116                 out<<" Done!</p>";
00117                 //std::cout<<"----------nVertices for shpobject: "<<thisObjHandle->nVertices;
00118                 
00119                 //std::cout<<"\n"<<thisObjHandle->padfX[0]<<"\n";
00120                 
00121                 //char filename[40]={"input.poly"};
00122                 out<<"<p><font size=2 color=black>Writing poly File...";
00123                 ofstream fp;
00124                 fp.open(outputFileName);
00125                 
00126                 //std::cout<<pointCounter<<"\t"<<"2"<<"\t"<<"0"<<"\t"<<"0"<<"\n";
00127                 fp<<pointCounter<<" "<<"2"<<" "<<"0"<<" "<<" "<<"0"<<"\n";
00128                 for(int i=0; i<pointCounter; i++){
00129                                 fp<<i+1<<" "<<setprecision (20)<<pointArray[i].x<<" "<<setprecision (20)<<pointArray[i].y<<"\n";
00130                                 //std::cout<<i+1<<"\t"<<pointArray[i].x<<"\t"<<pointArray[i].y<<"\n";
00131                 }
00132                 //std::cout<<"\n";
00133                 fp<<"\n";
00134                 //std::cout<<numEntities<<"\t"<<"0"<<"\n";
00135                 fp<<numEntities<<" "<<"0"<<"\n";
00136                 for(int i=0;i<numEntities;i++){
00137                                 fp<<i+1<<" "<<lineArray[i].pt1+1<<" "<<lineArray[i].pt2+1<<"\n";
00138                                 //std::cout<<i+1<<"\t"<<lineArray[i].pt1+1<<"\t"<<lineArray[i].pt2+1<<"\n";
00139                 }
00140                 //std::cout<<"0\n";
00141                 fp<<"0"<<"\n";
00142                 out<<" Done!</p>";
00143                 
00144                 out.close();
00145                 //std::cout<<"----------- POINT COUNTER = "<<pointCounter<<"\n";
00146 
00147                 
00148                 //SHPClose(currentHandle);
00149                 //
00150                 SHPClose(thisObj);
00151                 /* std::cout<<"After closing shapefile"<<"\n";
00152                 
00153                 //currentHandle = SHPOpen("/root/Desktop/testData/q20.shp","r+");
00154                 
00155                 //SHPClose();
00156                 
00157                 
00158                 
00159                 std::cout<<"Again opened a file!"<<"\n";
00160                 */
00161                 //dummy();
00162                 return;
00163                 //std::cout<<"After return\n";
00164 }

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