shapereader
Read ESRI shapefiles
Data Structures | Typedefs | Functions
shx.h File Reference

Go to the source code of this file.

Data Structures

struct  shx_record_t
 Index record. More...
 

Typedefs

typedef shp_header_t shx_header_t
 Header.
 
typedef shp_file_t shx_file_t
 File handle.
 
typedef int(* shx_header_callback_t) (shx_file_t *fh, const shx_header_t *header)
 Handle the file header. More...
 
typedef int(* shx_record_callback_t) (shx_file_t *fh, const shx_header_t *header, const shx_record_t *record)
 Handle a record. More...
 

Functions

shx_file_tshx_init_file (shx_file_t *fh, FILE *fp, void *user_data)
 Initialize a file handle. More...
 
void shx_set_error (shx_file_t *fh, const char *format,...)
 Set an error message. More...
 
int shx_read (shx_file_t *fh, shx_header_callback_t handle_header, shx_record_callback_t handle_record)
 Read an index file. More...
 
int shx_read_header (shx_file_t *fh, shx_header_t *header)
 Read the file header. More...
 
int shx_read_record (shx_file_t *fh, shx_record_t *record)
 Read an index record. More...
 
int shx_seek_record (shx_file_t *fh, size_t record_number, shx_record_t *record)
 Read an index record by record number. More...
 

Typedef Documentation

◆ shx_header_callback_t

typedef int(* shx_header_callback_t) (shx_file_t *fh, const shx_header_t *header)

A callback function that is called for the file header.

Parameters
fha file handle.
headera pointer to a shx_header_t structure.
Return values
1on sucess.
0to stop the processing.
-1on error.

◆ shx_record_callback_t

typedef int(* shx_record_callback_t) (shx_file_t *fh, const shx_header_t *header, const shx_record_t *record)

A callback function that is called for each record.

Parameters
fha file handle.
headera pointer to a shx_header_t structure.
recorda pointer to a shx_record_t structure.
Return values
1on sucess.
0to stop the processing.
-1on error.

Function Documentation

◆ shx_init_file()

shx_file_t * shx_init_file ( shx_file_t fh,
FILE *  fp,
void *  user_data 
)

Initializes a shx_file_t structure.

Parameters
fhan uninitialized file handle.
fpa file pointer.
user_datacallback data or NULL.
Returns
the initialized file handle.

◆ shx_set_error()

void shx_set_error ( shx_file_t fh,
const char *  format,
  ... 
)

Formats and sets an error message.

Parameters
fha file handle.
formata printf format string followed by a variable number of arguments.

◆ shx_read()

int shx_read ( shx_file_t fh,
shx_header_callback_t  handle_header,
shx_record_callback_t  handle_record 
)

Reads a file that has the file extension ".shx" and calls functions for the file header and each record.

The data that is passed to the callback functions is only valid during the function call. Do not keep pointers to the data.

Example

int handle_header(shx_file_t *fh, const shx_header_t *header) {
mydata_t *mydata = (mydata_t *) fh->user_data;
// Do something
return 1;
}
int handle_record(shx_file_t *fh, const shx_header_t *header,
const shx_record_t *record) {
mydata_t *mydata = (mydata_t *) fh->user_data;
// Do something
return 1;
}
shx_init_file(fh, fp, mydata)
rc = shx_read(fh, handle_header, handle_record);
shx_file_t * shx_init_file(shx_file_t *fh, FILE *fp, void *user_data)
Initialize a file handle.
Definition: shx.c:20
int shx_read(shx_file_t *fh, shx_header_callback_t handle_header, shx_record_callback_t handle_record)
Read an index file.
Definition: shx.c:138
File handle.
Definition: shp.h:114
void * user_data
Callback data.
Definition: shp.h:126
File header.
Definition: shp.h:58
Index record.
Definition: shx.h:31
Parameters
fha file handle.
handle_headera function that is called for the file header.
handle_recorda function that is called for each record.
Return values
1on success.
0on end of file.
-1on error.
See also
the "ESRI Shapefile Technical Description" [2] for information on the file format.

◆ shx_read_header()

int shx_read_header ( shx_file_t fh,
shx_header_t header 
)

Reads the header from a file that has the file extension ".shx".

Parameters
fha file handle.
[out]headera shx_header_t structure.
Return values
1on success.
0on end of file.
-1on error.
See also
shx_read_record

◆ shx_read_record()

int shx_read_record ( shx_file_t fh,
shx_record_t record 
)

Reads an index record from a file that has the file extension ".shx".

Example

shx_header_t header;
shx_record_t record;
if ((rc = shx_read_header(fh, &header)) > 0) {
while ((rc = shx_read_record(fh, &record)) > 0) {
// Do something
}
}
int shx_read_header(shx_file_t *fh, shx_header_t *header)
Read the file header.
Definition: shx.c:39
int shx_read_record(shx_file_t *fh, shx_record_t *record)
Read an index record.
Definition: shx.c:95
Parameters
fha file handle.
[out]recorda shx_record_t structure.
Return values
1on success.
0on end of file.
-1on error.
See also
shx_read_header

◆ shx_seek_record()

int shx_seek_record ( shx_file_t fh,
size_t  record_number,
shx_record_t record 
)

Sets the file position to the specified record number and reads the requested index record.

Please note that this function uses zero-based record numbers, whereas the record numbers in shapefiles begin at 1.

Parameters
fha file handle.
record_numbera zero-based record number.
[out]recorda shx_record_t structure.
Return values
1on success.
0on end of file.
-1on error.
See also
shp_seek_record