PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ SHPWriteHeader()

void SHPAPI_CALL SHPWriteHeader ( SHPHandle  psSHP)

Definition at line 339 of file shpopen.c.

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

Referenced by SHPClose().

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