open_ogr.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*!
  2. \file vector/Vlib/open_ogr.c
  3. \brief Vector library - Open OGR layer as vector map layer
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2010 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. \author Update to GRASS 7.0 Martin Landa <landa.martin gmail.com> (2009)
  11. */
  12. #include <grass/config.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <grass/vector.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. #ifdef HAVE_OGR
  21. #include <ogr_api.h>
  22. #include <cpl_string.h>
  23. /*!
  24. \brief Open existing OGR layer (level 1 - without feature index file)
  25. Map->name, Map->mapset, Map->fInfo.ogr.dsn and
  26. Map->fInfo.ogr.layer_name must be set before.
  27. \param[in,out] Map pointer to Map_info structure
  28. \param update non-zero for write mode, otherwise read-only
  29. (write mode is currently not supported)
  30. \return 0 success
  31. \return -1 error
  32. */
  33. int V1_open_old_ogr(struct Map_info *Map, int update)
  34. {
  35. int i, layer, nLayers;
  36. OGRDataSourceH Ogr_ds;
  37. OGRLayerH Ogr_layer;
  38. OGRFeatureDefnH Ogr_featuredefn;
  39. Ogr_layer = NULL;
  40. /*
  41. if (update) {
  42. G_warning(_("Write mode is not supported for OGR format"));
  43. return -1;
  44. }
  45. */
  46. if (!Map->fInfo.ogr.dsn) {
  47. G_fatal_error(_("OGR datasource not defined"));
  48. return -1;
  49. }
  50. if (!Map->fInfo.ogr.layer_name) {
  51. G_fatal_error(_("OGR layer not defined"));
  52. return -1;
  53. }
  54. G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", Map->fInfo.ogr.dsn,
  55. Map->fInfo.ogr.layer_name);
  56. OGRRegisterAll();
  57. /* open data source handle */
  58. Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
  59. if (Ogr_ds == NULL)
  60. G_fatal_error(_("Unable to open OGR data source '%s'"),
  61. Map->fInfo.ogr.dsn);
  62. Map->fInfo.ogr.ds = Ogr_ds;
  63. /* get layer number */
  64. layer = -1;
  65. nLayers = OGR_DS_GetLayerCount(Ogr_ds);
  66. G_debug(2, "%d layers found in data source", nLayers);
  67. for (i = 0; i < nLayers; i++) {
  68. Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
  69. Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
  70. if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name) == 0) {
  71. layer = i;
  72. break;
  73. }
  74. }
  75. if (layer == -1) {
  76. OGR_DS_Destroy(Ogr_ds);
  77. G_fatal_error(_("OGR layer <%s> not found"),
  78. Map->fInfo.ogr.layer_name);
  79. }
  80. G_debug(2, "OGR layer %d opened", layer);
  81. Map->fInfo.ogr.layer = Ogr_layer;
  82. Map->fInfo.ogr.lines = NULL;
  83. Map->fInfo.ogr.lines_types = NULL;
  84. Map->fInfo.ogr.lines_alloc = 0;
  85. Map->fInfo.ogr.lines_num = 0;
  86. Map->fInfo.ogr.lines_next = 0;
  87. Map->head.with_z = WITHOUT_Z; /* TODO: 3D */
  88. Map->fInfo.ogr.feature_cache = NULL;
  89. Map->fInfo.ogr.feature_cache_id = -1; /* FID >= 0 */
  90. return (0);
  91. }
  92. /*!
  93. \brief Open existing OGR layer (level 2 - feature index)
  94. \param[in,out] Map pointer to Map_info structure
  95. \return 0 success
  96. \return -1 error
  97. */
  98. int V2_open_old_ogr(struct Map_info *Map)
  99. {
  100. char elem[GPATH_MAX];
  101. char buf[5]; /* used for format version */
  102. long length;
  103. struct gvfile fp;
  104. struct Port_info port;
  105. int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
  106. G_debug(3, "V2_open_old_ogr()");
  107. sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
  108. dig_file_init(&fp);
  109. fp.file = G_fopen_old(elem, GV_FIDX_ELEMENT, Map->mapset);
  110. if (fp.file == NULL) {
  111. G_warning(_("Unable to open fidx file for vector map <%s@%s>"),
  112. Map->name, Map->mapset);
  113. return -1;
  114. }
  115. /* Header */
  116. if (0 >= dig__fread_port_C(buf, 5, &fp))
  117. return (-1);
  118. Version_Major = buf[0];
  119. Version_Minor = buf[1];
  120. Back_Major = buf[2];
  121. Back_Minor = buf[3];
  122. byte_order = buf[4];
  123. /* check version numbers */
  124. if (Version_Major > 5 || Version_Minor > 0) {
  125. if (Back_Major > 5 || Back_Minor > 0) {
  126. G_fatal_error(_("Feature index format version %d.%d is not supported by this release."
  127. " Try to rebuild topology or upgrade GRASS."),
  128. Version_Major, Version_Minor);
  129. return (-1);
  130. }
  131. G_warning(_("Your GRASS version does not fully support feature index format %d.%d of the vector."
  132. " Consider to rebuild topology or upgrade GRASS."),
  133. Version_Major, Version_Minor);
  134. }
  135. dig_init_portable(&port, byte_order);
  136. dig_set_cur_port(&port);
  137. /* Body */
  138. /* bytes 6 - 9 : header size */
  139. if (0 >= dig__fread_port_L(&length, 1, &fp))
  140. return (-1);
  141. G_debug(3, " header size %ld", length);
  142. G_fseek(fp.file, length, SEEK_SET);
  143. /* number of records */
  144. if (0 >= dig__fread_port_I(&(Map->fInfo.ogr.offset_num), 1, &fp))
  145. return (-1);
  146. /* alloc space */
  147. Map->fInfo.ogr.offset =
  148. (int *)G_malloc(Map->fInfo.ogr.offset_num * sizeof(int));
  149. Map->fInfo.ogr.offset_alloc = Map->fInfo.ogr.offset_num;
  150. /* offsets */
  151. if (0 >= dig__fread_port_I(Map->fInfo.ogr.offset,
  152. Map->fInfo.ogr.offset_num, &fp))
  153. return (-1);
  154. fclose(fp.file);
  155. G_debug(3, "%d records read from fidx", Map->fInfo.ogr.offset_num);
  156. Map->fInfo.ogr.next_line = 1;
  157. return 0;
  158. }
  159. /*!
  160. \brief Create new OGR layer in given OGR datasource (level 1)
  161. \param[out] Map pointer to Map_info structure
  162. \param name name of OGR layer to create
  163. \param with_z 2D or 3D (unused?)
  164. \return 0 success
  165. \return -1 error
  166. */
  167. int V1_open_new_ogr(struct Map_info *Map, const char *name, int with_z)
  168. {
  169. OGRSFDriverH Ogr_driver;
  170. OGRDataSourceH Ogr_ds;
  171. OGRLayerH Ogr_layer;
  172. OGRFeatureDefnH Ogr_featuredefn;
  173. int i, nlayers;
  174. char **Ogr_layer_options;
  175. Ogr_layer_options = NULL;
  176. OGRRegisterAll();
  177. Ogr_driver = OGRGetDriverByName(Map->fInfo.ogr.driver_name);
  178. if (!Ogr_driver) {
  179. G_warning(_("Unable to get OGR driver <%s>"), Map->fInfo.ogr.driver_name);
  180. return -1;
  181. }
  182. Map->fInfo.ogr.driver = Ogr_driver;
  183. /* TODO: creation options */
  184. Ogr_ds = OGR_Dr_CreateDataSource(Ogr_driver, Map->fInfo.ogr.dsn, NULL);
  185. if (!Ogr_ds) {
  186. G_warning(_("Unable to create OGR data source '%s'"),
  187. Map->fInfo.ogr.dsn);
  188. return -1;
  189. }
  190. Map->fInfo.ogr.ds = Ogr_ds;
  191. nlayers = OGR_DS_GetLayerCount(Ogr_ds);
  192. for (i = 0; i < nlayers; i++) {
  193. Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
  194. Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
  195. if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name) == 0) {
  196. if (G_get_overwrite()) {
  197. G_warning(_("OGR layer <%s> already exists and will be overwritten"),
  198. Map->fInfo.ogr.layer_name);
  199. if (OGR_DS_DeleteLayer(Ogr_ds, i) != OGRERR_NONE) {
  200. G_warning(_("Unable to delete OGR layer <%s>"),
  201. Map->fInfo.ogr.layer_name);
  202. return -1;
  203. }
  204. }
  205. else {
  206. G_fatal_error(_("OGR layer <%s> already exists in datasource '%s'"),
  207. Map->fInfo.ogr.layer_name, Map->fInfo.ogr.dsn);
  208. }
  209. break;
  210. }
  211. }
  212. /* create new OGR layer */
  213. /* TODO: spatial reference */
  214. /* Ogr_layer_options = CSLSetNameValue(Ogr_layer_options, "OVERWRITE", "YES"); */
  215. Ogr_layer = OGR_DS_CreateLayer(Ogr_ds, Map->fInfo.ogr.layer_name,
  216. NULL, wkbPoint, Ogr_layer_options);
  217. CSLDestroy(Ogr_layer_options);
  218. if (!Ogr_layer) {
  219. G_warning(_("Unable to create OGR layer <%s> in '%s'"),
  220. Map->fInfo.ogr.layer_name, Map->fInfo.ogr.dsn);
  221. return -1;
  222. }
  223. Map->fInfo.ogr.layer = Ogr_layer;
  224. return 0;
  225. }
  226. #endif