00001 #include <iostream>
00002 #include <fstream>
00003 #include <iomanip>
00004 #include <math.h>
00005
00006 #define PI 3.14
00007 #define distPt(p1, p2) fabs(p1.x-p2.x)+fabs(p1.y-p2.y)
00008 #define distXY(x1, y1, x2, y2) fabs(x1-x2)+fabs(y1-y2)
00009 #define distPtXY(p, x, y) fabs(p.x-x)+fabs(p.y-y)
00010
00011
00012
00013
00014 #include "shapefil.h"
00015
00016 using namespace std;
00017
00018 typedef struct{
00019 double x;
00020 double y;
00021 }Point;
00022
00023 double SLOPE(Point p1, Point p2){
00024 double delX = p2.x - p1.x;
00025 double delY = delX==0?p2.y - p1.y + 0.0000001:p2.y - p1.y;
00026 if(delX<0.0 && delY>0.0)
00027 return 90 + (180.0/PI)*atan(fabs(delY)/fabs(delX));
00028 else if(delX<=0.0 && delY<=0.0)
00029 return 180 + (180.0/PI)*atan(fabs(delY)/fabs(delX));
00030 else if(delX>0.0 && delY<0.0)
00031 return -90 + (180.0/PI)*atan(fabs(delY)/fabs(delX));
00032 else
00033 return 0 + (180.0/PI)*atan(fabs(delY)/fabs(delX));
00034 }
00035
00036 void extractRiver4mTIN(const char* shpFileName, const char* dbfFileName, const char* eleFileName, const char* nodeFileName, const char* neighFileName, const char *newshpFileName, const char *newdbfFileName){
00037 ifstream eleFile, nodeFile, neighFile;
00038 eleFile.open(eleFileName);
00039 nodeFile.open(nodeFileName);
00040 neighFile.open(neighFileName);
00041
00042 SHPHandle shp = SHPOpen(shpFileName, "rb");
00043 DBFHandle dbf = DBFOpen(dbfFileName, "rb");
00044
00045 SHPHandle newshp = SHPCreate(newshpFileName, SHPT_ARC);
00046 DBFHandle newdbf = DBFCreate(newdbfFileName);
00047
00048 int left = DBFAddField(newdbf, "LeftEle", FTInteger, 10, 0);
00049 int right= DBFAddField(newdbf, "RightEle", FTInteger, 10, 0);
00050 int fromTriNode = DBFAddField(newdbf, "FromTriNode", FTInteger, 10, 0);
00051 int toTriNode = DBFAddField(newdbf, "ToTriNode", FTInteger, 10, 0);
00052 int temp;
00053
00054 int numNode;
00055 nodeFile>>numNode;
00056 Point* node = new Point[numNode+1];
00057 nodeFile>>temp; nodeFile>>temp; nodeFile>>temp;
00058 for(int i=1; i<=numNode; i++){
00059 nodeFile>>temp;
00060 nodeFile >> node[i].x;
00061 nodeFile >> node[i].y;
00062 nodeFile>>temp;
00063 }
00064
00065 int numEle;
00066 eleFile>>numEle;
00067 int** element = new int*[numEle+1];
00068 eleFile>>temp; eleFile>>temp;
00069 for(int i=1; i<=numEle; i++){
00070 element[i] = new int[3];
00071 eleFile>>temp;
00072 eleFile>>element[i][0];
00073 eleFile>>element[i][1];
00074 eleFile>>element[i][2];
00075 }
00076
00077 int numNeigh;
00078 neighFile>>numNeigh;
00079 int** neighbour = new int*[numNeigh+1];
00080 neighFile>>temp;
00081 for(int i=1; i<=numNeigh; i++){
00082 neighbour[i] = new int[3];
00083 neighFile>>temp;
00084 neighFile>>neighbour[i][0];
00085 neighFile>>neighbour[i][1];
00086 neighFile>>neighbour[i][2];
00087 }
00088
00089
00090
00091 int** nodeInEle = new int*[numNode+1];
00092 int* nodeInEleCount = new int[numNode+1];
00093 for(int i=1; i<=numNode; i++){
00094 nodeInEle[i] = new int[20];
00095 nodeInEleCount[i]=0;
00096 }
00097
00098 for(int i=1; i<=numEle; i++){
00099
00100 for(int j=0; j<3; j++){
00101 nodeInEle[element[i][j]][nodeInEleCount[element[i][j]]++] = i;
00102 }
00103 }
00104
00105
00106
00107
00108
00109 int** neighNode = new int*[numNode+1];
00110 int* neighNodeCount = new int[numNode+1];
00111 for(int i=1; i<=numNode; i++){
00112 neighNode[i] = new int[100];
00113 neighNodeCount[i] = 0;
00114 }
00115
00116 for(int i=1; i<=numNode; i++){
00117 for(int j=0; j<nodeInEleCount[i]; j++){
00118 for(int k=0; k<3; k++){
00119 if(i!=element[nodeInEle[i][j]][k])
00120 neighNode[i][neighNodeCount[i]++]=element[nodeInEle[i][j]][k];
00121 }
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130 int FromTo[2];
00131 int LeftRight[2];
00132
00133 int recordCount = DBFGetRecordCount(dbf);
00134 int record = 0;
00135 double X[2], Y[2], Z[2];
00136 Z[0] = 0.0; Z[1] = 0.0;
00137 Point pt1, pt2;
00138 int numPt;
00139 double oldDist, slope;
00140 for(int i=0; i<recordCount; i++){
00141 SHPObject* shpObj = SHPReadObject(shp, i);
00142 pt1.x = shpObj->padfX[0];
00143 pt1.y = shpObj->padfY[0];
00144 pt2.x = shpObj->padfX[shpObj->nVertices-1];
00145 pt2.y = shpObj->padfY[shpObj->nVertices-1];
00146 slope = SLOPE(pt1, pt2);
00147 Point pt;
00148 int j;
00149 for(j=1; j<=numNode; j++){
00150 if(distPt(pt1, node[j])<0.001)
00151 break;
00152 }
00153 numPt = j;
00154
00155
00157
00158
00159
00160
00161
00162
00164
00165 oldDist = distPt(node[numPt], pt2);
00166
00167
00168
00169 while(distPt(pt2, node[numPt])>0.001){
00170
00171
00172
00173
00174
00175
00176 int j, k, l;
00177 for(j=0; j<neighNodeCount[numPt]; j++){
00178
00179
00180
00181
00182
00183
00184 if(fabs(slope-SLOPE(node[numPt], node[neighNode[numPt][j]])) < 0.001)
00185
00186 break;
00187 }
00188
00189
00190 X[0] = node[numPt].x;
00191 Y[0] = node[numPt].y;
00192 X[1] = node[neighNode[numPt][j]].x;
00193 Y[1] = node[neighNode[numPt][j]].y;
00194 FromTo[0]=numPt;
00195 FromTo[1]=neighNode[numPt][j];
00196 int m =0;
00197 for(k=0; k<nodeInEleCount[numPt]; k++){
00198 for(l=0; l<nodeInEleCount[neighNode[numPt][j]]; l++){
00199 if(nodeInEle[numPt][k] == nodeInEle[neighNode[numPt][j]][l])
00200 LeftRight[m++] = nodeInEle[numPt][k];
00201 }
00202 }
00203 SHPObject* newobj = SHPCreateSimpleObject(SHPT_ARC, 2, X, Y, Z);
00204 SHPWriteObject(newshp, -1, newobj);
00205 DBFWriteIntegerAttribute(newdbf, record, left, LeftRight[0]);
00206 DBFWriteIntegerAttribute(newdbf, record, right, LeftRight[1]);
00207 DBFWriteIntegerAttribute(newdbf, record, fromTriNode, FromTo[0]);
00208 DBFWriteIntegerAttribute(newdbf, record, toTriNode, FromTo[1]);
00209 record++;
00210
00211 numPt = neighNode[numPt][j];
00212
00213 oldDist = distPt(node[numPt], pt2);
00214 }
00215
00216 }
00217
00218 SHPClose(shp);
00219 DBFClose(dbf);
00220 SHPClose(newshp);
00221 DBFClose(newdbf);
00222
00223
00224
00225 }
00226
00227
00228