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);
90 G_debug(2,
"a2b: Skipping commented line");
124 G_warning(
_(
"Error reading ASCII file: (unknown type) [%s]"), buff);
128 G_debug(5,
"feature type = %d", type);
141 for (i = 0; i < n_coors; i++) {
144 _(
"End of ASCII file reached before end of coordinates"));
147 if (buff[0] ==
'\0') {
148 G_debug(3,
"a2b: skipping blank line while reading vertices");
154 if (sscanf(buff,
"%lf%lf%lf",
x, y, z) < 2) {
155 if (sscanf(buff,
" %s %s %lf", east_str, north_str, z) < 2) {
156 G_warning(
_(
"Error reading ASCII file: (bad point) [%s]"),
162 G_warning(
_(
"Unparsable longitude value: [%s]"),
167 G_warning(
_(
"Unparsable latitude value: [%s]"),
174 G_debug(5,
"coor in: %s -> x = %f y = %f z = %f",
G_chop(buff), *
x,
182 if (n_points >= alloc_points) {
183 alloc_points = n_points + 1000;
184 xarray = (
double *)
G_realloc((
void *)xarray,
185 alloc_points *
sizeof(double));
186 yarray = (
double *)
G_realloc((
void *)yarray,
187 alloc_points *
sizeof(double));
188 zarray = (
double *)
G_realloc((
void *)zarray,
189 alloc_points *
sizeof(double));
190 x = xarray + n_points;
191 y = yarray + n_points;
192 z = zarray + n_points;
198 for (i = 0; i <
n_cats; i++) {
201 _(
"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,
"%d%d", &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) "
263 if (0 ==
G_getl2(buff,
sizeof(buff) - 1, dascii))
267 if (strncmp(buff,
"VERTI:", 6) == 0)
270 if (!(ptr = strchr(buff,
':'))) {
271 G_warning(
_(
"Unexpected data in vector header:\n[%s]"), buff);
279 if (strncmp(buff,
"ORGANIZATION:", 13) == 0)
281 else if (strncmp(buff,
"DIGIT DATE:", 11) == 0)
283 else if (strncmp(buff,
"DIGIT NAME:", 11) == 0)
285 else if (strncmp(buff,
"MAP NAME:", 9) == 0)
287 else if (strncmp(buff,
"MAP DATE:", 9) == 0)
289 else if (strncmp(buff,
"MAP SCALE:", 10) == 0)
291 else if (strncmp(buff,
"OTHER INFO:", 11) == 0)
293 else if (strncmp(buff,
"ZONE:", 5) == 0 ||
294 strncmp(buff,
"UTM ZONE:", 9) == 0)
296 else if (strncmp(buff,
"WEST EDGE:", 10) == 0) {
298 else if (strncmp(buff,
"EAST EDGE:", 10) == 0) {
300 else if (strncmp(buff,
"SOUTH EDGE:", 11) == 0) {
302 else if (strncmp(buff,
"NORTH EDGE:", 11) == 0) {
304 else if (strncmp(buff,
"MAP THRESH:", 11) == 0)
307 G_warning(
_(
"Unknown keyword <%s> in vector head"), buff);
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;
393 xstring = ystring = zstring =
NULL;
396 if (field > 0 && (where || column_names)) {
416 G_debug(3,
"%d categories selected from table <%s>", ncats, Fi->
table);
424 const char *col_name;
436 if (column_names[0] && strcmp(column_names[0],
"*") == 0) {
440 for (i = 0; i < ncols; i++) {
444 if (strcmp(Fi->
key, col_name) != 0)
454 while (column_names[i]) {
456 if (strcmp(Fi->
key, column_names[i]) != 0) {
458 for (j = 0; j < ncols; j++) {
461 if (strcmp(col_name, column_names[i]) == 0) {
471 for (j = 0; j < ncols; j++) {
495 len_all += strlen(
columns[i++]);
497 coltypes =
G_malloc(i *
sizeof(
int));
499 all_columns =
G_malloc(len_all + i + 2);
507 if (coltypes[i] < 0) {
511 _(
"Unknown type of column <%s>, export cancelled"),
516 strcat(all_columns,
",");
517 strcat(all_columns,
columns[i]);
537 fprintf(ascii,
"east%snorth%sheight%scat", fs, fs, fs);
539 fprintf(ascii,
"east%snorth%scat", fs, fs);
545 "(key %s, column %s)"),
548 fprintf(ascii,
"%s%s", fs,
columns[i]);
550 fprintf(ascii,
"%s",
columns[i]);
567 count = n_skipped = line = 0;
576 coltypes, all_columns,
577 column_names && strcmp(column_names[0],
"*") == 0 ?
columns
590 coltypes, all_columns,
591 column_names && strcmp(column_names[0],
"*") == 0 ?
columns
605 found = get_cat(Cats, Clist, cats, ncats, field, &cat);
614 found = get_cat(ACats, Clist, cats, ncats, field, &cat);
618 if (!found && right > 0) {
620 found = get_cat(ACats, Clist, cats, ncats, field, &cat);
646 G_rasprintf(&xstring, &xsize,
"%.*f", dp, Points->
x[0]);
648 G_rasprintf(&ystring, &ysize,
"%.*f", dp, Points->
y[0]);
650 fprintf(att,
"A %s %s %d%s", xstring, ystring, cat,
672 G_warning(
_(
"Unknown feature type %d"), (
int)ltype);
678 if ((window.
east < Points->
x[0]) ||
679 (window.
west > Points->
x[0]))
682 G_rasprintf(&xstring, &xsize,
"%.*f", dp, Points->
x[0]);
686 if ((window.
north < Points->
y[0]) ||
687 (window.
south > Points->
y[0]))
690 G_rasprintf(&ystring, &ysize,
"%.*f", dp, Points->
y[0]);
697 if ((window.
top < Points->
z[0]) ||
698 (window.
bottom > Points->
z[0]))
701 G_rasprintf(&zstring, &zsize,
"%.*f", dp, Points->
z[0]);
703 fprintf(ascii,
"%s%s%s%s%s", xstring, fs, ystring, fs, zstring);
706 fprintf(ascii,
"%s%s%s", xstring, fs, ystring);
709 if (fcats->
n_values > 0 && cat > -1) {
712 _(
"Feature has more categories. Only one category (%d) "
716 fprintf(ascii,
"%s%d", fs, cat);
722 "SELECT %s FROM %s WHERE %s = %d", all_columns,
732 _(
"Cannot select attributes for cat = %d"), cat);
747 fprintf(ascii,
"%s", fs);
750 switch (coltypes[i]) {
752 fprintf(ascii,
"%s%d", fs,
757 fprintf(ascii,
"%s%.*f", fs, dp,
762 fprintf(ascii,
"%s%s", fs,
771 _(
"Column <%s> not found in table <%s>"),
775 _(
"Column <%s>: unsupported data type"),
788 if (ver == 5 && Cats->
n_cats > 0)
789 fprintf(ascii,
"%c %d %d%s", ctype, Points->
n_points,
792 fprintf(ascii,
"%c %d%s", ctype, Points->
n_points,
801 G_rasprintf(&xstring, &xsize,
"%.*f", dp, *xptr++);
803 G_rasprintf(&ystring, &ysize,
"%.*f", dp, *yptr++);
808 G_rasprintf(&zstring, &zsize,
"%.*f", dp, *zptr++);
810 fprintf(ascii,
" %-12s %-12s %-12s%s", xstring, ystring,
814 fprintf(ascii,
" %-12s %-12s%s", xstring, ystring,
819 fprintf(ascii,
" %-12s %-12s%s", ystring, xstring,
825 for (i = 0; i < Cats->
n_cats; i++) {
826 fprintf(ascii,
" %-5d %-10d%s", Cats->
field[i],
833 G_rasprintf(&xstring, &xsize,
"%.*f", dp, Points->
x[0]);
835 G_rasprintf(&ystring, &ysize,
"%.*f", dp, Points->
y[0]);
837 fprintf(att,
"P %s %s %d%s", xstring, ystring, cat,
841 x = (Points->
x[1] + Points->
x[0]) / 2;
842 y = (Points->
y[1] + Points->
y[0]) / 2;
848 fprintf(att,
"L %s %s %d%s", xstring, ystring, cat,
869 int i, area, nareas, isle, nisles;
872 G_warning(
_(
"Topology not available, unable to process areas"));
878 for (area = 1; area <= nareas; area++) {
885 G_warning(
_(
"Unable to get boundary of area id %d"), area);
888 fprintf(ascii,
"POLYGON(");
894 for (i = 0; i < nisles; i++) {
899 _(
"Unable to get boundary of isle id %d (area id %d)"),
903 fprintf(ascii,
", ");
916 _(
"%d features without category skipped. To export also "
917 "features without category use '%s=-1'."),
927 int srch(
const void *pa,
const void *pb)
961 const int *cats,
int ncats,
int field,
int *cat)
970 if (Clist && Clist->
field == field) {
971 for (i = 0; i < Cats->
n_cats; i++) {
972 if (Cats->
field[i] == field &&
983 for (i = 0; i < Cats->
n_cats; i++) {
984 if (Cats->
field[i] == field) {
985 found = (
int *)bsearch((
void *)&(Cats->
cat[i]), cats, ncats,
996 if (!Clist && !cats && field > 0) {
1006 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_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.
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)
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
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.