/** * @brief Merge two given POINTARRAY and returns a pointer * on the new aggregate one. * Warning: this function free the two inputs POINTARRAY * @return #POINTARRAY is newly allocated */ POINTARRAY * ptarray_merge(POINTARRAY *pa1, POINTARRAY *pa2) { POINTARRAY *pa; size_t ptsize = ptarray_point_size(pa1); if (FLAGS_GET_ZM(pa1->flags) != FLAGS_GET_ZM(pa2->flags)) lwerror("ptarray_cat: Mixed dimension"); pa = ptarray_construct( FLAGS_GET_Z(pa1->flags), FLAGS_GET_M(pa1->flags), pa1->npoints + pa2->npoints); memcpy( getPoint_internal(pa, 0), getPoint_internal(pa1, 0), ptsize*(pa1->npoints)); memcpy( getPoint_internal(pa, pa1->npoints), getPoint_internal(pa2, 0), ptsize*(pa2->npoints)); lwfree(pa1); lwfree(pa2); return pa; }
static LWMPOINT* lwmline_locate_along(const LWMLINE *lwmline, double m, double offset) { LWMPOINT *lwmpoint = NULL; LWGEOM *lwg = lwmline_as_lwgeom(lwmline); int i, j; /* Return degenerates upwards */ if ( (!lwmline) || (lwmline->ngeoms < 1) ) return NULL; /* Construct return */ lwmpoint = lwmpoint_construct_empty(lwgeom_get_srid(lwg), lwgeom_has_z(lwg), lwgeom_has_m(lwg)); /* Locate along each sub-line */ for ( i = 0; i < lwmline->ngeoms; i++ ) { LWMPOINT *along = lwline_locate_along(lwmline->geoms[i], m, offset); if ( along != NULL ) { for ( j = 0; j < along->ngeoms; j++ ) { lwmpoint_add_lwpoint(lwmpoint, along->geoms[i]); } /* Free the containing geometry, but leave the sub-geometries around */ if ( along->bbox ) lwfree(along->bbox); lwfree(along); } } return lwmpoint; }
static void test_geos_subdivide(void) { #if POSTGIS_GEOS_VERSION < 35 // printf("%d\n", POSTGIS_GEOS_VERSION); return; #else char *ewkt = "MULTILINESTRING((0 0, 0 100))"; char *out_ewkt; LWGEOM *geom1 = lwgeom_from_wkt(ewkt, LW_PARSER_CHECK_NONE); LWGEOM *geom2 = lwgeom_segmentize2d(geom1, 1.0); LWCOLLECTION *geom3 = lwgeom_subdivide(geom2, 80); out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3); // printf("\n--------\n%s\n--------\n", out_ewkt); CU_ASSERT_EQUAL(2, geom3->ngeoms); lwfree(out_ewkt); lwcollection_free(geom3); geom3 = lwgeom_subdivide(geom2, 20); out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3); // printf("\n--------\n%s\n--------\n", out_ewkt); CU_ASSERT_EQUAL(8, geom3->ngeoms); lwfree(out_ewkt); lwcollection_free(geom3); lwgeom_free(geom2); lwgeom_free(geom1); #endif }
static void test_geos_linemerge(void) { char *ewkt; char *out_ewkt; LWGEOM *geom1; LWGEOM *geom2; ewkt = "MULTILINESTRING((0 0, 0 100),(0 -5, 0 0))"; geom1 = lwgeom_from_wkt(ewkt, LW_PARSER_CHECK_NONE); geom2 = lwgeom_linemerge(geom1); out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2); ASSERT_STRING_EQUAL(out_ewkt, "LINESTRING(0 -5,0 0,0 100)"); lwfree(out_ewkt); lwgeom_free(geom1); lwgeom_free(geom2); ewkt = "MULTILINESTRING EMPTY"; geom1 = lwgeom_from_wkt(ewkt, LW_PARSER_CHECK_NONE); geom2 = lwgeom_linemerge(geom1); out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2); ASSERT_STRING_EQUAL(out_ewkt, "GEOMETRYCOLLECTION EMPTY"); lwfree(out_ewkt); lwgeom_free(geom1); lwgeom_free(geom2); }
void lwline_free (LWLINE *line) { if ( line->bbox ) lwfree(line->bbox); ptarray_free(line->points); lwfree(line); }
void lwcircstring_free(LWCIRCSTRING *curve) { if ( curve->bbox ) lwfree(curve->bbox); if ( curve->points ) ptarray_free(curve->points); lwfree(curve); }
static LWGEOM* lwcollection_split(const LWCOLLECTION* lwcoll_in, const LWGEOM* blade_in) { LWGEOM** split_vector=NULL; LWCOLLECTION* out; size_t split_vector_capacity; size_t split_vector_size=0; size_t i,j; split_vector_capacity=8; split_vector = lwalloc(split_vector_capacity * sizeof(LWGEOM*)); if ( ! split_vector ) { lwerror("Out of virtual memory"); return NULL; } for (i=0; i<lwcoll_in->ngeoms; ++i) { LWCOLLECTION* col; LWGEOM* split = lwgeom_split(lwcoll_in->geoms[i], blade_in); /* an exception should prevent this from ever returning NULL */ if ( ! split ) return NULL; col = lwgeom_as_lwcollection(split); /* Output, if any, will always be a collection */ assert(col); /* Reallocate split_vector if needed */ if ( split_vector_size + col->ngeoms > split_vector_capacity ) { /* NOTE: we could be smarter on reallocations here */ split_vector_capacity += col->ngeoms; split_vector = lwrealloc(split_vector, split_vector_capacity * sizeof(LWGEOM*)); if ( ! split_vector ) { lwerror("Out of virtual memory"); return NULL; } } for (j=0; j<col->ngeoms; ++j) { col->geoms[j]->srid = SRID_UNKNOWN; /* strip srid */ split_vector[split_vector_size++] = col->geoms[j]; } lwfree(col->geoms); lwfree(col); } /* Now split_vector has split_vector_size geometries */ out = lwcollection_construct(COLLECTIONTYPE, lwcoll_in->srid, NULL, split_vector_size, split_vector); return (LWGEOM*)out; }
static void test_lwgeom_simplify(void) { LWGEOM *l; LWGEOM *g; char *ewkt; /* Simplify but only so far... */ g = lwgeom_from_wkt("LINESTRING(0 0, 1 0, 1 1, 0 1, 0 0)", LW_PARSER_CHECK_NONE); l = lwgeom_simplify(g, 10, LW_TRUE); ewkt = lwgeom_to_ewkt(l); CU_ASSERT_STRING_EQUAL(ewkt, "LINESTRING(0 0,0 0)"); lwgeom_free(g); lwgeom_free(l); lwfree(ewkt); /* Simplify but only so far... */ g = lwgeom_from_wkt("POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))", LW_PARSER_CHECK_NONE); l = lwgeom_simplify(g, 10, LW_TRUE); ewkt = lwgeom_to_ewkt(l); CU_ASSERT_STRING_EQUAL(ewkt, "POLYGON((0 0,1 0,1 1,0 0))"); lwgeom_free(g); lwgeom_free(l); lwfree(ewkt); /* Simplify and collapse */ g = lwgeom_from_wkt("LINESTRING(0 0, 1 0, 1 1, 0 1, 0 0)", LW_PARSER_CHECK_NONE); l = lwgeom_simplify(g, 10, LW_FALSE); CU_ASSERT_EQUAL(l, NULL); lwgeom_free(g); lwgeom_free(l); /* Simplify and collapse */ g = lwgeom_from_wkt("POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))", LW_PARSER_CHECK_NONE); l = lwgeom_simplify(g, 10, LW_FALSE); CU_ASSERT_EQUAL(l, NULL); lwgeom_free(g); lwgeom_free(l); /* Not simplifiable */ g = lwgeom_from_wkt("LINESTRING(0 0, 50 1.00001, 100 0)", LW_PARSER_CHECK_NONE); l = lwgeom_simplify(g, 1.0, LW_FALSE); ewkt = lwgeom_to_ewkt(l); CU_ASSERT_STRING_EQUAL(ewkt, "LINESTRING(0 0,50 1.00001,100 0)"); lwgeom_free(g); lwgeom_free(l); lwfree(ewkt); /* Simplifiable */ g = lwgeom_from_wkt("LINESTRING(0 0,50 0.99999,100 0)", LW_PARSER_CHECK_NONE); l = lwgeom_simplify(g, 1.0, LW_FALSE); ewkt = lwgeom_to_ewkt(l); CU_ASSERT_STRING_EQUAL(ewkt, "LINESTRING(0 0,100 0)"); lwgeom_free(g); lwgeom_free(l); lwfree(ewkt); }
LWCURVEPOLY * lwcurvepoly_deserialize(uchar *srl) { LWCURVEPOLY *result; LWGEOM_INSPECTED *insp; int type = lwgeom_getType(srl[0]); int i; LWDEBUG(3, "lwcurvepoly_deserialize called."); if (type != CURVEPOLYTYPE) { lwerror("lwcurvepoly_deserialize called on NON curvepoly: %d", type); return NULL; } insp = lwgeom_inspect(srl); result = lwalloc(sizeof(LWCURVEPOLY)); result->type = insp->type; result->SRID = insp->SRID; result->nrings = insp->ngeometries; result->rings = lwalloc(sizeof(LWGEOM *)*insp->ngeometries); if (lwgeom_hasBBOX(srl[0])) { result->bbox = lwalloc(sizeof(BOX2DFLOAT4)); memcpy(result->bbox, srl + 1, sizeof(BOX2DFLOAT4)); } else result->bbox = NULL; for (i = 0; i < insp->ngeometries; i++) { result->rings[i] = lwgeom_deserialize(insp->sub_geoms[i]); if (lwgeom_getType(result->rings[i]->type) != CIRCSTRINGTYPE && lwgeom_getType(result->rings[i]->type) != LINETYPE && lwgeom_getType(result->rings[i]->type) != COMPOUNDTYPE) { lwerror("Only Circular curves, Linestrings and Compound curves are supported as rings, not %s (%d)", lwgeom_typename(result->rings[i]->type), result->rings[i]->type); lwfree(result); lwfree(insp); return NULL; } if (TYPE_NDIMS(result->rings[i]->type) != TYPE_NDIMS(result->type)) { lwerror("Mixed dimensions (curvepoly %d, ring %d)", TYPE_NDIMS(result->type), i, TYPE_NDIMS(result->rings[i]->type)); lwfree(result); lwfree(insp); return NULL; } } return result; }
void ptarray_free(POINTARRAY *pa) { if(pa) { if(pa->serialized_pointlist && ( ! FLAGS_GET_READONLY(pa->flags) ) ) lwfree(pa->serialized_pointlist); lwfree(pa); LWDEBUG(5,"Freeing a PointArray"); } }
void lwpoint_free(LWPOINT *pt) { if ( ! pt ) return; if ( pt->bbox ) lwfree(pt->bbox); if ( pt->point ) ptarray_free(pt->point); lwfree(pt); }
/** * Recurse from top of node tree and free all children. * does not free underlying point array. */ void circ_tree_free(CIRC_NODE* node) { int i; if ( ! node ) return; for ( i = 0; i < node->num_nodes; i++ ) circ_tree_free(node->nodes[i]); if ( node->nodes ) lwfree(node->nodes); lwfree(node); }
void lwtriangle_free(LWTRIANGLE *triangle) { if ( ! triangle ) return; if (triangle->bbox) lwfree(triangle->bbox); if (triangle->points) ptarray_free(triangle->points); lwfree(triangle); }
static void test_wkt_in_collection(void) { s = "SRID=5;GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(1 0,0 0),CIRCULARSTRING(0 0,0 1,1 1,0 1,2 2))"; r = cu_wkt_in(s, WKT_EXTENDED); //printf("\nIN: %s\nOUT: %s\n",s,r); CU_ASSERT_STRING_EQUAL(r,s); lwfree(r); s = "GEOMETRYCOLLECTION(POINT(0 0),POINT EMPTY,LINESTRING(1 0,0 0),POLYGON EMPTY,CIRCULARSTRING(0 0,0 1,1 1,0 1,2 2))"; r = cu_wkt_in(s, WKT_SFSQL); //printf("\nIN: %s\nOUT: %s\n",s,r); CU_ASSERT_STRING_EQUAL(r,s); lwfree(r); }
static void test_wkt_in_compoundcurve(void) { s = "SRID=4326;COMPOUNDCURVEM(CIRCULARSTRING(0 0 2,1 1 2,1 0 2),(1 0 2,0 1 2))"; r = cu_wkt_in(s, WKT_EXTENDED); CU_ASSERT_STRING_EQUAL(r,s); //printf("\nIN: %s\nOUT: %s\n",s,r); lwfree(r); s = "COMPOUNDCURVE Z (CIRCULARSTRING Z (0 0 0,0 1 0,1 1 0,0 0 0,2 2 0),(2 2 0,0 0 1,1 1 1,2 2 1))"; r = cu_wkt_in(s, WKT_ISO); CU_ASSERT_STRING_EQUAL(r,s); //printf("\nIN: %s\nOUT: %s\n",s,r); lwfree(r); }
projPJ lwproj_from_string(const char *str1) { int t; char *params[1024]; /* one for each parameter */ char *loc; char *str; size_t slen; projPJ result; if (str1 == NULL) return NULL; slen = strlen(str1); if (slen == 0) return NULL; str = lwalloc(slen+1); strcpy(str, str1); /* * first we split the string into a bunch of smaller strings, * based on the " " separator */ params[0] = str; /* 1st param, we'll null terminate at the " " soon */ loc = str; t = 1; while ((loc != NULL) && (*loc != 0) ) { loc = strchr(loc, ' '); if (loc != NULL) { *loc = 0; /* null terminate */ params[t] = loc+1; loc++; /* next char */ t++; /*next param */ } } if (!(result=pj_init(t, params))) { lwfree(str); return NULL; } lwfree(str); return result; }
void lwmpoint_free(LWMPOINT *mpt) { int i; if ( mpt->bbox ) lwfree(mpt->bbox); for ( i = 0; i < mpt->ngeoms; i++ ) if ( mpt->geoms && mpt->geoms[i] ) lwpoint_free(mpt->geoms[i]); if ( mpt->geoms ) lwfree(mpt->geoms); lwfree(mpt); }
void lwmline_free(LWMLINE *mline) { int i; if ( mline->bbox ) lwfree(mline->bbox); for ( i = 0; i < mline->ngeoms; i++ ) if ( mline->geoms && mline->geoms[i] ) lwline_free(mline->geoms[i]); if ( mline->geoms ) lwfree(mline->geoms); lwfree(mline); }
static CIRC_NODE* lwcollection_calculate_circ_tree(const LWCOLLECTION* lwcol) { int i = 0, j = 0; CIRC_NODE** nodes; CIRC_NODE* node; /* One geometry? Done! */ if ( lwcol->ngeoms == 1 ) return lwgeom_calculate_circ_tree(lwcol->geoms[0]); /* Calculate a tree for each sub-geometry*/ nodes = lwalloc(lwcol->ngeoms * sizeof(CIRC_NODE*)); for ( i = 0; i < lwcol->ngeoms; i++ ) { node = lwgeom_calculate_circ_tree(lwcol->geoms[i]); if ( node ) nodes[j++] = node; } /* Put the trees into a spatially correlated order */ circ_nodes_sort(nodes, j); /* Merge the trees pairwise up to a parent node and return */ node = circ_nodes_merge(nodes, j); /* Don't need the working list any more */ lwfree(nodes); return node; }
void lwgeom_drop_bbox(LWGEOM *lwgeom) { if ( lwgeom->bbox ) lwfree(lwgeom->bbox); lwgeom->bbox = NULL; FLAGS_SET_BBOX(lwgeom->flags, 0); }
LWGEOM * lwmpolygon_desegmentize(LWMPOLY *mpoly) { LWGEOM **geoms; int i, hascurve = 0; LWDEBUG(2, "lwmpoly_desegmentize called."); geoms = lwalloc(sizeof(LWGEOM *)*mpoly->ngeoms); for (i=0; i<mpoly->ngeoms; i++) { geoms[i] = lwpolygon_desegmentize((LWPOLY *)mpoly->geoms[i]); if (geoms[i]->type == CURVEPOLYTYPE) { hascurve = 1; } } if (hascurve == 0) { for (i=0; i<mpoly->ngeoms; i++) { lwfree(geoms[i]); } return lwgeom_clone((LWGEOM *)mpoly); } return (LWGEOM *)lwcollection_construct(MULTISURFACETYPE, mpoly->srid, NULL, mpoly->ngeoms, geoms); }
LWGEOM * lwmline_desegmentize(LWMLINE *mline) { LWGEOM **geoms; int i, hascurve = 0; LWDEBUG(2, "lwmline_desegmentize called."); geoms = lwalloc(sizeof(LWGEOM *)*mline->ngeoms); for (i=0; i<mline->ngeoms; i++) { geoms[i] = lwline_desegmentize((LWLINE *)mline->geoms[i]); if (geoms[i]->type == CIRCSTRINGTYPE || geoms[i]->type == COMPOUNDTYPE) { hascurve = 1; } } if (hascurve == 0) { for (i=0; i<mline->ngeoms; i++) { lwfree(geoms[i]); } return lwgeom_clone((LWGEOM *)mline); } return (LWGEOM *)lwcollection_construct(MULTICURVETYPE, mline->srid, NULL, mline->ngeoms, geoms); }
void lwmpoly_free(LWMPOLY *mpoly) { int i; if ( ! mpoly ) return; if ( mpoly->bbox ) lwfree(mpoly->bbox); for ( i = 0; i < mpoly->ngeoms; i++ ) if ( mpoly->geoms && mpoly->geoms[i] ) lwpoly_free(mpoly->geoms[i]); if ( mpoly->geoms ) lwfree(mpoly->geoms); lwfree(mpoly); }
void lwpsurface_free(LWPSURFACE *psurf) { int i; if ( ! psurf ) return; if ( psurf->bbox ) lwfree(psurf->bbox); for ( i = 0; i < psurf->ngeoms; i++ ) if ( psurf->geoms && psurf->geoms[i] ) lwpoly_free(psurf->geoms[i]); if ( psurf->geoms ) lwfree(psurf->geoms); lwfree(psurf); }
Datum point_from_geohash(PG_FUNCTION_ARGS) { GBOX *box = NULL; LWPOINT *point = NULL; GSERIALIZED *result = NULL; text *geohash_input = NULL; char *geohash = NULL; double lon, lat; int precision = -1; if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); } if (!PG_ARGISNULL(1)) { precision = PG_GETARG_INT32(1); } geohash_input = PG_GETARG_TEXT_P(0); geohash = text2cstring(geohash_input); box = parse_geohash(geohash, precision); lon = box->xmin + (box->xmax - box->xmin) / 2; lat = box->ymin + (box->ymax - box->ymin) / 2; point = lwpoint_make2d(SRID_UNKNOWN, lon, lat); result = geometry_serialize((LWGEOM *) point); lwfree(box); PG_RETURN_POINTER(result); }
void lwtin_free(LWTIN *tin) { int i; if ( ! tin ) return; if ( tin->bbox ) lwfree(tin->bbox); for ( i = 0; i < tin->ngeoms; i++ ) if ( tin->geoms && tin->geoms[i] ) lwtriangle_free(tin->geoms[i]); if ( tin->geoms ) lwfree(tin->geoms); lwfree(tin); }
char * lwcollection_summary(LWCOLLECTION *col, int offset) { size_t size = 128; char *result; char *tmp; int i; char *pad=""; LWDEBUG(2, "lwcollection_summary called"); result = (char *)lwalloc(size); sprintf(result, "%*.s%s[%s] with %d elements\n", offset, pad, lwgeom_typename(TYPE_GETTYPE(col->type)), lwgeom_typeflags(col->type), col->ngeoms); for (i=0; i<col->ngeoms; i++) { tmp = lwgeom_summary(col->geoms[i], offset+2); size += strlen(tmp)+1; result = lwrealloc(result, size); LWDEBUGF(4, "Reallocated %d bytes for result", size); strcat(result, tmp); lwfree(tmp); } LWDEBUG(3, "lwcollection_summary returning"); return result; }
static CIRC_NODE* lwpoly_calculate_circ_tree(const LWPOLY* lwpoly) { int i = 0, j = 0; CIRC_NODE** nodes; CIRC_NODE* node; /* One ring? Handle it like a line. */ if ( lwpoly->nrings == 1 ) return circ_tree_new(lwpoly->rings[0]); /* Calculate a tree for each non-trivial ring of the polygon */ nodes = lwalloc(lwpoly->nrings * sizeof(CIRC_NODE*)); for ( i = 0; i < lwpoly->nrings; i++ ) { node = circ_tree_new(lwpoly->rings[i]); if ( node ) nodes[j++] = node; } /* Put the trees into a spatially correlated order */ circ_nodes_sort(nodes, j); /* Merge the trees pairwise up to a parent node and return */ node = circ_nodes_merge(nodes, j); /* Don't need the working list any more */ lwfree(nodes); return node; }
LWGEOM * lwpolygon_desegmentize(LWPOLY *poly) { LWGEOM **geoms; int i, hascurve = 0; LWDEBUG(2, "lwpolygon_desegmentize called."); geoms = lwalloc(sizeof(LWGEOM *)*poly->nrings); for (i=0; i<poly->nrings; i++) { geoms[i] = pta_desegmentize(poly->rings[i], poly->flags, poly->srid); if (geoms[i]->type == CIRCSTRINGTYPE || geoms[i]->type == COMPOUNDTYPE) { hascurve = 1; } } if (hascurve == 0) { for (i=0; i<poly->nrings; i++) { lwfree(geoms[i]); } return lwgeom_clone((LWGEOM *)poly); } return (LWGEOM *)lwcollection_construct(CURVEPOLYTYPE, poly->srid, NULL, poly->nrings, geoms); }
static void test_lwgeom_delaunay_triangulation(void) { #if POSTGIS_GEOS_VERSION >= 34 LWGEOM *in, *tmp, *out; char *wkt, *exp_wkt; /* Because i don't trust that much prior tests... ;) */ cu_error_msg_reset(); in = lwgeom_from_wkt("MULTIPOINT(10 0, 20 0, 5 5)", LW_PARSER_CHECK_NONE); tmp = lwgeom_delaunay_triangulation(in, 0, 0); lwgeom_free(in); out = lwgeom_normalize(tmp); lwgeom_free(tmp); wkt = lwgeom_to_ewkt(out); lwgeom_free(out); exp_wkt = "GEOMETRYCOLLECTION(POLYGON((5 5,20 0,10 0,5 5)))"; if ( strcmp(wkt, exp_wkt) ) { fprintf(stderr, "\nExp: %s\nObt: %s\n", exp_wkt, wkt); } CU_ASSERT_STRING_EQUAL(wkt, exp_wkt); lwfree(wkt); #endif /* POSTGIS_GEOS_VERSION >= 33 */ }