shapereader
Read ESRI shapefiles
shp.h
Go to the documentation of this file.
1/*
2 * Read ESRI shapefiles
3 *
4 * Copyright (C) 2023 Andreas Vögele
5 *
6 * This library is free software; you can redistribute it and/or modify it
7 * under either the terms of the ISC License or the same terms as Perl.
8 */
9
10/* SPDX-License-Identifier: ISC OR Artistic-1.0-Perl OR GPL-1.0-or-later */
11
16#ifndef _SHAPEREADER_SHP_H
17#define _SHAPEREADER_SHP_H
18
19#include "shp-multipatch.h"
20#include "shp-multipoint.h"
21#include "shp-multipointm.h"
22#include "shp-multipointz.h"
23#include "shp-point.h"
24#include "shp-pointm.h"
25#include "shp-pointz.h"
26#include "shp-polygon.h"
27#include "shp-polygonm.h"
28#include "shp-polygonz.h"
29#include "shp-polyline.h"
30#include "shp-polylinem.h"
31#include "shp-polylinez.h"
32#include <stddef.h>
33#include <stdio.h>
34
38typedef enum shp_type_t {
54
58typedef struct shp_header_t {
59 long file_code;
60 long unused[5];
61 size_t file_size;
62 long version;
64 double x_min;
65 double y_min;
66 double x_max;
67 double y_max;
68 double z_min;
69 double z_max;
70 double m_min;
71 double m_max;
73
77typedef struct shp_record_t {
79 size_t record_size;
81 union {
108 } shape;
110
114typedef struct shp_file_t {
115 /* File pointer */
116 void *stream;
117 /* Read bytes from the stream */
118 size_t (*fread)(struct shp_file_t *fh, void *buf, size_t count);
119 /* Test the stream's end-of-file indicator */
120 int (*feof)(struct shp_file_t *fh);
121 /* Test the stream's error indicator */
122 int (*ferror)(struct shp_file_t *fh);
123 /* Set the stream's file position */
124 int (*fsetpos)(struct shp_file_t *fh, size_t offset);
128 size_t num_bytes;
130 char error[128];
131} shp_file_t;
132
143extern shp_file_t *shp_init_file(shp_file_t *fh, FILE *stream,
144 void *user_data);
145
155#ifdef __GNUC__
156extern void shp_set_error(shp_file_t *fh, const char *format, ...)
157 __attribute__((format(printf, 2, 3)));
158#else
159extern void shp_set_error(shp_file_t *fh, const char *format, ...);
160#endif
161
174 const shp_header_t *header);
175
190 const shp_header_t *header,
191 const shp_record_t *record,
192 size_t file_offset);
193
233extern int shp_read(shp_file_t *fh, shp_header_callback_t handle_header,
234 shp_record_callback_t handle_record);
235
249extern int shp_read_header(shp_file_t *fh, shp_header_t *header);
250
279extern int shp_read_record(shp_file_t *fh, shp_record_t **precord);
280
311extern int shp_seek_record(shp_file_t *fh, size_t file_offset,
312 shp_record_t **precord);
313
314#endif
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
int(* shp_header_callback_t)(shp_file_t *fh, const shp_header_t *header)
Handle the file header.
Definition: shp.h:173
void shp_set_error(shp_file_t *fh, const char *format,...)
Set an error message.
Definition: shp.c:74
int shp_read_header(shp_file_t *fh, shp_header_t *header)
Read the file header.
Definition: shp.c:87
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.
Definition: shp.h:189
int shp_read_record(shp_file_t *fh, shp_record_t **precord)
Read a record.
Definition: shp.c:849
shp_type_t
Shape types.
Definition: shp.h:38
@ SHP_TYPE_MULTIPOINT
Set of Points.
Definition: shp.h:43
@ SHP_TYPE_POINTZ
PointZ with X, Y, Z, M coordinates.
Definition: shp.h:44
@ SHP_TYPE_MULTIPOINTZ
Set of PointZs.
Definition: shp.h:47
@ SHP_TYPE_NULL
Null shape without geometric data.
Definition: shp.h:39
@ SHP_TYPE_POLYLINEM
PolyLineM with X, Y, M coordinates.
Definition: shp.h:49
@ SHP_TYPE_POINTM
PointM with X, Y, M coordinates.
Definition: shp.h:48
@ SHP_TYPE_POLYLINE
PolyLine with X, Y coordinates.
Definition: shp.h:41
@ SHP_TYPE_POINT
Point with X, Y coordinates.
Definition: shp.h:40
@ SHP_TYPE_POLYGON
Polygon with X, Y coordinates.
Definition: shp.h:42
@ SHP_TYPE_POLYGONM
PolygonM with X, Y, M coordinates.
Definition: shp.h:50
@ SHP_TYPE_POLYLINEZ
PolyLineZ with X, Y, Z, M coordinates.
Definition: shp.h:45
@ SHP_TYPE_MULTIPOINTM
Set of PointMs.
Definition: shp.h:51
@ SHP_TYPE_POLYGONZ
PolygonZ with X, Y, Z, M coordinates.
Definition: shp.h:46
@ SHP_TYPE_MULTIPATCH
Complex surfaces.
Definition: shp.h:52
shp_file_t * shp_init_file(shp_file_t *fh, FILE *stream, void *user_data)
Initialize a file handle.
Definition: shp.c:56
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
File handle.
Definition: shp.h:114
size_t num_bytes
Number of bytes read.
Definition: shp.h:128
void * user_data
Callback data.
Definition: shp.h:126
char error[128]
Error message.
Definition: shp.h:130
File header.
Definition: shp.h:58
long version
Always 1000.
Definition: shp.h:62
double z_max
Maximum Z.
Definition: shp.h:69
double x_max
Maximum X.
Definition: shp.h:66
long file_code
Always 9994.
Definition: shp.h:59
double y_min
Minimum Y.
Definition: shp.h:65
size_t file_size
Total file length in bytes.
Definition: shp.h:61
shp_type_t type
Shape type.
Definition: shp.h:63
long unused[5]
Unused fields.
Definition: shp.h:60
double m_max
Maximum M.
Definition: shp.h:71
double m_min
Minimum M.
Definition: shp.h:70
double x_min
Minimum X.
Definition: shp.h:64
double y_max
Maximum Y.
Definition: shp.h:67
double z_min
Minimum Z.
Definition: shp.h:68
MultiPatch.
Definition: shp-multipatch.h:66
MultiPoint.
Definition: shp-multipoint.h:27
MultiPointM.
Definition: shp-multipointm.h:28
MultiPointZ.
Definition: shp-multipointz.h:28
Point.
Definition: shp-point.h:24
PointM.
Definition: shp-pointm.h:25
PointZ.
Definition: shp-pointz.h:25
Polygon.
Definition: shp-polygon.h:30
PolygonM.
Definition: shp-polygonm.h:29
PolygonZ.
Definition: shp-polygonz.h:29
PolyLine.
Definition: shp-polyline.h:29
PolyLineM.
Definition: shp-polylinem.h:29
PolyLineZ.
Definition: shp-polylinez.h:29
Record.
Definition: shp.h:77
shp_multipointm_t multipointm
Set of PointMs if type is SHP_TYPE_MULTIPOINTM.
Definition: shp.h:91
shp_multipatch_t multipatch
MultiPatch if type is SHP_TYPE_MULTIPATCH.
Definition: shp.h:107
shp_polylinem_t polylinem
PolyLineM if type is SHP_TYPE_POLYLINEM.
Definition: shp.h:97
shp_multipointz_t multipointz
Set of PointZs if type is SHP_TYPE_MULTIPOINTZ.
Definition: shp.h:93
shp_multipoint_t multipoint
Set of Points if type is SHP_TYPE_MULTIPOINT.
Definition: shp.h:89
size_t record_size
Content length in bytes.
Definition: shp.h:79
shp_point_t point
Point if type is SHP_TYPE_POINT.
Definition: shp.h:83
shp_polygon_t polygon
Polygon if type is SHP_TYPE_POLYGON.
Definition: shp.h:101
shp_pointm_t pointm
PointM if type is SHP_TYPE_POINTM.
Definition: shp.h:85
shp_polygonm_t polygonm
PolygonM if type is SHP_TYPE_POLYGONM.
Definition: shp.h:103
shp_polygonz_t polygonz
PolygonZ if type is SHP_TYPE_POLYGONZ.
Definition: shp.h:105
shp_polylinez_t polylinez
PolyLineZ if type is SHP_TYPE_POLYLINEZ.
Definition: shp.h:99
size_t record_number
Record number (beginning at 1)
Definition: shp.h:78
shp_type_t type
Shape type.
Definition: shp.h:80
shp_polyline_t polyline
PolyLine if type is SHP_TYPE_POLYLINE.
Definition: shp.h:95
shp_pointz_t pointz
PointZ if type is SHP_TYPE_POINTZ.
Definition: shp.h:87