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;
00011 double x2, y2;
00012 int cl;
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
00026
00027 int err;
00028 FILE *fpNodeFile_Cat;
00029 int maxClass=0;
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
00058 err=gridread(catFile,(void ***)&elev,RPFLTDTYPE,&nx,&ny,&dx,&dy,bndbox,&csize,&mval,&filetype);
00059
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
00066
00067
00068
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
00075
00076
00077
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
00087
00088
00089 for(i=1; i<nx-1; i++){
00090
00091 for(j=1; j<ny-1; j++){
00092
00093 if(elev[i][j]>0){
00094
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
00104 }
00105
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
00115 }
00116
00117 if(elev[i][j] != elev[i+1][j]){
00118
00119 line[numLine].x1 = bndbox[0] + (i+1)*csize;
00120
00121 line[numLine].y1 = bndbox[3] - (j)*csize;
00122
00123 line[numLine].x2 = bndbox[0] + (i+1)*csize;
00124
00125 line[numLine].y2 = bndbox[3] - (j+1)*csize;
00126
00127 line[numLine].cl = elev[i][j];
00128
00129 linesInClass[line[numLine].cl]++;
00130
00131 numLine++;
00132
00133 }
00134
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
00144 }
00145
00146 }
00147 }
00148 }
00149
00150
00151
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
00163
00164 tempobj = SHPCreateSimpleObject(SHPT_ARC, 2, ptx, pty, ptz);
00165
00166 SHPWriteObject(tempshp, -1, tempobj);
00167
00168
00169 DBFWriteIntegerAttribute(tempdbf, temprecordNum, tempFld, line[i].cl);
00170 temprecordNum++;
00171
00172 }
00173 SHPClose(tempshp);
00174 DBFClose(tempdbf);
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
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
00196 numPt++;
00197 if(lineDirFlag == 0){ --numPt; lineDirFlag = 1; }
00198
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];
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
00217
00218
00219
00220 printf("writing polygon:\t%d\twith\t%d\tnodes -- class#\t%d\n", recordNum, numPt, classNum);
00221
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
00229
00230
00231
00232
00233
00234 classNum++;
00235 startPivot = endPivot;
00236 endPivot = startPivot + linesInClass[classNum];
00237
00238 i = startPivot;
00239 }
00240 else if(count == 2){
00241 line[i].cl=0;
00242
00243
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");
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 }