1 #ifndef teca_shape_file_util_h
2 #define teca_shape_file_util_h
4 #include "teca_config.h"
5 #include "teca_binary_stream.h"
16 namespace teca_shape_file_util
24 std::ostream &
operator<<(std::ostream &os,
const SHPObject &obj);
27 template<
typename coord_t>
31 int normalize = 0,
int verbose = 0)
36 SHPHandle h = SHPOpen(
filename.c_str(),
"rb");
48 SHPGetInfo(h, &n_shapes, &types, x0, x1);
51 TECA_STATUS(
"Found " << n_shapes <<
" shapes of type "
54 if (types != SHPT_POLYGON)
57 <<
"\" are not polygons.")
62 polys.reserve(n_shapes);
64 for (
int i = 0; i < n_shapes; ++i)
67 std::cerr <<
"Reading object " << i <<
" ... " << std::endl;
69 SHPObject *obj = SHPReadObject(h, i);
72 TECA_ERROR(
"Failed to read the " << i <<
"th polygon")
78 std::cerr <<
"Object" << i <<
" = " << *obj << std::endl;
81 std::vector<int> starts(obj->panPartStart, obj->panPartStart + obj->nParts);
82 std::vector<int> ends(obj->panPartStart + 1, obj->panPartStart + obj->nParts);
83 ends.push_back(obj->nVertices);
84 for (
int j = 0; j < obj->nParts; ++j)
87 if (obj->panPartType[j] != SHPT_POLYGON)
89 TECA_WARNING(
"Part " << j <<
" of object " << i <<
" is "
91 <<
" but a SHPT_POLYGON is required")
95 int start = starts[j];
96 int n_verts = ends[j] - start;
100 poly.
copy(obj->padfX + start, obj->padfY + start, n_verts);
106 polys.push_back(poly);
109 SHPDestroyObject(obj);
120 template<
typename coord_t>
124 int normalize = 0,
int verbose = 0)
127 #if defined(TECA_HAS_MPI)
129 MPI_Initialized(&is_init);
132 MPI_Comm_rank(comm, &rank);
150 size_t n_polys = polys.size();
154 for (
size_t i = 0; i < n_polys; ++i)
155 polys[i].to_stream(bs);
167 polys.resize(n_polys);
169 for (
size_t i = 0; i < n_polys; ++i)
170 polys[i].from_stream(bs);
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:17
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
TECA_EXPORT std::string filename(const std::string &filename)
Definition: teca_geometry.h:86
void copy(const in_coord_t *in_vx, const in_coord_t *in_vy, unsigned long in_n_verts)
deep copy the vertices of the polygon.
Definition: teca_geometry.h:122
void normalize_coordinates()
transforms coordinates to be in [0 360 -90 90]
Definition: teca_geometry.h:138
TECA_EXPORT std::ostream & operator<<(std::ostream &os, const teca_calendar_util::time_point &tpt)
send the time_point to a stream in humnan readable form
#define TECA_ERROR(_msg)
Constructs an error message and sends it to the stderr stream.
Definition: teca_common.h:161
#define TECA_WARNING(_msg)
Constructs a warning message and sends it to the stderr stream.
Definition: teca_common.h:164
#define TECA_STATUS(_msg)
Constructs a status message and sends it to the stderr stream.
Definition: teca_common.h:167
TECA_EXPORT int load_polygons(const std::string &filename, std::vector< teca_geometry::polygon< coord_t >> &polys, int normalize=0, int verbose=0)
Read polygons from the given file.
Definition: teca_shape_file_util.h:29
TECA_EXPORT const char * shape_type_name(int shpt)
Get a string with the name of the shape type.