00001 #include <iostream>
00002 #include <fstream>
00003 #include <iomanip>
00004 #include <math.h>
00005 #include "shapefil.h"
00006
00007
00008
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
00024
00025 for(int i=0;i<pointCounter;i++){
00026
00027
00028 if(fabs(pointArray[i].x - coord1)<0.000001 && fabs(pointArray[i].y - coord2)<0.000001){
00029
00030 *temp=i;
00031
00032 return 1;
00033 }
00034 }
00035 return 0;
00036 }
00037
00038
00039 void generatePolyFile(const char* constFileName, const char* outputFileName, QString logFileName){
00040
00041
00042
00043
00044
00045
00046
00047
00048 SHPHandle thisObj = SHPOpen(constFileName,"rb");
00049 ofstream out;
00050 out.open(logFileName,ios::app);
00051 out<<" Done!</p>";
00052
00053
00054 int numEntities, shapeType;
00055 double minBound[4], maxBound[4];
00056 SHPGetInfo(thisObj, &numEntities, &shapeType, thisObj->adBoundsMin,thisObj->adBoundsMax);
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
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
00086
00087 SHPObject* thisObjHandle;
00088
00089 for(int i=0;i<numEntities;i++){
00090 thisObjHandle = SHPReadObject(thisObj,i);
00091
00092
00093
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
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
00114
00115 }
00116 out<<" Done!</p>";
00117
00118
00119
00120
00121
00122 out<<"<p><font size=2 color=black>Writing poly File...";
00123 ofstream fp;
00124 fp.open(outputFileName);
00125
00126
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
00131 }
00132
00133 fp<<"\n";
00134
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
00139 }
00140
00141 fp<<"0"<<"\n";
00142 out<<" Done!</p>";
00143
00144 out.close();
00145
00146
00147
00148
00149
00150 SHPClose(thisObj);
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 return;
00163
00164 }