82 static int compare_xpnts(
const void *Xpnta,
const void *Xpntb)
102 G_warning(
_(
"Break polygons: Bug in binary tree!"));
112 int i, j, k, ret, ltype, broken, last, nlines;
117 double dx, dy, a1 = 0, a2 = 0;
118 int closed, last_point;
123 static int rect_init = 0;
130 G_debug(1,
"File-based version of Vect_break_polygons()");
133 fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
135 (void)remove(filename);
139 xpntfd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
146 (void)remove(filename);
156 G_debug(3,
"nlines = %d", nlines);
164 G_message(
_(
"Breaking polygons (pass 1: select break points)..."));
166 for (i = 1; i <= nlines; i++) {
185 if (Points->
x[0] == Points->
x[last_point] &&
186 Points->
y[0] == Points->
y[last_point])
191 for (j = 0; j < Points->
n_points; j++) {
194 if (j == last_point && closed)
208 G_debug(3,
"fpoint = %d", fpoint);
211 (!closed && (j == 0 || j == last_point))) {
216 if (j == 0 && closed) {
217 dx = Points->
x[last_point] - Points->
x[0];
218 dy = Points->
y[last_point] - Points->
y[0];
220 dx = Points->
x[1] - Points->
x[0];
221 dy = Points->
y[1] - Points->
y[0];
225 dx = Points->
x[j - 1] - Points->
x[j];
226 dy = Points->
y[j - 1] - Points->
y[j];
228 dx = Points->
x[j + 1] - Points->
x[j];
229 dy = Points->
y[j + 1] - Points->
y[j];
236 lseek(xpntfd, (off_t)(fpoint - 1) *
sizeof(XPNT2), SEEK_SET);
237 if (read(xpntfd, &XPnt,
sizeof(XPNT2)) < 0)
239 __func__, errno, strerror(errno));
247 lseek(xpntfd, (off_t)(fpoint - 1) *
sizeof(XPNT2),
249 if (write(xpntfd, &XPnt,
sizeof(XPNT2)) < 0)
251 __func__, errno, strerror(errno));
254 G_debug(3,
"a1 = %f xa1 = %f a2 = %f xa2 = %f", a1, XPnt.a1,
256 if ((a1 == XPnt.a1 && a2 == XPnt.a2) ||
257 (a1 == XPnt.a2 && a2 == XPnt.a1)) {
262 lseek(xpntfd, (off_t)(fpoint - 1) *
sizeof(XPNT2),
264 if (write(xpntfd, &XPnt,
sizeof(XPNT2)) < 0)
266 __func__, errno, strerror(errno));
273 if (j == 0 || j == (Points->
n_points - 1) ||
285 lseek(xpntfd, (off_t)(npoints - 1) *
sizeof(XPNT2), SEEK_SET);
286 if (write(xpntfd, &XPnt,
sizeof(XPNT2)) < 0)
288 __func__, errno, strerror(errno));
300 G_message(
_(
"Breaking polygons (pass 2: break at selected points)..."));
302 for (i = 1; i <= nlines; i++) {
323 for (j = 1; j < Points->
n_points; j++) {
335 (j == (Points->
n_points - 1) && !broken))
341 G_debug(3,
"fpoint = %d", fpoint);
344 lseek(xpntfd, (off_t)(fpoint - 1) *
sizeof(XPNT2), SEEK_SET);
345 if (read(xpntfd, &XPnt,
sizeof(XPNT2)) < 0)
347 errno, strerror(errno));
350 if ((j == (Points->
n_points - 1) && broken) || XPnt.cross) {
352 for (k = last; k <= j; k++) {
362 "Line %d written j = %d n_points(orig,pruned) = %d "
363 "n_points(new) = %d",
372 if (XPnt.cross && !XPnt.used) {
381 lseek(xpntfd, (off_t)(fpoint - 1) *
sizeof(XPNT2),
383 if (write(xpntfd, &XPnt,
sizeof(XPNT2)) < 0)
385 __func__, errno, strerror(errno));
403 G_debug(3,
"Line %d was deleted", i);
407 G_debug(3,
"Line %d was not changed", i);
427 int i, j, k, ret, ltype, broken, last, nlines;
430 XPNT *XPnt_found, XPnt_search;
431 double dx, dy, a1 = 0, a2 = 0;
432 int closed, last_point, cross;
434 G_debug(1,
"Memory-based version of Vect_break_polygons()");
445 G_debug(3,
"nlines = %d", nlines);
450 XPnt_search.used = 0;
452 G_message(
_(
"Breaking polygons (pass 1: select break points)..."));
454 for (i = 1; i <= nlines; i++) {
473 if (Points->
x[0] == Points->
x[last_point] &&
474 Points->
y[0] == Points->
y[last_point])
479 for (j = 0; j < Points->
n_points; j++) {
482 if (j == last_point && closed)
485 XPnt_search.x = Points->
x[j];
486 XPnt_search.y = Points->
y[j];
492 (!closed && (j == 0 || j == last_point))) {
497 if (j == 0 && closed) {
498 dx = Points->
x[last_point] - Points->
x[0];
499 dy = Points->
y[last_point] - Points->
y[0];
501 dx = Points->
x[1] - Points->
x[0];
502 dy = Points->
y[1] - Points->
y[0];
506 dx = Points->
x[j - 1] - Points->
x[j];
507 dy = Points->
y[j - 1] - Points->
y[j];
509 dx = Points->
x[j + 1] - Points->
x[j];
510 dy = Points->
y[j + 1] - Points->
y[j];
516 if (XPnt_found->cross == 1)
521 XPnt_found->cross = 1;
524 G_debug(3,
"a1 = %f xa1 = %f a2 = %f xa2 = %f", a1,
525 XPnt_found->a1, a2, XPnt_found->a2);
526 if ((a1 == XPnt_found->a1 && a2 == XPnt_found->a2) ||
527 (a1 == XPnt_found->a2 &&
528 a2 == XPnt_found->a1)) {
531 XPnt_found->cross = 1;
536 if (j == 0 || j == (Points->
n_points - 1) ||
540 XPnt_search.cross = 1;
545 XPnt_search.cross = 0;
555 G_debug(2,
"Break polygons: unique vertices: %ld", (
long int)RBTree->
count);
564 G_message(
_(
"Breaking polygons (pass 2: break at selected points)..."));
566 for (i = 1; i <= nlines; i++) {
587 for (j = 1; j < Points->
n_points; j++) {
591 (j == (Points->
n_points - 1) && !broken))
596 XPnt_search.x = Points->
x[j];
597 XPnt_search.y = Points->
y[j];
602 if (XPnt_found ==
NULL)
606 if ((j == (Points->
n_points - 1) && broken) || XPnt_found->cross) {
608 for (k = last; k <= j; k++) {
618 "Line %d written j = %d n_points(orig,pruned) = %d "
619 "n_points(new) = %d",
628 if (XPnt_found->cross && !XPnt_found->used) {
634 XPnt_found->used = 1;
651 G_debug(3,
"Line %d was deleted", i);
655 G_debug(3,
"Line %d was not changed", i);
687 if (getenv(
"GRASS_VECTOR_LOWMEM"))
void Vect_break_polygons_mem(struct Map_info *Map, int type, struct Map_info *Err)
void Vect_break_polygons_file(struct Map_info *Map, int type, struct Map_info *Err)
void Vect_break_polygons(struct Map_info *Map, int type, struct Map_info *Err)
Break polygons in vector map.
void G_percent(long, long, int)
Print percent complete messages.
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void void G_verbose_message(const char *,...) __attribute__((format(printf
char * G_tempfile(void)
Returns a temporary file name.
void G_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
int rbtree_insert(struct RB_TREE *, void *)
void * rbtree_find(struct RB_TREE *, const void *)
void rbtree_destroy(struct RB_TREE *)
struct RB_TREE * rbtree_create(rb_compare_fn *, size_t)
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
off_t Vect_rewrite_line(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites existing feature (topological level required)
plus_t Vect_get_num_lines(struct Map_info *)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_read_line(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
int Vect_line_alive(struct Map_info *, int)
Check if feature is alive or dead (topological level required)
int Vect_delete_line(struct Map_info *, off_t)
Delete existing feature (topological level required)
off_t Vect_write_line(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes a new feature.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
void Vect_reset_line(struct line_pnts *)
Reset line.
int Vect_line_prune(struct line_pnts *)
Remove duplicate points, i.e. zero length segments.
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
#define GV_POINT
Feature types used in memory on run time (may change)
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Feature geometry info - coordinates.
double * y
Array of Y coordinates.
double * x
Array of X coordinates.
int n_points
Number of points.
double * z
Array of Z coordinates.
int RTreeInsertRect(struct RTree_Rect *r, int tid, struct RTree *t)
Insert an item into a R*-Tree.
void RTreeDestroyTree(struct RTree *t)
Destroy an R*-Tree.
int RTreeSearch(struct RTree *t, struct RTree_Rect *r, SearchHitCallback *shcb, void *cbarg)
Search an R*-Tree.
struct RTree * RTreeCreateTree(int fd, off_t rootpos, int ndims)
Create new empty R*-Tree.