PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ SHPWriteHeader()

void SHPAPI_CALL SHPWriteHeader ( SHPHandle  hSHP)

Definition at line 147 of file shpopen.c.

149{
150 uchar abyHeader[100] = { 0 };
151 int i;
152 int32 i32;
153 double dValue;
154 int32 *panSHX;
155
156 if (psSHP->fpSHX == SHPLIB_NULLPTR)
157 {
158 psSHP->sHooks.Error( "SHPWriteHeader failed : SHX file is closed");
159 return;
160 }
161
162/* -------------------------------------------------------------------- */
163/* Prepare header block for .shp file. */
164/* -------------------------------------------------------------------- */
165
166 abyHeader[2] = 0x27; /* magic cookie */
167 abyHeader[3] = 0x0a;
168
169 i32 = psSHP->nFileSize/2; /* file size */
170 ByteCopy( &i32, abyHeader+24, 4 );
171 if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
172
173 i32 = 1000; /* version */
174 ByteCopy( &i32, abyHeader+28, 4 );
175 if( bBigEndian ) SwapWord( 4, abyHeader+28 );
176
177 i32 = psSHP->nShapeType; /* shape type */
178 ByteCopy( &i32, abyHeader+32, 4 );
179 if( bBigEndian ) SwapWord( 4, abyHeader+32 );
180
181 dValue = psSHP->adBoundsMin[0]; /* set bounds */
182 ByteCopy( &dValue, abyHeader+36, 8 );
183 if( bBigEndian ) SwapWord( 8, abyHeader+36 );
184
185 dValue = psSHP->adBoundsMin[1];
186 ByteCopy( &dValue, abyHeader+44, 8 );
187 if( bBigEndian ) SwapWord( 8, abyHeader+44 );
188
189 dValue = psSHP->adBoundsMax[0];
190 ByteCopy( &dValue, abyHeader+52, 8 );
191 if( bBigEndian ) SwapWord( 8, abyHeader+52 );
192
193 dValue = psSHP->adBoundsMax[1];
194 ByteCopy( &dValue, abyHeader+60, 8 );
195 if( bBigEndian ) SwapWord( 8, abyHeader+60 );
196
197 dValue = psSHP->adBoundsMin[2]; /* z */
198 ByteCopy( &dValue, abyHeader+68, 8 );
199 if( bBigEndian ) SwapWord( 8, abyHeader+68 );
200
201 dValue = psSHP->adBoundsMax[2];
202 ByteCopy( &dValue, abyHeader+76, 8 );
203 if( bBigEndian ) SwapWord( 8, abyHeader+76 );
204
205 dValue = psSHP->adBoundsMin[3]; /* m */
206 ByteCopy( &dValue, abyHeader+84, 8 );
207 if( bBigEndian ) SwapWord( 8, abyHeader+84 );
208
209 dValue = psSHP->adBoundsMax[3];
210 ByteCopy( &dValue, abyHeader+92, 8 );
211 if( bBigEndian ) SwapWord( 8, abyHeader+92 );
212
213/* -------------------------------------------------------------------- */
214/* Write .shp file header. */
215/* -------------------------------------------------------------------- */
216 if( psSHP->sHooks.FSeek( psSHP->fpSHP, 0, 0 ) != 0
217 || psSHP->sHooks.FWrite( abyHeader, 100, 1, psSHP->fpSHP ) != 1 )
218 {
219 char szErrorMsg[200];
220
221 snprintf( szErrorMsg, sizeof(szErrorMsg),
222 "Failure writing .shp header: %s", strerror(errno) );
223 szErrorMsg[sizeof(szErrorMsg)-1] = '\0';
224 psSHP->sHooks.Error( szErrorMsg );
225 return;
226 }
227
228/* -------------------------------------------------------------------- */
229/* Prepare, and write .shx file header. */
230/* -------------------------------------------------------------------- */
231 i32 = (psSHP->nRecords * 2 * sizeof(int32) + 100)/2; /* file size */
232 ByteCopy( &i32, abyHeader+24, 4 );
233 if( !bBigEndian ) SwapWord( 4, abyHeader+24 );
234
235 if( psSHP->sHooks.FSeek( psSHP->fpSHX, 0, 0 ) != 0
236 || psSHP->sHooks.FWrite( abyHeader, 100, 1, psSHP->fpSHX ) != 1 )
237 {
238 char szErrorMsg[200];
239
240 snprintf( szErrorMsg, sizeof(szErrorMsg),
241 "Failure writing .shx header: %s", strerror(errno) );
242 szErrorMsg[sizeof(szErrorMsg)-1] = '\0';
243 psSHP->sHooks.Error( szErrorMsg );
244
245 return;
246 }
247
248/* -------------------------------------------------------------------- */
249/* Write out the .shx contents. */
250/* -------------------------------------------------------------------- */
251 panSHX = STATIC_CAST(int32 *, malloc(sizeof(int32) * 2 * psSHP->nRecords));
252 if( panSHX == SHPLIB_NULLPTR )
253 {
254 psSHP->sHooks.Error( "Failure allocating panSHX" );
255 return;
256 }
257
258 for( i = 0; i < psSHP->nRecords; i++ )
259 {
260 panSHX[i*2 ] = psSHP->panRecOffset[i]/2;
261 panSHX[i*2+1] = psSHP->panRecSize[i]/2;
262 if( !bBigEndian ) SwapWord( 4, panSHX+i*2 );
263 if( !bBigEndian ) SwapWord( 4, panSHX+i*2+1 );
264 }
265
266 if( STATIC_CAST(int, psSHP->sHooks.FWrite( panSHX, sizeof(int32)*2, psSHP->nRecords, psSHP->fpSHX ))
267 != psSHP->nRecords )
268 {
269 char szErrorMsg[200];
270
271 snprintf( szErrorMsg, sizeof(szErrorMsg),
272 "Failure writing .shx contents: %s", strerror(errno) );
273 szErrorMsg[sizeof(szErrorMsg)-1] = '\0';
274 psSHP->sHooks.Error( szErrorMsg );
275 }
276
277 free( panSHX );
278
279/* -------------------------------------------------------------------- */
280/* Flush to disk. */
281/* -------------------------------------------------------------------- */
282 psSHP->sHooks.FFlush( psSHP->fpSHP );
283 psSHP->sHooks.FFlush( psSHP->fpSHX );
284}
void * malloc(YYSIZE_T)
void free(void *)
static int bBigEndian
Definition shpopen.c:93
unsigned int int32
Definition shpopen.c:54
unsigned char uchar
Definition shpopen.c:49
static void SwapWord(int length, void *wordP)
Definition shpopen.c:110
#define STATIC_CAST(type, x)
Definition shpopen.c:100
#define SHPLIB_NULLPTR
Definition shpopen.c:101
#define ByteCopy(a, b, c)
Definition shpopen.c:62

References SHPInfo::adBoundsMax, SHPInfo::adBoundsMin, bBigEndian, 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, SHPLIB_NULLPTR, STATIC_CAST, and SwapWord().

Referenced by SHPClose().

Here is the call graph for this function:
Here is the caller graph for this function: