00001
00002
00003
00004 #include <iostream>
00005 #include <fstream>
00006 #include <iomanip>
00007 #include <math.h>
00008
00009 #include "shapefil.h"
00010
00011 using namespace std;
00012
00013
00014 #define dist(x1,y1,x2,y2) fabs(x1-x2)+fabs(y1-y2)
00015
00016 void sortArray(int ***junction, int* jctCount, int recordCount){
00017 int temp;
00018 int a, b, c, d;
00019 for(int i=0; i<recordCount; i++){
00020 for(int j=0; j<jctCount[i]; j++){
00021 for(int k=0; k<j; k++){
00022 if(junction[i][j][1] < junction[i][k][1]){
00023 temp = junction[i][j][2];
00024 junction[i][j][2] = junction[i][k][2];
00025 junction[i][k][2] = temp;
00026
00027 temp = junction[i][j][1];
00028 junction[i][j][1] = junction[i][k][1];
00029 junction[i][k][1] = temp;
00030
00031 temp = junction[i][j][0];
00032 junction[i][j][0] = junction[i][k][0];
00033 junction[i][k][0] = temp;
00034 }
00035 }
00036 }
00037 for(int j=0; j<jctCount[i]-1; j++){
00038 if(junction[i][j][1]==junction[i][j+1][1]){
00039 if(junction[i][j][0]<junction[i][j+1][0]){
00040 int k=j+1;
00041 temp = junction[i][j][2];
00042 junction[i][j][2] = junction[i][k][2];
00043 junction[i][k][2] = temp;
00044
00045 temp = junction[i][j][1];
00046 junction[i][j][1] = junction[i][k][1];
00047 junction[i][k][1] = temp;
00048
00049 temp = junction[i][j][0];
00050 junction[i][j][0] = junction[i][k][0];
00051 junction[i][k][0] = temp;
00052 }
00053 }
00054 }
00055 if(jctCount[i]>=2){
00056 a = junction[i][jctCount[i]-2][0];
00057 b = junction[i][jctCount[i]-2][1];
00058 c = junction[i][jctCount[i]-1][0];
00059 d = junction[i][jctCount[i]-1][1];
00060 if(c<=a && b<=d){
00061 junction[i][jctCount[i]-1][0] = d;
00062 junction[i][jctCount[i]-1][1] = c;
00063 }
00064 }
00065
00066 }
00067 }
00068
00069
00070 void calPts(int start, int end, SHPObject* handle, double *X, double *Y, double *Z, int node, int *vertices){
00071
00072 *vertices = 0;
00073 X = new double[node];
00074 Y = new double[node];
00075 Z = new double[node];
00076 for(int i=start; i<=end; i++){
00077 X[*vertices]=handle->padfX[i];
00078 Y[*vertices]=handle->padfY[i];
00079 Z[*vertices]=0;
00080 (*vertices)++;
00081 }
00082 }
00083
00084
00085 void polygonToPolyline(const char* shpFileName, const char* dbfFileName, const char *newshpFileName, const char *newdbfFileName){
00086 int recordCount;
00087
00088 SHPHandle shp = SHPOpen(shpFileName, "rb");
00089 DBFHandle dbf = DBFOpen(dbfFileName, "rb");
00090
00091 SHPHandle newshp = SHPCreate(newshpFileName, SHPT_ARC);
00092 DBFHandle newdbf = DBFCreate(newdbfFileName);
00093
00094 int field = DBFAddField(newdbf, "Field", FTInteger, 5, 0);
00095
00096 SHPObject *obj1, *obj2;
00097 recordCount = DBFGetRecordCount(dbf);
00098
00099 int ***junction = new int**[recordCount];
00100 for(int i=0; i<recordCount; i++){
00101 junction[i] = new int*[30];
00102 for(int j=0; j<30; j++){
00103 junction[i][j]=new int[3];
00104 }
00105 }
00106 int *vertex = new int[recordCount];
00107 int *jctCount = new int[recordCount];
00108 int jct1[2];
00109 int jct2[2];
00110 double x1, x2, x3, x4, y1, y2, y3, y4;
00111 int *pts1, *pts2, pts1count, pts2count;
00112 for( int i=0; i<recordCount; i++){
00113
00114 obj1 = SHPReadObject(shp, i);
00115 vertex[i]=obj1->nVertices;
00116 jctCount[i]=0;
00117
00118 for( int j=0; j<i; j++){
00119
00120
00121 obj2 = SHPReadObject(shp, j);
00122 int ii, jj, iii, jjj;
00123
00124 int ptcount=0;
00125 int breakFlag=0;
00126 if(!((obj1->dfXMax<obj2->dfXMin)||(obj2->dfXMax<obj1->dfXMin)||(obj1->dfYMax<obj2->dfYMin)||(obj2->dfYMax<obj1->dfYMin))){
00127
00128
00129 pts1 = new int[obj1->nVertices];
00130 pts1count = 0;
00131 pts2 = new int[obj2->nVertices];
00132 pts2count = 0;
00133
00134 for(int p1=0; p1<obj1->nVertices-1; p1++){
00135 if( (obj2->dfXMin-1<=obj1->padfX[p1]&&obj1->padfX[p1]<=obj2->dfXMax+1) && (obj2->dfYMin-1<=obj1->padfY[p1]&&obj1->padfY[p1]<=obj2->dfYMax+1)){
00136 pts1[pts1count++]=p1;
00137 }
00138 }
00139
00140 for(int p2=0; p2<obj2->nVertices-1; p2++){
00141 if( (obj1->dfXMin-1<=obj2->padfX[p2]&&obj2->padfX[p2]<=obj1->dfXMax+1) && (obj1->dfYMin-1<=obj2->padfY[p2]&&obj2->padfY[p2]<=obj1->dfYMax+1)){
00142 pts2[pts2count++]=p2;
00143 }
00144 }
00145
00146 for(iii=0; iii<pts1count; iii++){
00147 for(jjj=0; jjj<pts2count; jjj++){
00148 ii=pts1[iii];
00149 jj=pts2[jjj];
00150 x1 = obj1->padfX[ii];
00151 x2 = obj2->padfX[jj];
00152 y1 = obj1->padfY[ii];
00153 y2 = obj2->padfY[jj];
00154
00155 if(dist(x1,y1,x2,y2)<0.001){
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 x1 = ii==obj1->nVertices-1?obj1->padfX[1]:obj1->padfX[ii+1];
00168 x2 = ii==0?obj1->padfX[obj1->nVertices-2]:obj1->padfX[ii-1];
00169 x3 = jj==obj2->nVertices-1?obj2->padfX[1]:obj2->padfX[jj+1];
00170 x4 = jj==0?obj2->padfX[obj2->nVertices-2]:obj2->padfX[jj-1];
00171
00172 y1 = ii==obj1->nVertices-1?obj1->padfY[1]:obj1->padfY[ii+1];
00173 y2 = ii==0?obj1->padfY[obj1->nVertices-2]:obj1->padfY[ii-1];
00174 y3 = jj==obj2->nVertices-1?obj2->padfY[1]:obj2->padfY[jj+1];
00175 y4 = jj==0?obj2->padfY[obj2->nVertices-2]:obj2->padfY[jj-1];
00176
00177 if((dist(x1, y1, x3, y3) < 0.001)){
00178 if(dist(x2, y2, x4, y4) >= 0.001){
00179
00180 jct1[ptcount]=ii;
00181 jct2[ptcount]=jj;
00182 ptcount++;
00183
00184 }
00185 }
00186 if((dist(x1, y1, x4, y4) < 0.001)){
00187 if(dist(x2, y2, x3, y3) >= 0.001){
00188
00189 jct1[ptcount]=ii;
00190 jct2[ptcount]=jj;
00191 ptcount++;
00192
00193 }
00194 }
00195 if((dist(x2, y2, x4, y4) < 0.001)){
00196 if( dist(x1, y1, x3, y3)>= 0.001){
00197
00198 jct1[ptcount]=ii;
00199 jct2[ptcount]=jj;
00200 ptcount++;
00201
00202 }
00203 }
00204 if((dist(x2, y2, x3, y3) < 0.001)){
00205 if( dist(x1, y1, x4, y4)>= 0.001){
00206
00207 jct1[ptcount]=ii;
00208 jct2[ptcount]=jj;
00209 ptcount++;
00210
00211 }
00212 }
00213
00214
00215 }
00216
00217 if(ptcount==2){
00218 breakFlag=1;
00219 break;
00220 }
00221 }
00222
00223 if(breakFlag==1)
00224 break;
00225 }
00226 }
00227
00228 if(ptcount==2){
00229 junction[i][jctCount[i]][0]=jct1[0]<jct1[1]?jct1[0]:jct1[1];
00230 junction[i][jctCount[i]][1]=jct1[0]<jct1[1]?jct1[1]:jct1[0];
00231 junction[i][jctCount[i]][2]=1;
00232 jctCount[i]++;
00233
00234 junction[j][jctCount[j]][0]=jct2[0]<jct2[1]?jct2[0]:jct2[1];
00235 junction[j][jctCount[j]][1]=jct2[0]<jct2[1]?jct2[1]:jct2[0];
00236 junction[j][jctCount[j]][2]=0;
00237 jctCount[j]++;
00238
00239
00240 }
00241 }
00242 }
00243
00244
00245 for(int i=0; i<recordCount; i++){
00246 obj1 = SHPReadObject(shp, i);
00247
00248 if(jctCount[i]==0){
00249 jctCount[i]=1;
00250 junction[i][0][0]=0;
00251 junction[i][0][1]=obj1->nVertices-1;
00252 junction[i][0][2]=1;
00253 }
00254 }
00255
00256 sortArray(junction, jctCount, recordCount);
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 int record=0, start, end;
00267
00268 SHPObject *newobj;
00269 for(int i=0; i<recordCount; i++){
00270 obj1=SHPReadObject(shp, i);
00271 int nodes=obj1->nVertices;
00272 double *X= new double[nodes];
00273 double *Y= new double[nodes];
00274 double *Z= new double[nodes];
00275 int vertices;
00276
00277 if(junction[i][jctCount[i]-1][1]<junction[i][jctCount[i]-1][0]){
00278 if(junction[i][jctCount[i]-1][2]==1){
00279 if(junction[i][jctCount[i]-1][1]!=0){
00280
00281
00282 start=0;
00283 end=junction[i][jctCount[i]-1][1];
00284 vertices=0;
00285 for(int i=start; i<=end; i++){
00286 X[vertices]=obj1->padfX[i];
00287 Y[vertices]=obj1->padfY[i];
00288 Z[vertices]=0;
00289 vertices++;
00290 }
00291 newobj = SHPCreateSimpleObject(SHPT_ARC, vertices, X, Y, Z);
00292 SHPWriteObject(newshp, -1, newobj);
00293 DBFWriteIntegerAttribute(newdbf, record, field, record+1);record++;
00294
00295 }
00296
00297
00298 start=junction[i][jctCount[i]-1][0];
00299 end=vertex[i]-1;
00300 vertices=0;
00301 for(int i=start; i<=end; i++){
00302 X[vertices]=obj1->padfX[i];
00303 Y[vertices]=obj1->padfY[i];
00304 Z[vertices]=0;
00305 vertices++;
00306 }
00307 newobj = SHPCreateSimpleObject(SHPT_ARC, vertices, X, Y, Z);
00308 SHPWriteObject(newshp, -1, newobj);
00309 DBFWriteIntegerAttribute(newdbf, record, field, record+1);record++;
00310
00311 }
00312 if(junction[i][0][0]>junction[i][jctCount[i]-1][1]){
00313
00314
00315 start=junction[i][jctCount[i]-1][1];
00316 end=junction[i][0][0];
00317 vertices=0;
00318 for(int i=start; i<=end; i++){
00319 X[vertices]=obj1->padfX[i];
00320 Y[vertices]=obj1->padfY[i];
00321 Z[vertices]=0;
00322 vertices++;
00323 }
00324 newobj = SHPCreateSimpleObject(SHPT_ARC, vertices, X, Y, Z);
00325 SHPWriteObject(newshp, -1, newobj);
00326 DBFWriteIntegerAttribute(newdbf, record, field, record+1);record++;
00327
00328 }
00329 }
00330 else{
00331 if(junction[i][0][0]!=0){
00332
00333
00334 start=0;
00335 end=junction[i][0][0];
00336 vertices=0;
00337 for(int i=start; i<=end; i++){
00338 X[vertices]=obj1->padfX[i];
00339 Y[vertices]=obj1->padfY[i];
00340 Z[vertices]=0;
00341 vertices++;
00342 }
00343 newobj = SHPCreateSimpleObject(SHPT_ARC, vertices, X, Y, Z);
00344 SHPWriteObject(newshp, -1, newobj);
00345 DBFWriteIntegerAttribute(newdbf, record, field, record+1);record++;
00346
00347 }
00348 if(junction[i][jctCount[i]-1][2]==1){
00349
00350
00351 start=junction[i][jctCount[i]-1][0];
00352 end=junction[i][jctCount[i]-1][1];
00353 vertices=0;
00354 for(int i=start; i<=end; i++){
00355 X[vertices]=obj1->padfX[i];
00356 Y[vertices]=obj1->padfY[i];
00357 Z[vertices]=0;
00358 vertices++;
00359 }
00360 newobj = SHPCreateSimpleObject(SHPT_ARC, vertices, X, Y, Z);
00361 SHPWriteObject(newshp, -1, newobj);
00362 DBFWriteIntegerAttribute(newdbf, record, field, record+1);record++;
00363
00364 }
00365 if(junction[i][jctCount[i]-1][1]<vertex[i]-1){
00366
00367
00368 start=junction[i][jctCount[i]-1][1];
00369 end=vertex[i]-1;
00370 vertices=0;
00371 for(int i=start; i<=end; i++){
00372 X[vertices]=obj1->padfX[i];
00373 Y[vertices]=obj1->padfY[i];
00374 Z[vertices]=0;
00375 vertices++;
00376 }
00377 newobj = SHPCreateSimpleObject(SHPT_ARC, vertices, X, Y, Z);
00378 SHPWriteObject(newshp, -1, newobj);
00379 DBFWriteIntegerAttribute(newdbf, record, field, record+1);record++;
00380
00381 }
00382 }
00383 for(int j=0; j<jctCount[i]-1; j++){
00384 if(junction[i][j][2]==1){
00385
00386
00387 start=junction[i][j][0];
00388 end=junction[i][j][1];
00389 vertices=0;
00390 for(int i=start; i<=end; i++){
00391 X[vertices]=obj1->padfX[i];
00392 Y[vertices]=obj1->padfY[i];
00393 Z[vertices]=0;
00394 vertices++;
00395 }
00396 newobj = SHPCreateSimpleObject(SHPT_ARC, vertices, X, Y, Z);
00397 SHPWriteObject(newshp, -1, newobj);
00398 DBFWriteIntegerAttribute(newdbf, record, field, record+1);record++;
00399
00400 }
00401 if(junction[i][j][1]<junction[i][j+1][0]){
00402
00403
00404 start=junction[i][j][1];
00405 end=junction[i][j+1][0];
00406 vertices=0;
00407 for(int i=start; i<=end; i++){
00408 X[vertices]=obj1->padfX[i];
00409 Y[vertices]=obj1->padfY[i];
00410 Z[vertices]=0;
00411 vertices++;
00412 }
00413 newobj = SHPCreateSimpleObject(SHPT_ARC, vertices, X, Y, Z);
00414 SHPWriteObject(newshp, -1, newobj);
00415 DBFWriteIntegerAttribute(newdbf, record, field, record+1);record++;
00416
00417
00418 }
00419 }
00420 }
00421 SHPClose(shp);
00422 DBFClose(dbf);
00423 SHPClose(newshp);
00424 DBFClose(newdbf);
00425 }
00426
00427
00428