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
char *s;
double d;
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;
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)) {
while (field != NULL) {
print_field(record, field);
}
}
free(record);
}
free(header);
}
if (rc < 0) {
fprintf(stderr,
"%s\n", fh.
error);
}
fclose(stream);
}
return rc;
}
double x, y;
printf("[%lf, %lf]\n", x, y);
break;
}
}
int read_shp(const char *filename) {
int rc = -1;
FILE *stream;
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
@ SHP_TYPE_POINT
Point with X, Y coordinates.
Definition shp.h:40
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
Record.
Definition dbf.h:135
File handle.
Definition shp.h:114
char error[128]
Error message.
Definition shp.h:130
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.