Changeset 10275


Ignore:
Timestamp:
10/24/11 14:45:32 (13 years ago)
Author:
jschierm
Message:

Shp2Kmlx: Working version, except for polygons.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk/src/c/modules/Shp2Kmlx/Shp2Kmlx.cpp

    r10240 r10275  
    1919        double  cm,sp;
    2020
    21         Xy2lldef(&cm,&sp,sgn);
     21        if (sgn)
     22                Xy2lldef(&cm,&sp,sgn);
    2223
    2324        return(Shp2Kmlx(filshp,filkml,
     
    3435        #ifdef _HAVE_SHAPELIB_ //only works if Shapelib library has been compiled in.
    3536       
    36         int     i,j,iret=0;
     37        int     i,j,k,iret=0;
    3738        int     lwidth=1;
    3839        double  popac=0.50;
    39         int     nprof;
    40         int     *pnvert=NULL,*pptype=NULL;
    41         double  **pprofx=NULL,**pprofy=NULL,**pprofz=NULL,**pprofm=NULL;
     40        int     nshap;
     41        int     *pstype=NULL,*pnpart=NULL,**ppstrt=NULL,**pptype=NULL,*pnvert=NULL;
     42        double  **pshapx=NULL,**pshapy=NULL,**pshapz=NULL,**pshapm=NULL;
    4243        bool    *closed=NULL;
    4344        double  *lat=NULL,*lon=NULL;
     
    4950
    5051        char    indent[81]="";
    51         KML_File*          kfile =NULL;
    52         KML_Document*      kdoc  =NULL;
    53         KML_Style*         kstyle=NULL;
    54         KML_LineStyle*     klsty =NULL;
    55         KML_PolyStyle*     kpsty =NULL;
    56         KML_Folder*        kfold =NULL;
    57         KML_Placemark*     kplace=NULL;
    58         KML_Polygon*       kpoly =NULL;
    59         KML_LinearRing*    kring =NULL;
    60         KML_LineString*    kline =NULL;
     52        KML_File*             kfile =NULL;
     53        KML_Document*         kdoc  =NULL;
     54        KML_Style*            kstyle=NULL;
     55        KML_LineStyle*        klsty =NULL;
     56        KML_PolyStyle*        kpsty =NULL;
     57        KML_Folder*           kfold =NULL;
     58        KML_Placemark*        kplace=NULL;
     59        KML_MultiGeometry*    kmulti=NULL;
     60        KML_Polygon*          kpoly =NULL;
     61        KML_LinearRing*       kring =NULL;
     62        KML_LineString*       kline =NULL;
     63        KML_Point*            kpoint=NULL;
    6164
    6265        FILE*   fid=NULL;
     
    9497                        adfMaxBound[3] );
    9598
    96         nprof=nEntities;
    97         pnvert=(int *) xmalloc(nprof*sizeof(int));
    98         pptype=(int *) xmalloc(nprof*sizeof(int));
    99         pprofx=(double **) xmalloc(nprof*sizeof(double *));
    100         pprofy=(double **) xmalloc(nprof*sizeof(double *));
    101         pprofz=(double **) xmalloc(nprof*sizeof(double *));
    102         pprofm=(double **) xmalloc(nprof*sizeof(double *));
    103 
    104         if (!DomainOutlineRead(&nprof,&pnvert,&pprofx,&pprofy,&closed,filshp,false))
     99        nshap=nEntities;
     100        pstype=(int *) xmalloc(nshap*sizeof(int));
     101        pnpart=(int *) xmalloc(nshap*sizeof(int));
     102        ppstrt=(int **) xmalloc(nshap*sizeof(int *));
     103        pptype=(int **) xmalloc(nshap*sizeof(int *));
     104        pnvert=(int *) xmalloc(nshap*sizeof(int));
     105        pshapx=(double **) xmalloc(nshap*sizeof(double *));
     106        pshapy=(double **) xmalloc(nshap*sizeof(double *));
     107        pshapz=(double **) xmalloc(nshap*sizeof(double *));
     108        pshapm=(double **) xmalloc(nshap*sizeof(double *));
    105109
    106110/*  loop over the list of shapes  */
     
    123127                                psShape->dfZMax, psShape->dfMMax );
    124128
     129        pstype[i]=psShape->nSHPType;
     130        pnpart[i]=psShape->nParts;
     131        if (pnpart[i]) {
     132                ppstrt[i]=(int *) xmalloc(pnpart[i]*sizeof(int));
     133                pptype[i]=(int *) xmalloc(pnpart[i]*sizeof(int));
     134        }
     135        else {
     136                ppstrt[i]=NULL;
     137                pptype[i]=NULL;
     138        }
    125139        pnvert[i]=psShape->nVertices;
    126         pptype[i]=psShape->nSHPType;
    127         pprofx[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
    128         pprofy[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
    129         pprofz[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
    130         pprofm[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
     140        if (pnvert[i]) {
     141                pshapx[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
     142                pshapy[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
     143                pshapz[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
     144                pshapm[i]=(double *) xmalloc(pnvert[i]*sizeof(double));
     145        }
     146        else {
     147                pshapx[i]=NULL;
     148                pshapy[i]=NULL;
     149                pshapz[i]=NULL;
     150                pshapm[i]=NULL;
     151        }
    131152
    132153        for( j = 0, iPart = 1; j < psShape->nVertices; j++ )
     
    135156
    136157                        if( j == 0 && psShape->nParts > 0 )
     158                        {
    137159                                pszPartType = SHPPartTypeName( psShape->panPartType[0] );
     160                                ppstrt[i][0]=psShape->panPartStart[0];
     161                                pptype[i][0]=psShape->panPartType[0];
     162                        }
    138163
    139164                if( iPart < psShape->nParts
     
    141166                {
    142167                                pszPartType = SHPPartTypeName( psShape->panPartType[iPart] );
     168                                ppstrt[i][iPart]=psShape->panPartStart[iPart];
     169                                pptype[i][iPart]=psShape->panPartType[iPart];
    143170                iPart++;
    144171                pszPlus = "+";
     
    155182//                                 pszPartType );
    156183
    157                 pprofx[i][j]=psShape->padfX[j];
    158                 pprofy[i][j]=psShape->padfY[j];
    159                 pprofz[i][j]=psShape->padfZ[j];
    160                 pprofm[i][j]=psShape->padfM[j];
     184                pshapx[i][j]=psShape->padfX[j];
     185                pshapy[i][j]=psShape->padfY[j];
     186                pshapz[i][j]=psShape->padfZ[j];
     187                pshapm[i][j]=psShape->padfM[j];
    161188        }
    162189
     
    179206
    180207        SHPClose( hSHP );
    181         return(iret);
    182 
    183 /*  read shp file  */
    184 
    185         if (!DomainOutlineRead(&nprof,&pnvert,&pprofx,&pprofy,&closed,filshp,false))
    186                 _error_("Error reading shp file.");
     208
     209/*  read exp file  */
     210
     211//      if (!DomainOutlineRead(&nshap,&pnvert,&pshapx,&pshapy,&closed,filshp,false))
     212//              _error_("Error reading exp file.");
    187213
    188214/*  construct kml file  */
     
    238264        (kdoc->style     )->AddObject((Object*)kstyle);
    239265
    240 /*  construct kml folder for polygons  */
     266/*  construct kml folder for shapes  */
    241267
    242268        kfold =new KML_Folder();
    243         sprintf(kfold->name      ,"Profiles translated from file \"%s\".",filshp);
     269        sprintf(kfold->name      ,"Shapefile: %s  Type: %s  nShapes: %d",
     270                        filshp, SHPTypeName( nShapeType ), nEntities );
    244271        kfold->open      =1;
    245272
     273/*  loop over the list of shapes  */
     274
     275        for (i=0; i<nshap; i++) {
     276
     277/*  null type  */
     278
     279                if      (pstype[i] == SHPT_NULL) {
     280                        ;
     281                }
     282
     283/*  point types  */
     284
     285                else if (pstype[i] == SHPT_POINT ||
     286                                 pstype[i] == SHPT_POINTZ ||
     287                                 pstype[i] == SHPT_POINTM) {
     288                        kplace=new KML_Placemark();
     289
     290                        sprintf(kplace->name      ,"Shape:%d (%s)  nVertices=%d, nParts=%d",
     291                                        i,SHPTypeName(pstype[i]),pnvert[i],pnpart[i]);
     292                        kplace->visibility=true;
     293                        sprintf(kplace->styleurl  ,"#RandomLineEmptyPoly");
     294
     295                        if (pnpart[i] > 0)
     296                                _printf_(true,"Warning -- Shape %d of type \"%s\" should not have %d > 0 parts.\n",
     297                                                 i,SHPTypeName( pstype[i] ),pnpart[i]);
     298                        if (pnvert[i] > 1)
     299                                _printf_(true,"Warning -- Shape %d of type \"%s\" should not have %d > 1 vertices.\n",
     300                                                 i,SHPTypeName( pstype[i] ),pnvert[i]);
     301
     302                        kpoint=new KML_Point();
     303
     304                        lat=(double *) xmalloc(pnvert[i]*sizeof(double));
     305                        lon=(double *) xmalloc(pnvert[i]*sizeof(double));
     306                        if (sgn) {
     307                                Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
     308                        }
     309                        else  {
     310                                memcpy(lon,pshapx[i],pnvert[i]*sizeof(double));
     311                                memcpy(lat,pshapy[i],pnvert[i]*sizeof(double));
     312                        }
     313
     314                        kpoint->coords[0]=lon      [0];
     315                        kpoint->coords[1]=lat      [0];
     316                        kpoint->coords[2]=pshapz[i][0];
     317
     318                        xfree((void**)&lon);
     319                        xfree((void**)&lat);
     320
     321                        (kplace->geometry  )->AddObject((Object*)kpoint);
     322                        kpoint=NULL;
     323                        (kfold ->feature   )->AddObject((Object*)kplace);
     324                        kplace=NULL;
     325                }
     326
     327/*  polyline types  */
     328
     329                else if (pstype[i] == SHPT_ARC ||
     330                                 pstype[i] == SHPT_ARCZ ||
     331                                 pstype[i] == SHPT_ARCM) {
     332                        kplace=new KML_Placemark();
     333
     334                        sprintf(kplace->name      ,"Shape:%d (%s)  nVertices=%d, nParts=%d",
     335                                        i,SHPTypeName(pstype[i]),pnvert[i],pnpart[i]);
     336                        kplace->visibility=true;
     337                        sprintf(kplace->styleurl  ,"#RandomLineEmptyPoly");
     338
     339                        kmulti=new KML_MultiGeometry();
     340
     341                        lat=(double *) xmalloc(pnvert[i]*sizeof(double));
     342                        lon=(double *) xmalloc(pnvert[i]*sizeof(double));
     343                        if (sgn) {
     344                                Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
     345                        }
     346                        else  {
     347                                memcpy(lon,pshapx[i],pnvert[i]*sizeof(double));
     348                                memcpy(lat,pshapy[i],pnvert[i]*sizeof(double));
     349                        }
     350
     351                        for (j=0; j<pnpart[i]; j++) {
     352                                kline =new KML_LineString();
     353
     354                                kline->ncoord    =(j<pnpart[i]-1 ? ppstrt[i][j+1]-ppstrt[i][j] : pnvert[i]-ppstrt[i][j]);
     355                                kline->coords    =(double (*)[3]) xmalloc(kline->ncoord*3*sizeof(double));
     356                                for (k=0; k<kline->ncoord; k++) {
     357                                        kline->coords[k][0]=lon      [ppstrt[i][j]+k];
     358                                        kline->coords[k][1]=lat      [ppstrt[i][j]+k];
     359                                        kline->coords[k][2]=pshapz[i][ppstrt[i][j]+k];
     360                                }
     361
     362                                (kmulti->geometry  )->AddObject((Object*)kline);
     363                                kline =NULL;
     364                        }
     365
     366                        xfree((void**)&lon);
     367                        xfree((void**)&lat);
     368
     369                        (kplace->geometry  )->AddObject((Object*)kmulti);
     370                        kmulti=NULL;
     371                        (kfold ->feature   )->AddObject((Object*)kplace);
     372                        kplace=NULL;
     373                }
     374
     375/*  polygon types  */
     376
     377                else if (pstype[i] == SHPT_POLYGON ||
     378                                 pstype[i] == SHPT_POLYGONZ ||
     379                                 pstype[i] == SHPT_POLYGONM) {
     380                        _printf_(true,"Warning -- Shape %d of type \"%s\" will be ignored.\n",
     381                                         i,SHPTypeName( pstype[i] ));
     382                        continue;
     383
    246384/*  polygon with multiple holes  */
    247 
    248         if (holes && nprof && !closed[0]) {
    249                 _printf_(true,"Warning -- Outer profile is not closed, so \"holes\" option will be ignored.\n");
    250                 holes=false;
    251         }
    252385
    253386        if (holes) {
     
    263396                lat=(double *) xmalloc(pnvert[i]*sizeof(double));
    264397                lon=(double *) xmalloc(pnvert[i]*sizeof(double));
    265                 Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
     398                Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
    266399
    267400                kring->ncoord    =pnvert[i];
     
    278411                kring =NULL;
    279412
    280                 for (i=1; i<nprof; i++) {
     413                for (i=1; i<nshap; i++) {
    281414                        if (!closed[i]) {
    282415                                _printf_(true,"Warning -- Inner profile %d is not closed with \"holes\" specified, so it will be ignored.\n",i+1);
     
    288421                        lat=(double *) xmalloc(pnvert[i]*sizeof(double));
    289422                        lon=(double *) xmalloc(pnvert[i]*sizeof(double));
    290                         Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
     423                        Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
    291424                        kring->ncoord    =pnvert[i];
    292425                        kring->coords    =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
     
    312445
    313446        else {
    314                 for (i=0; i<nprof; i++) {
     447                for (i=0; i<nshap; i++) {
    315448                        kplace=new KML_Placemark();
    316449
     
    325458                                lat=(double *) xmalloc(pnvert[i]*sizeof(double));
    326459                                lon=(double *) xmalloc(pnvert[i]*sizeof(double));
    327                                 Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
     460                                Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
    328461
    329462                                kring->ncoord    =pnvert[i];
     
    344477                        }
    345478
    346                         else {
    347                                 sprintf(kplace->name      ,"LineString %d",i+1);
    348                                 kplace->visibility=true;
    349                                 sprintf(kplace->styleurl  ,"#RandomLineEmptyPoly");
    350 
    351                                 kline =new KML_LineString();
    352 
    353                                 lat=(double *) xmalloc(pnvert[i]*sizeof(double));
    354                                 lon=(double *) xmalloc(pnvert[i]*sizeof(double));
    355                                 Xy2llx(lat,lon,pprofx[i],pprofy[i],pnvert[i],sgn,cm,sp);
    356 
    357                                 kline->ncoord    =pnvert[i];
    358                                 kline->coords    =(double (*)[3]) xmalloc(pnvert[i]*3*sizeof(double));
    359                                 for (j=0; j<pnvert[i]; j++) {
    360                                         kline->coords[j][0]=lon[j];
    361                                         kline->coords[j][1]=lat[j];
    362                                         kline->coords[j][2]=0.;
    363                                 }
    364                                 xfree((void**)&lon);
    365                                 xfree((void**)&lat);
    366 
    367                                 (kplace->geometry  )->AddObject((Object*)kline);
    368                                 kline =NULL;
    369                         }
    370 
    371479                        (kfold ->feature   )->AddObject((Object*)kplace);
    372480                        kplace=NULL;
    373481                }
     482        }
     483                }
     484
     485/*  multipoint types  */
     486
     487                else if (pstype[i] == SHPT_MULTIPOINT ||
     488                                 pstype[i] == SHPT_MULTIPOINTZ ||
     489                                 pstype[i] == SHPT_MULTIPOINTM) {
     490                        kplace=new KML_Placemark();
     491
     492                        sprintf(kplace->name      ,"Shape:%d (%s)  nVertices=%d, nParts=%d",
     493                                        i,SHPTypeName(pstype[i]),pnvert[i],pnpart[i]);
     494                        kplace->visibility=true;
     495                        sprintf(kplace->styleurl  ,"#RandomLineEmptyPoly");
     496
     497                        if (pnpart[i] > 0)
     498                                _printf_(true,"Warning -- Shape %d of type \"%s\" should not have %d > 0 parts.\n",
     499                                                 i,SHPTypeName( pstype[i] ),pnpart[i]);
     500
     501                        kmulti=new KML_MultiGeometry();
     502
     503                        lat=(double *) xmalloc(pnvert[i]*sizeof(double));
     504                        lon=(double *) xmalloc(pnvert[i]*sizeof(double));
     505                        if (sgn) {
     506                                Xy2llx(lat,lon,pshapx[i],pshapy[i],pnvert[i],sgn,cm,sp);
     507                        }
     508                        else  {
     509                                memcpy(lon,pshapx[i],pnvert[i]*sizeof(double));
     510                                memcpy(lat,pshapy[i],pnvert[i]*sizeof(double));
     511                        }
     512
     513                        for (j=0; j<pnvert[i]; j++) {
     514                                kpoint=new KML_Point();
     515
     516                                kpoint->coords[0]=lon      [j];
     517                                kpoint->coords[1]=lat      [j];
     518                                kpoint->coords[2]=pshapz[i][j];
     519
     520                                (kmulti->geometry  )->AddObject((Object*)kpoint);
     521                                kpoint=NULL;
     522                        }
     523
     524                        xfree((void**)&lon);
     525                        xfree((void**)&lat);
     526
     527                        (kplace->geometry  )->AddObject((Object*)kmulti);
     528                        kmulti=NULL;
     529                        (kfold ->feature   )->AddObject((Object*)kplace);
     530                        kplace=NULL;
     531                }
     532
     533/*  multipatch types  */
     534
     535                else if (pstype[i] == SHPT_MULTIPATCH) {
     536                }
     537
     538/*  unknown type  */
     539
     540                else {
     541                        _printf_(true,"Warning -- Shape %d of type \"%s\" will be ignored.\n",
     542                                         i,SHPTypeName( pstype[i] ));
     543                }
     544
    374545        }
    375546
     
    390561
    391562        delete kfile;
    392         for (i=nprof-1; i>=0; i--) {
    393                 xfree((void**)&(pprofy[i]));
    394                 xfree((void**)&(pprofx[i]));
    395         }
     563        for (i=nshap-1; i>=0; i--) {
     564                xfree((void**)&(pshapm[i]));
     565                xfree((void**)&(pshapz[i]));
     566                xfree((void**)&(pshapy[i]));
     567                xfree((void**)&(pshapx[i]));
     568        }
     569        xfree((void**)&pshapm);
     570        xfree((void**)&pshapz);
     571        xfree((void**)&pshapy);
     572        xfree((void**)&pshapx);
    396573        xfree((void**)&pnvert);
     574        for (i=nshap-1; i>=0; i--) {
     575                xfree((void**)&(pptype[i]));
     576                xfree((void**)&(ppstrt[i]));
     577        }
     578        xfree((void**)&pptype);
     579        xfree((void**)&ppstrt);
     580        xfree((void**)&pnpart);
     581        xfree((void**)&pstype);
    397582
    398583        clock1=clock();
Note: See TracChangeset for help on using the changeset viewer.