write_ogr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*!
  2. \file lib/vector/Vlib/write_ogr.c
  3. \brief Vector library - write vector feature (OGR format)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. Inspired by v.out.ogr's code.
  6. (C) 2009-2011, 2012 by Martin Landa, and the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. \author Martin Landa <landa.martin gmail.com>
  10. */
  11. #include <grass/vector.h>
  12. #include <grass/dbmi.h>
  13. #include <grass/glocale.h>
  14. #ifdef HAVE_OGR
  15. #include <ogr_api.h>
  16. static int sqltype_to_ogrtype(int);
  17. static int write_attributes(dbDriver *, int, const struct field_info *,
  18. OGRLayerH, OGRFeatureH);
  19. #endif
  20. /*!
  21. \brief Writes feature on level 1 (OGR interface)
  22. Note:
  23. - centroids are not supported in OGR, pseudotopo holds virtual
  24. centroids
  25. - boundaries are not supported in OGR, pseudotopo treats polygons
  26. as boundaries
  27. \todo How to deal with OGRNullFID ?
  28. \param Map pointer to Map_info structure
  29. \param type feature type (GV_POINT, GV_LINE, ...)
  30. \param points pointer to line_pnts structure (feature geometry)
  31. \param cats pointer to line_cats structure (feature categories)
  32. \return feature offset into file
  33. \return -1 on error
  34. */
  35. off_t V1_write_line_ogr(struct Map_info *Map, int type,
  36. const struct line_pnts *points,
  37. const struct line_cats *cats)
  38. {
  39. #ifdef HAVE_OGR
  40. int i, cat, ret;
  41. struct field_info *Fi;
  42. struct Format_info_ogr *ogr_info;
  43. struct Format_info_offset *offset_info;
  44. off_t offset;
  45. OGRGeometryH Ogr_geometry;
  46. OGRFeatureH Ogr_feature;
  47. OGRFeatureDefnH Ogr_featuredefn;
  48. OGRwkbGeometryType Ogr_geom_type;
  49. ogr_info = &(Map->fInfo.ogr);
  50. offset_info = &(ogr_info->offset);
  51. if (!ogr_info->layer) {
  52. /* create OGR layer if doesn't exist */
  53. if (V2_open_new_ogr(Map, type) < 0)
  54. return -1;
  55. }
  56. cat = -1; /* no attributes to be written */
  57. if (cats->n_cats > 0 && Vect_get_num_dblinks(Map) > 0) {
  58. /* check for attributes */
  59. Fi = Vect_get_dblink(Map, 0);
  60. if (Fi) {
  61. if (!Vect_cat_get(cats, Fi->number, &cat))
  62. G_warning(_("No category defined for layer %d"), Fi->number);
  63. if (cats->n_cats > 1) {
  64. G_warning(_("Feature has more categories, using "
  65. "category %d (from layer %d)"),
  66. cat, cats->field[0]);
  67. }
  68. }
  69. }
  70. Ogr_featuredefn = OGR_L_GetLayerDefn(ogr_info->layer);
  71. Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn);
  72. /* determine matching OGR feature geometry type */
  73. if (type & (GV_POINT | GV_KERNEL)) {
  74. if (Ogr_geom_type != wkbPoint &&
  75. Ogr_geom_type != wkbPoint25D) {
  76. G_warning(_("Feature is not a point. Skipping."));
  77. return -1;
  78. }
  79. Ogr_geometry = OGR_G_CreateGeometry(wkbPoint);
  80. }
  81. else if (type & GV_LINE) {
  82. if (Ogr_geom_type != wkbLineString &&
  83. Ogr_geom_type != wkbLineString25D) {
  84. G_warning(_("Feature is not a line. Skipping."));
  85. return -1;
  86. }
  87. Ogr_geometry = OGR_G_CreateGeometry(wkbLineString);
  88. }
  89. else if (type & GV_BOUNDARY) {
  90. if (Ogr_geom_type != wkbPolygon) {
  91. G_warning(_("Feature is not a polygon. Skipping."));
  92. return -1;
  93. }
  94. Ogr_geometry = OGR_G_CreateGeometry(wkbPolygon);
  95. }
  96. else if (type & GV_FACE) {
  97. if (Ogr_geom_type != wkbPolygon25D) {
  98. G_warning(_("Feature is not a face. Skipping."));
  99. return -1;
  100. }
  101. Ogr_geometry = OGR_G_CreateGeometry(wkbPolygon25D);
  102. }
  103. else {
  104. G_warning(_("Unsupported feature type (%d)"), type);
  105. return -1;
  106. }
  107. G_debug(3, "V1_write_line_ogr(): type = %d", type);
  108. if (Ogr_geom_type == wkbPolygon || Ogr_geom_type == wkbPolygon25D) {
  109. /* create exterior ring */
  110. OGRGeometryH Ogr_ring;
  111. int npoints;
  112. npoints = points->n_points - 1;
  113. Ogr_ring = OGR_G_CreateGeometry(wkbLinearRing);
  114. if (points->x[0] != points->x[npoints] ||
  115. points->y[0] != points->y[npoints] ||
  116. points->z[0] != points->z[npoints]) {
  117. G_warning(_("Boundary is not closed. Skipping."));
  118. return -1;
  119. }
  120. /* add points */
  121. for (i = 0; i < points->n_points; i++) {
  122. OGR_G_AddPoint(Ogr_ring, points->x[i], points->y[i],
  123. points->z[i]);
  124. }
  125. OGR_G_AddGeometry(Ogr_geometry, Ogr_ring);
  126. }
  127. else {
  128. for (i = 0; i < points->n_points; i++) {
  129. OGR_G_AddPoint(Ogr_geometry, points->x[i], points->y[i],
  130. points->z[i]);
  131. }
  132. }
  133. G_debug(4, " n_points = %d", points->n_points);
  134. /* create feature & set geometry */
  135. Ogr_feature = OGR_F_Create(Ogr_featuredefn);
  136. OGR_F_SetGeometry(Ogr_feature, Ogr_geometry);
  137. /* write attributes */
  138. if (cat > -1 && ogr_info->dbdriver) {
  139. write_attributes(ogr_info->dbdriver,
  140. cat, Fi, ogr_info->layer, Ogr_feature);
  141. G_free(Fi);
  142. }
  143. /* write feature into layer */
  144. ret = OGR_L_CreateFeature(ogr_info->layer, Ogr_feature);
  145. /* update offset array */
  146. if (offset_info->array_num >= offset_info->array_alloc) {
  147. offset_info->array_alloc += 1000;
  148. offset_info->array = (int *) G_realloc(offset_info->array,
  149. offset_info->array_alloc *
  150. sizeof(int));
  151. }
  152. offset = offset_info->array_num;
  153. offset_info->array[offset_info->array_num++] = (int) OGR_F_GetFID(Ogr_feature);
  154. if (Ogr_geom_type == wkbPolygon || Ogr_geom_type == wkbPolygon25D) {
  155. /* register exterior ring in offset array */
  156. offset_info->array[offset_info->array_num++] = 0;
  157. }
  158. /* destroy */
  159. OGR_G_DestroyGeometry(Ogr_geometry);
  160. OGR_F_Destroy(Ogr_feature);
  161. if (ret != OGRERR_NONE)
  162. return -1;
  163. G_debug(3, "V1_write_line_ogr(): -> offset = %lu offset_num = %d cat = %d",
  164. (unsigned long) offset, offset_info->array_num, cat);
  165. return offset;
  166. #else
  167. G_fatal_error(_("GRASS is not compiled with OGR support"));
  168. return -1;
  169. #endif
  170. }
  171. /*!
  172. \brief Rewrites feature at the given offset (level 1) (OGR interface)
  173. \param Map pointer to Map_info structure
  174. \param offset feature offset
  175. \param type feature type (GV_POINT, GV_LINE, ...)
  176. \param points feature geometry
  177. \param cats feature categories
  178. \return feature offset (rewriten feature)
  179. \return -1 on error
  180. */
  181. off_t V1_rewrite_line_ogr(struct Map_info *Map,
  182. int line, int type, off_t offset,
  183. const struct line_pnts *points, const struct line_cats *cats)
  184. {
  185. G_debug(3, "V1_rewrite_line_ogr(): line=%d type=%d offset=%llu",
  186. line, type, offset);
  187. #ifdef HAVE_OGR
  188. if (type != V1_read_line_ogr(Map, NULL, NULL, offset)) {
  189. G_warning(_("Unable to rewrite feature (incompatible feature types)"));
  190. return -1;
  191. }
  192. /* delete old */
  193. V1_delete_line_ogr(Map, offset);
  194. return V1_write_line_ogr(Map, type, points, cats);
  195. #else
  196. G_fatal_error(_("GRASS is not compiled with OGR support"));
  197. return -1;
  198. #endif
  199. }
  200. /*!
  201. \brief Deletes feature at the given offset (level 1)
  202. \param Map pointer Map_info structure
  203. \param offset feature offset
  204. \return 0 on success
  205. \return -1 on error
  206. */
  207. int V1_delete_line_ogr(struct Map_info *Map, off_t offset)
  208. {
  209. #ifdef HAVE_OGR
  210. struct Format_info_ogr *ogr_info;
  211. G_debug(3, "V1_delete_line_ogr(), offset = %lu", (unsigned long) offset);
  212. ogr_info = &(Map->fInfo.ogr);
  213. if (!ogr_info->layer) {
  214. G_warning(_("OGR layer not defined"));
  215. return -1;
  216. }
  217. if (offset >= ogr_info->offset.array_num) {
  218. G_warning(_("Invalid offset (%d)"), offset);
  219. return -1;
  220. }
  221. if (OGR_L_DeleteFeature(ogr_info->layer,
  222. ogr_info->offset.array[offset]) != OGRERR_NONE)
  223. G_warning(_("Unable to delete feature"));
  224. return -1;
  225. return 0;
  226. #else
  227. G_fatal_error(_("GRASS is not compiled with OGR support"));
  228. return -1;
  229. #endif
  230. }
  231. #ifdef HAVE_OGR
  232. int write_attributes(dbDriver *driver, int cat, const struct field_info *Fi,
  233. OGRLayerH Ogr_layer, OGRFeatureH Ogr_feature)
  234. {
  235. int j, ogrfieldnum;
  236. char buf[2000];
  237. int ncol, sqltype, ctype, ogrtype, more;
  238. const char *fidcol, *colname;
  239. dbTable *table;
  240. dbString dbstring;
  241. dbColumn *column;
  242. dbCursor cursor;
  243. dbValue *value;
  244. OGRFieldDefnH hFieldDefn;
  245. G_debug(3, "write_attributes(): cat = %d", cat);
  246. if (cat < 0) {
  247. G_warning(_("Feature without category of layer %d"), Fi->number);
  248. return 0;
  249. }
  250. db_init_string(&dbstring);
  251. /* read & set attributes */
  252. sprintf(buf, "SELECT * FROM %s WHERE %s = %d", Fi->table, Fi->key,
  253. cat);
  254. G_debug(4, "SQL: %s", buf);
  255. db_set_string(&dbstring, buf);
  256. /* select data */
  257. if (db_open_select_cursor(driver, &dbstring, &cursor, DB_SEQUENTIAL) != DB_OK) {
  258. G_fatal_error(_("Unable to select attributes for category %d"),
  259. cat);
  260. }
  261. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK) {
  262. G_fatal_error(_("Unable to fetch data from table <%s>"),
  263. Fi->table);
  264. }
  265. if (!more) {
  266. G_warning(_("No database record for category %d, "
  267. "no attributes will be written"),
  268. cat);
  269. return -1;
  270. }
  271. fidcol = OGR_L_GetFIDColumn(Ogr_layer);
  272. table = db_get_cursor_table(&cursor);
  273. ncol = db_get_table_number_of_columns(table);
  274. for (j = 0; j < ncol; j++) {
  275. column = db_get_table_column(table, j);
  276. colname = db_get_column_name(column);
  277. if (fidcol && *fidcol && strcmp(colname, fidcol) == 0) {
  278. /* skip fid column */
  279. continue;
  280. }
  281. value = db_get_column_value(column);
  282. /* for debug only */
  283. db_convert_column_value_to_string(column, &dbstring);
  284. G_debug(2, "col %d : val = %s", j,
  285. db_get_string(&dbstring));
  286. sqltype = db_get_column_sqltype(column);
  287. ctype = db_sqltype_to_Ctype(sqltype);
  288. ogrtype = sqltype_to_ogrtype(sqltype);
  289. G_debug(2, " colctype = %d", ctype);
  290. ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, colname);
  291. if (ogrfieldnum < 0) {
  292. /* create field if not exists */
  293. hFieldDefn = OGR_Fld_Create(colname, ogrtype);
  294. if (OGR_L_CreateField(Ogr_layer, hFieldDefn, TRUE) != OGRERR_NONE)
  295. G_warning(_("Unable to create field <%s>"), colname);
  296. ogrfieldnum = OGR_F_GetFieldIndex(Ogr_feature, colname);
  297. }
  298. /* Reset */
  299. OGR_F_UnsetField(Ogr_feature, ogrfieldnum);
  300. /* prevent writing NULL values */
  301. if (!db_test_value_isnull(value)) {
  302. switch (ctype) {
  303. case DB_C_TYPE_INT:
  304. OGR_F_SetFieldInteger(Ogr_feature, ogrfieldnum,
  305. db_get_value_int(value));
  306. break;
  307. case DB_C_TYPE_DOUBLE:
  308. OGR_F_SetFieldDouble(Ogr_feature, ogrfieldnum,
  309. db_get_value_double(value));
  310. break;
  311. case DB_C_TYPE_STRING:
  312. OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
  313. db_get_value_string(value));
  314. break;
  315. case DB_C_TYPE_DATETIME:
  316. db_convert_column_value_to_string(column,
  317. &dbstring);
  318. OGR_F_SetFieldString(Ogr_feature, ogrfieldnum,
  319. db_get_string(&dbstring));
  320. break;
  321. default:
  322. G_warning(_("Unsupported column type %d"), ctype);
  323. break;
  324. }
  325. }
  326. }
  327. db_close_cursor (&cursor);
  328. db_free_string(&dbstring);
  329. return 1;
  330. }
  331. int sqltype_to_ogrtype(int sqltype)
  332. {
  333. int ctype, ogrtype;
  334. ctype = db_sqltype_to_Ctype(sqltype);
  335. switch(ctype) {
  336. case DB_C_TYPE_INT:
  337. ogrtype = OFTInteger;
  338. break;
  339. case DB_C_TYPE_DOUBLE:
  340. ogrtype = OFTReal;
  341. break;
  342. case DB_C_TYPE_STRING:
  343. ogrtype = OFTString;
  344. break;
  345. case DB_C_TYPE_DATETIME:
  346. ogrtype = OFTString;
  347. break;
  348. default:
  349. ogrtype = OFTString;
  350. break;
  351. }
  352. return ogrtype;
  353. }
  354. #endif /* HAVE_OGR */