pihmLIBS/createTinShapeFile.h

Go to the documentation of this file.
00001 #include <fstream>
00002 #include <iomanip>
00003 #include "shapefil.h"
00004 
00005 using namespace std;
00006 
00007 void createTinShapeFile(const char* eleFileName, const char* nodeFileName, const char* shpFileName, const char*dbfFileName, const char *logFileName){
00008                           ifstream eleFile;
00009                           ifstream nodeFile;
00010                           eleFile.open(eleFileName);
00011                           nodeFile.open(nodeFileName);
00012                           
00013                           ofstream out;
00014                           out.open(logFileName, ios::app);
00015                           int temp;
00016                           
00017                            struct Point{
00018                                  double x,y;
00019                           };
00020                           struct Element{
00021                                  int p1, p2, p3;
00022                           };
00023                           
00024                           int countEle, countNode;
00025                           eleFile>>countEle; eleFile>>temp; eleFile>>temp;
00026                           Element* ele = new Element[countEle];
00027                           nodeFile>>countNode; 
00028                           nodeFile>>temp; nodeFile>>temp; nodeFile>>temp;
00029                           Point* nodes = new Point[countNode];
00030                           out<<"<p>Reading elements...\n";
00031                           for(int i=0; i<countEle; i++){
00032                                                                   //cout<<i<<" ";
00033                                   eleFile>>temp;
00034                                   eleFile>>ele[i].p1;
00035                                   eleFile>>ele[i].p2;
00036                                   eleFile>>ele[i].p3;
00037                           }
00038                           out<<" Done!</p>";
00039                                                   
00040                           out<<"<p>Reading nodes...";
00041                           for(int i=0; i<countNode; i++){
00042                                   nodeFile>>temp;
00043                                   nodeFile>>setprecision(20)>>nodes[i].x;
00044                                   nodeFile>>setprecision(20)>>nodes[i].y;
00045                                   nodeFile>>temp;
00046                           }
00047                           out<<" Done!</p>";
00048                                                   
00049                           //Creates .shp and .shx files
00050                           out<<"<p>Writing .shp and .shx file ("<<shpFileName<<") ...";
00051                           SHPHandle newShp=SHPCreate(shpFileName, SHPT_POLYGON);
00052                           SHPObject* shpObj = new SHPObject;
00053                           double px[4], py[4], pz[4], pm[4];
00054                           for(int i=0; i<countEle; i++){
00055                                                                   //if (i>countEle-10){cout<<i<<" ";getchar(); getchar();}
00056                                   setprecision(20);
00057                                   px[0]=nodes[ele[i].p1-1].x; px[1]=nodes[ele[i].p2-1].x; px[2]=nodes[ele[i].p3-1].x;px[3]=nodes[ele[i].p1-1].x;
00058                                   py[0]=nodes[ele[i].p1-1].y; py[1]=nodes[ele[i].p2-1].y; py[2]=nodes[ele[i].p3-1].y;py[3]=nodes[ele[i].p1-1].y;
00059                                   pz[0]=0; pz[1]=0; pz[2]=0; pz[3]=0;
00060                                   pm[0]=0; pm[1]=0; pm[2]=0; pm[3]=0;
00061                                   int panPartStart[1], panPartType[1];
00062                                   panPartStart[0]=0;
00063                                   panPartType[0]=5;
00064                                                                   //shpObj=SHPCreateObject(SHPT_POLYGON, i, 0, NULL, NULL, 3, px, py, NULL, NULL);
00065                                   shpObj=SHPCreateObject(5, i, 1, panPartStart, panPartType, 4, px, py, pz, pm);
00066                                   SHPComputeExtents(shpObj);
00067                                   temp=SHPWriteObject(newShp, -1, shpObj);
00068                           }
00069                           SHPClose(newShp);
00070                           out<<" Done!</p>";
00071 
00072                                                   //Creates .dbf file
00073                           out<<"<p>Writing .dbf file ("<<dbfFileName<<") ...";
00074                           DBFHandle newDbf = DBFCreate(dbfFileName);
00075                           temp = DBFAddField(newDbf, "Ele_ID", FTInteger, 9, 0);
00076                           //out<<"Field Ele_ID with Field number: "<<temp<<" added to .dbf file\n";
00077                           for(int i=0; i<countEle; i++){
00078                                   temp = DBFWriteIntegerAttribute(newDbf, i, 0, i+1);
00079                           }
00080                           DBFClose(newDbf);
00081                           out<<" Done!</p>";
00082                           out<<"<br><br>";
00083 }

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