16 #include "CUnit/Basic.h"
90 CU_ASSERT_EQUAL(rv, 0);
110 CU_ASSERT_DOUBLE_EQUAL(d1, 0.0046097720751, 0.0001);
111 CU_ASSERT_DOUBLE_EQUAL(c1.
x, 2047538.599, 0.001);
112 CU_ASSERT_DOUBLE_EQUAL(c1.
y, 7268770.4395, 0.001);
123 #define setpoint(p, x1, y1) {(p).x = (x1); (p).y = (y1);}
409 CU_ASSERT_EQUAL( p.
x, 1.5 );
412 CU_ASSERT_EQUAL( p.
m, 2.5 );
415 CU_ASSERT_EQUAL( p.
z, 3.5 );
452 CU_ASSERT_EQUAL(
r.x, 15.0);
456 CU_ASSERT_EQUAL(
r.y, 21.0);
460 CU_ASSERT_EQUAL(
r.y, 30.0);
464 CU_ASSERT_EQUAL(
r.y, 20.0);
560 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((0 1.5,0 2,0 2.5))");
568 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((0 3.5,0 4))");
576 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((0 0,0 1,0 2,0 2.5))" );
584 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((0 0,0 1,0 2,0 3,0 4))" );
592 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((0 1,0 2))" );
600 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((0 0,0 1,0 2))" );
608 CU_ASSERT_STRING_EQUAL(ewkt,
"GEOMETRYCOLLECTION(POINT(0 0))" );
616 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((2 2 2,1 1 1))" );
626 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((2 2 2,1 1 1))" );
636 CU_ASSERT_STRING_EQUAL(ewkt,
"GEOMETRYCOLLECTION(POINT(1 1 1))" );
646 CU_ASSERT_STRING_EQUAL(ewkt,
"GEOMETRYCOLLECTION(POINT(1 1 1))" );
671 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((0 1.5,0 2,0 2.5))");
680 mline = (
LWMLINE*)
lwgeom_from_wkt(
"MULTILINESTRING((1 0,1 1,1 2,1 3,1 4), (0 0,0 1,0 2,0 3,0 4))",
LW_PARSER_CHECK_NONE);
686 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((1 3.5,1 4),(0 3.5,0 4))");
695 mline = (
LWMLINE*)
lwgeom_from_wkt(
"MULTILINESTRING((1 0,1 -1,1 -2,1 -3,1 -4), (0 0,0 1,0 2,0 3,0 4))",
LW_PARSER_CHECK_NONE);
701 CU_ASSERT_STRING_EQUAL(ewkt,
"GEOMETRYCOLLECTION(POINT(1 0),LINESTRING(0 0,0 1,0 2,0 2.5))");
716 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((3 3 3 3,3.5 3.5 3.5 3.5),(3.5 3.5 3.5 4.5,3 3 3 5))");
724 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((2 2 2 2,3 3 3 3,3.5 3.5 3.5 3.5),(3.5 3.5 3.5 4.5,3 3 3 5,2 2 2 6))");
732 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((3 3 3 3,4 4 4 4,3 3 3 5))");
740 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((2 2 2 2,3 3 3 3),(3 3 3 5,2 2 2 6))");
777 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTILINESTRING((0.5 0.5 0.5,1 1 1,1.5 1.5 1.5))" );
824 CU_ASSERT_STRING_EQUAL(geohash,
"s000000000000000");
829 CU_ASSERT_STRING_EQUAL(geohash,
"w000000000000000");
834 CU_ASSERT_STRING_EQUAL(geohash,
"kkqnpkue9ktbpe5");
844 char *geohash = NULL;
849 CU_ASSERT_STRING_EQUAL(geohash,
"ss2r77s0du7p2ewb8hmx");
856 CU_ASSERT_STRING_EQUAL(geohash,
"ss2r77s0du7p2ewb8hmx");
863 CU_ASSERT_STRING_EQUAL(geohash,
"ss0");
870 CU_ASSERT_STRING_EQUAL(geohash,
"ss06g7h");
877 CU_ASSERT_STRING_EQUAL(geohash,
"ss0");
991 unsigned long long rs;
996 CU_ASSERT_EQUAL(gh, rs);
1000 CU_ASSERT_EQUAL(gh, rs);
1001 p.
x = 140; p.
y = 55;
1004 CU_ASSERT_EQUAL(gh, rs);
1012 g =
lwgeom_from_wkt(
"MULTIPOINT(0 0, 10 0, 10 10, 10 10, 0 10, 0 10, 0 10, 0 0, 0 0, 0 0, 5 5, 0 0, 5 5)",
LW_PARSER_CHECK_NONE);
1015 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTIPOINT(0 0,10 0,10 10,0 10,5 5)");
1022 CU_ASSERT_STRING_EQUAL(ewkt,
"LINESTRING(1612830.15445 4841287.12672,1612829.98813 4841274.56198)");
1026 g =
lwgeom_from_wkt(
"MULTIPOINT(0 0,10 0,10 10,10 10,0 10,0 10,0 10,0 0,0 0,0 0,5 5,5 5,5 8,8 8,8 8,8 8,8 5,8 5,5 5,5 5,5 5,5 5,5 5,50 50,50 50,50 50,50 60,50 60,50 60,60 60,60 50,60 50,50 50,55 55,55 58,58 58,58 55,58 55,55 55)",
LW_PARSER_CHECK_NONE);
1029 CU_ASSERT_STRING_EQUAL(ewkt,
"MULTIPOINT(0 0,10 0,10 10,0 10,5 5,5 8,8 8,8 5,50 50,50 60,60 60,60 50,55 55,55 58,58 58,58 55)");
1036 CU_ASSERT_STRING_EQUAL(ewkt,
"POLYGON((0 0,1 1,1 0,0 0))");
1044 double lat[2], lon[2];
1048 CU_ASSERT_DOUBLE_EQUAL(lat[0], 48, 1e-11);
1049 CU_ASSERT_DOUBLE_EQUAL(lat[1], 48, 1e-11);
1050 CU_ASSERT_DOUBLE_EQUAL(lon[0], -126, 1e-11);
1051 CU_ASSERT_DOUBLE_EQUAL(lon[1], -126, 1e-11);
1068 CU_ASSERT_STRING_EQUAL(ewkt,
"LINESTRING(0 0,0 0)");
1077 CU_ASSERT_STRING_EQUAL(ewkt,
"POLYGON((0 0,1 0,1 1,0 0))");
1085 CU_ASSERT_EQUAL(l, NULL);
1092 CU_ASSERT_EQUAL(l, NULL);
1100 CU_ASSERT_STRING_EQUAL(ewkt,
"LINESTRING(0 0,50 1.00001,100 0)");
1109 CU_ASSERT_STRING_EQUAL(ewkt,
"LINESTRING(0 0,100 0)");
1132 do_median_dims_check(
"MULTIPOINT ZM ((1 3 4 5), (4 7 8 6), (2 9 1 7), (0 4 4 8), (2 2 3 9))", 3);
1140 for (i = 0; i < npoints; i++)
1148 static void do_median_test(
char* input,
char* expected,
int fail_if_not_converged,
int iter_count)
1152 LWPOINT* expected_result = NULL;
1160 if (expected != NULL)
1170 if (result != NULL && expected != NULL)
1180 passed &= fabs(actual_pt.
x - expected_pt.
x) < tolerance;
1181 passed &= fabs(actual_pt.
y - expected_pt.
y) < tolerance;
1182 passed &= (!
lwgeom_has_z((
LWGEOM*) expected_result) || fabs(actual_pt.
z - expected_pt.
z) < tolerance);
1183 passed &= (!
lwgeom_has_m((
LWGEOM*) expected_result) || fabs(actual_pt.
m - expected_pt.
m) < tolerance);
1195 passed = distance_result <= (1.0 + tolerance) * distance_expected;
1198 printf(
"Diff: Got %.10f Expected %.10f\n", distance_result, distance_expected);
1206 printf(
"median_test input %s (parsed %s) expected %s got %s\n",
1213 else if (result == NULL && expected == NULL)
1217 else if (result != NULL && expected == NULL)
1222 else if (result == NULL && expected != NULL)
1229 CU_ASSERT_TRUE(passed);
1253 do_median_test(
"MULTIPOINT ((10 10 10), (10 20 10), (20 10 10), (20 20 10), (10 10 20), (10 20 20), (20 10 20), (20 20 20))",
1254 "POINT (15 15 15)",
LW_TRUE, 1000);
1266 do_median_test(
"MULTIPOINT ZM (1 -1 3 1, 1 0 2 7, 2 1 1 1, EMPTY)",
"POINT (1 0 2)",
LW_TRUE, 1000);
1270 do_median_test(
"MULTIPOINT ZM (-1 1 -3 1, -1 0 -2 7, -2 -1 -1 1)",
"POINT (-1 0 -2)",
LW_TRUE, 1000);
1271 do_median_test(
"MULTIPOINT ZM (-1 1 -3 1, -1 0 -2 7, -2 -1 -1 0.5, -2 -1 -1 0.5)",
"POINT (-1 0 -2)",
LW_TRUE, 1000);
1274 do_median_test(
"MULTIPOINT ZM ((0 -1 0 1), (0 0 0 1), (0 1 0 0.5), (0 1 0 0.5))",
"POINT (0 0 0)",
LW_TRUE, 1000);
1276 do_median_test(
"MULTIPOINT ZM ((1 -1 3 1), (1 0 2 7), (2 1 1 2), (2 1 1 -1))", NULL,
LW_TRUE, 1000);
1296 "(1000 0 -200 100),"
1297 "(1000 0 -590 100),"
1301 "POINT (1025 0 -65)",
LW_TRUE, 10000);
1308 "(0 -3000 -3472.22222222222262644208967685699462890625 1),"
1309 "(0 3000 3472.22222222222262644208967685699462890625 1),"
1310 "(0 0 -1644.736842105263121993630193173885345458984375 1),"
1311 "(0 0 1644.736842105263121993630193173885345458984375 1),"
1312 "(0 48000 -20000 1.3),"
1313 "(0 -48000 -20000 1.3)"
1315 "POINT (0 0 0)",
LW_TRUE, 10000);
1323 "(0 -3000 -3472.22222222222262644208967685699462890625 1),"
1324 "(0 3000 3472.22222222222262644208967685699462890625 1),"
1325 "(0 -0.00000000000028047739569477638384522295466033823196 -1644.736842105263121993630193173885345458984375 1),"
1326 "(0 0.00000000000028047739569477638384522295466033823196 1644.736842105263121993630193173885345458984375 1),"
1327 "(0 48000 -20000 1.3),"
1328 "(0 -48000 -20000 1.3)"
1330 "POINT (0 0 0)",
LW_TRUE, 10000);
1343 CU_ASSERT_EQUAL(mpt->
ngeoms,100);
1350 CU_ASSERT_EQUAL(mpt->
ngeoms,1);
1354 CU_ASSERT_EQUAL(mpt, NULL);
1360 geom =
lwgeom_from_wkt(
"MULTIPOLYGON(((10 0,0 10,10 20,20 10,10 0)),((0 0,5 0,5 5,0 5,0 0)))",
LW_PARSER_CHECK_NONE);
1363 CU_ASSERT_EQUAL(mpt->
ngeoms,1000);
1367 CU_ASSERT_EQUAL(mpt->
ngeoms,1);
1377 const int srid = 4326;
1378 const uint32_t segments_per_quad = 17;
1390 CU_ASSERT_DOUBLE_EQUAL(g->
xmin,
x-
r, 0.1);
1391 CU_ASSERT_DOUBLE_EQUAL(g->
xmax,
x+
r, 0.1);
1392 CU_ASSERT_DOUBLE_EQUAL(g->
ymin,
y-
r, 0.1);
1393 CU_ASSERT_DOUBLE_EQUAL(g->
ymax,
y+
r, 0.1);
1401 CU_ASSERT_TRUE(p == NULL);
1405 CU_ASSERT_TRUE(p == NULL);
1409 CU_ASSERT_TRUE(p != NULL);
1415 static int cluster_size = 100;
1416 static int num_clusters = 4;
1417 static double spread = 1.5;
1418 int N = cluster_size * num_clusters;
1425 for (j = 0; j < num_clusters; j++) {
1426 for (i = 0; i < cluster_size; i++)
1428 double u1 = 1.0 * rand() / RAND_MAX;
1429 double u2 = 1.0 * rand() / RAND_MAX;
1430 double z1 = spread * j + sqrt(-2*log2(u1))*cos(2*M_PI*u2);
1431 double z2 = spread * j + sqrt(-2*log2(u1))*sin(2*M_PI*u2);
1447 for (i = 0; i < k; i++)
1462 pt.
x = 1.2345678901234;
1463 pt.
y = 2.3456789012345;
1464 pt.
z = 3.4567890123456;
1465 pt.
m = 4.5678901234567;
1488 CU_ASSERT_DOUBLE_EQUAL(pt1.
x, pt2.
x, pow(10, -1*
precision));
1489 CU_ASSERT_DOUBLE_EQUAL(pt1.
y, pt2.
y, pow(10, -1*
precision));
1490 CU_ASSERT_DOUBLE_EQUAL(pt1.
z, pt2.
z, pow(10, -1*
precision));
1491 CU_ASSERT_DOUBLE_EQUAL(pt1.
m, pt2.
m, pow(10, -1*
precision));
static void test_kmeans(void)
static void test_geohash(void)
static void test_geohash_bbox(void)
static void test_lw_segment_intersects(void)
static void test_lwline_clip_big(void)
static void test_point_interpolate(void)
static int init_cg_suite(void)
static void test_lwline_crossing_bugs(void)
static void test_geohash_point(void)
#define setpoint(p, x1, y1)
static void test_isclosed(void)
static void test_lw_arc_center(void)
static void test_lwgeom_remove_repeated_points(void)
static void test_point_density(void)
static void test_lwpoly_construct_circle(void)
static void test_median_robustness(void)
static double test_weighted_distance(const POINT4D *curr, const POINT4D *points, uint32_t npoints)
static void test_lwgeom_simplify(void)
LWGEOM_PARSER_RESULT parse_result
static int clean_cg_suite(void)
static void test_geohash_precision(void)
static void test_lwline_crossing_short_lines(void)
static void test_lwmline_clip(void)
static void test_trim_bits(void)
static void test_geohash_point_as_int(void)
static void test_median_handles_3d_correctly(void)
static void test_lwline_crossing_long_lines(void)
static void do_median_dims_check(char *wkt, int expected_dims)
void algorithms_suite_setup(void)
static void test_lwpoint_get_ordinate(void)
static void test_lwline_interpolate_points(void)
static void do_median_test(char *input, char *expected, int fail_if_not_converged, int iter_count)
static void test_lwpoint_set_ordinate(void)
static void test_lw_segment_side(void)
static void test_lwline_clip(void)
void gbox_init(GBOX *gbox)
Zero out all the entries in the GBOX.
void cu_error_msg_reset()
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
#define ASSERT_POINT4D_EQUAL(o, e, eps)
#define ASSERT_INT_EQUAL(o, e)
#define PG_ADD_TEST(suite, testfunc)
#define ASSERT_STRING_EQUAL(o, e)
#define ASSERT_POINT2D_EQUAL(o, e, eps)
int lwpoint_getPoint4d_p(const LWPOINT *point, POINT4D *out)
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
LWPOINT * lwpoint_make2d(int srid, double x, double y)
POINT4D getPoint4d(const POINTARRAY *pa, uint32_t n)
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
LWGEOM * lwgeom_simplify(const LWGEOM *igeom, double dist, int preserve_collapsed)
int lwgeom_ndims(const LWGEOM *geom)
Return the number of dimensions (2, 3, 4) in a geometry.
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
void lwmpoint_free(LWMPOINT *mpt)
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
void lwgeom_trim_bits_in_place(LWGEOM *geom, int32_t prec_x, int32_t prec_y, int32_t prec_z, int32_t prec_m)
Trim the bits of an LWGEOM in place, to optimize it for compression.
void lwpoint_free(LWPOINT *pt)
void lwgeom_free(LWGEOM *geom)
#define LW_PARSER_CHECK_NONE
void lwgeom_remove_repeated_points_in_place(LWGEOM *in, double tolerance)
unsigned int geohash_point_as_int(POINT2D *pt)
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
int lwline_crossing_direction(const LWLINE *l1, const LWLINE *l2)
Given two lines, characterize how (and if) they cross each other.
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
LWPOLY * lwpoly_construct_circle(int srid, double x, double y, double radius, uint32_t segments_per_quarter, char exterior)
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
double lwgeom_area(const LWGEOM *geom)
int ptarray_insert_point(POINTARRAY *pa, const POINT4D *p, uint32_t where)
Insert a point into an existing POINTARRAY.
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
char * lwgeom_geohash(const LWGEOM *lwgeom, int precision)
Calculate the GeoHash (http://geohash.org) string for a geometry.
double distance3d_pt_pt(const POINT3D *p1, const POINT3D *p2)
POINTARRAY * lwline_interpolate_points(const LWLINE *line, double length_fraction, char repeat)
Interpolate one or more points along a line.
LWMPOINT * lwgeom_to_points(const LWGEOM *lwgeom, uint32_t npoints)
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
@ LINE_MULTICROSS_END_RIGHT
@ LINE_MULTICROSS_END_SAME_FIRST_LEFT
@ LINE_MULTICROSS_END_LEFT
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
void lwcollection_free(LWCOLLECTION *col)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
void ptarray_free(POINTARRAY *pa)
const GBOX * lwgeom_get_bbox(const LWGEOM *lwgeom)
Get a non-empty geometry bounding box, computing and caching it if not already there.
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
void * lwalloc(size_t size)
void lwpoly_free(LWPOLY *poly)
LWPOINT * lwgeom_median(const LWGEOM *g, double tol, uint32_t maxiter, char fail_if_not_converged)
void lwmline_free(LWMLINE *mline)
#define LW_TRUE
Return types for functions with status returns.
#define SRID_UNKNOWN
Unknown SRID value.
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
void lwline_free(LWLINE *line)
int * lwgeom_cluster_2d_kmeans(const LWGEOM **geoms, uint32_t ngeoms, uint32_t k)
Take a list of LWGEOMs and a number of clusters and return an integer array indicating which cluster ...
void lwpoint_set_ordinate(POINT4D *p, char ordinate, double value)
Given a point, ordinate number and value, set that ordinate on the point.
int lwgeom_geohash_precision(GBOX bbox, GBOX *bounds)
int lwcircstring_is_closed(const LWCIRCSTRING *curve)
double lwpoint_get_ordinate(const POINT4D *p, char ordinate)
Given a POINT4D and an ordinate number, return the value of the ordinate.
LWCOLLECTION * lwline_clip_to_ordinate_range(const LWLINE *line, char ordinate, double from, double to)
Clip a line based on the from/to range of one of its ordinates.
int lwline_is_closed(const LWLINE *line)
int lwcompound_is_closed(const LWCOMPOUND *curve)
int lw_segment_intersects(const POINT2D *p1, const POINT2D *p2, const POINT2D *q1, const POINT2D *q2)
returns the kind of CG_SEGMENT_INTERSECTION_TYPE behavior of lineseg 1 (constructed from p1 and p2) a...
POINT4D * lwmpoint_extract_points_4d(const LWMPOINT *g, uint32_t *npoints, int *input_empty)
double lw_arc_center(const POINT2D *p1, const POINT2D *p2, const POINT2D *p3, POINT2D *result)
Determines the center of the circle defined by the three given points.
#define FP_TOLERANCE
Floating point comparators.
int ptarray_has_z(const POINTARRAY *pa)
int lw_segment_side(const POINT2D *p1, const POINT2D *p2, const POINT2D *q)
lw_segment_side()
int point_interpolate(const POINT4D *p1, const POINT4D *p2, POINT4D *p, int hasz, int hasm, char ordinate, double interpolation_value)
Given two points, a dimensionality, an ordinate, and an interpolation value generate a new point that...
void decode_geohash_bbox(char *geohash, double *lat, double *lon, int precision)
int ptarray_has_m(const POINTARRAY *pa)
LWCOLLECTION * lwmline_clip_to_ordinate_range(const LWMLINE *mline, char ordinate, double from, double to)
Clip a multi-line based on the from/to range of one of its ordinates.
char * geohash_point(double longitude, double latitude, int precision)
Datum distance(PG_FUNCTION_ARGS)
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM.