open_ogr.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*!
  2. \file lib/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 <unistd.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <grass/vector.h>
  17. #include <grass/gprojects.h>
  18. #include <grass/glocale.h>
  19. #ifdef HAVE_OGR
  20. #include <ogr_api.h>
  21. #include <cpl_string.h>
  22. /*!
  23. \brief Open existing OGR layer (level 1 - without feature index file)
  24. Map->name, Map->mapset, Map->fInfo.ogr.dsn and
  25. Map->fInfo.ogr.layer_name must be set before.
  26. \param[in,out] Map pointer to Map_info structure
  27. \param update TRUE for write mode, otherwise read-only
  28. \return 0 success
  29. \return -1 error
  30. */
  31. int V1_open_old_ogr(struct Map_info *Map, int update)
  32. {
  33. int i, layer, nLayers;
  34. OGRDataSourceH Ogr_ds;
  35. OGRLayerH Ogr_layer;
  36. OGRFeatureDefnH Ogr_featuredefn;
  37. OGRwkbGeometryType Ogr_geom_type;
  38. Ogr_layer = NULL;
  39. Ogr_geom_type = wkbUnknown;
  40. if (!Map->fInfo.ogr.dsn) {
  41. G_fatal_error(_("OGR datasource not defined"));
  42. return -1;
  43. }
  44. if (!Map->fInfo.ogr.layer_name) {
  45. G_fatal_error(_("OGR layer not defined"));
  46. return -1;
  47. }
  48. G_debug(2, "V1_open_old_ogr(): dsn = %s layer = %s", Map->fInfo.ogr.dsn,
  49. Map->fInfo.ogr.layer_name);
  50. OGRRegisterAll();
  51. /* open data source handle */
  52. Ogr_ds = OGROpen(Map->fInfo.ogr.dsn, FALSE, NULL);
  53. if (Ogr_ds == NULL)
  54. G_fatal_error(_("Unable to open OGR data source '%s'"),
  55. Map->fInfo.ogr.dsn);
  56. Map->fInfo.ogr.ds = Ogr_ds;
  57. /* get layer number */
  58. layer = -1;
  59. nLayers = OGR_DS_GetLayerCount(Ogr_ds);
  60. G_debug(2, "%d layers found in data source", nLayers);
  61. for (i = 0; i < nLayers; i++) {
  62. Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
  63. Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
  64. if (strcmp(OGR_FD_GetName(Ogr_featuredefn), Map->fInfo.ogr.layer_name) == 0) {
  65. Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn);
  66. layer = i;
  67. break;
  68. }
  69. }
  70. if (layer == -1) {
  71. OGR_DS_Destroy(Ogr_ds);
  72. G_fatal_error(_("OGR layer <%s> not found"),
  73. Map->fInfo.ogr.layer_name);
  74. }
  75. G_debug(2, "OGR layer %d opened", layer);
  76. Map->fInfo.ogr.layer = Ogr_layer;
  77. Map->fInfo.ogr.lines = NULL;
  78. Map->fInfo.ogr.lines_types = NULL;
  79. Map->fInfo.ogr.lines_alloc = 0;
  80. Map->fInfo.ogr.lines_num = 0;
  81. Map->fInfo.ogr.lines_next = 0;
  82. switch(Ogr_geom_type) {
  83. case wkbPoint25D: case wkbLineString25D: case wkbPolygon25D:
  84. case wkbMultiPoint25D: case wkbMultiLineString25D: case wkbMultiPolygon25D:
  85. case wkbGeometryCollection25D:
  86. Map->head.with_z = WITH_Z;
  87. break;
  88. default:
  89. Map->head.with_z = WITHOUT_Z;
  90. break;
  91. }
  92. Map->fInfo.ogr.feature_cache = NULL;
  93. Map->fInfo.ogr.feature_cache_id = -1; /* FID >= 0 */
  94. return 0;
  95. }
  96. /*!
  97. \brief Open existing OGR layer (level 2 - feature index)
  98. \param[in,out] Map pointer to Map_info structure
  99. \return 0 success
  100. \return -1 error
  101. */
  102. int V2_open_old_ogr(struct Map_info *Map)
  103. {
  104. char elem[GPATH_MAX];
  105. char buf[5]; /* used for format version */
  106. long length;
  107. struct gvfile fp;
  108. struct Port_info port;
  109. int Version_Major, Version_Minor, Back_Major, Back_Minor, byte_order;
  110. G_debug(3, "V2_open_old_ogr()");
  111. sprintf(elem, "%s/%s", GV_DIRECTORY, Map->name);
  112. dig_file_init(&fp);
  113. fp.file = G_fopen_old(elem, GV_FIDX_ELEMENT, Map->mapset);
  114. if (fp.file == NULL) {
  115. G_warning(_("Unable to open fidx file for vector map <%s@%s>"),
  116. Map->name, Map->mapset);
  117. return -1;
  118. }
  119. /* Header */
  120. if (0 >= dig__fread_port_C(buf, 5, &fp))
  121. return (-1);
  122. Version_Major = buf[0];
  123. Version_Minor = buf[1];
  124. Back_Major = buf[2];
  125. Back_Minor = buf[3];
  126. byte_order = buf[4];
  127. /* check version numbers */
  128. if (Version_Major > 5 || Version_Minor > 0) {
  129. if (Back_Major > 5 || Back_Minor > 0) {
  130. G_fatal_error(_("Feature index format version %d.%d is not supported by this release."
  131. " Try to rebuild topology or upgrade GRASS."),
  132. Version_Major, Version_Minor);
  133. return (-1);
  134. }
  135. G_warning(_("Your GRASS version does not fully support feature index format %d.%d of the vector."
  136. " Consider to rebuild topology or upgrade GRASS."),
  137. Version_Major, Version_Minor);
  138. }
  139. dig_init_portable(&port, byte_order);
  140. dig_set_cur_port(&port);
  141. /* Body */
  142. /* bytes 6 - 9 : header size */
  143. if (0 >= dig__fread_port_L(&length, 1, &fp))
  144. return (-1);
  145. G_debug(3, " header size %ld", length);
  146. G_fseek(fp.file, length, SEEK_SET);
  147. /* number of records */
  148. if (0 >= dig__fread_port_I(&(Map->fInfo.ogr.offset_num), 1, &fp))
  149. return (-1);
  150. /* alloc space */
  151. Map->fInfo.ogr.offset =
  152. (int *)G_malloc(Map->fInfo.ogr.offset_num * sizeof(int));
  153. Map->fInfo.ogr.offset_alloc = Map->fInfo.ogr.offset_num;
  154. /* offsets */
  155. if (0 >= dig__fread_port_I(Map->fInfo.ogr.offset,
  156. Map->fInfo.ogr.offset_num, &fp))
  157. return (-1);
  158. fclose(fp.file);
  159. G_debug(3, "%d records read from fidx", Map->fInfo.ogr.offset_num);
  160. Map->fInfo.ogr.next_line = 1;
  161. return 0;
  162. }
  163. /*!
  164. \brief Prepare OGR datasource for creating new OGR layer (level 1)
  165. New OGR layer can be created by V2_open_new_ogr().
  166. \param[out] Map pointer to Map_info structure
  167. \param name name of OGR layer to create
  168. \param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
  169. \return 0 success
  170. \return -1 error
  171. */
  172. int V1_open_new_ogr(struct Map_info *Map, const char *name, int with_z)
  173. {
  174. OGRSFDriverH Ogr_driver;
  175. OGRDataSourceH Ogr_ds;
  176. OGRLayerH Ogr_layer;
  177. OGRFeatureDefnH Ogr_featuredefn;
  178. int i, nlayers;
  179. char **Ogr_layer_options;
  180. Ogr_layer_options = NULL;
  181. OGRRegisterAll();
  182. Ogr_driver = OGRGetDriverByName(Map->fInfo.ogr.driver_name);
  183. if (!Ogr_driver) {
  184. G_warning(_("Unable to get OGR driver <%s>"), Map->fInfo.ogr.driver_name);
  185. return -1;
  186. }
  187. Map->fInfo.ogr.driver = Ogr_driver;
  188. /* TODO: creation options */
  189. Ogr_ds = OGR_Dr_CreateDataSource(Ogr_driver, Map->fInfo.ogr.dsn, NULL);
  190. if (!Ogr_ds) {
  191. G_warning(_("Unable to create OGR data source '%s'"),
  192. Map->fInfo.ogr.dsn);
  193. return -1;
  194. }
  195. Map->fInfo.ogr.ds = Ogr_ds;
  196. nlayers = OGR_DS_GetLayerCount(Ogr_ds);
  197. for (i = 0; i < nlayers; i++) {
  198. Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
  199. Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
  200. if (strcmp(OGR_FD_GetName(Ogr_featuredefn), name) == 0) {
  201. if (G_get_overwrite()) {
  202. G_warning(_("OGR layer <%s> already exists and will be overwritten"),
  203. Map->fInfo.ogr.layer_name);
  204. if (OGR_DS_DeleteLayer(Ogr_ds, i) != OGRERR_NONE) {
  205. G_warning(_("Unable to delete OGR layer <%s>"),
  206. Map->fInfo.ogr.layer_name);
  207. return -1;
  208. }
  209. }
  210. else {
  211. G_fatal_error(_("OGR layer <%s> already exists in datasource '%s'"),
  212. Map->fInfo.ogr.layer_name, Map->fInfo.ogr.dsn);
  213. }
  214. Map->fInfo.ogr.layer = NULL;
  215. break;
  216. }
  217. }
  218. return 0;
  219. }
  220. /*!
  221. \brief Create new OGR layer in given OGR datasource (level 2)
  222. V1_open_new_ogr() is required to be called before this function.
  223. \param[in,out] Map pointer to Map_info structure
  224. \param type feature type (GV_POINT, GV_LINE, ...)
  225. \return 0 success
  226. \return -1 error
  227. */
  228. int V2_open_new_ogr(struct Map_info *Map, int type)
  229. {
  230. OGRLayerH Ogr_layer;
  231. OGRSpatialReferenceH Ogr_spatial_ref;
  232. struct Key_Value *projinfo, *projunits;
  233. OGRwkbGeometryType Ogr_geom_type;
  234. char **Ogr_layer_options;
  235. if (!Map->fInfo.ogr.driver_name ||
  236. !Map->fInfo.ogr.layer_name ||
  237. !Map->fInfo.ogr.ds)
  238. return -1;
  239. /* get spatial reference */
  240. projinfo = G_get_projinfo();
  241. projunits = G_get_projunits();
  242. Ogr_spatial_ref = GPJ_grass_to_osr(projinfo, projunits);
  243. switch(type) {
  244. case GV_POINT:
  245. Ogr_geom_type = wkbPoint;
  246. break;
  247. case GV_LINE:
  248. Ogr_geom_type = wkbLineString;
  249. break;
  250. case GV_AREA:
  251. Ogr_geom_type = wkbPolygon;
  252. break;
  253. default:
  254. G_warning(_("Unsupported geometry type (%d)"), type);
  255. return -1;
  256. }
  257. Ogr_layer_options = Map->fInfo.ogr.layer_options;
  258. if (Vect_is_3d(Map)) {
  259. if (strcmp(Map->fInfo.ogr.driver_name, "PostgreSQL") == 0) {
  260. Ogr_layer_options = CSLSetNameValue(Ogr_layer_options, "DIM", "3");
  261. }
  262. }
  263. else {
  264. if (strcmp(Map->fInfo.ogr.driver_name, "PostgreSQL") == 0) {
  265. Ogr_layer_options = CSLSetNameValue(Ogr_layer_options, "DIM", "2");
  266. }
  267. }
  268. Ogr_layer = OGR_DS_CreateLayer(Map->fInfo.ogr.ds, Map->fInfo.ogr.layer_name,
  269. Ogr_spatial_ref, Ogr_geom_type, Ogr_layer_options);
  270. CSLDestroy(Ogr_layer_options);
  271. if (!Ogr_layer) {
  272. G_warning(_("Unable to create OGR layer <%s> in '%s'"),
  273. Map->fInfo.ogr.layer_name, Map->fInfo.ogr.dsn);
  274. return -1;
  275. }
  276. Map->fInfo.ogr.layer = Ogr_layer;
  277. return 0;
  278. }
  279. #endif