shapereader
Read ESRI shapefiles
shapereader

A C library for reading ESRI shapefiles. The shapefile format is a geospatial vector data format for geographic information system software.

The library supports the shp, shx and dbf formats.

DEPENDENCIES

Requires a C compiler that supports the C99 standard and CMake.

INSTALLATION

Run the following commands to install the library:

mkdir build
cd build
cmake ..
make
make test
make install

See Makefile.PL for information on how to add the library to a Perl XS module.

EXAMPLES

void print_field(const dbf_record_t *record, const dbf_field_t *field) {
char *s;
double d;
switch (field->type) {
s = dbf_record_strdup(record, field);
if (s != NULL) {
puts(s);
free(s);
}
break;
if (dbf_record_strtod(record, field, &d)) {
printf("%lf\n", d);
}
break;
}
}
int read_dbf(const char *filename) {
int rc = -1;
FILE *stream;
dbf_header_t *header;
dbf_record_t *record;
dbf_field_t *field;
stream = fopen(filename, "rb");
if (stream != NULL) {
dbf_init_file(&fh, stream, NULL);
if ((rc = dbf_read_header(&fh, &header)) > 0) {
while ((rc = dbf_read_record(&fh, &record)) > 0) {
if (!dbf_record_is_deleted(record)) {
field = header->fields;
while (field != NULL) {
print_field(record, field);
field = field->next;
}
}
free(record);
}
free(header);
}
if (rc < 0) {
fprintf(stderr, "%s\n", fh.error);
}
fclose(stream);
}
return rc;
}
void print_shape(const shp_record_t *record) {
double x, y;
switch (record->type) {
x = record->shape.point.x;
y = record->shape.point.y;
printf("[%lf, %lf]\n", x, y);
break;
}
}
int read_shp(const char *filename) {
int rc = -1;
FILE *stream;
shp_header_t header;
shp_record_t *record;
stream = fopen(filename, "rb");
if (stream != NULL) {
shp_init_file(&fh, stream, NULL);
if ((rc = shp_read_header(&fh, &header)) > 0) {
while ((rc = shp_read_record(&fh, &record)) > 0) {
print_shape(record);
free(record);
}
}
if (rc < 0) {
fprintf(stderr, "%s\n", fh.error);
}
fclose(stream);
}
return rc;
}
int main(int argc, char *argv[]) {
read_dbf("file.dbf");
read_shp("file.shp");
return 0;
}
@ DBF_TYPE_CHARACTER
String.
Definition: dbf.h:69
@ DBF_TYPE_NUMBER
Number (stored as a string)
Definition: dbf.h:91
@ DBF_TYPE_FLOAT
Number (stored as a string)
Definition: dbf.h:79
dbf_file_t * dbf_init_file(dbf_file_t *fh, FILE *stream, void *user_data)
Initialize a file handle.
Definition: dbf.c:55
int dbf_read_record(dbf_file_t *fh, dbf_record_t **precord)
Read a record.
Definition: dbf.c:1107
int dbf_read_header(dbf_file_t *fh, dbf_header_t **pheader)
Read the file header.
Definition: dbf.c:1060
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
@ SHP_TYPE_POINT
Point with X, Y coordinates.
Definition: shp.h:40
shp_file_t * shp_init_file(shp_file_t *fh, FILE *stream, void *user_data)
Initialize a file handle.
Definition: shp.c:56
Field.
Definition: dbf.h:105
struct dbf_field_t * next
Next field or NULL.
Definition: dbf.h:106
dbf_type_t type
Type.
Definition: dbf.h:108
File handle.
Definition: dbf.h:475
char error[128]
Error message.
Definition: dbf.h:491
File header.
Definition: dbf.h:119
dbf_field_t * fields
The fields in each record.
Definition: dbf.h:129
Record.
Definition: dbf.h:135
int dbf_record_strtod(const dbf_record_t *record, const dbf_field_t *field, double *value)
Convert a string to double representation.
Definition: dbf.c:590
char * dbf_record_strdup(const dbf_record_t *record, const dbf_field_t *field)
Duplicate a string.
Definition: dbf.c:558
int dbf_record_is_deleted(const dbf_record_t *record)
Check if a record is deleted.
Definition: dbf.c:430
File handle.
Definition: shp.h:114
char error[128]
Error message.
Definition: shp.h:130
File header.
Definition: shp.h:58
double x
X coordinate.
Definition: shp-point.h:25
double y
Y coordinate.
Definition: shp-point.h:26
Record.
Definition: shp.h:77
shp_point_t point
Point if type is SHP_TYPE_POINT.
Definition: shp.h:83
shp_type_t type
Shape type.
Definition: shp.h:80

LICENSE AND COPYRIGHT

Copyright (C) 2023 Andreas Vögele

This library is free software; you can redistribute it and/or modify it under either the terms of the ISC License or the same terms as Perl.