pihmLIBS/simplifyPolySHP.h

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <iomanip>
00003 #include "simplifyPolyline.h"
00004 #include "shapefil.h"
00005 
00006 using namespace std;
00007 
00008 void simplifyPolySHP(const char *shpFileName, const char *dbfFileName, const char *newshpFileName, const char *newdbfFileName, double tolerance){
00009         
00010         cout<<"Simplifying "<<shpFileName<<"...";
00011         //cout<<dbfFileName<<"\n";
00012         SHPHandle shp = SHPOpen(shpFileName, "rb");
00013         DBFHandle dbf = DBFOpen(dbfFileName, "rb");
00014 
00015         int recordCount, shpType;
00016         
00017         SHPObject *obj1 = SHPReadObject(shp, 0);
00018         shpType=obj1->nSHPType;
00019         recordCount=DBFGetRecordCount(dbf);
00020 
00021         int fld = DBFGetFieldIndex(dbf, "ARCID");
00022 
00023         //cout<<"recordCount= "<<recordCount<<"\n";
00024         //cout<<"shpType= "<<shpType<<"\n";
00025         
00026         SHPHandle newshp = SHPCreate(newshpFileName, shpType);
00027         DBFHandle newdbf = DBFCreate(newdbfFileName);
00028         
00029         int arcidField = DBFAddField(newdbf, "ARCID", FTInteger, 5, 0);
00030 
00031         for(int i=0; i<recordCount; i++){
00032                 SHPObject* obj = SHPReadObject(shp, i);
00033 
00034                 Point* pts = new Point[obj->nVertices];
00035                 int *marker= new int[obj->nVertices];
00036 
00037                 for(int j=0; j<obj->nVertices; j++){
00038                         marker[j]=0;
00039                         pts[j].x=obj->padfX[j];
00040                         pts[j].y=obj->padfY[j];
00041                 }
00042 
00043                 marker[0]=1;
00044                 simplifyPolyline(pts, 0, obj->nVertices-1, tolerance, marker);//, 0);
00045                 marker[obj->nVertices-1]=1;
00046 
00047                 int count=0;
00048                 for(int j=0; j<obj->nVertices; j++){
00049                         count=count+marker[j];
00050                 }
00051         
00052                 double *X = new double[count];
00053                 double *Y = new double[count];
00054                 double *Z = new double[count];
00055                 
00056                 int k=0;
00057                 for(int j=0; j<obj->nVertices; j++){
00058                         if(marker[j]==1){
00059                                 X[k]=pts[j].x;
00060                                 Y[k]=pts[j].y;
00061                                 Z[k]=0;
00062                                 k++;
00063                         }
00064                 }
00065                 SHPObject *newobj = SHPCreateSimpleObject(shpType, count, X, Y, Z);
00066                 SHPComputeExtents(newobj);
00067                 SHPWriteObject(newshp, -1, newobj);
00068                 
00069                 int arcid = DBFReadIntegerAttribute(dbf, i, fld);
00070                 DBFWriteIntegerAttribute(newdbf, i, arcidField, arcid);
00071         }
00072         cout<<"Done!\n\n";
00073 
00074         SHPClose(shp);
00075         DBFClose(dbf);
00076 
00077         SHPClose(newshp);
00078         DBFClose(newdbf);
00079 }
00080 
00081 

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