28 static int srch(
const void *,
const void *);
30 const int *,
int,
int,
int *);
31 static void free_col_arrays(
int *,
char *,
char **);
46 char east_str[256], north_str[256];
51 int i, n_points, n_coors, n_cats, n_lines;
52 int type, with_z, skip_feat, nskipped_3d;
64 xarray = (
double *)
G_calloc(alloc_points,
sizeof(
double));
65 yarray = (
double *)
G_calloc(alloc_points,
sizeof(
double));
66 zarray = (
double *)
G_calloc(alloc_points,
sizeof(
double));
68 n_lines = nskipped_3d = 0;
75 if (buff[0] ==
'\0') {
76 G_debug(3,
"a2b: skipping blank line");
80 if (sscanf(buff,
"%1c%d%d", &ctype, &n_coors, &
n_cats) < 2 ||
81 n_coors < 0 ||
n_cats < 0) {
83 G_debug(2,
"a2b: skipping commented line");
86 G_warning(
_(
"Error reading ASCII file: (bad type) [%s]"), buff);
91 G_debug(2,
"a2b: Skipping commented line");
125 G_warning(
_(
"Error reading ASCII file: (unknown type) [%s]"), buff);
130 G_debug(5,
"feature type = %d", type);
143 for (i = 0; i < n_coors; i++) {
146 _(
"End of ASCII file reached before end of coordinates"));
149 if (buff[0] ==
'\0') {
150 G_debug(3,
"a2b: skipping blank line while reading vertices");
156 if (sscanf(buff,
"%lf%lf%lf",
x, y, z) < 2) {
157 if (sscanf(buff,
" %s %s %lf", east_str, north_str, z) < 2) {
158 G_warning(
_(
"Error reading ASCII file: (bad point) [%s]"),
165 G_warning(
_(
"Unparsable longitude value: [%s]"),
171 G_warning(
_(
"Unparsable latitude value: [%s]"),
179 G_debug(5,
"coor in: %s -> x = %f y = %f z = %f",
G_chop(buff), *
x,
187 if (n_points >= alloc_points) {
188 alloc_points = n_points + 1000;
189 xarray = (
double *)
G_realloc((
void *)xarray,
190 alloc_points *
sizeof(double));
191 yarray = (
double *)
G_realloc((
void *)yarray,
192 alloc_points *
sizeof(double));
193 zarray = (
double *)
G_realloc((
void *)zarray,
194 alloc_points *
sizeof(double));
195 x = xarray + n_points;
196 y = yarray + n_points;
197 z = zarray + n_points;
203 for (i = 0; i <
n_cats; i++) {
206 _(
"End of ASCII file reached before end of categories"));
209 if (buff[0] ==
'\0') {
211 "a2b: skipping blank line while reading category info");
216 if (sscanf(buff,
"%d%d", &catn, &
cat) != 2) {
217 G_warning(
_(
"Error reading categories: [%s]"), buff);
246 G_warning(
_(
"Vector map <%s> is 2D. %d 3D features (faces or kernels) "
274 if (0 ==
G_getl2(buff,
sizeof(buff) - 1, dascii))
278 if (strncmp(buff,
"VERTI:", 6) == 0)
281 if (!(ptr = strchr(buff,
':'))) {
282 G_warning(
_(
"Unexpected data in vector header:\n[%s]"), buff);
290 if (strncmp(buff,
"ORGANIZATION:", 13) == 0)
292 else if (strncmp(buff,
"DIGIT DATE:", 11) == 0)
294 else if (strncmp(buff,
"DIGIT NAME:", 11) == 0)
296 else if (strncmp(buff,
"MAP NAME:", 9) == 0)
298 else if (strncmp(buff,
"MAP DATE:", 9) == 0)
300 else if (strncmp(buff,
"MAP SCALE:", 10) == 0)
302 else if (strncmp(buff,
"OTHER INFO:", 11) == 0)
304 else if (strncmp(buff,
"ZONE:", 5) == 0 ||
305 strncmp(buff,
"UTM ZONE:", 9) == 0)
307 else if (strncmp(buff,
"WEST EDGE:", 10) == 0) {
309 else if (strncmp(buff,
"EAST EDGE:", 10) == 0) {
311 else if (strncmp(buff,
"SOUTH EDGE:", 11) == 0) {
313 else if (strncmp(buff,
"NORTH EDGE:", 11) == 0) {
315 else if (strncmp(buff,
"MAP THRESH:", 11) == 0)
318 G_warning(
_(
"Unknown keyword <%s> in vector head"), buff);
349 int format,
int dp,
char *fs,
int region_flag,
int type,
351 const char **column_names,
int header)
353 int ltype, ctype, i,
cat, line, left, right, found;
354 double *xptr, *yptr, *zptr,
x, y;
357 char *xstring, *ystring, *zstring;
358 size_t xsize, ysize, zsize;
361 int count, n_skipped;
368 int *cats, ncats, more;
404 xstring = ystring = zstring =
NULL;
407 if (field > 0 && (where || column_names)) {
427 G_debug(3,
"%d categories selected from table <%s>", ncats, Fi->
table);
435 const char *col_name;
449 if (column_names[0] && strcmp(column_names[0],
"*") == 0) {
453 for (i = 0; i < ncols; i++) {
457 if (strcmp(Fi->
key, col_name) != 0)
467 while (column_names[i]) {
469 if (strcmp(Fi->
key, column_names[i]) != 0) {
471 for (j = 0; j < ncols; j++) {
474 if (strcmp(col_name, column_names[i]) == 0) {
484 for (j = 0; j < ncols; j++) {
510 len_all += strlen(
columns[i++]);
512 coltypes =
G_malloc(i *
sizeof(
int));
514 all_columns =
G_malloc(len_all + i + 2);
522 if (coltypes[i] < 0) {
526 _(
"Unknown type of column <%s>, export cancelled"),
530 free_col_arrays(coltypes, all_columns,
columns);
534 strcat(all_columns,
",");
535 strcat(all_columns,
columns[i]);
555 fprintf(ascii,
"east%snorth%sheight%scat", fs, fs, fs);
557 fprintf(ascii,
"east%snorth%scat", fs, fs);
563 "(key %s, column %s)"),
566 fprintf(ascii,
"%s%s", fs,
columns[i]);
568 fprintf(ascii,
"%s",
columns[i]);
585 count = n_skipped = line = 0;
594 coltypes, all_columns,
595 column_names && strcmp(column_names[0],
"*") == 0 ?
columns
612 coltypes, all_columns,
613 column_names && strcmp(column_names[0],
"*") == 0 ?
columns
627 found = get_cat(Cats, Clist, cats, ncats, field, &cat);
636 found = get_cat(ACats, Clist, cats, ncats, field, &cat);
640 if (!found && right > 0) {
642 found = get_cat(ACats, Clist, cats, ncats, field, &cat);
668 G_rasprintf(&xstring, &xsize,
"%.*f", dp, Points->
x[0]);
670 G_rasprintf(&ystring, &ysize,
"%.*f", dp, Points->
y[0]);
672 fprintf(att,
"A %s %s %d%s", xstring, ystring, cat,
694 G_warning(
_(
"Unknown feature type %d"), (
int)ltype);
700 if ((window.
east < Points->
x[0]) ||
701 (window.
west > Points->
x[0]))
704 G_rasprintf(&xstring, &xsize,
"%.*f", dp, Points->
x[0]);
708 if ((window.
north < Points->
y[0]) ||
709 (window.
south > Points->
y[0]))
712 G_rasprintf(&ystring, &ysize,
"%.*f", dp, Points->
y[0]);
719 if ((window.
top < Points->
z[0]) ||
720 (window.
bottom > Points->
z[0]))
723 G_rasprintf(&zstring, &zsize,
"%.*f", dp, Points->
z[0]);
725 fprintf(ascii,
"%s%s%s%s%s", xstring, fs, ystring, fs, zstring);
728 fprintf(ascii,
"%s%s%s", xstring, fs, ystring);
731 if (fcats->
n_values > 0 && cat > -1) {
734 _(
"Feature has more categories. Only one category (%d) "
738 fprintf(ascii,
"%s%d", fs, cat);
744 "SELECT %s FROM %s WHERE %s = %d", all_columns,
754 _(
"Cannot select attributes for cat = %d"), cat);
769 fprintf(ascii,
"%s", fs);
772 switch (coltypes[i]) {
774 fprintf(ascii,
"%s%d", fs,
779 fprintf(ascii,
"%s%.*f", fs, dp,
784 fprintf(ascii,
"%s%s", fs,
793 _(
"Column <%s> not found in table <%s>"),
797 _(
"Column <%s>: unsupported data type"),
810 if (ver == 5 && Cats->
n_cats > 0)
811 fprintf(ascii,
"%c %d %d%s", ctype, Points->
n_points,
814 fprintf(ascii,
"%c %d%s", ctype, Points->
n_points,
823 G_rasprintf(&xstring, &xsize,
"%.*f", dp, *xptr++);
825 G_rasprintf(&ystring, &ysize,
"%.*f", dp, *yptr++);
830 G_rasprintf(&zstring, &zsize,
"%.*f", dp, *zptr++);
832 fprintf(ascii,
" %-12s %-12s %-12s%s", xstring, ystring,
836 fprintf(ascii,
" %-12s %-12s%s", xstring, ystring,
841 fprintf(ascii,
" %-12s %-12s%s", ystring, xstring,
847 for (i = 0; i < Cats->
n_cats; i++) {
848 fprintf(ascii,
" %-5d %-10d%s", Cats->
field[i],
855 G_rasprintf(&xstring, &xsize,
"%.*f", dp, Points->
x[0]);
857 G_rasprintf(&ystring, &ysize,
"%.*f", dp, Points->
y[0]);
859 fprintf(att,
"P %s %s %d%s", xstring, ystring, cat,
863 x = (Points->
x[1] + Points->
x[0]) / 2;
864 y = (Points->
y[1] + Points->
y[0]) / 2;
870 fprintf(att,
"L %s %s %d%s", xstring, ystring, cat,
891 int i, area, nareas, isle, nisles;
894 G_warning(
_(
"Topology not available, unable to process areas"));
900 for (area = 1; area <= nareas; area++) {
907 G_warning(
_(
"Unable to get boundary of area id %d"), area);
910 fprintf(ascii,
"POLYGON(");
916 for (i = 0; i < nisles; i++) {
921 _(
"Unable to get boundary of isle id %d (area id %d)"),
925 fprintf(ascii,
", ");
938 _(
"%d features without category skipped. To export also "
939 "features without category use '%s=-1'."),
952 int srch(
const void *pa,
const void *pb)
986 const int *cats,
int ncats,
int field,
int *cat)
995 if (Clist && Clist->
field == field) {
996 for (i = 0; i < Cats->
n_cats; i++) {
997 if (Cats->
field[i] == field &&
1008 for (i = 0; i < Cats->
n_cats; i++) {
1009 if (Cats->
field[i] == field) {
1010 found = (
int *)bsearch((
void *)&(Cats->
cat[i]), cats, ncats,
1021 if (!Clist && !cats && field > 0) {
1031 void free_col_arrays(
int *coltypes,
char *all_columns,
char **
columns)
int Vect_write_ascii(FILE *ascii, FILE *att, struct Map_info *Map, int ver, int format, int dp, char *fs, int region_flag, int type, int field, const struct cat_list *Clist, const char *where, const char **column_names, int header)
Write data to GRASS ASCII vector format.
int Vect_read_ascii_head(FILE *dascii, struct Map_info *Map)
Read header of GRASS ASCII vector format.
void Vect_write_ascii_head(FILE *dascii, struct Map_info *Map)
Write data to GRASS ASCII vector format.
int Vect_read_ascii(FILE *ascii, struct Map_info *Map)
Read data in GRASS ASCII vector format.
#define DB_C_TYPE_DATETIME
int db_column_Ctype(dbDriver *, const char *, const char *)
Get column ctype.
int db_test_value_isnull(dbValue *)
Check of value is null.
const char * db_get_value_string(dbValue *)
Get string value.
int db_describe_table(dbDriver *, dbString *, dbTable **)
Describe table.
dbValue * db_get_column_value(dbColumn *)
Returns column value for given column structure.
dbColumn * db_get_table_column(dbTable *, int)
Returns column structure for given table and column number.
int db_select_value(dbDriver *, const char *, const char *, int, const char *, dbValue *)
Select one (first) value from table/column for key/id.
double db_get_value_double(dbValue *)
Get double precision value.
int db_shutdown_driver(dbDriver *)
Closedown the driver, and free the driver structure.
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
void db_free_table(dbTable *)
Free the table.
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
dbTable * db_get_cursor_table(dbCursor *)
Get table allocated by cursor.
void db_zero_string(dbString *)
Zero string.
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
int db_select_int(dbDriver *, const char *, const char *, const char *, int **)
Select array of ordered integers from table/column.
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
int db_set_handle(dbHandle *, const char *, const char *)
Set handle (database and schema name)
int db_close_database(dbDriver *)
Close database connection.
int db_get_value_int(dbValue *)
Get integer value.
void db_init_handle(dbHandle *)
Initialize handle (i.e database/schema)
void db_init_string(dbString *)
Initialize dbString.
int db_close_cursor(dbCursor *)
Close cursor.
int db_open_select_cursor(dbDriver *, dbString *, dbCursor *, int)
Open select cursor.
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
int db_fetch(dbCursor *, int, int *)
Fetch data from open cursor.
int db_get_table_number_of_columns(dbTable *)
Return the number of columns of the table.
int int G_rasprintf(char **, size_t *, const char *,...) __attribute__((format(printf
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
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
int G_scan_easting(const char *, double *, int)
ASCII easting to double.
void G_trim_decimal(char *)
Removes trailing zeros from decimal number.
void void void G_important_message(const char *,...) __attribute__((format(printf
char * G_chop(char *)
Chop leading and trailing white spaces.
int G_debug(int, const char *,...) __attribute__((format(printf
int G_scan_northing(const char *, double *, int)
ASCII northing to double.
char * G_store(const char *)
Copy string to allocated memory.
void G_get_window(struct Cell_head *)
Get the current region.
int G_projection(void)
Query cartographic projection.
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
int Vect_set_map_name(struct Map_info *, const char *)
Set map name in map header.
const char * Vect_get_map_date(struct Map_info *)
Get date when the source map was originally produced from map header.
plus_t Vect_get_num_areas(struct Map_info *)
Get number of areas in vector map.
int Vect_cat_in_cat_list(int, const struct cat_list *)
Check if category number is in list.
int Vect_set_zone(struct Map_info *, int)
Set projection zone in map header.
int Vect_reset_cats(struct line_cats *)
Reset category structure to make sure cats structure is clean to be re-used.
int Vect_area_alive(struct Map_info *, int)
Check if area is alive or dead (topological level required)
const char * Vect_get_organization(struct Map_info *)
Get organization string from map header.
const char * Vect_get_comment(struct Map_info *)
Get comment or other info string from map header.
int Vect_get_scale(struct Map_info *)
Get map scale from map header.
const char * Vect_get_date(struct Map_info *)
Get date of digitization from map header.
int Vect_cat_set(struct line_cats *, int, int)
Add new field/cat to category structure if doesn't exist yet.
int Vect_cat_get(const struct line_cats *, int, int *)
Get first found category of given field.
int Vect_get_area_cats(struct Map_info *, int, struct line_cats *)
Get area categories.
int Vect_set_scale(struct Map_info *, int)
Set map scale in map header.
int Vect_get_isle_points(struct Map_info *, int, struct line_pnts *)
Returns polygon array of points for given isle.
int Vect_get_area_points(struct Map_info *, int, struct line_pnts *)
Returns polygon array of points (outer ring) of given area.
void Vect_destroy_list(struct ilist *)
Frees all memory associated with a struct ilist, including the struct itself.
int Vect_level(struct Map_info *)
Returns level that Map is opened at.
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_set_map_date(struct Map_info *, const char *)
Set date when the source map was originally produced in map header.
int Vect_copy_xyz_to_pnts(struct line_pnts *, const double *, const double *, const double *, int)
Copy points from array to line_pnts structure.
int Vect_set_comment(struct Map_info *, const char *)
Set comment or other info string in map header.
int Vect_get_zone(struct Map_info *)
int Vect_get_area_isle(struct Map_info *, int, int)
Returns isle id for area.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_get_area_num_isles(struct Map_info *, int)
Returns number of isles for given area.
double Vect_get_thresh(struct Map_info *)
Get threshold used for digitization from map header.
int Vect_set_organization(struct Map_info *, const char *)
Set organization string in map header.
int Vect_get_area_cat(struct Map_info *, int, int)
Find FIRST category of given field and area.
off_t Vect_write_line(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes a new feature.
struct field_info * Vect_get_field(struct Map_info *, int)
Get information about link to database (by layer number)
void Vect_destroy_field_info(struct field_info *)
Free a struct field_info and all memory associated with it.
int Vect_set_date(struct Map_info *, const char *)
Set date of digitization in map header.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
const char * Vect_get_person(struct Map_info *)
Get user name string who digitized the map from map header.
int Vect_rewind(struct Map_info *)
Rewind vector map to cause reads to start at beginning.
const char * Vect_get_name(struct Map_info *)
Get name of vector map.
int Vect_read_next_line(struct Map_info *, struct line_pnts *, struct line_cats *)
Read next vector feature.
int Vect_sfa_line_astext(const struct line_pnts *, int, int, int, FILE *)
Export geometry to Well-Known Text.
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
int Vect_get_line_areas(struct Map_info *, int, int *, int *)
Get area id on the left and right side of the boundary.
int Vect_field_cat_get(const struct line_cats *, int, struct ilist *)
Get list of categories of given field.
int Vect_set_person(struct Map_info *, const char *)
Set name of user who digitized the map in map header.
int Vect_set_thresh(struct Map_info *, double)
Set threshold used for digitization in map header.
int Vect_is_3d(struct Map_info *)
Check if vector map is 3D.
const char * Vect_get_map_name(struct Map_info *)
Get map name from map header.
int Vect_get_isle_area(struct Map_info *, int)
Returns area id for isle.
#define GV_ASCII_FORMAT_WKT
GRASS ASCII vector format - well-known-text format.
#define GV_POINT
Feature types used in memory on run time (may change)
#define GV_ASCII_FORMAT_STD
GRASS ASCII vector format - standard format.
#define GV_ASCII_FORMAT_POINT
GRASS ASCII vector format - point format.
const struct driver * driver
GRASS_INTERPFL_EXPORT int count
2D/3D raster map header (used also for region)
double north
Extent coordinates (north)
double bottom
Extent coordinates (bottom) - 3D data.
double east
Extent coordinates (east)
double top
Extent coordinates (top) - 3D data.
double south
Extent coordinates (south)
double west
Extent coordinates (west)
struct dig_head head
Header info.
int field
Category layer (field)
int with_z
2D/3D vector data
Layer (old: field) information.
char * table
Name of DB table.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
char * key
Name of key column (usually 'cat')
int n_values
Number of values in the list.
int * field
Array of layers (fields)
int * cat
Array of categories.
int n_cats
Number of categories attached to element.
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.