pihmRasterLIBS/catPoly.cpp

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include "lsm.h"
00003 #include "./../pihmLIBS/shapefil.h"
00004 
00005 #define dist(x1, x2) fabs(x1-x2)
00006 #define direction(l1, l2) (fabs(atan(fabs((l1.y2 - l1.y1)/(l1.x2 - l1.x1))) - atan(fabs(( l2.y2 - l2.y1)/(l2.x2 - l2.x1))))<0.001)?0:1;
00007 
00008 
00009 struct structure{
00010     double x1, y1; // from point
00011     double x2, y2; // to   point
00012     int cl; //catchment it belongs to
00013 };
00014 
00015 typedef struct structure LINE;
00016 
00017 
00018 int catchmentPoly(char *catFile, char *nodeFile, char *shpFile, char *dbfFile);
00019 void sortLine(LINE *lines, int);
00020 
00021 
00022 int catchmentPoly(char *catFile, char *nodeFile, char *shpFile, char *dbfFile){
00023     
00024     int i, j;
00025     //int test;
00026     //int mIJ[2];
00027     int err;
00028     FILE *fpNodeFile_Cat; 
00029     int maxClass=0; // stores # of nodes (starting pt of a stream) to process 
00030     int **node;
00031     
00032     LINE *line;
00033     
00034     int numLine=0;
00035     int *linesInClass;
00036 
00037         int lineDirFlag;
00038 
00039     
00040     int startPivot, endPivot, classNum, point[2], count;
00041     
00042     int numPt;
00043     double *ptx, *pty, *ptz;
00044     
00045     SHPHandle shp = SHPCreate(shpFile, SHPT_POLYGON);
00046         DBFHandle dbf = DBFCreate(dbfFile);
00047         SHPObject *obj;
00048         int polyFld = DBFAddField(dbf, "catNum", FTInteger, 5, 0);
00049         int recordNum=0;
00050         
00051         SHPHandle tempshp = SHPCreate("temp.shp", SHPT_ARC);
00052         DBFHandle tempdbf = DBFCreate("temp.dbf");
00053         SHPObject *tempobj;
00054         int tempFld = DBFAddField(tempdbf, "lineNum", FTInteger, 5, 0);
00055         int temprecordNum=0;
00056         
00057     //err=gridread(fdrFile,(void ***)&dir,RPSHRDTYPE,&nx,&ny,&dx,&dy,bndbox,&csize,&mval,&filetype);
00058     err=gridread(catFile,(void ***)&elev,RPFLTDTYPE,&nx,&ny,&dx,&dy,bndbox,&csize,&mval,&filetype); 
00059     //err=gridread(fdrFile,(void ***)&slope,RPFLTDTYPE,&nx,&ny,&dx,&dy,bndbox,&csize,&mval,&filetype); 
00060     
00061     ptx = (double *)malloc(sizeof(double)*nx*ny*2);
00062     pty = (double *)malloc(sizeof(double)*nx*ny*2);
00063     ptz = (double *)malloc(sizeof(double)*nx*ny*2);
00064  
00065         //printf("here 1\n"); getchar(); getchar();
00066         
00067 //      fpNodeFile_Cat = fopen(nodeFile, "r");
00068     //fscanf(fpNodeFile_Cat, "%d", &maxClass);
00069     for(i=0; i<nx; i++)
00070         for(j=0; j<ny; j++)
00071             if(maxClass<elev[i][j])
00072                 maxClass = elev[i][j];
00073     /*
00074     node = (int **)malloc(sizeof(int *)*numNode);
00075     for(i=0; i<numNode; i++){
00076         node[i]=(int *)malloc(sizeof(int)*2);
00077         fscanf(fpNodeFile_Cat, "%d %d", &(node[i][0]), &(node[i][1]));
00078     }
00079     */
00080     line = (LINE *)malloc(sizeof(LINE)*4*nx*ny);
00081     
00082     linesInClass = (int *)malloc((maxClass+1)*sizeof(int)); 
00083     for(i=1; i<=maxClass; i++)
00084         linesInClass[i]=0;
00085         
00086     //printf("here 2\n"); getchar(); getchar();
00087     //printf("nx = %d ny = %d\n", nx, ny);
00088     //store all the lines
00089     for(i=1; i<nx-1; i++){
00090         //printf("for 1\n");
00091         for(j=1; j<ny-1; j++){
00092             //printf("for 2: %d %d\n", i, j);
00093             if(elev[i][j]>0){
00094                 //printf("if %d %d\n", i, j);
00095                 if(elev[i][j] != elev[i-1][j]){
00096                     line[numLine].x1 = bndbox[0] + (i)*csize;
00097                     line[numLine].y1 = bndbox[3] - (j+1)*csize;
00098                     line[numLine].x2 = bndbox[0] + (i)*csize;
00099                     line[numLine].y2 = bndbox[3] - (j)*csize;
00100                     line[numLine].cl = elev[i][j];
00101                     linesInClass[line[numLine].cl]++;
00102                     numLine++;
00103                     //printf("here 2.1\n"); getchar(); getchar();
00104                 }
00105                 //printf("here 2.1.1\n"); getchar(); getchar();
00106                 if(elev[i][j] != elev[i][j-1]){
00107                     line[numLine].x1 = bndbox[0] + (i)*csize;
00108                     line[numLine].y1 = bndbox[3] - (j)*csize;
00109                     line[numLine].x2 = bndbox[0] + (i+1)*csize;
00110                     line[numLine].y2 = bndbox[3] - (j)*csize;
00111                     line[numLine].cl = elev[i][j];
00112                     linesInClass[line[numLine].cl]++;
00113                     numLine++;
00114                     //printf("here 2.2\n"); getchar(); getchar();
00115                 }
00116                 //printf("here 2.2.1\n"); getchar(); getchar();
00117                 if(elev[i][j] != elev[i+1][j]){
00118                     //printf("here 2.3a\n"); getchar(); getchar();
00119                     line[numLine].x1 = bndbox[0] + (i+1)*csize;
00120                     //printf("here 2.3b\n"); getchar(); getchar();
00121                     line[numLine].y1 = bndbox[3] - (j)*csize;
00122                     //printf("here 2.3c\n"); getchar(); getchar();
00123                     line[numLine].x2 = bndbox[0] + (i+1)*csize;
00124                     //printf("here 2.3d\n"); getchar(); getchar();
00125                     line[numLine].y2 = bndbox[3] - (j+1)*csize;
00126                     //printf("here 2.3e\n"); getchar(); getchar();
00127                     line[numLine].cl = elev[i][j];
00128                     //printf("here 2.3f\n"); getchar(); getchar();
00129                     linesInClass[line[numLine].cl]++;
00130                     //printf("here 2.3g\n"); getchar(); getchar();
00131                     numLine++;
00132                     //printf("here 2.3\n"); getchar(); getchar();
00133                 }
00134                 //printf("here 2.3.1\n"); getchar(); getchar();
00135                 if(elev[i][j] != elev[i][j+1]){
00136                     line[numLine].x1 = bndbox[0] + (i+1)*csize;
00137                     line[numLine].y1 = bndbox[3] - (j+1)*csize;
00138                     line[numLine].x2 = bndbox[0] + (i)*csize;
00139                     line[numLine].y2 = bndbox[3] - (j+1)*csize;
00140                     line[numLine].cl = elev[i][j];
00141                     linesInClass[line[numLine].cl]++;
00142                     numLine++;
00143                     //printf("here 2.4\n"); getchar(); getchar();
00144                 }
00145                 //printf("here 2.4.1\n"); getchar(); getchar();
00146             }
00147         }
00148     }
00149     //printf("here 3\n"); getchar(); getchar();
00150     
00151     //sort the lines with class information
00152     sortLine(line, numLine);
00153     
00154     for(i=0; i<numLine; i++){
00155         
00156         ptx[0]=line[i].x1;
00157         ptx[1]=line[i].x2;
00158         pty[0]=line[i].y1;
00159         pty[1]=line[i].y2;
00160         ptz[0]=0.0;
00161         ptz[1]=0.0;
00162         //printf("x: %lf\t%lf\n", ptx[0], ptx[1]);
00163         //printf("y: %lf\t%lf\n", pty[0], pty[1]); getchar(); getchar();
00164         tempobj = SHPCreateSimpleObject(SHPT_ARC, 2, ptx, pty, ptz);
00165        // printf("%d\n", i); getchar();getchar();
00166         SHPWriteObject(tempshp, -1, tempobj);
00167         //printf("%d\n", i); getchar();getchar();
00168         
00169         DBFWriteIntegerAttribute(tempdbf, temprecordNum, tempFld, line[i].cl);
00170         temprecordNum++;
00171         //printf("%d\n", i); getchar(); getchar();
00172     }
00173     SHPClose(tempshp);
00174     DBFClose(tempdbf);
00175     
00176     /*
00177     for(err=0; err<numLine; err++)
00178         printf("%lf\t%lf\t%lf\t%lf\n", line[err].x1, line[err].y1,line[err].x1, line[err].y1,line[err].x2, line[err].y2); 
00179     */
00180     /*
00181     for(i=1; i<=maxClass; i++)
00182         printf("class %d numLine %d\n", i, linesInClass[i]);
00183     */    
00184     // start forming polygons now  NOTE: polygon:=class
00185     classNum = 1;
00186     startPivot = 0;
00187     endPivot   = startPivot + linesInClass[classNum];
00188     i = startPivot;
00189     numPt = 0;
00190     while(classNum <= maxClass){
00191         
00192         ptx[numPt]=line[i].x1;
00193         pty[numPt]=line[i].y1;
00194         ptz[numPt]=0.0;
00195         //printf("= %d %lf %lf\n", numPt, ptx[numPt], ptx[numPt]);getchar(); getchar();
00196         numPt++;
00197         if(lineDirFlag == 0){ --numPt; lineDirFlag = 1; }
00198         // search for connection        
00199         count=0;
00200         for(j=startPivot; j<endPivot; j++){
00201             if(dist(line[i].x2,line[j].x1)<0.0001 && dist(line[i].y2,line[j].y1)<0.0001 && line[j].cl != 0){
00202                 point[count++]=j;
00203             }
00204         }
00205         if(count == 1){
00206             line[i].cl=0;
00207             lineDirFlag = direction(line[i], line[point[0]]);
00208             i = point[0]; // start search from found connecting line
00209         }
00210         else if(count == 0){
00211             ptx[numPt]=line[i].x2;
00212             pty[numPt]=line[i].y2;
00213             ptz[numPt]=0.0;
00214             numPt++;
00215             /*
00216             for(err=0; err<numPt; err++)
00217                 printf("-> %lf\t%lf\n", ptx[err], pty[err]); 
00218             */  
00219             //write polygon here
00220             printf("writing polygon:\t%d\twith\t%d\tnodes  -- class#\t%d\n", recordNum, numPt, classNum); 
00221             //getchar(); getchar();
00222             obj = SHPCreateSimpleObject(SHPT_POLYGON, numPt, ptx, pty, ptz);
00223             SHPWriteObject(shp, -1, obj);
00224             DBFWriteIntegerAttribute(dbf, recordNum, polyFld, classNum);
00225             recordNum++;
00226             numPt = 0;
00227             /*
00228             if(classNum==31){
00229                 SHPClose(shp);
00230                 DBFClose(dbf);
00231                 exit(1);
00232             }
00233             */
00234             classNum++;
00235             startPivot = endPivot;
00236             endPivot   = startPivot + linesInClass[classNum];
00237             //printf("start = %d, end = %d %d %d %d\n", startPivot, endPivot, linesInClass[classNum], line[startPivot].cl, line[endPivot-1].cl); getchar();
00238             i = startPivot;
00239         }
00240         else if(count == 2){
00241             line[i].cl=0;
00242             //select one of those:
00243             //printf("%d %d\n",abs(i-point[0]), abs(i-point[1]));
00244             if(abs(i-point[0]) > abs(i-point[1]))
00245                 i = point[0];
00246             else
00247                 i = point[1];
00248         }
00249         else{
00250             printf("No connection found!! Whats going on? \n"); //getchar(); getchar();  
00251         }      
00252     }  
00253     SHPClose(shp);
00254     DBFClose(dbf);      
00255 }
00256 
00257 void sortLine(LINE *lines, int numLine){
00258      int i, j, tempI, swapFlag=0;
00259      double tempD;
00260      for(i=0; i<numLine-1; i++){
00261          swapFlag=0;
00262          for(j=0; j<numLine-1-i; j++){
00263              if(lines[j+1].cl < lines[j].cl){
00264                  swapFlag = 1;
00265                  
00266                  tempD = lines[j].x1;
00267                  lines[j].x1   = lines[j+1].x1;
00268                  lines[j+1].x1 = tempD;
00269                  
00270                  tempD = lines[j].y1;
00271                  lines[j].y1   = lines[j+1].y1;
00272                  lines[j+1].y1 = tempD;
00273                  
00274                  tempD = lines[j].x2;
00275                  lines[j].x2   = lines[j+1].x2;
00276                  lines[j+1].x2 = tempD;
00277                  
00278                  tempD = lines[j].y2;
00279                  lines[j].y2   = lines[j+1].y2;
00280                  lines[j+1].y2 = tempD;
00281                  
00282                  tempI = lines[j].cl;
00283                  lines[j].cl   = lines[j+1].cl;
00284                  lines[j+1].cl = tempI;
00285              }
00286          }
00287          if(swapFlag == 0)
00288              break;
00289      }
00290      return;
00291 }

Generated on Sun Aug 5 17:34:00 2007 for PIHMgis by  doxygen 1.5.2