28 #include <cpl_string.h>
31 static int create_ogr_layer(
struct Map_info *,
int);
35 OGRLayerH, OGRFeatureH);
36 static int sqltype_to_ogrtype(
int);
68 return write_feature(Map, type, &points, 1, cats);
93 G_debug(3,
"V1_rewrite_line_ogr(): type=%d offset=%" PRId64, type, offset);
96 G_warning(
_(
"Unable to rewrite feature (incompatible feature types)"));
124 G_debug(3,
"V1_delete_line_ogr(), offset = %lu", (
unsigned long)
offset);
128 if (!ogr_info->
layer) {
165 int nparts,
const struct line_cats *cats)
167 return write_feature(Map,
GV_BOUNDARY, points, nparts, cats);
173 int sqltype, ogrtype, length;
184 OGRFieldDefnH hFieldDefn;
185 OGRFeatureDefnH hFeatureDefn;
197 G_warning(
_(
"Unable to open database <%s> by driver <%s>"),
217 hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
219 for (col = 0; col < ncols; col++) {
223 ogrtype = sqltype_to_ogrtype(sqltype);
226 if (strcmp(OGR_L_GetFIDColumn(hLayer), colname) == 0 ||
227 OGR_FD_GetFieldIndex(hFeatureDefn, colname) > -1) {
232 hFieldDefn = OGR_Fld_Create(colname, ogrtype);
234 if (ogrtype == OFTString && length > 0)
235 OGR_Fld_SetWidth(hFieldDefn, length);
236 if (OGR_L_CreateField(hLayer, hFieldDefn,
TRUE) != OGRERR_NONE) {
237 G_warning(
_(
"Creating field <%s> failed"), colname);
242 OGR_Fld_Destroy(hFieldDefn);
263 int create_ogr_layer(
struct Map_info *Map,
int type)
267 OGRSpatialReferenceH Ogr_spatial_ref;
270 struct Key_Value *projinfo, *projunits, *projepsg;
273 OGRwkbGeometryType Ogr_geom_type;
274 char **Ogr_layer_options;
292 Ogr_geom_type = wkbPoint;
295 Ogr_geom_type = wkbLineString;
298 Ogr_geom_type = wkbPolygon;
301 G_warning(
_(
"Unsupported geometry type (%d)"), type);
308 if (strcmp(ogr_info->
driver_name,
"PostgreSQL") == 0) {
309 Ogr_layer_options = CSLSetNameValue(Ogr_layer_options,
"DIM",
"3");
313 if (strcmp(ogr_info->
driver_name,
"PostgreSQL") == 0) {
314 Ogr_layer_options = CSLSetNameValue(Ogr_layer_options,
"DIM",
"2");
320 OGR_DS_CreateLayer(ogr_info->
ds, ogr_info->
layer_name, Ogr_spatial_ref,
321 Ogr_geom_type, Ogr_layer_options);
322 CSLDestroy(Ogr_layer_options);
324 G_warning(
_(
"Unable to create OGR layer <%s> in '%s'"),
328 ogr_info->
layer = Ogr_layer;
336 G_warning(
_(
"More layers defined, using driver <%s> and "
343 G_warning(
_(
"Database connection not defined. "
344 "Unable to write attributes."));
347 if (OGR_L_TestCapability(ogr_info->
layer, OLCTransactions) &&
348 (OGR_L_StartTransaction(ogr_info->
layer) != OGRERR_NONE)) {
349 G_warning(
_(
"OGR transaction with layer <%s> failed to start"),
370 off_t write_feature(
struct Map_info *Map,
int type,
371 const struct line_pnts **p_points,
int nparts,
383 OGRGeometryH Ogr_geometry;
384 OGRFeatureH Ogr_feature;
385 OGRFeatureDefnH Ogr_featuredefn;
386 OGRwkbGeometryType Ogr_geom_type;
389 offset_info = &(ogr_info->
offset);
394 points = p_points[0];
396 if (!ogr_info->
layer) {
398 if (create_ogr_layer(Map, type) < 0)
413 G_warning(
_(
"Feature has more categories, using "
414 "category %d (from layer %d)"),
415 cat, cats->
field[0]);
420 Ogr_featuredefn = OGR_L_GetLayerDefn(ogr_info->
layer);
421 Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn);
425 if (Ogr_geom_type != wkbPoint && Ogr_geom_type != wkbPoint25D) {
426 G_warning(
_(
"Feature is not a point. Skipping."));
429 Ogr_geometry = OGR_G_CreateGeometry(wkbPoint);
432 if (Ogr_geom_type != wkbLineString &&
433 Ogr_geom_type != wkbLineString25D) {
434 G_warning(
_(
"Feature is not a line. Skipping."));
437 Ogr_geometry = OGR_G_CreateGeometry(wkbLineString);
440 if (Ogr_geom_type != wkbPolygon) {
441 G_warning(
_(
"Feature is not a polygon. Skipping."));
444 Ogr_geometry = OGR_G_CreateGeometry(wkbPolygon);
447 if (Ogr_geom_type != wkbPolygon25D) {
448 G_warning(
_(
"Feature is not a face. Skipping."));
451 Ogr_geometry = OGR_G_CreateGeometry(wkbPolygon25D);
454 G_warning(
_(
"Unsupported feature type (%d)"), type);
458 G_debug(3,
"V1_write_line_ogr(): type = %d", type);
460 if (Ogr_geom_type == wkbPolygon || Ogr_geom_type == wkbPolygon25D) {
464 for (iring = 0; iring < nparts; iring++) {
465 OGRGeometryH Ogr_ring;
467 points = p_points[iring];
469 Ogr_ring = OGR_G_CreateGeometry(wkbLinearRing);
470 if (points->
x[0] != points->
x[npoints] ||
471 points->
y[0] != points->
y[npoints] ||
472 points->
z[0] != points->
z[npoints]) {
473 G_warning(
_(
"Boundary is not closed. Feature skipped."));
478 for (i = 0; i < npoints; i++) {
479 OGR_G_AddPoint(Ogr_ring, points->
x[i], points->
y[i],
482 G_debug(4,
" ring(%d): n_points = %d", iring, npoints);
483 OGR_G_AddGeometry(Ogr_geometry, Ogr_ring);
487 for (i = 0; i < points->
n_points; i++) {
488 OGR_G_AddPoint(Ogr_geometry, points->
x[i], points->
y[i],
495 Ogr_feature = OGR_F_Create(Ogr_featuredefn);
496 OGR_F_SetGeometry(Ogr_feature, Ogr_geometry);
499 if (cat > -1 && ogr_info->
dbdriver) {
500 if (0 > write_attributes(ogr_info->
dbdriver, cat, Fi, ogr_info->
layer,
502 G_warning(
_(
"Unable to writes feature attributes"));
506 ret = OGR_L_CreateFeature(ogr_info->
layer, Ogr_feature);
518 (int)OGR_F_GetFID(Ogr_feature);
519 if (Ogr_geom_type == wkbPolygon || Ogr_geom_type == wkbPolygon25D) {
525 OGR_G_DestroyGeometry(Ogr_geometry);
526 OGR_F_Destroy(Ogr_feature);
528 if (ret != OGRERR_NONE)
531 G_debug(3,
"write_feature(): -> offset = %lu offset_num = %d cat = %d",
532 (
unsigned long)offset, offset_info->
array_num, cat);
550 OGRLayerH Ogr_layer, OGRFeatureH Ogr_feature)
554 int ncol, sqltype, ctype, ogrtype, more;
555 const char *fidcol, *colname;
562 OGRFieldDefnH hFieldDefn;
564 G_debug(3,
"write_attributes(): cat = %d", cat);
574 sprintf(buf,
"SELECT * FROM %s WHERE %s = %d", Fi->
table, Fi->
key, cat);
581 G_warning(
_(
"Unable to select attributes for category %d"), cat);
591 G_warning(
_(
"No database record for category %d, "
592 "no attributes will be written"),
597 fidcol = OGR_L_GetFIDColumn(Ogr_layer);
601 for (j = 0; j < ncol; j++) {
604 if (fidcol && *fidcol && strcmp(colname, fidcol) == 0) {
615 ogrtype = sqltype_to_ogrtype(sqltype);
616 G_debug(3,
" colctype = %d", ctype);
618 ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, colname);
619 if (ogrfieldnum < 0) {
621 hFieldDefn = OGR_Fld_Create(colname, ogrtype);
622 if (OGR_L_CreateField(Ogr_layer, hFieldDefn,
TRUE) != OGRERR_NONE)
623 G_warning(
_(
"Unable to create field <%s>"), colname);
624 ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, colname);
628 OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
634 OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum,
638 OGR_F_SetFieldDouble(Ogr_feature, ogrfieldnum,
642 OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
647 OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
651 G_warning(
_(
"Unsupported column type %d"), ctype);
664 int sqltype_to_ogrtype(
int sqltype)
672 ogrtype = OFTInteger;
#define DB_C_TYPE_DATETIME
int db_test_value_isnull(dbValue *)
Check of value is null.
const char * db_get_value_string(dbValue *)
Get string value.
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_get_column_length(dbColumn *)
Get column's length.
double db_get_value_double(dbValue *)
Get double precision value.
int db_sqltype_to_Ctype(int)
Get C data type based on given SQL data type.
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
int db_get_column_sqltype(dbColumn *)
Returns column sqltype for column.
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
dbTable * db_get_cursor_table(dbCursor *)
Get table allocated by cursor.
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
void db_free_string(dbString *)
Free allocated space for dbString.
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_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_append_string(dbString *, const char *)
Append string to dbString.
int db_convert_column_value_to_string(dbColumn *, dbString *)
?
char * db_get_string(const dbString *)
Get string.
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.
void G_free(void *)
Free allocated memory.
struct Key_Value * G_get_projinfo(void)
Gets projection information for location.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
struct Key_Value * G_get_projunits(void)
Gets units information for location.
void G_warning(const char *,...) __attribute__((format(printf
void G_free_key_value(struct Key_Value *)
Free allocated Key_Value structure.
struct Key_Value * G_get_projepsg(void)
Gets EPSG information for the current location.
int G_debug(int, const char *,...) __attribute__((format(printf
OGRSpatialReferenceH GPJ_grass_to_osr2(const struct Key_Value *, const struct Key_Value *, const struct Key_Value *)
Converts a GRASS co-ordinate system to an OGRSpatialReferenceH object. EPSG code is preferred if avai...
int Vect_cat_get(const struct line_cats *, int, int *)
Get first found category of given field.
int V1_read_line_ogr(struct Map_info *, struct line_pnts *, struct line_cats *, off_t)
Read feature from OGR layer at given offset (level 1 without topology)
int Vect_get_num_dblinks(struct Map_info *)
Get number of defined dblinks.
struct field_info * Vect_get_dblink(struct Map_info *, int)
Get information about link to database.
int Vect_is_3d(struct Map_info *)
Check if vector map is 3D.
#define GV_POINT
Feature types used in memory on run time (may change)
const struct driver * driver
struct Format_info fInfo
Format info for non-native formats.
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 * field
Array of layers (fields)
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.
int V1_delete_line_ogr(struct Map_info *Map, off_t offset)
Deletes feature at the given offset on level 1 (OGR interface)
off_t V2__write_area_ogr(struct Map_info *Map, const struct line_pnts **points, int nparts, const struct line_cats *cats)
Writes area on topological level (OGR Simple Features interface, internal use only)
off_t V1_write_line_ogr(struct Map_info *Map, int type, const struct line_pnts *points, const struct line_cats *cats)
Writes feature on level 1 (OGR interface)
off_t V1_rewrite_line_ogr(struct Map_info *Map, off_t offset, int type, const struct line_pnts *points, const struct line_cats *cats)
Rewrites feature at the given offset on level 1 (OGR interface)