int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int repeated_points) { /* Check for pathology */ if( ! pa || ! pt ) { lwerror("ptarray_append_point: null input"); return LW_FAILURE; } /* Check for duplicate end point */ if ( repeated_points == LW_FALSE && pa->npoints > 0 ) { POINT4D tmp; getPoint4d_p(pa, pa->npoints-1, &tmp); LWDEBUGF(4,"checking for duplicate end point (pt = POINT(%g %g) pa->npoints-q = POINT(%g %g))",pt->x,pt->y,tmp.x,tmp.y); /* Return LW_SUCCESS and do nothing else if previous point in list is equal to this one */ if ( (pt->x == tmp.x) && (pt->y == tmp.y) && (FLAGS_GET_Z(pa->flags) ? pt->z == tmp.z : 1) && (FLAGS_GET_M(pa->flags) ? pt->m == tmp.m : 1) ) { return LW_SUCCESS; } } /* Append is just a special case of insert */ return ptarray_insert_point(pa, pt, pa->npoints); }
static void test_ptarray_insert_point(void) { LWLINE *line; char *wkt; POINT4D p; line = lwgeom_as_lwline(lwgeom_from_text("LINESTRING EMPTY")); p.x = 1; p.y = 1; ptarray_insert_point(line->points, &p, 0); wkt = lwgeom_to_text(lwline_as_lwgeom(line)); CU_ASSERT_STRING_EQUAL(wkt,"LINESTRING(1 1)"); lwfree(wkt); p.x = 2; p.y = 20; ptarray_insert_point(line->points, &p, 0); wkt = lwgeom_to_text(lwline_as_lwgeom(line)); CU_ASSERT_STRING_EQUAL(wkt,"LINESTRING(2 20,1 1)"); lwfree(wkt); p.x = 3; p.y = 30; ptarray_insert_point(line->points, &p, 1); wkt = lwgeom_to_text(lwline_as_lwgeom(line)); CU_ASSERT_STRING_EQUAL(wkt,"LINESTRING(2 20,3 30,1 1)"); lwfree(wkt); p.x = 4; p.y = 40; ptarray_insert_point(line->points, &p, 0); wkt = lwgeom_to_text(lwline_as_lwgeom(line)); CU_ASSERT_STRING_EQUAL(wkt,"LINESTRING(4 40,2 20,3 30,1 1)"); lwfree(wkt); p.x = 5; p.y = 50; ptarray_insert_point(line->points, &p, 4); wkt = lwgeom_to_text(lwline_as_lwgeom(line)); CU_ASSERT_STRING_EQUAL(wkt,"LINESTRING(4 40,2 20,3 30,1 1,5 50)"); lwfree(wkt); lwline_free(line); }