PostGIS  2.1.10dev-r@@SVN_REVISION@@
void SHPAPI_CALL SHPWriteHeader ( SHPHandle  psSHP)

Definition at line 340 of file shpopen.c.

References SHPInfo::adBoundsMax, SHPInfo::adBoundsMin, ByteCopy, SAHooks::Error, SAHooks::FFlush, SHPInfo::fpSHP, SHPInfo::fpSHX, SAHooks::FSeek, SAHooks::FWrite, SHPInfo::nFileSize, SHPInfo::nRecords, SHPInfo::nShapeType, SHPInfo::panRecOffset, SHPInfo::panRecSize, SHPInfo::sHooks, and SwapWord().

Referenced by SHPClose().

342 {
343  uchar abyHeader[100];
344  int i;
345  uint32 i32;
346  double dValue;
347  uint32 *panSHX;
348 
349  if (psSHP->fpSHX == NULL)
350  {
351  psSHP->sHooks.Error( "SHPWriteHeader failed : SHX file is closed");
352  return;
353  }
354 
355 /* -------------------------------------------------------------------- */
356 /* Prepare header block for .shp file. */
357 /* -------------------------------------------------------------------- */
358  for( i = 0; i < 100; i++ )
359  abyHeader[i] = 0;
360 
361  abyHeader[2] = 0x27; /* magic cookie */
362  abyHeader[3] = 0x0a;
363 
364  i32 = psSHP->nFileSize/2; /* file size */
365  ByteCopy( &i32, abyHeader+24, 4 );
366  if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
367 
368  i32 = 1000; /* version */
369  ByteCopy( &i32, abyHeader+28, 4 );
370  if( bBigEndian ) SwapWord( 4, abyHeader+28 );
371 
372  i32 = psSHP->nShapeType; /* shape type */
373  ByteCopy( &i32, abyHeader+32, 4 );
374  if( bBigEndian ) SwapWord( 4, abyHeader+32 );
375 
376  dValue = psSHP->adBoundsMin[0]; /* set bounds */
377  ByteCopy( &dValue, abyHeader+36, 8 );
378  if( bBigEndian ) SwapWord( 8, abyHeader+36 );
379 
380  dValue = psSHP->adBoundsMin[1];
381  ByteCopy( &dValue, abyHeader+44, 8 );
382  if( bBigEndian ) SwapWord( 8, abyHeader+44 );
383 
384  dValue = psSHP->adBoundsMax[0];
385  ByteCopy( &dValue, abyHeader+52, 8 );
386  if( bBigEndian ) SwapWord( 8, abyHeader+52 );
387 
388  dValue = psSHP->adBoundsMax[1];
389  ByteCopy( &dValue, abyHeader+60, 8 );
390  if( bBigEndian ) SwapWord( 8, abyHeader+60 );
391 
392  dValue = psSHP->adBoundsMin[2]; /* z */
393  ByteCopy( &dValue, abyHeader+68, 8 );
394  if( bBigEndian ) SwapWord( 8, abyHeader+68 );
395 
396  dValue = psSHP->adBoundsMax[2];
397  ByteCopy( &dValue, abyHeader+76, 8 );
398  if( bBigEndian ) SwapWord( 8, abyHeader+76 );
399 
400  dValue = psSHP->adBoundsMin[3]; /* m */
401  ByteCopy( &dValue, abyHeader+84, 8 );
402  if( bBigEndian ) SwapWord( 8, abyHeader+84 );
403 
404  dValue = psSHP->adBoundsMax[3];
405  ByteCopy( &dValue, abyHeader+92, 8 );
406  if( bBigEndian ) SwapWord( 8, abyHeader+92 );
407 
408 /* -------------------------------------------------------------------- */
409 /* Write .shp file header. */
410 /* -------------------------------------------------------------------- */
411  if( psSHP->sHooks.FSeek( psSHP->fpSHP, 0, 0 ) != 0
412  || psSHP->sHooks.FWrite( abyHeader, 100, 1, psSHP->fpSHP ) != 1 )
413  {
414  psSHP->sHooks.Error( "Failure writing .shp header" );
415  return;
416  }
417 
418 /* -------------------------------------------------------------------- */
419 /* Prepare, and write .shx file header. */
420 /* -------------------------------------------------------------------- */
421  i32 = (psSHP->nRecords * 2 * sizeof(uint32) + 100)/2; /* file size */
422  ByteCopy( &i32, abyHeader+24, 4 );
423  if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
424 
425  if( psSHP->sHooks.FSeek( psSHP->fpSHX, 0, 0 ) != 0
426  || psSHP->sHooks.FWrite( abyHeader, 100, 1, psSHP->fpSHX ) != 1 )
427  {
428  psSHP->sHooks.Error( "Failure writing .shx header" );
429  return;
430  }
431 
432 /* -------------------------------------------------------------------- */
433 /* Write out the .shx contents. */
434 /* -------------------------------------------------------------------- */
435  panSHX = (uint32 *) malloc(sizeof(uint32) * 2 * psSHP->nRecords);
436 
437  for( i = 0; i < psSHP->nRecords; i++ )
438  {
439  panSHX[i*2 ] = psSHP->panRecOffset[i]/2;
440  panSHX[i*2+1] = psSHP->panRecSize[i]/2;
441  if( !bBigEndian ) SwapWord( 4, panSHX+i*2 );
442  if( !bBigEndian ) SwapWord( 4, panSHX+i*2+1 );
443  }
444 
445  if( (int)psSHP->sHooks.FWrite( panSHX, sizeof(uint32)*2, psSHP->nRecords, psSHP->fpSHX )
446  != psSHP->nRecords )
447  {
448  psSHP->sHooks.Error( "Failure writing .shx contents" );
449  }
450 
451  free( panSHX );
452 
453 /* -------------------------------------------------------------------- */
454 /* Flush to disk. */
455 /* -------------------------------------------------------------------- */
456  psSHP->sHooks.FFlush( psSHP->fpSHP );
457  psSHP->sHooks.FFlush( psSHP->fpSHX );
458 }
double adBoundsMin[4]
Definition: shapefil.h:293
unsigned int nFileSize
Definition: shapefil.h:286
int(* FFlush)(SAFile file)
Definition: shapefil.h:261
#define ByteCopy(a, b, c)
Definition: shpopen.c:282
int nRecords
Definition: shapefil.h:288
SAOffset(* FWrite)(void *p, SAOffset size, SAOffset nmemb, SAFile file)
Definition: shapefil.h:258
unsigned int * panRecOffset
Definition: shapefil.h:290
static int bBigEndian
Definition: shpopen.c:294
unsigned int uint32
Definition: shpopen.c:274
SAHooks sHooks
Definition: shapefil.h:279
SAFile fpSHX
Definition: shapefil.h:282
double adBoundsMax[4]
Definition: shapefil.h:294
unsigned int * panRecSize
Definition: shapefil.h:291
SAOffset(* FSeek)(SAFile file, SAOffset offset, int whence)
Definition: shapefil.h:259
static void SwapWord(int length, void *wordP)
Definition: shpopen.c:303
unsigned char uchar
Definition: shpopen.c:269
void(* Error)(const char *message)
Definition: shapefil.h:265
int nShapeType
Definition: shapefil.h:284
SAFile fpSHP
Definition: shapefil.h:281

Here is the call graph for this function:

Here is the caller graph for this function: