build_ogr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*!
  2. \file lib/vector/Vlib/build_ogr.c
  3. \brief Vector library - Building topology for OGR
  4. Higher level functions for reading/writing/manipulating vectors.
  5. Line offset is
  6. - centroids : FID
  7. - other types : index of the first record (which is FID) in offset array.
  8. Category: FID, not all layer have FID, OGRNullFID is defined
  9. (5/2004) as -1, so FID should be only >= 0
  10. (C) 2001-2010 by the GRASS Development Team
  11. This program is free software under the GNU General Public License
  12. (>=v2). Read the file COPYING that comes with GRASS for details.
  13. \author Radim Blazek, Piero Cavalieri
  14. \author Various updates for GRASS 7 by Martin Landa <landa.martin gmail.com>
  15. */
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <grass/vector.h>
  20. #include <grass/glocale.h>
  21. #ifdef HAVE_OGR
  22. #include <ogr_api.h>
  23. /*!
  24. \brief This structure keeps info about geometry parts above
  25. current geometry, path to curent geometry in the * feature. First
  26. 'part' number however is feature Id
  27. */
  28. typedef struct
  29. {
  30. int *part;
  31. int a_parts;
  32. int n_parts;
  33. } GEOM_PARTS;
  34. /*!
  35. \brief Init parts
  36. */
  37. static void init_parts(GEOM_PARTS * parts)
  38. {
  39. parts->part = NULL;
  40. parts->a_parts = parts->n_parts = 0;
  41. }
  42. /*!
  43. \brief Reset parts
  44. */
  45. static void reset_parts(GEOM_PARTS * parts)
  46. {
  47. parts->n_parts = 0;
  48. }
  49. /*!
  50. \brief Free parts
  51. */
  52. static void free_parts(GEOM_PARTS * parts)
  53. {
  54. G_free(parts->part);
  55. parts->a_parts = parts->n_parts = 0;
  56. }
  57. /*!
  58. \brief Add new part number to parts
  59. */
  60. static void add_part(GEOM_PARTS * parts, int part)
  61. {
  62. if (parts->a_parts == parts->n_parts) {
  63. parts->a_parts += 10;
  64. parts->part =
  65. (int *)G_realloc((void *)parts->part,
  66. parts->a_parts * sizeof(int));
  67. }
  68. parts->part[parts->n_parts] = part;
  69. parts->n_parts++;
  70. }
  71. /*!
  72. \brief Remove last part
  73. */
  74. static void del_part(GEOM_PARTS * parts)
  75. {
  76. parts->n_parts--;
  77. }
  78. /*!
  79. \brief Add parts to offset
  80. */
  81. static void add_parts_to_offset(struct Map_info *Map, GEOM_PARTS * parts)
  82. {
  83. int i, j;
  84. if (Map->fInfo.ogr.offset_num + parts->n_parts >=
  85. Map->fInfo.ogr.offset_alloc) {
  86. Map->fInfo.ogr.offset_alloc += parts->n_parts + 1000;
  87. Map->fInfo.ogr.offset = (int *)G_realloc(Map->fInfo.ogr.offset,
  88. Map->fInfo.ogr.offset_alloc *
  89. sizeof(int));
  90. }
  91. j = Map->fInfo.ogr.offset_num;
  92. for (i = 0; i < parts->n_parts; i++) {
  93. G_debug(4, "add offset %d", parts->part[i]);
  94. Map->fInfo.ogr.offset[j] = parts->part[i];
  95. j++;
  96. }
  97. Map->fInfo.ogr.offset_num += parts->n_parts;
  98. }
  99. /*!
  100. \brief Add line to support structures
  101. */
  102. static int add_line(struct Map_info *Map, int type, struct line_pnts *Points,
  103. int FID, GEOM_PARTS * parts)
  104. {
  105. int line;
  106. struct Plus_head *plus;
  107. long offset;
  108. struct bound_box box;
  109. plus = &(Map->plus);
  110. if (type != GV_CENTROID) {
  111. /* beginning in the offset array */
  112. offset = Map->fInfo.ogr.offset_num;
  113. }
  114. else {
  115. /* TODO : could be used to statore category ? */
  116. /* because centroids are read from topology, not from layer */
  117. offset = FID;
  118. }
  119. G_debug(4, "Register line: FID = %d offset = %ld", FID, offset);
  120. dig_line_box(Points, &box);
  121. line = dig_add_line(plus, type, Points, &box, offset);
  122. G_debug(4, "Line registered with line = %d", line);
  123. /* Set box */
  124. if (line == 1)
  125. Vect_box_copy(&(plus->box), &box);
  126. else
  127. Vect_box_extend(&(plus->box), &box);
  128. if (type != GV_BOUNDARY) {
  129. dig_cidx_add_cat(plus, 1, (int)FID, line, type);
  130. }
  131. else {
  132. dig_cidx_add_cat(plus, 0, 0, line, type);
  133. }
  134. if (type != GV_CENTROID) /* because centroids are read from topology, not from layer */
  135. add_parts_to_offset(Map, parts);
  136. return line;
  137. }
  138. /*!
  139. \brief Recursively add geometry to topology
  140. */
  141. static int add_geometry(struct Map_info *Map, OGRGeometryH hGeom, int FID, int build,
  142. GEOM_PARTS * parts)
  143. {
  144. struct Plus_head *plus;
  145. int i, ret;
  146. int line;
  147. int area, isle, outer_area = 0;
  148. int lines[1];
  149. static struct line_pnts **Points = NULL;
  150. static int alloc_points = 0;
  151. struct bound_box box;
  152. struct P_line *Line;
  153. double area_size, x, y;
  154. int eType, nRings, iPart, nParts, nPoints;
  155. OGRGeometryH hGeom2, hRing;
  156. G_debug(4, "add_geometry() FID = %d", FID);
  157. plus = &(Map->plus);
  158. if (!Points) {
  159. alloc_points = 1;
  160. Points = (struct line_pnts **)G_malloc(sizeof(struct line_pnts *));
  161. Points[0] = Vect_new_line_struct();
  162. }
  163. Vect_reset_line(Points[0]);
  164. eType = wkbFlatten(OGR_G_GetGeometryType(hGeom));
  165. G_debug(4, "OGR type = %d", eType);
  166. switch (eType) {
  167. case wkbPoint:
  168. G_debug(4, "Point");
  169. Vect_append_point(Points[0], OGR_G_GetX(hGeom, 0),
  170. OGR_G_GetY(hGeom, 0), OGR_G_GetZ(hGeom, 0));
  171. add_line(Map, GV_POINT, Points[0], FID, parts);
  172. break;
  173. case wkbLineString:
  174. G_debug(4, "LineString");
  175. nPoints = OGR_G_GetPointCount(hGeom);
  176. for (i = 0; i < nPoints; i++) {
  177. Vect_append_point(Points[0],
  178. OGR_G_GetX(hGeom, i), OGR_G_GetY(hGeom, i),
  179. OGR_G_GetZ(hGeom, i));
  180. }
  181. add_line(Map, GV_LINE, Points[0], FID, parts);
  182. break;
  183. case wkbPolygon:
  184. G_debug(4, "Polygon");
  185. nRings = OGR_G_GetGeometryCount(hGeom);
  186. G_debug(4, "Number of rings: %d", nRings);
  187. /* Alloc space for islands */
  188. if (nRings >= alloc_points) {
  189. Points = (struct line_pnts **)G_realloc((void *)Points,
  190. nRings *
  191. sizeof(struct line_pnts
  192. *));
  193. for (i = alloc_points; i < nRings; i++) {
  194. Points[i] = Vect_new_line_struct();
  195. }
  196. }
  197. for (iPart = 0; iPart < nRings; iPart++) {
  198. hRing = OGR_G_GetGeometryRef(hGeom, iPart);
  199. nPoints = OGR_G_GetPointCount(hRing);
  200. G_debug(4, " ring %d : nPoints = %d", iPart, nPoints);
  201. Vect_reset_line(Points[iPart]);
  202. for (i = 0; i < nPoints; i++) {
  203. Vect_append_point(Points[iPart],
  204. OGR_G_GetX(hRing, i), OGR_G_GetY(hRing, i),
  205. OGR_G_GetZ(hRing, i));
  206. }
  207. /* register boundary */
  208. add_part(parts, iPart);
  209. line = add_line(Map, GV_BOUNDARY, Points[iPart], FID, parts);
  210. del_part(parts);
  211. if (build < GV_BUILD_AREAS)
  212. continue;
  213. /* add area (each inner ring is also area) */
  214. dig_line_box(Points[iPart], &box);
  215. dig_find_area_poly(Points[iPart], &area_size);
  216. if (area_size > 0) /* area clockwise */
  217. lines[0] = line;
  218. else
  219. lines[0] = -line;
  220. area = dig_add_area(plus, 1, lines, &box);
  221. /* Each area is also isle */
  222. lines[0] = -lines[0]; /* island is counter clockwise */
  223. isle = dig_add_isle(plus, 1, lines, &box);
  224. if (build < GV_BUILD_ATTACH_ISLES)
  225. continue;
  226. if (iPart == 0) { /* outer ring */
  227. outer_area = area;
  228. }
  229. else { /* inner ring */
  230. struct P_isle *Isle;
  231. Isle = plus->Isle[isle];
  232. Isle->area = outer_area;
  233. dig_area_add_isle(plus, outer_area, isle);
  234. }
  235. }
  236. if (build >= GV_BUILD_CENTROIDS) {
  237. /* create virtual centroid */
  238. ret = Vect_get_point_in_poly_isl((const struct line_pnts *) Points[0],
  239. (const struct line_pnts **) Points + 1,
  240. nRings - 1, &x, &y);
  241. if (ret < -1) {
  242. G_warning(_("Unable to calculate centroid for area %d"),
  243. outer_area);
  244. }
  245. else {
  246. struct P_area *Area;
  247. struct P_topo_c *topo;
  248. G_debug(4, " Centroid: %f, %f", x, y);
  249. Vect_reset_line(Points[0]);
  250. Vect_append_point(Points[0], x, y, 0.0);
  251. line = add_line(Map, GV_CENTROID, Points[0], FID, parts);
  252. Line = plus->Line[line];
  253. topo = (struct P_topo_c *)Line->topo;
  254. topo->area = outer_area;
  255. /* register centroid to area */
  256. Area = plus->Area[outer_area];
  257. Area->centroid = line;
  258. }
  259. }
  260. break;
  261. case wkbMultiPoint:
  262. case wkbMultiLineString:
  263. case wkbMultiPolygon:
  264. case wkbGeometryCollection:
  265. nParts = OGR_G_GetGeometryCount(hGeom);
  266. G_debug(4, "%d geoms -> next level", nParts);
  267. for (i = 0; i < nParts; i++) {
  268. add_part(parts, i);
  269. hGeom2 = OGR_G_GetGeometryRef(hGeom, i);
  270. add_geometry(Map, hGeom2, FID, build, parts);
  271. del_part(parts);
  272. }
  273. break;
  274. default:
  275. G_warning(_("OGR feature type %d not supported"), eType);
  276. break;
  277. }
  278. return 0;
  279. }
  280. #endif
  281. /*!
  282. \brief Build pseudo-topology for OGR layer
  283. Build levels:
  284. - GV_BUILD_NONE
  285. - GV_BUILD_BASE
  286. - GV_BUILD_ATTACH_ISLES
  287. - GV_BUILD_CENTROIDS
  288. - GV_BUILD_ALL
  289. \param Map pointer to Map_info structure
  290. \param build build level
  291. \return 1 on success
  292. \return 0 on error
  293. */
  294. int Vect_build_ogr(struct Map_info *Map, int build)
  295. {
  296. #ifdef HAVE_OGR
  297. int iFeature, FID, line;
  298. struct Plus_head *plus;
  299. struct P_line *Line;
  300. GEOM_PARTS parts;
  301. OGRFeatureH hFeature;
  302. OGRGeometryH hGeom;
  303. G_debug(1, "Vect_build_ogr(): dsn=%s layer=%s, build=%d",
  304. Map->fInfo.ogr.dsn, Map->fInfo.ogr.layer_name, build);
  305. plus = &(Map->plus);
  306. if (build == plus->built)
  307. return 1; /* do nothing */
  308. /* TODO move this init to better place (Vect_open_ ?), because in theory build may be reused on level2 */
  309. if (build >= plus->built && build > GV_BUILD_BASE) {
  310. G_free((void *) Map->fInfo.ogr.offset);
  311. Map->fInfo.ogr.offset = NULL;
  312. Map->fInfo.ogr.offset_num = 0;
  313. Map->fInfo.ogr.offset_alloc = 0;
  314. }
  315. if (!Map->fInfo.ogr.layer) {
  316. G_warning(_("Empty OGR layer, nothing to build"));
  317. return 0;
  318. }
  319. if (OGR_L_TestCapability(Map->fInfo.ogr.layer, OLCTransactions))
  320. OGR_L_CommitTransaction(Map->fInfo.ogr.layer);
  321. /* test layer capabilities */
  322. if (!OGR_L_TestCapability(Map->fInfo.ogr.layer, OLCRandomRead)) {
  323. G_warning(_("Random read is not supported by OGR for this layer, "
  324. "unable to build topology"));
  325. return 0;
  326. }
  327. G_message(_("Using external data format '%s' (feature type '%s')"),
  328. Vect_get_ogr_format_info(Map),
  329. Vect_get_ogr_geometry_type(Map));
  330. /* initialize data structures */
  331. init_parts(&parts);
  332. /* Check if upgrade or downgrade */
  333. if (build < plus->built) { /* lower level request, currently only GV_BUILD_NONE */
  334. if (plus->built >= GV_BUILD_CENTROIDS && build < GV_BUILD_CENTROIDS) {
  335. /* reset info about areas stored for centroids */
  336. int nlines = Vect_get_num_lines(Map);
  337. for (line = 1; line <= nlines; line++) {
  338. Line = plus->Line[line];
  339. if (Line && Line->type == GV_CENTROID) {
  340. struct P_topo_c *topo = (struct P_topo_c *)Line->topo;
  341. topo->area = 0;
  342. }
  343. }
  344. dig_free_plus_areas(plus);
  345. dig_spidx_free_areas(plus);
  346. dig_free_plus_isles(plus);
  347. dig_spidx_free_isles(plus);
  348. }
  349. if (plus->built >= GV_BUILD_AREAS && build < GV_BUILD_AREAS) {
  350. /* reset info about areas stored for lines */
  351. int nlines = Vect_get_num_lines(Map);
  352. for (line = 1; line <= nlines; line++) {
  353. Line = plus->Line[line];
  354. if (Line && Line->type == GV_BOUNDARY) {
  355. struct P_topo_b *topo = (struct P_topo_b *)Line->topo;
  356. topo->left = 0;
  357. topo->right = 0;
  358. }
  359. }
  360. dig_free_plus_areas(plus);
  361. dig_spidx_free_areas(plus);
  362. dig_free_plus_isles(plus);
  363. dig_spidx_free_isles(plus);
  364. }
  365. if (plus->built >= GV_BUILD_BASE && build < GV_BUILD_BASE) {
  366. dig_free_plus_nodes(plus);
  367. dig_spidx_free_nodes(plus);
  368. dig_free_plus_lines(plus);
  369. dig_spidx_free_lines(plus);
  370. }
  371. }
  372. else {
  373. if (plus->built < GV_BUILD_BASE) {
  374. /* Note: Do not use OGR_L_GetFeatureCount (it may scan all features) */
  375. OGR_L_ResetReading(Map->fInfo.ogr.layer);
  376. iFeature = 0;
  377. while ((hFeature = OGR_L_GetNextFeature(Map->fInfo.ogr.layer)) != NULL) {
  378. iFeature++;
  379. G_debug(3, " Feature %d", iFeature);
  380. hGeom = OGR_F_GetGeometryRef(hFeature);
  381. if (hGeom == NULL) {
  382. G_warning(_("Feature %d without geometry ignored"), iFeature);
  383. OGR_F_Destroy(hFeature);
  384. continue;
  385. }
  386. FID = (int)OGR_F_GetFID(hFeature);
  387. if (FID == OGRNullFID) {
  388. G_warning(_("OGR feature %d without ID ignored"), iFeature);
  389. OGR_F_Destroy(hFeature);
  390. continue;
  391. }
  392. G_debug(4, " FID = %d", FID);
  393. reset_parts(&parts);
  394. add_part(&parts, FID);
  395. add_geometry(Map, hGeom, FID, build, &parts);
  396. OGR_F_Destroy(hFeature);
  397. } /* while */
  398. plus->built = GV_BUILD_BASE;
  399. }
  400. }
  401. free_parts(&parts);
  402. plus->built = build;
  403. return 1;
  404. #else
  405. G_fatal_error(_("GRASS is not compiled with OGR support"));
  406. return -1;
  407. #endif
  408. }