27 #include <cpl_string.h> 30 static int create_ogr_layer(
struct Map_info *,
int);
31 static off_t write_feature(
struct Map_info *,
int,
const struct line_pnts **,
int,
34 OGRLayerH, OGRFeatureH);
35 static int sqltype_to_ogrtype(
int);
67 return write_feature(Map, type, &points, 1, cats);
89 off_t offset,
int type,
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) {
138 if (OGR_L_DeleteFeature(ogr_info->
layer,
165 const struct line_pnts **points,
int nparts,
168 return write_feature(Map,
GV_BOUNDARY, points, nparts, cats);
174 int sqltype, ogrtype, length;
185 OGRFieldDefnH hFieldDefn;
186 OGRFeatureDefnH hFeatureDefn;
198 G_warning(
_(
"Unable to open database <%s> by driver <%s>"),
211 G_warning(
_(
"Unable to open select cursor: '%s'"),
220 hFeatureDefn = OGR_L_GetLayerDefn(hLayer);
222 for (col = 0; col < ncols; col++) {
226 ogrtype = sqltype_to_ogrtype(sqltype);
229 if (strcmp(OGR_L_GetFIDColumn(hLayer), colname) == 0 ||
230 OGR_FD_GetFieldIndex(hFeatureDefn, colname) > -1) {
235 hFieldDefn = OGR_Fld_Create(colname, ogrtype);
237 if (ogrtype == OFTString && length > 0)
238 OGR_Fld_SetWidth(hFieldDefn, length);
239 if (OGR_L_CreateField(hLayer, hFieldDefn,
TRUE) != OGRERR_NONE) {
240 G_warning(
_(
"Creating field <%s> failed"), colname);
245 OGR_Fld_Destroy(hFieldDefn);
266 int create_ogr_layer(
struct Map_info *Map,
int type)
270 OGRSpatialReferenceH Ogr_spatial_ref;
273 struct Key_Value *projinfo, *projunits, *projepsg;
276 OGRwkbGeometryType Ogr_geom_type;
277 char **Ogr_layer_options;
297 Ogr_geom_type = wkbPoint;
300 Ogr_geom_type = wkbLineString;
303 Ogr_geom_type = wkbPolygon;
306 G_warning(
_(
"Unsupported geometry type (%d)"), type);
313 if (strcmp(ogr_info->
driver_name,
"PostgreSQL") == 0) {
314 Ogr_layer_options = CSLSetNameValue(Ogr_layer_options,
"DIM",
"3");
318 if (strcmp(ogr_info->
driver_name,
"PostgreSQL") == 0) {
319 Ogr_layer_options = CSLSetNameValue(Ogr_layer_options,
"DIM",
"2");
324 Ogr_layer = OGR_DS_CreateLayer(ogr_info->
ds, ogr_info->
layer_name,
325 Ogr_spatial_ref, Ogr_geom_type, Ogr_layer_options);
326 CSLDestroy(Ogr_layer_options);
328 G_warning(
_(
"Unable to create OGR layer <%s> in '%s'"),
332 ogr_info->
layer = Ogr_layer;
340 G_warning(
_(
"More layers defined, using driver <%s> and " 346 G_warning(
_(
"Database connection not defined. " 347 "Unable to write attributes."));
350 if (OGR_L_TestCapability(ogr_info->
layer, OLCTransactions) &&
351 (OGR_L_StartTransaction(ogr_info->
layer) != OGRERR_NONE)) {
352 G_warning(
_(
"OGR transaction with layer <%s> failed to start"),
373 off_t write_feature(
struct Map_info *Map,
int type,
374 const struct line_pnts **p_points,
int nparts,
386 OGRGeometryH Ogr_geometry;
387 OGRFeatureH Ogr_feature;
388 OGRFeatureDefnH Ogr_featuredefn;
389 OGRwkbGeometryType Ogr_geom_type;
392 offset_info = &(ogr_info->
offset);
397 points = p_points[0];
399 if (!ogr_info->
layer) {
401 if (create_ogr_layer(Map, type) < 0)
416 G_warning(
_(
"Feature has more categories, using " 417 "category %d (from layer %d)"),
418 cat, cats->
field[0]);
423 Ogr_featuredefn = OGR_L_GetLayerDefn(ogr_info->
layer);
424 Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn);
428 if (Ogr_geom_type != wkbPoint &&
429 Ogr_geom_type != wkbPoint25D) {
430 G_warning(
_(
"Feature is not a point. Skipping."));
433 Ogr_geometry = OGR_G_CreateGeometry(wkbPoint);
436 if (Ogr_geom_type != wkbLineString &&
437 Ogr_geom_type != wkbLineString25D) {
438 G_warning(
_(
"Feature is not a line. Skipping."));
441 Ogr_geometry = OGR_G_CreateGeometry(wkbLineString);
444 if (Ogr_geom_type != wkbPolygon) {
445 G_warning(
_(
"Feature is not a polygon. Skipping."));
448 Ogr_geometry = OGR_G_CreateGeometry(wkbPolygon);
451 if (Ogr_geom_type != wkbPolygon25D) {
452 G_warning(
_(
"Feature is not a face. Skipping."));
455 Ogr_geometry = OGR_G_CreateGeometry(wkbPolygon25D);
458 G_warning(
_(
"Unsupported feature type (%d)"), type);
462 G_debug(3,
"V1_write_line_ogr(): type = %d", type);
464 if (Ogr_geom_type == wkbPolygon || Ogr_geom_type == wkbPolygon25D) {
468 for (iring = 0; iring < nparts; iring++) {
469 OGRGeometryH Ogr_ring;
471 points = p_points[iring];
473 Ogr_ring = OGR_G_CreateGeometry(wkbLinearRing);
474 if (points->
x[0] != points->
x[npoints] ||
475 points->
y[0] != points->
y[npoints] ||
476 points->
z[0] != points->
z[npoints]) {
477 G_warning(
_(
"Boundary is not closed. Feature skipped."));
482 for (i = 0; i < npoints; i++) {
483 OGR_G_AddPoint(Ogr_ring, points->
x[i], points->
y[i],
486 G_debug(4,
" ring(%d): n_points = %d", iring, npoints);
487 OGR_G_AddGeometry(Ogr_geometry, Ogr_ring);
491 for (i = 0; i < points->
n_points; i++) {
492 OGR_G_AddPoint(Ogr_geometry, points->
x[i], points->
y[i],
499 Ogr_feature = OGR_F_Create(Ogr_featuredefn);
500 OGR_F_SetGeometry(Ogr_feature, Ogr_geometry);
503 if (cat > -1 && ogr_info->
dbdriver) {
504 if (0 > write_attributes(ogr_info->
dbdriver,
505 cat, Fi, ogr_info->
layer, Ogr_feature))
506 G_warning(
_(
"Unable to writes feature attributes"));
510 ret = OGR_L_CreateFeature(ogr_info->
layer, Ogr_feature);
522 offset_info->
array[offset_info->
array_num++] = (int) OGR_F_GetFID(Ogr_feature);
523 if (Ogr_geom_type == wkbPolygon || Ogr_geom_type == wkbPolygon25D) {
529 OGR_G_DestroyGeometry(Ogr_geometry);
530 OGR_F_Destroy(Ogr_feature);
532 if (ret != OGRERR_NONE)
535 G_debug(3,
"write_feature(): -> offset = %lu offset_num = %d cat = %d",
536 (
unsigned long) offset, offset_info->
array_num, cat);
554 OGRLayerH Ogr_layer, OGRFeatureH Ogr_feature)
558 int ncol, sqltype, ctype, ogrtype, more;
559 const char *fidcol, *colname;
566 OGRFieldDefnH hFieldDefn;
568 G_debug(3,
"write_attributes(): cat = %d", cat);
578 sprintf(buf,
"SELECT * FROM %s WHERE %s = %d", Fi->
table, Fi->
key,
585 G_warning(
_(
"Unable to select attributes for category %d"),
591 G_warning(
_(
"Unable to fetch data from table <%s>"),
597 G_warning(
_(
"No database record for category %d, " 598 "no attributes will be written"),
603 fidcol = OGR_L_GetFIDColumn(Ogr_layer);
607 for (j = 0; j < ncol; j++) {
610 if (fidcol && *fidcol && strcmp(colname, fidcol) == 0) {
617 G_debug(3,
"col %d : val = %s", j,
622 ogrtype = sqltype_to_ogrtype(sqltype);
623 G_debug(3,
" colctype = %d", ctype);
625 ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, colname);
626 if (ogrfieldnum < 0) {
628 hFieldDefn = OGR_Fld_Create(colname, ogrtype);
629 if (OGR_L_CreateField(Ogr_layer, hFieldDefn,
TRUE) != OGRERR_NONE)
630 G_warning(
_(
"Unable to create field <%s>"), colname);
631 ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, colname);
635 OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
641 OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum,
645 OGR_F_SetFieldDouble(Ogr_feature, ogrfieldnum,
649 OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
655 OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
659 G_warning(
_(
"Unsupported column type %d"), ctype);
672 int sqltype_to_ogrtype(
int sqltype)
680 ogrtype = OFTInteger;
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
dbValue * db_get_column_value(dbColumn *)
Returns column value for given column structure.
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) ...
dbDriver * db_start_driver(const char *)
Initialize a new dbDriver for db transaction.
struct Key_Value * G_get_projinfo(void)
Gets projection information for location.
int db_get_column_length(dbColumn *)
Get column's length.
const char * db_get_column_name(dbColumn *)
Returns column name for given column.
void db_init_string(dbString *)
Initialize dbString.
int db_fetch(dbCursor *, int, int *)
Fetch data from open cursor.
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)
int db_open_select_cursor(dbDriver *, dbString *, dbCursor *, int)
Open select cursor.
int n_points
Number of points.
char * db_get_string(const dbString *)
Get string.
char * table
Name of DB table.
#define DB_C_TYPE_DATETIME
void G_free(void *)
Free allocated memory.
int db_get_value_int(dbValue *)
Get integer value.
int V1_delete_line_ogr(struct Map_info *Map, off_t offset)
Deletes feature at the given offset on level 1 (OGR interface)
struct Format_info fInfo
Format info for non-native formats.
int db_set_handle(dbHandle *, const char *, const char *)
Set handle (database and schema name)
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 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)
int db_close_database_shutdown_driver(dbDriver *)
Close driver/database connection.
int Vect_get_num_dblinks(const struct Map_info *)
Get number of defined dblinks.
int db_convert_column_value_to_string(dbColumn *, dbString *)
?
struct Key_Value * G_get_projunits(void)
Gets units information for location.
Layer (old: field) information.
double * x
Array of X coordinates.
Feature geometry info - coordinates.
int db_get_column_sqltype(dbColumn *)
Returns column sqltype for column.
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)
int db_test_value_isnull(dbValue *)
Check of value is null.
int db_append_string(dbString *, const char *)
Append string to dbString.
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...
struct field_info * Vect_get_dblink(const struct Map_info *, int)
Get information about link to database.
int n_cats
Number of categories attached to element.
const struct driver * driver
int db_sqltype_to_Ctype(int)
Get C data type based on given SQL data type.
void db_init_handle(dbHandle *)
Initialize handle (i.e database/schema)
const char * db_get_value_string(dbValue *)
Get string value.
double * y
Array of Y coordinates.
char * driver
Name of DB driver ('sqlite', 'dbf', ...)
int db_open_database(dbDriver *, dbHandle *)
Open database connection.
double db_get_value_double(dbValue *)
Get double precision value.
int Vect_is_3d(const struct Map_info *)
Check if vector map is 3D.
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 * z
Array of Z coordinates.
int * field
Array of layers (fields)
dbTable * db_get_cursor_table(dbCursor *)
Get table allocated by cursor.
int db_get_table_number_of_columns(dbTable *)
Return the number of columns of the table.
int G_debug(int, const char *,...) __attribute__((format(printf
void db_free_string(dbString *)
Free allocated space for dbString.
void G_free_key_value(struct Key_Value *)
Free allocated Key_Value structure.
int db_close_cursor(dbCursor *)
Close cursor.
int Vect_cat_get(const struct line_cats *, int, int *)
Get first found category of given field.
struct Key_Value * G_get_projepsg(void)
Gets EPSG information for the current location.
char * key
Name of key column (usually 'cat')