shpdxf.c 9.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
/******************************************************************************
 * Copyright (c) 1999, Carl Anderson
 *
 * This code is based in part on the earlier work of Frank Warmerdam
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 ******************************************************************************
 *
 * shp2dxf.c
 *
 * derived from a ESRI Avenue Script
 * and DXF specification from AutoCad 3 (yes 1984)
 *
 * modifications Carl Andrson 11/96
 * modifications Carl Andrson 3/97
 *
 * converted to C code 12/98
 *
 * requires shapelib 1.2
 *   gcc shpdxf.c shpopen.o dbfopen.o -o shpdxf 
 *
 */

#include <stdlib.h>
#include <string.h>
#include "shapefil.h"

#define FLOAT_PREC "%16.5f\r\n"

void dxf_hdr (x1,y1,x2,y2,df)
double x1,y1,x2,y2;
FILE  *df;
{
/* Create HEADER section */

  fprintf( df, "  0\r\n");
  fprintf( df, "SECTION\r\n");
  fprintf( df, "  2\r\n" );
  fprintf( df, "HEADER\r\n" );
  fprintf( df, "  9\r\n" );
  fprintf( df, "$EXTMAX\r\n" );
  fprintf( df, " 10\r\n" );
  fprintf( df, FLOAT_PREC, x2 );
  fprintf( df, " 20\r\n" );
  fprintf( df, FLOAT_PREC, y2 );
  fprintf( df, "  9\r\n" );
  fprintf( df, "$EXTMIN\r\n" );
  fprintf( df, " 10\r\n" );
  fprintf( df, FLOAT_PREC, x1 );
  fprintf( df, " 20\r\n" );
  fprintf( df, FLOAT_PREC, y1 );
  fprintf( df, "  9\r\n" );
  fprintf( df, "$LUPREC\r\n" );
  fprintf( df, " 70\r\n" );
  fprintf( df, "    14\r\n" );
  fprintf( df, "  0\r\n" );
  fprintf( df, "ENDSEC\r\n" );

/*  ' Create TABLES section */

  fprintf( df, "  0\r\n" );
  fprintf( df, "SECTION\r\n" );
  fprintf( df, "  2\r\n" );
  fprintf( df, "TABLES\r\n" );
/*  ' Table 1 - set up line type */
  fprintf( df, "  0\r\n" );
  fprintf( df, "TABLE\r\n" );
  fprintf( df, "  2\r\n" );
  fprintf( df, "LTYPE\r\n" );
  fprintf( df, " 70\r\n" );
  fprintf( df, "2\r\n" );
/*  ' Entry 1 of Table 1 */
  fprintf( df, "  0\r\n" );
  fprintf( df, "LTYPE\r\n" );
  fprintf( df, "  2\r\n" );
  fprintf( df, "CONTINUOUS\r\n" );
  fprintf( df, " 70\r\n" );
  fprintf( df, "64\r\n" );
  fprintf( df, "  3\r\n" );
  fprintf( df, "Solid line\r\n" );
  fprintf( df, " 72\r\n" );
  fprintf( df, "65\r\n" );
  fprintf( df, " 73\r\n" );
  fprintf( df, "0\r\n" );
  fprintf( df, " 40\r\n" );
  fprintf( df, "0.0\r\n" );
  fprintf( df, "  0\r\n" );
  fprintf( df, "ENDTAB\r\n" );
 /* End of TABLES section */
  fprintf( df, "  0\r\n" );
  fprintf( df, "ENDSEC\r\n" );

  /* Create BLOCKS section  */
  
  fprintf( df, "  0\r\n" );
  fprintf( df, "SECTION\r\n" );
  fprintf( df, "  2\r\n" );
  fprintf( df, "BLOCKS\r\n" );
  fprintf( df, "  0\r\n" );
  fprintf( df, "ENDSEC\r\n" );
  fprintf( df, "  0\r\n" );
  fprintf( df, "SECTION\r\n" );
  fprintf( df, "  2\r\n" );
  fprintf( df, "ENTITIES\r\n" );
  
}


void
dxf_ent_preamble (dxf_type, id, df)
int dxf_type;
char *id;
FILE *df;
{

  fprintf( df, "  0\r\n" );

  switch (dxf_type) {
    case  SHPT_POLYGON:
    case  SHPT_ARC:	  fprintf (df, "POLYLINE\r\n");
          break;
    default:  fprintf(df, "POINT\r\n");
  }	   

  fprintf( df, "  8\r\n");
  fprintf( df, "%s\r\n", id );
  switch ( dxf_type ) {
    case SHPT_ARC:
                    fprintf( df, "  6\r\n" );
                    fprintf( df, "CONTINUOUS\r\n" );
                    fprintf( df, " 66\r\n" );
                    fprintf( df, "1\r\n" );
                 break;
    case SHPT_POLYGON:
                    fprintf( df, "  6\r\n" );
                    fprintf( df, "CONTINUOUS\r\n" );
                    fprintf( df, " 66\r\n" );
                    fprintf( df, "1\r\n" );
                    fprintf( df, " 70\r\n");
		    fprintf (df, "1\r\n");
    default:     break;
  } 

}

void
dxf_ent (id, x, y, z, dxf_type, df)
char *id;
double x,y,z;
int dxf_type;
FILE *df;
{
  if ((dxf_type == SHPT_ARC) || ( dxf_type == SHPT_POLYGON))  {
    fprintf( df, "  0\r\n");
    fprintf( df, "VERTEX\r\n");
    fprintf( df, "  8\r\n");
    fprintf( df, "%s\r\n", id);
  }  
  fprintf( df, " 10\r\n" );
  fprintf( df, FLOAT_PREC, x );
  fprintf( df, " 20\r\n" );
  fprintf( df, FLOAT_PREC, y );
  fprintf( df, " 30\r\n" );
  if ( z != 0 ) 
    fprintf( df, FLOAT_PREC, z );
  else
    fprintf( df, "0.0\r\n" );	       
}


void
dxf_ent_postamble (dxf_type, df)
int dxf_type;
FILE *df;
{
  if ((dxf_type == SHPT_ARC) || ( dxf_type == SHPT_POLYGON)) 
    fprintf( df, "  0\r\nSEQEND\r\n  8\r\n0\r\n");
}


int
main (int argc, char **argv)

{
    char shpFileName[80] = "", dbfFileName[80] = "";
    char dxfFileName[80] = "";
    char idfldName[15];
    char zfldName[6] = "ELEV";
    char fldName[15];
    char id[255];
    double elev;
    int   parts, *panParts, nParts, nVertices;
    FILE	*dxf;
    SHPHandle shp;
    DBFHandle dbf;
    DBFFieldType  idfld_type;
    double adfBoundsMin[4], adfBoundsMax[4];
    int	vrtx, shp_type, shp_numrec, zfld, idfld, nflds, recNum, part;
    unsigned int MaxElem = -1;
 
    if ( argc < 2 )  {
        printf ("usage: shpdxf shapefile {idfield}\r\n");
        exit (-1);
    }
   
    strcpy (shpFileName,argv[1]);
    strncpy (dbfFileName, shpFileName, strlen(shpFileName)-3);
    strcat (dbfFileName,"dbf"); 
  
    strncpy (dxfFileName, shpFileName,strlen(shpFileName)-3);
    strcat( dxfFileName, "dxf");
  
    shp = SHPOpen (shpFileName, "rb");
    dbf = DBFOpen (dbfFileName, "rb");
    dxf = fopen( dxfFileName, "w");

    printf("Starting conversion %s(%s) -> %s\r\n",
           shpFileName,dbfFileName,dxfFileName);
  
    SHPGetInfo (shp, &shp_numrec, &shp_type, adfBoundsMin, adfBoundsMax );
    printf ("file has %d objects\r\n", shp_numrec);
    dxf_hdr(adfBoundsMin[0], adfBoundsMin[1], adfBoundsMax[0], adfBoundsMax[1],
            dxf);
      
/* Before proceeding, allow the user to specify the ID field to use or default to the record number.... */

    if ( argc > 3 )  MaxElem = atoi(argv[3]);
  
    nflds = DBFGetFieldCount(dbf);
    if ( argc > 2 ) {
        strcpy (idfldName, argv[2]);
        for ( idfld=0; idfld < nflds; idfld++ ) {
            idfld_type = DBFGetFieldInfo( dbf, idfld, fldName, NULL, NULL);
            if (!strcmp (idfldName, fldName ))
                break;
        }
        if ( idfld >= nflds ) {
            printf ("Id field %s not found, using default\r\n",idfldName); 
            idfld = -1;  
        } else
            printf ("proceeding with field %s for LayerNames\r\n",fldName);
    }
    else
        idfld = -1;  
    
    for ( zfld=0; zfld < nflds; zfld++ ) {
        DBFGetFieldInfo( dbf, zfld, fldName, NULL, NULL);
        if (!strcmp (zfldName, fldName  ))
            break;
    }
    if ( zfld >= nflds ) 
        zfld = -1;
//  printf ("proceeding with id = %d, elevation = %d\r\n",idfld, zfld);
  
/* Proceed to process data..... */
  
    for ( recNum = 0; (recNum < shp_numrec) && (recNum < MaxElem); recNum++)  {

        SHPObject	*shape;

        if ( idfld >= 0 )
            switch (idfld_type) {
              case FTString:  sprintf (id, "lvl_%s",DBFReadStringAttribute ( dbf, recNum, idfld ));
                break;
              default:    sprintf(id, "%-20.0lf", DBFReadDoubleAttribute (dbf, recNum, idfld));
            }
        else
            sprintf (id,"lvl_%-20d",(recNum +1 )); 

    
        if ( zfld >= 0 ) 
            elev = 0;
        else  
            elev = DBFReadDoubleAttribute ( dbf, recNum, zfld );
  
#ifdef DEBUG
        printf("\r\nworking on obj %d", recNum);
#endif

        shape = SHPReadObject( shp, recNum );
   
        nVertices = shape->nVertices;
        nParts = shape->nParts;
        panParts = shape->panPartStart;
        part = 0;
        for (vrtx=0; vrtx < nVertices; vrtx ++ ) { 
#ifdef DEBUG
        printf("\rworking on part %d, vertex %d", part,vrtx);     
#endif 
            if ( panParts[part] == vrtx ) {
#ifdef DEBUG
       printf ("object preamble\r\n");
#endif
                dxf_ent_preamble (shp_type, id, dxf);
            }
     
            dxf_ent (id, shape->padfX[vrtx], shape->padfY[vrtx],
                     elev, shp_type, dxf);
 
            if ((panParts[part] == (vrtx + 1))|| (vrtx == (nVertices -1)) ) {
                dxf_ent_postamble (shp_type, dxf);
                part ++;
            }
        }
        SHPDestroyObject( shape );
    }

/* close out DXF file */
    fprintf( dxf, "0\r\n" );
    fprintf( dxf, "ENDSEC\r\n" );
    fprintf( dxf, "0\r\n" );
    fprintf( dxf, "EOF\r\n" );


    SHPClose (shp);
    DBFClose (dbf);
    fclose (dxf);
}