/*! \brief Select pairs key/value to array, values are sorted by key (must be integer) \param driver DB driver \param tab table name \param key key column name \param col value column name \param[out] cvarr dbCatValArray to store within \return number of selected values \return -1 on error */ int db_select_CatValArray(dbDriver * driver, const char *tab, const char *key, const char *col, const char *where, dbCatValArray * cvarr) { int i, type, more, nrows; char buf[1024]; dbString stmt; dbCursor cursor; dbColumn *column; dbValue *value; dbTable *table; G_debug(3, "db_select_CatValArray ()"); if (key == NULL || strlen(key) == 0) { G_warning(_("Missing key column name")); return -1; } if (col == NULL || strlen(col) == 0) { G_warning(_("Missing column name")); return -1; } db_init_string(&stmt); sprintf(buf, "SELECT %s, %s FROM %s", key, col, tab); db_set_string(&stmt, buf); if (where != NULL && strlen(where) > 0) { db_append_string(&stmt, " WHERE "); db_append_string(&stmt, where); } G_debug(3, " SQL: %s", db_get_string(&stmt)); if (db_open_select_cursor(driver, &stmt, &cursor, DB_SEQUENTIAL) != DB_OK) return (-1); nrows = db_get_num_rows(&cursor); G_debug(3, " %d rows selected", nrows); if (nrows < 0) { G_warning(_("Unable select records from table <%s>"), tab); db_close_cursor(&cursor); db_free_string(&stmt); return -1; } db_CatValArray_alloc(cvarr, nrows); table = db_get_cursor_table(&cursor); /* Check if key column is integer */ column = db_get_table_column(table, 0); type = db_sqltype_to_Ctype(db_get_column_sqltype(column)); G_debug(3, " key type = %d", type); if (type != DB_C_TYPE_INT) { G_warning(_("Key column type is not integer")); db_close_cursor(&cursor); db_free_string(&stmt); return -1; } column = db_get_table_column(table, 1); type = db_sqltype_to_Ctype(db_get_column_sqltype(column)); G_debug(3, " col type = %d", type); /* if ( type != DB_C_TYPE_INT && type != DB_C_TYPE_DOUBLE ) { G_fatal_error ( "Column type not supported by db_select_to_array()" ); } */ cvarr->ctype = type; /* fetch the data */ for (i = 0; i < nrows; i++) { if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) return (-1); column = db_get_table_column(table, 0); /* first column */ value = db_get_column_value(column); cvarr->value[i].cat = db_get_value_int(value); column = db_get_table_column(table, 1); value = db_get_column_value(column); cvarr->value[i].isNull = value->isNull; switch (type) { case (DB_C_TYPE_INT): if (value->isNull) cvarr->value[i].val.i = 0; else cvarr->value[i].val.i = db_get_value_int(value); break; case (DB_C_TYPE_DOUBLE): if (value->isNull) cvarr->value[i].val.d = 0.0; else cvarr->value[i].val.d = db_get_value_double(value); break; case (DB_C_TYPE_STRING): cvarr->value[i].val.s = (dbString *) malloc(sizeof(dbString)); db_init_string(cvarr->value[i].val.s); if (!(value->isNull)) db_set_string(cvarr->value[i].val.s, db_get_value_string(value)); break; case (DB_C_TYPE_DATETIME): cvarr->value[i].val.t = (dbDateTime *) calloc(1, sizeof(dbDateTime)); if (!(value->isNull)) memcpy(cvarr->value[i].val.t, &(value->t), sizeof(dbDateTime)); break; default: return (-1); } } cvarr->n_values = nrows; db_close_cursor(&cursor); db_free_string(&stmt); db_CatValArray_sort(cvarr); return (nrows); }
/*! \brief Lines z-bulk labeling Automated labeling (z coordinate assignment) of vector lines (iso-lines). \param Map pointer to Map_info \param List list of selected lines \param point_start_end staring and ending point \param start starting value \param step step value \return number of modified features \return -1 on error */ int Vedit_bulk_labeling(struct Map_info *Map, struct ilist *List, double x1, double y1, double x2, double y2, double start, double step) { int i, cv_i, p_i; int line, type, temp_line; int nlines_modified; double value, dist; struct line_cats *Cats; struct line_pnts *Points, *Points_se; /* start - end */ struct bound_box box, box_se; /* for intersection */ struct line_pnts **Points_a, **Points_b; int nlines_a, nlines_b; dbCatValArray cv; /* line_id / dist */ nlines_modified = 0; value = start; Points = Vect_new_line_struct(); Points_se = Vect_new_line_struct(); Cats = Vect_new_cats_struct(); db_CatValArray_alloc(&cv, List->n_values); cv.ctype = DB_C_TYPE_DOUBLE; cv.n_values = 0; Vect_append_point(Points_se, x1, y1, -PORT_DOUBLE_MAX); Vect_append_point(Points_se, x2, y2, PORT_DOUBLE_MAX); /* write temporaly line */ temp_line = Vect_write_line(Map, GV_LINE, Points_se, Cats); if (temp_line < 0) { return -1; } Vect_line_box(Points_se, &box_se); /* determine order of lines */ cv_i = 0; for (i = 0; i < List->n_values; i++) { line = List->value[i]; if (!Vect_line_alive(Map, line)) continue; type = Vect_read_line(Map, Points, NULL, line); if (!(type & GV_LINE)) continue; Vect_line_box(Points, &box); if (Vect_line_check_intersection(Points_se, Points, WITH_Z)) { Vect_line_intersection(Points_se, Points, &box_se, &box, &Points_a, &Points_b, &nlines_a, &nlines_b, WITHOUT_Z); if (nlines_a < 2 || nlines_b < 1) /* should not happen */ continue; /* calculate distance start point -> point of intersection */ for (p_i = 0; p_i < Points_a[0]->n_points; p_i++) { Points_a[0]->z[p_i] = 0; } dist = Vect_line_length(Points_a[0]); /* always first line in array? */ cv.value[cv_i].cat = line; cv.value[cv_i++].val.d = dist; cv.n_values++; } } /* sort array by distance */ db_CatValArray_sort_by_value(&cv); /* z bulk-labeling */ for (cv_i = 0; cv_i < cv.n_values; cv_i++) { line = cv.value[cv_i].cat; type = Vect_read_line(Map, Points, Cats, line); for (p_i = 0; p_i < Points->n_points; p_i++) { Points->z[p_i] = value; } if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) { return -1; } nlines_modified++; value += step; } if (Vect_delete_line(Map, temp_line) < 0) { return -1; } db_CatValArray_free(&cv); Vect_destroy_line_struct(Points); Vect_destroy_line_struct(Points_se); Vect_destroy_cats_struct(Cats); return nlines_modified; }