char * pc_point_to_string(const PCPOINT *pt) { /* { "pcid":1, "values":[<dim1>, <dim2>, <dim3>, <dim4>] }*/ stringbuffer_t *sb = stringbuffer_create(); char *str; int i; stringbuffer_aprintf(sb, "{\"pcid\":%d,\"pt\":[", pt->schema->pcid); for ( i = 0; i < pt->schema->ndims; i++ ) { double d; if ( ! pc_point_get_double_by_index(pt, i, &d) ) { pcerror("pc_point_to_string: unable to read double at position %d", i); } if ( i ) { stringbuffer_append(sb, ","); } stringbuffer_aprintf(sb, "%g", d); } stringbuffer_append(sb, "]}"); str = stringbuffer_getstringcopy(sb); stringbuffer_destroy(sb); return str; }
/* * TIN */ static void lwtin_to_wkt_sb(const LWTIN *tin, stringbuffer_t *sb, int precision, uint8_t variant) { int i = 0; if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "TIN"); /* "TIN" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)tin, sb, variant); } if ( tin->ngeoms < 1 ) { empty_to_wkt_sb(sb); return; } stringbuffer_append(sb, "("); for ( i = 0; i < tin->ngeoms; i++ ) { if ( i > 0 ) stringbuffer_append(sb, ","); /* We don't want type strings on our subgeoms */ lwtriangle_to_wkt_sb(tin->geoms[i], sb, precision, variant | WKT_NO_TYPE ); } stringbuffer_append(sb, ")"); }
/* * POLYHEDRALSURFACE */ static void lwpsurface_to_wkt_sb(const LWPSURFACE *psurf, stringbuffer_t *sb, int precision, uint8_t variant) { int i = 0; if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "POLYHEDRALSURFACE"); /* "POLYHEDRALSURFACE" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)psurf, sb, variant); } if ( psurf->ngeoms < 1 ) { empty_to_wkt_sb(sb); return; } variant = variant | WKT_IS_CHILD; /* Inform the sub-geometries they are childre */ stringbuffer_append(sb, "("); for ( i = 0; i < psurf->ngeoms; i++ ) { if ( i > 0 ) stringbuffer_append(sb, ","); /* We don't want type strings on our subgeoms */ lwpoly_to_wkt_sb(psurf->geoms[i], sb, precision, variant | WKT_NO_TYPE ); } stringbuffer_append(sb, ")"); }
static int ptarray_to_kml2_sb(const POINTARRAY *pa, int precision, stringbuffer_t *sb) { int i, j; int dims = FLAGS_GET_Z(pa->flags) ? 3 : 2; POINT4D pt; double *d; for ( i = 0; i < pa->npoints; i++ ) { getPoint4d_p(pa, i, &pt); d = (double*)(&pt); if ( i ) stringbuffer_append(sb," "); for (j = 0; j < dims; j++) { if ( j ) stringbuffer_append(sb,","); if( fabs(d[j]) < OUT_MAX_DOUBLE ) { if ( stringbuffer_aprintf(sb, "%.*f", precision, d[j]) < 0 ) return LW_FAILURE; } else { if ( stringbuffer_aprintf(sb, "%g", d[j]) < 0 ) return LW_FAILURE; } stringbuffer_trim_trailing_zeroes(sb); } } return LW_SUCCESS; }
/* * MULTIPOLYGON */ static void lwmpoly_to_wkt_sb(const LWMPOLY *mpoly, stringbuffer_t *sb, int precision, uint8_t variant) { int i = 0; if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "MULTIPOLYGON"); /* "MULTIPOLYGON" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)mpoly, sb, variant); } if ( mpoly->ngeoms < 1 ) { empty_to_wkt_sb(sb); return; } stringbuffer_append(sb, "("); variant = variant | WKT_IS_CHILD; /* Inform the sub-geometries they are childre */ for ( i = 0; i < mpoly->ngeoms; i++ ) { if ( i > 0 ) stringbuffer_append(sb, ","); /* We don't want type strings on our subgeoms */ lwpoly_to_wkt_sb(mpoly->geoms[i], sb, precision, variant | WKT_NO_TYPE ); } stringbuffer_append(sb, ")"); }
/* * ISO format uses both Z and M qualifiers. * Extended format only uses an M qualifier for 3DM variants, where it is not * clear what the third dimension represents. * SFSQL format never has more than two dimensions, so no qualifiers. */ static void dimension_qualifiers_to_wkt_sb(const LWGEOM *geom, stringbuffer_t *sb, uint8_t variant) { /* Extended WKT: POINTM(0 0 0) */ #if 0 if ( (variant & WKT_EXTENDED) && ! (variant & WKT_IS_CHILD) && FLAGS_GET_M(geom->flags) && (!FLAGS_GET_Z(geom->flags)) ) #else if ( (variant & WKT_EXTENDED) && FLAGS_GET_M(geom->flags) && (!FLAGS_GET_Z(geom->flags)) ) #endif { stringbuffer_append(sb, "M"); /* "M" */ return; } /* ISO WKT: POINT ZM (0 0 0 0) */ if ( (variant & WKT_ISO) && (FLAGS_NDIMS(geom->flags) > 2) ) { stringbuffer_append(sb, " "); if ( FLAGS_GET_Z(geom->flags) ) stringbuffer_append(sb, "Z"); if ( FLAGS_GET_M(geom->flags) ) stringbuffer_append(sb, "M"); stringbuffer_append(sb, " "); } }
/* align a stringbuffer on begin and end columns. */ void stringbuffer_align(stringbuffer_t *sb, int begin, int end) { char *ptr, *word_string; stringbuffer_t *aligned_string; int len, i; stringbuffer_zap_newline(sb); aligned_string = stringbuffer_create(); ptr = sb->buf; while (1) { word_string = get_string_align(ptr, end, &len); if (word_string == NULL) break; ptr += len; for (i = 0; i < begin; i++) stringbuffer_append(aligned_string, " "); stringbuffer_append(aligned_string, word_string); stringbuffer_append(aligned_string, "\n"); free(word_string); } stringbuffer_copy(sb, aligned_string); stringbuffer_destroy(aligned_string); return; }
/* * Write an empty token out, padding with a space if * necessary. */ static void empty_to_wkt_sb(stringbuffer_t *sb) { if ( ! strchr(" ,(", stringbuffer_lastchar(sb)) ) /* "EMPTY" */ { stringbuffer_append(sb, " "); } stringbuffer_append(sb, "EMPTY"); }
void checkParameter(char *key, char *value) { if (parameter_get(_config_param, key) == NULL) { STRINGBUFFER *tmp = stringbuffer_clone(_config_directory); stringbuffer_append(tmp, SEPARATOR); stringbuffer_append(tmp, value); parameter_add(_config_param, key, stringbuffer_getTextPointer(tmp)); stringbuffer_free(tmp); } }
void initConfig() { _config_param = parameter_new(); /* config-dir */ char *home = getenv("HOME"); if (home == NULL) { error_set(ERROR_CONFIG_HOME, "Unable to get home directory."); return; } STRINGBUFFER *tmp_cfg = stringbuffer_new(); /* config-directory */ stringbuffer_append(tmp_cfg, home); stringbuffer_append(tmp_cfg, SEPARATOR); stringbuffer_append(tmp_cfg, CONFIG_DIRECTORY); _config_directory = stringbuffer_clone(tmp_cfg); /* config-file */ stringbuffer_append(tmp_cfg, SEPARATOR); stringbuffer_append(tmp_cfg, CONFIG_FILE); _config_file = stringbuffer_clone(tmp_cfg); printf ( "CONFIGFILE:\t%s\n", stringbuffer_getTextPointer(_config_file) ); stringbuffer_free(tmp_cfg); /* load config */ _config_param = parameter_new(); parameter_loadFromFile(_config_param, stringbuffer_getTextPointer(_config_file)); checkParameter(CONFIG_DIRECTORY_STORE_KEY, CONFIG_DIRECTORY_STORE_VALUE); checkParameter(CONFIG_DIRECTORY_RESTORE_KEY, CONFIG_DIRECTORY_RESTORE_VALUE); checkParameter(CONFIG_DIRECTORY_KEY_KEY, CONFIG_DIRECTORY_KEY_VALUE); /* make directories */ char *tmp_dir; /* make store directory */ tmp_dir = parameter_get(_config_param, CONFIG_DIRECTORY_STORE_KEY); assert(tmp_dir); if (!directory_exists(tmp_dir)) directory_create(tmp_dir); free(tmp_dir); /* make restore directory */ tmp_dir = parameter_get(_config_param, CONFIG_DIRECTORY_RESTORE_KEY); assert(tmp_dir); if (!directory_exists(tmp_dir)) directory_create(tmp_dir); free(tmp_dir); /* make key directory */ tmp_dir = parameter_get(_config_param, CONFIG_DIRECTORY_KEY_KEY); assert(tmp_dir); if (!directory_exists(tmp_dir)) directory_create(tmp_dir); free(tmp_dir); }
void initList(int strc, char **strv) { if ( strc <= 0) return; stringbuffer_t *sb = stringbuffer_init(); int i; for (i = 0; i < strc; ++i) { stringbuffer_append(sb, strv[i]); stringbuffer_append(sb, " "); } gList = stringbuffer_text(sb); stringbuffer_free(sb); }
/* * TRIANGLE */ static void lwtriangle_to_wkt_sb(const LWTRIANGLE *tri, stringbuffer_t *sb, int precision, uint8_t variant) { if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "TRIANGLE"); /* "TRIANGLE" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)tri, sb, variant); } if ( (! tri->points) || (tri->points->npoints < 1) ) { empty_to_wkt_sb(sb); return; } stringbuffer_append(sb, "("); /* Triangles have extraneous brackets */ ptarray_to_wkt_sb(tri->points, sb, precision, variant); stringbuffer_append(sb, ")"); }
char * pc_patch_dimensional_bytes_array_to_string(PCPATCH_DIMENSIONAL* pd) { int i; char *str; stringbuffer_t *sb = stringbuffer_create(); stringbuffer_append(sb, "{"); if ( pd->schema->ndims ) { stringbuffer_append(sb, "\"bytes\" : [\n"); for ( i = 0; i < pd->schema->ndims; i++ ) { if ( pd->bytes[i].bytes ) { PCBYTES *d = &pd->bytes[i]; if ( i ) stringbuffer_append(sb, ","); stringbuffer_append(sb, "\n { \n"); stringbuffer_aprintf(sb, " %s ", pc_bytes_to_string(d)); stringbuffer_append(sb, " }"); } } stringbuffer_append(sb, "\n]\n"); } stringbuffer_append(sb, "}\n"); str = stringbuffer_getstringcopy(sb); stringbuffer_destroy(sb); return str; }
void config_open(const char *filename) { if (!file_exists(filename)) config_save(filename); STRINGBUFFER *cmd = stringbuffer_new(); #ifdef SYSTEM_OS_TYPE_WINDOW stringbuffer_append(cmd, "notepad.exe"); #else if (file_exists("/usr/bin/vim")) stringbuffer_append(cmd, "/usr/bin/vim"); else if (file_exists("/usr/bin/emacs")) stringbuffer_append(cmd, "/usr/bin/emacs"); else if (file_exists("/usr/bin/nano")) stringbuffer_append(cmd, "/usr/bin/nano"); else if (file_exists("/bin/vi")) stringbuffer_append(cmd, "/bin/vi"); else message_error("no editor found"); #endif stringbuffer_append(cmd, " "); stringbuffer_append(cmd, filename); system(stringbuffer_getTextPointer(cmd)); stringbuffer_free(cmd); config_load(filename); }
/** Convert a PCSCHEMA to a human-readable JSON string */ char * pc_schema_to_json(const PCSCHEMA *pcs) { int i; char *str; stringbuffer_t *sb = stringbuffer_create(); stringbuffer_append(sb, "{"); if ( pcs->pcid ) stringbuffer_aprintf(sb, "\"pcid\" : %d,\n", pcs->pcid); if ( pcs->ndims ) stringbuffer_aprintf(sb, "\"ndims\" : %d,\n", pcs->ndims); if ( pcs->srid ) stringbuffer_aprintf(sb, "\"srid\" : %d,\n", pcs->srid); if ( pcs->compression ) stringbuffer_aprintf(sb, "\"compression\" : %d,\n", pcs->compression); if ( pcs->size ) stringbuffer_aprintf(sb, "\"size\" : %zu,\n", pcs->size); if ( pcs->x_position>=0 ) stringbuffer_aprintf(sb, "\"x_position\" : %d,\n", pcs->x_position); if ( pcs->y_position >=0) stringbuffer_aprintf(sb, "\"y_position\" : %d,\n", pcs->y_position); if ( pcs->namehash->entrycount ) stringbuffer_aprintf(sb, "\"namehash->entrycount\" : %d,\n", pcs->namehash->entrycount); if ( pcs->ndims ) { stringbuffer_append(sb, "\"dims\" : [\n"); for ( i = 0; i < pcs->ndims; i++ ) { stringbuffer_append(sb,pc_dimension_to_json(pcs->dims[i] ) ); } stringbuffer_append(sb, "\n]\n"); } stringbuffer_append(sb, "}\n"); str = stringbuffer_getstringcopy(sb); stringbuffer_destroy(sb); return str; }
/* * Multi-curves provide type information for their curved sub-geometries * but not their linear sub-geometries. * MULTICURVE((0 0, 1 1), CURVESTRING(0 0, 1 1, 2 2)) */ static void lwmcurve_to_wkt_sb(const LWMCURVE *mcurv, stringbuffer_t *sb, int precision, uint8_t variant) { int i = 0; if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "MULTICURVE"); /* "MULTICURVE" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)mcurv, sb, variant); } if ( mcurv->ngeoms < 1 ) { empty_to_wkt_sb(sb); return; } stringbuffer_append(sb, "("); variant = variant | WKT_IS_CHILD; /* Inform the sub-geometries they are childre */ for ( i = 0; i < mcurv->ngeoms; i++ ) { int type = mcurv->geoms[i]->type; if ( i > 0 ) stringbuffer_append(sb, ","); switch (type) { case LINETYPE: /* Linestring subgeoms don't get type identifiers */ lwline_to_wkt_sb((LWLINE*)mcurv->geoms[i], sb, precision, variant | WKT_NO_TYPE ); break; case CIRCSTRINGTYPE: /* But circstring subgeoms *do* get type identifiers */ lwcircstring_to_wkt_sb((LWCIRCSTRING*)mcurv->geoms[i], sb, precision, variant ); break; case COMPOUNDTYPE: /* And compoundcurve subgeoms *do* get type identifiers */ lwcompound_to_wkt_sb((LWCOMPOUND*)mcurv->geoms[i], sb, precision, variant ); break; default: lwerror("lwmcurve_to_wkt_sb: Unknown type recieved %d - %s", type, lwtype_name(type)); } } stringbuffer_append(sb, ")"); }
/* * Compound curves provide type information for their curved sub-geometries * but not their linestring sub-geometries. * COMPOUNDCURVE((0 0, 1 1), CURVESTRING(1 1, 2 2, 3 3)) */ static void lwcompound_to_wkt_sb(const LWCOMPOUND *comp, stringbuffer_t *sb, int precision, uint8_t variant) { int i = 0; if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "COMPOUNDCURVE"); /* "COMPOUNDCURVE" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)comp, sb, variant); } if ( comp->ngeoms < 1 ) { empty_to_wkt_sb(sb); return; } stringbuffer_append(sb, "("); variant = variant | WKT_IS_CHILD; /* Inform the sub-geometries they are childre */ for ( i = 0; i < comp->ngeoms; i++ ) { int type = comp->geoms[i]->type; if ( i > 0 ) stringbuffer_append(sb, ","); /* Linestring subgeoms don't get type identifiers */ if ( type == LINETYPE ) { lwline_to_wkt_sb((LWLINE*)comp->geoms[i], sb, precision, variant | WKT_NO_TYPE ); } /* But circstring subgeoms *do* get type identifiers */ else if ( type == CIRCSTRINGTYPE ) { lwcircstring_to_wkt_sb((LWCIRCSTRING*)comp->geoms[i], sb, precision, variant ); } else { lwerror("lwcompound_to_wkt_sb: Unknown type recieved %d - %s", type, lwtype_name(type)); } } stringbuffer_append(sb, ")"); }
/* append vprintf with alignment. */ void stringbuffer_avprintf_align(stringbuffer_t *sb, int start, int end, const char *fmt, va_list ap) { stringbuffer_t *tmp_sb; char *str; int total, len; /* our first malloc is bogus. */ len = 1; str = malloc(sizeof(char) * len); total = vsnprintf(str, len, fmt, ap); /* total is the real length needed. */ free(str); len = total + 1; str = malloc(sizeof(char) * len); vsnprintf(str, len, fmt, ap); /* now align if we want to align. */ if (start != 0 && end != 0) { tmp_sb = stringbuffer_create(); stringbuffer_append(tmp_sb, str); stringbuffer_align(tmp_sb, start, end); stringbuffer_append(sb, stringbuffer_getstring(tmp_sb)); stringbuffer_destroy(tmp_sb); } else { stringbuffer_append(sb, str); } free(str); return; }
static void test_stringbuffer_append(void) { stringbuffer_t *sb; const char *str; sb = stringbuffer_create_with_size(2); stringbuffer_append(sb, "hello world"); str = stringbuffer_getstring(sb); CU_ASSERT_STRING_EQUAL("hello world", str); stringbuffer_destroy(sb); }
/* * Point array is a list of coordinates. Depending on output mode, * we may suppress some dimensions. ISO and Extended formats include * all dimensions. Standard OGC output only includes X/Y coordinates. */ static void ptarray_to_wkt_sb(const POINTARRAY *ptarray, stringbuffer_t *sb, int precision, uint8_t variant) { /* OGC only includes X/Y */ int dimensions = 2; int i, j; /* ISO and extended formats include all dimensions */ if ( variant & ( WKT_ISO | WKT_EXTENDED ) ) dimensions = FLAGS_NDIMS(ptarray->flags); /* Opening paren? */ if ( ! (variant & WKT_NO_PARENS) ) stringbuffer_append(sb, "("); /* Digits and commas */ for (i = 0; i < ptarray->npoints; i++) { uint8_t *p = getPoint_internal(ptarray, i); double d; /* Commas before ever coord but the first */ if ( i > 0 ) stringbuffer_append(sb, ","); for (j = 0; j < dimensions; j++) { memcpy(&d, p + j * sizeof(double), sizeof(double)); /* Spaces before every ordinate but the first */ if ( j > 0 ) stringbuffer_append(sb, " "); stringbuffer_aprintf(sb, "%.*g", precision, d); } } /* Closing paren? */ if ( ! (variant & WKT_NO_PARENS) ) stringbuffer_append(sb, ")"); }
/* * CIRCULARSTRING */ static void lwcircstring_to_wkt_sb(const LWCIRCSTRING *circ, stringbuffer_t *sb, int precision, uint8_t variant) { if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "CIRCULARSTRING"); /* "CIRCULARSTRING" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)circ, sb, variant); } if ( (! circ->points) || (circ->points->npoints < 1) ) { empty_to_wkt_sb(sb); return; } ptarray_to_wkt_sb(circ->points, sb, precision, variant); }
/* * POLYGON(0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1) */ static void lwpoly_to_wkt_sb(const LWPOLY *poly, stringbuffer_t *sb, int precision, uint8_t variant) { int i = 0; if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "POLYGON"); /* "POLYGON" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)poly, sb, variant); } if ( poly->nrings < 1 ) { empty_to_wkt_sb(sb); return; } stringbuffer_append(sb, "("); for ( i = 0; i < poly->nrings; i++ ) { if ( i > 0 ) stringbuffer_append(sb, ","); ptarray_to_wkt_sb(poly->rings[i], sb, precision, variant); } stringbuffer_append(sb, ")"); }
/** Convert a PCSCHEMA to a human-readable JSON string */ char * pc_schema_to_json(const PCSCHEMA *pcs) { int i; char *str; stringbuffer_t *sb = stringbuffer_create(); stringbuffer_append(sb, "{"); if ( pcs->pcid ) stringbuffer_aprintf(sb, "\"pcid\" : %d,\n", pcs->pcid); if ( pcs->srid ) stringbuffer_aprintf(sb, "\"srid\" : %d,\n", pcs->srid); if ( pcs->compression ) stringbuffer_aprintf(sb, "\"compression\" : %d,\n", pcs->compression); if ( pcs->ndims ) { stringbuffer_append(sb, "\"dims\" : [\n"); for ( i = 0; i < pcs->ndims; i++ ) { if ( pcs->dims[i] ) { PCDIMENSION *d = pcs->dims[i]; if ( i ) stringbuffer_append(sb, ","); stringbuffer_append(sb, "\n { \n"); if ( d->name ) stringbuffer_aprintf(sb, " \"name\" : \"%s\",\n", d->name); if ( d->description ) stringbuffer_aprintf(sb, " \"description\" : \"%s\",\n", d->description); stringbuffer_aprintf(sb, " \"size\" : %d,\n", d->size); stringbuffer_aprintf(sb, " \"byteoffset\" : %d,\n", d->byteoffset); stringbuffer_aprintf(sb, " \"scale\" : %g,\n", d->scale); stringbuffer_aprintf(sb, " \"interpretation\" : \"%s\",\n", pc_interpretation_string(d->interpretation)); stringbuffer_aprintf(sb, " \"offset\" : %g,\n", d->offset); stringbuffer_aprintf(sb, " \"active\" : %d\n", d->active); stringbuffer_append(sb, " }"); } } stringbuffer_append(sb, "\n]\n"); } stringbuffer_append(sb, "}\n"); str = stringbuffer_getstringcopy(sb); stringbuffer_destroy(sb); return str; }
/* TODO: expose to API ? Would require also exposing stringbuffer * See https://github.com/pgpointcloud/pointcloud/issues/74 */ static int pc_patch_uncompressed_to_stringbuffer(const PCPATCH_UNCOMPRESSED *patch, stringbuffer_t *sb) { PCPOINTLIST *pl; int i, j; /* { "pcid":1, "points":[[<dim1>, <dim2>, <dim3>, <dim4>],[<dim1>, <dim2>, <dim3>, <dim4>]] }*/ /* TODO: reserve space in buffer ? */ pl = pc_pointlist_from_uncompressed(patch); stringbuffer_aprintf(sb, "{\"pcid\":%d,\"pts\":[", patch->schema->pcid); for ( i = 0; i < pl->npoints; i++ ) { PCPOINT *pt = pc_pointlist_get_point(pl, i); if ( i ) stringbuffer_append(sb, ",["); else stringbuffer_append(sb, "["); for ( j = 0; j < pt->schema->ndims; j++ ) { double d; if ( ! pc_point_get_double_by_index(pt, j, &d)) { pcerror("%s: unable to read double at index %d", __func__, j); return PC_FAILURE; } if ( j ) stringbuffer_aprintf(sb, ",%g", d); else stringbuffer_aprintf(sb, "%g", d); } stringbuffer_append(sb, "]"); } stringbuffer_append(sb, "]}"); /* All done, copy and clean up */ pc_pointlist_free(pl); return PC_SUCCESS; }
/* * Multi-surfaces provide type information for their curved sub-geometries * but not their linear sub-geometries. * MULTISURFACE(((0 0, 1 1, 1 0, 0 0)), CURVEPOLYGON(CURVESTRING(0 0, 1 1, 2 2, 0 1, 0 0))) */ static void lwmsurface_to_wkt_sb(const LWMSURFACE *msurf, stringbuffer_t *sb, int precision, uint8_t variant) { int i = 0; if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "MULTISURFACE"); /* "MULTISURFACE" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)msurf, sb, variant); } if ( msurf->ngeoms < 1 ) { empty_to_wkt_sb(sb); return; } stringbuffer_append(sb, "("); variant = variant | WKT_IS_CHILD; /* Inform the sub-geometries they are childre */ for ( i = 0; i < msurf->ngeoms; i++ ) { int type = msurf->geoms[i]->type; if ( i > 0 ) stringbuffer_append(sb, ","); switch (type) { case POLYGONTYPE: /* Linestring subgeoms don't get type identifiers */ lwpoly_to_wkt_sb((LWPOLY*)msurf->geoms[i], sb, precision, variant | WKT_NO_TYPE ); break; case CURVEPOLYTYPE: /* But circstring subgeoms *do* get type identifiers */ lwcurvepoly_to_wkt_sb((LWCURVEPOLY*)msurf->geoms[i], sb, precision, variant); break; default: lwerror("lwmsurface_to_wkt_sb: Unknown type recieved %d - %s", type, lwtype_name(type)); } } stringbuffer_append(sb, ")"); }
/**@brief * Convert a PCSDIMENSION to a human-readable JSON string * @param the PCDIMENSION we want to print * @return q pointer to the string buff describing the object * */ char * pc_dimension_to_json(const PCDIMENSION *d) { int i; char *str; stringbuffer_t *sb = stringbuffer_create(); stringbuffer_append(sb, "{"); if ( d ) { if ( i ) stringbuffer_append(sb, ","); stringbuffer_append(sb, "\n { \n"); if ( d->name ) stringbuffer_aprintf(sb, " \"name\" : \"%s\",\n", d->name); if ( d->description ) stringbuffer_aprintf(sb, " \"description\" : \"%s\",\n", d->description); stringbuffer_aprintf(sb, " \"size\" : %d,\n", d->size); stringbuffer_aprintf(sb, " \"byteoffset\" : %d,\n", d->byteoffset); stringbuffer_aprintf(sb, " \"scale\" : %g,\n", d->scale); stringbuffer_aprintf(sb, " \"interpretation\" : \"%s\",\n", pc_interpretation_string(d->interpretation)); stringbuffer_aprintf(sb, " \"offset\" : %g,\n", d->offset); stringbuffer_aprintf(sb, " \"active\" : %d\n", d->active); stringbuffer_append(sb, " }"); } stringbuffer_append(sb, "}\n"); str = stringbuffer_getstringcopy(sb); stringbuffer_destroy(sb); return str; }
/* * LINESTRING(0 0 0, 1 1 1) */ static void lwline_to_wkt_sb(const LWLINE *line, stringbuffer_t *sb, int precision, uint8_t variant) { if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "LINESTRING"); /* "LINESTRING" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)line, sb, variant); } if ( (! line->points) || (line->points->npoints < 1) ) { empty_to_wkt_sb(sb); return; } ptarray_to_wkt_sb(line->points, sb, precision, variant); }
/* * Geometry collections provide type information for all their curved sub-geometries * but not their linear sub-geometries. * GEOMETRYCOLLECTION(POLYGON((0 0, 1 1, 1 0, 0 0)), CURVEPOLYGON(CURVESTRING(0 0, 1 1, 2 2, 0 1, 0 0))) */ static void lwcollection_to_wkt_sb(const LWCOLLECTION *collection, stringbuffer_t *sb, int precision, uint8_t variant) { int i = 0; if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "GEOMETRYCOLLECTION"); /* "GEOMETRYCOLLECTION" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)collection, sb, variant); } if ( collection->ngeoms < 1 ) { empty_to_wkt_sb(sb); return; } stringbuffer_append(sb, "("); variant = variant | WKT_IS_CHILD; /* Inform the sub-geometries they are children */ for ( i = 0; i < collection->ngeoms; i++ ) { if ( i > 0 ) stringbuffer_append(sb, ","); lwgeom_to_wkt_sb((LWGEOM*)collection->geoms[i], sb, precision, variant ); } stringbuffer_append(sb, ")"); }
/* * A four-dimensional point will have different outputs depending on variant. * ISO: POINT ZM (0 0 0 0) * Extended: POINT(0 0 0 0) * OGC: POINT(0 0) * A three-dimensional m-point will have different outputs too. * ISO: POINT M (0 0 0) * Extended: POINTM(0 0 0) * OGC: POINT(0 0) */ static void lwpoint_to_wkt_sb(const LWPOINT *pt, stringbuffer_t *sb, int precision, uint8_t variant) { if ( ! (variant & WKT_NO_TYPE) ) { stringbuffer_append(sb, "POINT"); /* "POINT" */ dimension_qualifiers_to_wkt_sb((LWGEOM*)pt, sb, variant); } if ( (! pt->point) || (pt->point->npoints < 1) ) { empty_to_wkt_sb(sb); return; } ptarray_to_wkt_sb(pt->point, sb, precision, variant); }
/* Append variadic formatted string to a stringbuffer */ void vasbappend(stringbuffer_t *sb, char *fmt, ... ) { va_list ap; char *msg; va_start(ap, fmt); if (!lw_vasprintf (&msg, fmt, ap)) { va_end (ap); return; } /* Append to the stringbuffer */ stringbuffer_append(sb, msg); free(msg); va_end(ap); }