26 static int srch(
const void *,
const void *);
28 const int *,
int,
int,
int *);
29 static void free_col_arrays(
int *,
char *,
char **);
44 char east_str[256], north_str[256];
49 int i, n_points, n_coors, n_cats, n_lines;
50 int type, with_z, skip_feat, nskipped_3d;
62 xarray = (
double *)
G_calloc(alloc_points,
sizeof(
double));
63 yarray = (
double *)
G_calloc(alloc_points,
sizeof(
double));
64 zarray = (
double *)
G_calloc(alloc_points,
sizeof(
double));
66 n_lines = nskipped_3d = 0;
73 if (buff[0] ==
'\0') {
74 G_debug(3,
"a2b: skipping blank line");
78 if (sscanf(buff,
"%1c%d%d", &ctype, &n_coors, &n_cats) < 2 ||
79 n_coors < 0 || n_cats < 0) {
81 G_debug(2,
"a2b: skipping commented line");
84 G_warning(
_(
"Error reading ASCII file: (bad type) [%s]"),
89 G_debug(2,
"a2b: Skipping commented line");
123 G_warning(
_(
"Error reading ASCII file: (unknown type) [%s]"),
128 G_debug(5,
"feature type = %d", type);
141 for (i = 0; i < n_coors; i++) {
143 G_warning(
_(
"End of ASCII file reached before end of coordinates"));
146 if (buff[0] ==
'\0') {
147 G_debug(3,
"a2b: skipping blank line while reading vertices");
153 if (sscanf(buff,
"%lf%lf%lf", x, y, z) < 2) {
154 if (sscanf(buff,
" %s %s %lf", east_str, north_str, z) < 2) {
155 G_warning(
_(
"Error reading ASCII file: (bad point) [%s]"),
160 G_warning(
_(
"Unparsable longitude value: [%s]"),
165 G_warning(
_(
"Unparsable latitude value: [%s]"),
172 G_debug(5,
"coor in: %s -> x = %f y = %f z = %f",
G_chop(buff),
180 if (n_points >= alloc_points) {
181 alloc_points = n_points + 1000;
184 alloc_points *
sizeof(double));
187 alloc_points *
sizeof(double));
190 alloc_points *
sizeof(double));
191 x = xarray + n_points;
192 y = yarray + n_points;
193 z = zarray + n_points;
199 for (i = 0; i <
n_cats; i++) {
201 G_warning(
_(
"End of ASCII file reached before end of categories"));
204 if (buff[0] ==
'\0') {
206 "a2b: skipping blank line while reading category info");
211 if (sscanf(buff,
"%u%u", &catn, &cat) != 2) {
212 G_warning(
_(
"Error reading categories: [%s]"), buff);
238 G_warning(
_(
"Vector map <%s> is 2D. %d 3D features (faces or kernels) skipped."),
262 if (0 ==
G_getl2(buff,
sizeof(buff) - 1, dascii))
266 if (strncmp(buff,
"VERTI:", 6) == 0)
269 if (!(ptr = strchr(buff,
':'))) {
270 G_warning(
_(
"Unexpected data in vector header:\n[%s]"), buff);
278 if (strncmp(buff,
"ORGANIZATION:", 13) == 0)
280 else if (strncmp(buff,
"DIGIT DATE:", 11) == 0)
282 else if (strncmp(buff,
"DIGIT NAME:", 11) == 0)
284 else if (strncmp(buff,
"MAP NAME:", 9) == 0)
286 else if (strncmp(buff,
"MAP DATE:", 9) == 0)
288 else if (strncmp(buff,
"MAP SCALE:", 10) == 0)
290 else if (strncmp(buff,
"OTHER INFO:", 11) == 0)
292 else if (strncmp(buff,
"ZONE:", 5) == 0 ||
293 strncmp(buff,
"UTM ZONE:", 9) == 0)
295 else if (strncmp(buff,
"WEST EDGE:", 10) == 0) {
297 else if (strncmp(buff,
"EAST EDGE:", 10) == 0) {
299 else if (strncmp(buff,
"SOUTH EDGE:", 11) == 0) {
301 else if (strncmp(buff,
"NORTH EDGE:", 11) == 0) {
303 else if (strncmp(buff,
"MAP THRESH:", 11) == 0)
306 G_warning(
_(
"Unknown keyword <%s> in vector head"), buff);
337 FILE *att,
struct Map_info *Map,
int ver,
338 int format,
int dp,
char *fs,
int region_flag,
int type,
340 const char **column_names,
int header)
342 int ltype, ctype, i,
cat, line, left, right, found;
343 double *xptr, *yptr, *zptr,
x, y;
346 char *xstring, *ystring, *zstring;
347 size_t xsize, ysize, zsize;
350 int count, n_skipped;
357 int *cats, ncats, more;
392 xstring = ystring = zstring =
NULL;
395 if (field > 0 && (where || column_names)) {
415 G_debug(3,
"%d categories selected from table <%s>", ncats, Fi->
table);
423 const char *col_name;
433 columns = (
char **)
G_malloc((ncols + 1) *
sizeof(
char *));
435 if (column_names[0] && strcmp(column_names[0],
"*") == 0) {
439 for (i = 0; i < ncols; i++) {
442 if (strcmp(Fi->
key, col_name) != 0)
443 columns[icol++] =
G_store(col_name);
445 columns[icol] =
NULL;
452 while (column_names[i]) {
454 if (strcmp(Fi->
key, column_names[i]) != 0) {
456 for (j = 0; j < ncols; j++) {
458 if (strcmp(col_name, column_names[i]) == 0) {
459 columns[icol++] =
G_store(col_name);
468 for (j = 0; j < ncols; j++) {
480 columns[icol] =
NULL;
491 len_all += strlen(columns[i++]);
493 coltypes =
G_malloc(i *
sizeof(
int));
495 all_columns =
G_malloc(len_all + i + 2);
498 strcpy(all_columns, columns[0]);
502 if (coltypes[i] < 0) {
505 G_warning(
_(
"Unknown type of column <%s>, export cancelled"),
510 strcat(all_columns,
",");
511 strcat(all_columns, columns[i]);
531 fprintf(ascii,
"east%snorth%sheight%scat", fs, fs, fs);
533 fprintf(ascii,
"east%snorth%scat", fs, fs);
535 for (i = 0; columns[i]; i++) {
538 columns[i], &value) < 0)
539 G_fatal_error(
_(
"Unable to select record from table <%s> (key %s, column %s)"),
542 fprintf(ascii,
"%s%s", fs, columns[i]);
544 fprintf(ascii,
"%s", columns[i]);
561 count = n_skipped = line = 0;
569 free_col_arrays(coltypes, all_columns,
570 column_names && strcmp(column_names[0],
"*") == 0 ? columns :
NULL);
581 free_col_arrays(coltypes, all_columns,
582 column_names && strcmp(column_names[0],
"*") == 0 ? columns :
NULL);
595 found = get_cat(Cats, Clist, cats, ncats, field, &cat);
604 found = get_cat(ACats, Clist, cats, ncats, field, &cat);
608 if (!found && right > 0) {
610 found = get_cat(ACats, Clist, cats, ncats, field, &cat);
636 G_rasprintf(&xstring, &xsize,
"%.*f", dp, Points->
x[0]);
638 G_rasprintf(&ystring, &ysize,
"%.*f", dp, Points->
y[0]);
640 fprintf(att,
"A %s %s %d%s", xstring, ystring, cat,
HOST_NEWLINE);
661 G_warning(
_(
"Unknown feature type %d"), (
int)ltype);
667 if ((window.
east < Points->
x[0]) ||
668 (window.
west > Points->
x[0]))
671 G_rasprintf(&xstring, &xsize,
"%.*f", dp, Points->
x[0]);
675 if ((window.
north < Points->
y[0]) ||
676 (window.
south > Points->
y[0]))
679 G_rasprintf(&ystring, &ysize,
"%.*f", dp, Points->
y[0]);
686 if ((window.
top < Points->
z[0]) ||
687 (window.
bottom > Points->
z[0]))
690 G_rasprintf(&zstring, &zsize,
"%.*f", dp, Points->
z[0]);
692 fprintf(ascii,
"%s%s%s%s%s", xstring, fs, ystring, fs,
696 fprintf(ascii,
"%s%s%s", xstring, fs, ystring);
699 if (fcats->
n_values > 0 && cat > -1) {
701 G_warning(
_(
"Feature has more categories. Only one category (%d) " 702 "is exported."), cat);
704 fprintf(ascii,
"%s%d", fs, cat);
709 G_rasprintf(&buf, &bufsize,
"SELECT %s FROM %s WHERE %s = %d",
710 all_columns, Fi->
table, Fi->
key, cat);
730 for (i = 0; columns[i]; i++) {
735 fprintf(ascii,
"%s", fs);
757 columns[i], Fi->
table);
771 if (ver == 5 && Cats->
n_cats > 0)
772 fprintf(ascii,
"%c %d %d%s", ctype, Points->
n_points,
783 G_rasprintf(&xstring, &xsize,
"%.*f", dp, *xptr++);
785 G_rasprintf(&ystring, &ysize,
"%.*f", dp, *yptr++);
790 G_rasprintf(&zstring, &zsize,
"%.*f", dp, *zptr++);
792 fprintf(ascii,
" %-12s %-12s %-12s%s", xstring,
796 fprintf(ascii,
" %-12s %-12s%s", xstring, ystring,
HOST_NEWLINE);
800 fprintf(ascii,
" %-12s %-12s%s", ystring, xstring,
HOST_NEWLINE);
805 for (i = 0; i < Cats->
n_cats; i++) {
806 fprintf(ascii,
" %-5d %-10d%s", Cats->
field[i],
813 G_rasprintf(&xstring, &xsize,
"%.*f", dp, Points->
x[0]);
815 G_rasprintf(&ystring, &ysize,
"%.*f", dp, Points->
y[0]);
817 fprintf(att,
"P %s %s %d%s", xstring, ystring, cat,
HOST_NEWLINE);
820 x = (Points->
x[1] + Points->
x[0]) / 2;
821 y = (Points->
y[1] + Points->
y[0]) / 2;
827 fprintf(att,
"L %s %s %d%s", xstring, ystring, cat,
HOST_NEWLINE);
847 int i, area, nareas, isle, nisles;
850 G_warning(
_(
"Topology not available, unable to process areas"));
856 for (area = 1; area <= nareas; area++) {
863 G_warning(
_(
"Unable to get boundary of area id %d"), area);
866 fprintf(ascii,
"POLYGON(");
871 for (i = 0; i < nisles; i++) {
875 G_warning(
_(
"Unable to get boundary of isle id %d (area id %d)"), isle, area);
878 fprintf(ascii,
", ");
890 "features without category use '%s=-1'."), n_skipped,
"layer");
899 int srch(
const void *pa,
const void *pb)
919 fprintf(dascii,
"ORGANIZATION: %s%s",
921 fprintf(dascii,
"DIGIT DATE: %s%s",
923 fprintf(dascii,
"DIGIT NAME: %s%s",
925 fprintf(dascii,
"MAP NAME: %s%s",
927 fprintf(dascii,
"MAP DATE: %s%s",
929 fprintf(dascii,
"MAP SCALE: %d%s",
931 fprintf(dascii,
"OTHER INFO: %s%s",
933 fprintf(dascii,
"ZONE: %d%s",
935 fprintf(dascii,
"MAP THRESH: %f%s",
941 const int *cats,
int ncats,
int field,
int *cat)
950 if (Clist && Clist->
field == field) {
951 for (i = 0; i < Cats->
n_cats; i++) {
952 if (Cats->
field[i] == field &&
963 for (i = 0; i < Cats->
n_cats; i++) {
964 if (Cats->
field[i] == field) {
965 found = (
int *)bsearch((
void *) &(Cats->
cat[i]), cats,
966 ncats,
sizeof(
int), srch);
976 if (!Clist && !cats && field > 0) {
986 void free_col_arrays(
int *coltypes,
char *all_columns,
char **
columns)
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
int Vect_read_ascii_head(FILE *dascii, struct Map_info *Map)
Read header of GRASS ASCII vector format.
int db_describe_table(dbDriver *, dbString *, dbTable **)
Describe table.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int Vect_get_area_cats(const struct Map_info *, int, struct line_cats *)
Get area categories.
int Vect_set_map_name(struct Map_info *, const char *)
Set map name in map header.
dbValue * db_get_column_value(dbColumn *)
Returns column value for given column structure.
void void void G_important_message(const char *,...) __attribute__((format(printf
int Vect_set_comment(struct Map_info *, const char *)
Set comment or other info string in map header.
#define GV_ASCII_FORMAT_POINT
GRASS ASCII vector format - point format.
int Vect_get_area_isle(const struct Map_info *, int, int)
Returns isle id for area.
int Vect_field_cat_get(const struct line_cats *, int, struct ilist *)
Get list of categories of given field.
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
2D/3D raster map header (used also for region)
void db_init_string(dbString *)
Initialize dbString.
int Vect_set_thresh(struct Map_info *, double)
Set threshold used for digitization in map header.
double west
Extent coordinates (west)
int db_fetch(dbCursor *, int, int *)
Fetch data from open cursor.
int G_projection(void)
Query cartographic projection.
int db_open_select_cursor(dbDriver *, dbString *, dbCursor *, int)
Open select cursor.
int n_points
Number of points.
char * table
Name of DB table.
#define DB_C_TYPE_DATETIME
int Vect_copy_xyz_to_pnts(struct line_pnts *, const double *, const double *, const double *, int)
Copy points from array to line_pnts structure.
void db_free_table(dbTable *)
Free the table.
int Vect_set_map_date(struct Map_info *, const char *)
Set date when the source map was originally produced in map header.
int n_values
Number of values in the list.
void G_free(void *)
Free allocated memory.
int db_get_value_int(dbValue *)
Get integer value.
int Vect_get_area_num_isles(const struct Map_info *, int)
Returns number of isles for given area.
int db_set_handle(dbHandle *, const char *, const char *)
Set handle (database and schema name)
double Vect_get_thresh(const struct Map_info *)
Get threshold used for digitization from map header.
int Vect_sfa_line_astext(const struct line_pnts *, int, int, int, FILE *)
Export geometry to Well-Known Text.
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
#define GV_POINT
Feature types used in memory on run time (may change)
struct field_info * Vect_get_field(const struct Map_info *, int)
Get information about link to database (by layer number)
double top
Extent coordinates (top) - 3D data.
int Vect_get_zone(const struct Map_info *)
Get projection zone from map header.
Layer (old: field) information.
const char * Vect_get_map_date(const struct Map_info *)
Get date when the source map was originally produced from map header.
double * x
Array of X coordinates.
Feature geometry info - coordinates.
int with_z
2D/3D vector data
int db_test_value_isnull(dbValue *)
Check of value is null.
double north
Extent coordinates (north)
int G_scan_easting(const char *, double *, int)
ASCII easting to double.
int db_select_value(dbDriver *, const char *, const char *, int, const char *, dbValue *)
Select one (first) value from table/column for key/id.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_set_date(struct Map_info *, const char *)
Set date of digitization in map header.
double south
Extent coordinates (south)
int field
Category layer (field)
int db_close_database(dbDriver *)
Close database connection.
int n_cats
Number of categories attached to element.
int * cat
Array of categories.
off_t Vect_write_line(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes a new feature.
int Vect_get_isle_points(const struct Map_info *, int, struct line_pnts *)
Returns polygon array of points for given isle.
const struct driver * driver
const char * Vect_get_organization(const struct Map_info *)
Get organization string from map header.
double bottom
Extent coordinates (bottom) - 3D data.
plus_t Vect_get_num_areas(const struct Map_info *)
Get number of areas in vector map.
int db_shutdown_driver(dbDriver *)
Closedown the driver, and free the driver structure.
void db_zero_string(dbString *)
Zero string.
int Vect_area_alive(const struct Map_info *, int)
Check if area is alive or dead (topological level required)
int int G_rasprintf(char **, size_t *, const char *,...) __attribute__((format(printf
int Vect_reset_cats(struct line_cats *)
Reset category structure to make sure cats structure is clean to be re-used.
int Vect_rewind(struct Map_info *)
Rewind vector map to cause reads to start at beginning.
void db_init_handle(dbHandle *)
Initialize handle (i.e database/schema)
struct dig_head head
Header info.
int Vect_get_isle_area(const struct Map_info *, int)
Returns area id for isle.
#define GV_ASCII_FORMAT_STD
GRASS ASCII vector format - standard format.
const char * Vect_get_map_name(const struct Map_info *)
Get map name from map header.
const char * db_get_value_string(dbValue *)
Get string value.
double * y
Array of Y coordinates.
const char * Vect_get_date(const struct Map_info *)
Get date of digitization from map header.
char * G_chop(char *)
Chop leading and trailing white spaces.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
int Vect_cat_in_cat_list(int, const struct cat_list *)
Check if category number is in list.
double db_get_value_double(dbValue *)
Get double precision value.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
int Vect_is_3d(const struct Map_info *)
Check if vector map is 3D.
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_warning(const char *,...) __attribute__((format(printf
dbColumn * db_get_table_column(dbTable *, int)
Returns column structure for given table and column number.
double east
Extent coordinates (east)
double * z
Array of Z coordinates.
const char * Vect_get_comment(const struct Map_info *)
Get comment or other info string from map header.
int db_column_Ctype(dbDriver *, const char *, const char *)
Get column ctype.
char * G_store(const char *)
Copy string to allocated memory.
int * field
Array of layers (fields)
int Vect_set_person(struct Map_info *, const char *)
Set name of user who digitized the map in map header.
dbTable * db_get_cursor_table(dbCursor *)
Get table allocated by cursor.
int Vect_get_scale(const struct Map_info *)
Get map scale from map header.
int Vect_read_next_line(const struct Map_info *, struct line_pnts *, struct line_cats *)
Read next vector feature.
int db_get_table_number_of_columns(dbTable *)
Return the number of columns of the table.
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
int Vect_get_area_points(const struct Map_info *, int, struct line_pnts *)
Returns polygon array of points (outer ring) of given area.
int Vect_set_organization(struct Map_info *, const char *)
Set organization string in map header.
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 db_select_int(dbDriver *, const char *, const char *, const char *, int **)
Select array of ordered integers from table/column.
void G_get_window(struct Cell_head *)
Get the current region.
const char * Vect_get_name(const struct Map_info *)
Get name of vector map.
int Vect_level(const struct Map_info *)
Returns level that Map is opened at.
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_set_zone(struct Map_info *, int)
Set projection zone in map header.
void Vect_write_ascii_head(FILE *dascii, struct Map_info *Map)
Write data to GRASS ASCII vector format.
#define GV_ASCII_FORMAT_WKT
GRASS ASCII vector format - well-known-text format.
int db_close_cursor(dbCursor *)
Close cursor.
int Vect_get_line_areas(const struct Map_info *, int, int *, int *)
Get area id on the left and right side of the boundary.
int Vect_get_area_cat(const struct Map_info *, int, int)
Find FIRST category of given field and area.
int Vect_cat_get(const struct line_cats *, int, int *)
Get first found category of given field.
int Vect_read_ascii(FILE *ascii, struct Map_info *Map)
Read data in GRASS ASCII vector format.
int Vect_set_scale(struct Map_info *, int)
Set map scale in map header.
int Vect_cat_set(struct line_cats *, int, int)
Add new field/cat to category structure if doesn't exist yet.
int G_scan_northing(const char *, double *, int)
ASCII northing to double.
const char * Vect_get_person(const struct Map_info *)
Get user name string who digitized the map from map header.
void G_trim_decimal(char *)
Removes trailing zeros from decimal number.
char * key
Name of key column (usually 'cat')