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

Go to the source code of this file.

Data Structures

struct  shp_header_t
 File header. More...
 
struct  shp_record_t
 Record. More...
 
struct  shp_file_t
 File handle. More...
 

Typedefs

typedef int(* shp_header_callback_t) (shp_file_t *fh, const shp_header_t *header)
 Handle the file header. More...
 
typedef int(* shp_record_callback_t) (shp_file_t *fh, const shp_header_t *header, const shp_record_t *record, size_t file_offset)
 Handle a record. More...
 

Enumerations

enum  shp_type_t {
  SHP_TYPE_NULL = 0 , SHP_TYPE_POINT = 1 , SHP_TYPE_POLYLINE = 3 , SHP_TYPE_POLYGON = 5 ,
  SHP_TYPE_MULTIPOINT = 8 , SHP_TYPE_POINTZ = 11 , SHP_TYPE_POLYLINEZ = 13 , SHP_TYPE_POLYGONZ = 15 ,
  SHP_TYPE_MULTIPOINTZ = 18 , SHP_TYPE_POINTM = 21 , SHP_TYPE_POLYLINEM = 23 , SHP_TYPE_POLYGONM = 25 ,
  SHP_TYPE_MULTIPOINTM = 28 , SHP_TYPE_MULTIPATCH = 31
}
 Shape types. More...
 

Functions

shp_file_tshp_init_file (shp_file_t *fh, FILE *stream, void *user_data)
 Initialize a file handle. More...
 
void shp_set_error (shp_file_t *fh, const char *format,...)
 Set an error message. More...
 
int shp_read (shp_file_t *fh, shp_header_callback_t handle_header, shp_record_callback_t handle_record)
 Read a shape file. More...
 
int shp_read_header (shp_file_t *fh, shp_header_t *header)
 Read the file header. More...
 
int shp_read_record (shp_file_t *fh, shp_record_t **precord)
 Read a record. More...
 
int shp_seek_record (shp_file_t *fh, size_t file_offset, shp_record_t **precord)
 Read a record at a particular file position. More...
 

Typedef Documentation

◆ shp_header_callback_t

typedef int(* shp_header_callback_t) (shp_file_t *fh, const shp_header_t *header)

A callback function that is called for the file header.

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

◆ shp_record_callback_t

typedef int(* shp_record_callback_t) (shp_file_t *fh, const shp_header_t *header, const shp_record_t *record, size_t file_offset)

A callback function that is called for each record.

Parameters
fha file handle.
headera pointer to a shp_header_t structure.
recorda pointer to a shp_record_t structure.
file_offsetthe record's position in the file.
Return values
1on sucess.
0to stop the processing.
-1on error.

Enumeration Type Documentation

◆ shp_type_t

enum shp_type_t
Enumerator
SHP_TYPE_NULL 

Null shape without geometric data.

SHP_TYPE_POINT 

Point with X, Y coordinates.

SHP_TYPE_POLYLINE 

PolyLine with X, Y coordinates.

SHP_TYPE_POLYGON 

Polygon with X, Y coordinates.

SHP_TYPE_MULTIPOINT 

Set of Points.

SHP_TYPE_POINTZ 

PointZ with X, Y, Z, M coordinates.

SHP_TYPE_POLYLINEZ 

PolyLineZ with X, Y, Z, M coordinates.

SHP_TYPE_POLYGONZ 

PolygonZ with X, Y, Z, M coordinates.

SHP_TYPE_MULTIPOINTZ 

Set of PointZs.

SHP_TYPE_POINTM 

PointM with X, Y, M coordinates.

SHP_TYPE_POLYLINEM 

PolyLineM with X, Y, M coordinates.

SHP_TYPE_POLYGONM 

PolygonM with X, Y, M coordinates.

SHP_TYPE_MULTIPOINTM 

Set of PointMs.

SHP_TYPE_MULTIPATCH 

Complex surfaces.

Function Documentation

◆ shp_init_file()

shp_file_t * shp_init_file ( shp_file_t fh,
FILE *  stream,
void *  user_data 
)

Initializes a shp_file_t structure.

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

◆ shp_set_error()

void shp_set_error ( shp_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.

◆ shp_read()

int shp_read ( shp_file_t fh,
shp_header_callback_t  handle_header,
shp_record_callback_t  handle_record 
)

Reads a file that has the file extension ".shp" 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(shp_file_t *fh, const shp_header_t *header) {
mydata_t *mydata = (mydata_t *) fh->user_data;
// Do something
return 1;
}
int handle_record(shp_file_t *fh, const shp_header_t *header,
const shp_record_t *record, size_t file_offset) {
mydata_t *mydata = (mydata_t *) fh->user_data;
// Do something
return 1;
}
shp_init_file(fh, stream, mydata)
rc = shp_read(fh, handle_header, handle_record);
int shp_read(shp_file_t *fh, shp_header_callback_t handle_header, shp_record_callback_t handle_record)
Read a shape file.
Definition: shp.c:901
shp_file_t * shp_init_file(shp_file_t *fh, FILE *stream, void *user_data)
Initialize a file handle.
Definition: shp.c:56
File handle.
Definition: shp.h:114
void * user_data
Callback data.
Definition: shp.h:126
File header.
Definition: shp.h:58
Record.
Definition: shp.h:77
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.

◆ shp_read_header()

int shp_read_header ( shp_file_t fh,
shp_header_t header 
)

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

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

◆ shp_read_record()

int shp_read_record ( shp_file_t fh,
shp_record_t **  precord 
)

Reads a record from a file that has the file extension ".shp".

Example

shp_header_t header;
shp_record_t *record;
if ((rc = shp_read_header(fh, &header)) > 0) {
while ((rc = shp_read_record(fh, &record)) > 0) {
// Do something
free(record);
}
}
int shp_read_header(shp_file_t *fh, shp_header_t *header)
Read the file header.
Definition: shp.c:87
int shp_read_record(shp_file_t *fh, shp_record_t **precord)
Read a record.
Definition: shp.c:849
Parameters
fha file handle.
[out]precordon success, a pointer to a shp_record_t structure. Free the record with free() when you are done.
Return values
1on success.
0on end of file.
-1on error.
See also
shp_read_header

◆ shp_seek_record()

int shp_seek_record ( shp_file_t fh,
size_t  file_offset,
shp_record_t **  precord 
)

Sets the file position to an offset from a ".shx" file and reads the requested record.

Example

shp_record_t *record = NULL;
if (shx_seek_record(shx_fh, record_number, &index) > 0) {
if (shp_seek_record(shp_fh, index.file_offset, &record) > 0) {
// Do something
free(record);
}
}
int shp_seek_record(shp_file_t *fh, size_t file_offset, shp_record_t **precord)
Read a record at a particular file position.
Definition: shp.c:870
int shx_seek_record(shx_file_t *fh, size_t record_number, shx_record_t *record)
Read an index record by record number.
Definition: shx.c:108
Index record.
Definition: shx.h:31
size_t file_offset
Offset in the ".shp" file in bytes.
Definition: shx.h:32
Parameters
fha file handle.
file_offsetan offset from a ".shx" file.
[out]precordon success, a pointer to a shp_record_t structure. Free the record with free() when you are done.
Return values
1on success.
0on end of file.
-1on error.
See also
shx_seek_record