build_ogr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. offset = Map->fInfo.ogr.offset_num; /* beginning in the offset array */
  112. }
  113. else {
  114. /* TODO : could be used to statore category ? */
  115. offset = FID; /* because centroids are read from topology, not from layer */
  116. }
  117. G_debug(4, "Register line: FID = %d offset = %ld", FID, offset);
  118. dig_line_box(Points, &box);
  119. line = dig_add_line(plus, type, Points, &box, offset);
  120. G_debug(4, "Line registered with line = %d", line);
  121. /* Set box */
  122. if (line == 1)
  123. Vect_box_copy(&(plus->box), &box);
  124. else
  125. Vect_box_extend(&(plus->box), &box);
  126. if (type != GV_BOUNDARY) {
  127. dig_cidx_add_cat(plus, 1, (int)FID, line, type);
  128. }
  129. else {
  130. dig_cidx_add_cat(plus, 0, 0, line, type);
  131. }
  132. if (type != GV_CENTROID) /* because centroids are read from topology, not from layer */
  133. add_parts_to_offset(Map, parts);
  134. return line;
  135. }
  136. /*!
  137. \brief Recursively add geometry to topology
  138. */
  139. static int add_geometry(struct Map_info *Map, OGRGeometryH hGeom, int FID,
  140. GEOM_PARTS * parts)
  141. {
  142. struct Plus_head *plus;
  143. int i, ret;
  144. int line;
  145. int area, isle, outer_area = 0;
  146. int lines[1];
  147. static struct line_pnts **Points = NULL;
  148. static int alloc_points = 0;
  149. struct bound_box box;
  150. struct P_line *Line;
  151. double area_size, x, y;
  152. int eType, nRings, iPart, nParts, nPoints;
  153. OGRGeometryH hGeom2, hRing;
  154. G_debug(4, "add_geometry() FID = %d", FID);
  155. plus = &(Map->plus);
  156. if (!Points) {
  157. alloc_points = 1;
  158. Points = (struct line_pnts **)G_malloc(sizeof(struct line_pnts *));
  159. Points[0] = Vect_new_line_struct();
  160. }
  161. Vect_reset_line(Points[0]);
  162. eType = wkbFlatten(OGR_G_GetGeometryType(hGeom));
  163. G_debug(4, "OGR type = %d", eType);
  164. switch (eType) {
  165. case wkbPoint:
  166. G_debug(4, "Point");
  167. Vect_append_point(Points[0], OGR_G_GetX(hGeom, 0),
  168. OGR_G_GetY(hGeom, 0), OGR_G_GetZ(hGeom, 0));
  169. add_line(Map, GV_POINT, Points[0], FID, parts);
  170. break;
  171. case wkbLineString:
  172. G_debug(4, "LineString");
  173. nPoints = OGR_G_GetPointCount(hGeom);
  174. for (i = 0; i < nPoints; i++) {
  175. Vect_append_point(Points[0],
  176. OGR_G_GetX(hGeom, i), OGR_G_GetY(hGeom, i),
  177. OGR_G_GetZ(hGeom, i));
  178. }
  179. add_line(Map, GV_LINE, Points[0], FID, parts);
  180. break;
  181. case wkbPolygon:
  182. G_debug(4, "Polygon");
  183. nRings = OGR_G_GetGeometryCount(hGeom);
  184. G_debug(4, "Number of rings: %d", nRings);
  185. /* Alloc space for islands */
  186. if (nRings >= alloc_points) {
  187. Points = (struct line_pnts **)G_realloc((void *)Points,
  188. nRings *
  189. sizeof(struct line_pnts
  190. *));
  191. for (i = alloc_points; i < nRings; i++) {
  192. Points[i] = Vect_new_line_struct();
  193. }
  194. }
  195. for (iPart = 0; iPart < nRings; iPart++) {
  196. hRing = OGR_G_GetGeometryRef(hGeom, iPart);
  197. nPoints = OGR_G_GetPointCount(hRing);
  198. G_debug(4, " ring %d : nPoints = %d", iPart, nPoints);
  199. Vect_reset_line(Points[iPart]);
  200. for (i = 0; i < nPoints; i++) {
  201. Vect_append_point(Points[iPart],
  202. OGR_G_GetX(hRing, i), OGR_G_GetY(hRing, i),
  203. OGR_G_GetZ(hRing, i));
  204. }
  205. /* register boundary */
  206. add_part(parts, iPart);
  207. line = add_line(Map, GV_BOUNDARY, Points[iPart], FID, parts);
  208. del_part(parts);
  209. /* add area (each inner ring is also area) */
  210. dig_line_box(Points[iPart], &box);
  211. dig_find_area_poly(Points[iPart], &area_size);
  212. if (area_size > 0) /* clockwise */
  213. lines[0] = line;
  214. else
  215. lines[0] = -line;
  216. area = dig_add_area(plus, 1, lines, &box);
  217. /* Each area is also isle */
  218. lines[0] = -lines[0]; /* island is counter clockwise */
  219. isle = dig_add_isle(plus, 1, lines, &box);
  220. if (iPart == 0) { /* outer ring */
  221. outer_area = area;
  222. }
  223. else { /* inner ring */
  224. struct P_isle *Isle;
  225. Isle = plus->Isle[isle];
  226. Isle->area = outer_area;
  227. dig_area_add_isle(plus, outer_area, isle);
  228. }
  229. }
  230. /* create virtual centroid */
  231. ret = Vect_get_point_in_poly_isl((const struct line_pnts *) Points[0],
  232. (const struct line_pnts **) Points + 1,
  233. nRings - 1, &x, &y);
  234. if (ret < -1) {
  235. G_warning(_("Unable to calculate centroid for area %d"),
  236. outer_area);
  237. }
  238. else {
  239. struct P_area *Area;
  240. struct P_topo_c *topo;
  241. G_debug(4, " Centroid: %f, %f", x, y);
  242. Vect_reset_line(Points[0]);
  243. Vect_append_point(Points[0], x, y, 0.0);
  244. line = add_line(Map, GV_CENTROID, Points[0], FID, parts);
  245. Line = plus->Line[line];
  246. topo = (struct P_topo_c *)Line->topo;
  247. topo->area = outer_area;
  248. /* register centroid to area */
  249. Area = plus->Area[outer_area];
  250. Area->centroid = line;
  251. }
  252. break;
  253. case wkbMultiPoint:
  254. case wkbMultiLineString:
  255. case wkbMultiPolygon:
  256. case wkbGeometryCollection:
  257. nParts = OGR_G_GetGeometryCount(hGeom);
  258. G_debug(4, "%d geoms -> next level", nParts);
  259. for (i = 0; i < nParts; i++) {
  260. add_part(parts, i);
  261. hGeom2 = OGR_G_GetGeometryRef(hGeom, i);
  262. add_geometry(Map, hGeom2, FID, parts);
  263. del_part(parts);
  264. }
  265. break;
  266. default:
  267. G_warning(_("OGR feature type %d not supported"), eType);
  268. break;
  269. }
  270. return 0;
  271. }
  272. /*!
  273. \brief Build pseudo-topology for OGR layer
  274. \param Map pointer to Map_info structure
  275. \param build build level (GV_BUILD_NONE and GV_BUILD_ALL currently supported)
  276. \return 1 on success
  277. \return 0 on error
  278. */
  279. int Vect_build_ogr(struct Map_info *Map, int build)
  280. {
  281. int iFeature, count, FID;
  282. struct Plus_head *plus;
  283. GEOM_PARTS parts;
  284. OGRFeatureH hFeature;
  285. OGRGeometryH hGeom;
  286. G_debug(1, "Vect_build_ogr(): dsn=%s layer=%s, build=%d",
  287. Map->fInfo.ogr.dsn, Map->fInfo.ogr.layer_name, build);
  288. if (build != GV_BUILD_ALL && build != GV_BUILD_NONE) {
  289. G_warning(_("Partial build for OGR is not supported"));
  290. return 0;
  291. }
  292. plus = &(Map->plus);
  293. if (build == plus->built)
  294. return 1; /* do nothing */
  295. /* TODO move this init to better place (Vect_open_ ?), because in theory build may be reused on level2 */
  296. G_free((void *) Map->fInfo.ogr.offset);
  297. Map->fInfo.ogr.offset = NULL;
  298. Map->fInfo.ogr.offset_num = 0;
  299. Map->fInfo.ogr.offset_alloc = 0;
  300. if (!Map->fInfo.ogr.layer) {
  301. G_warning(_("Empty OGR layer, nothing to build"));
  302. return 0;
  303. }
  304. if (OGR_L_TestCapability(Map->fInfo.ogr.layer, OLCTransactions))
  305. OGR_L_CommitTransaction(Map->fInfo.ogr.layer);
  306. /* test layer capabilities */
  307. if (!OGR_L_TestCapability(Map->fInfo.ogr.layer, OLCRandomRead)) {
  308. G_warning(_("Random read is not supported by OGR for this layer, "
  309. "unable to build topology"));
  310. return 0;
  311. }
  312. G_message(_("Using external data format '%s' (feature type '%s')"),
  313. Vect_get_ogr_format_info(Map),
  314. Vect_get_ogr_geometry_type(Map));
  315. /* initialize data structures */
  316. init_parts(&parts);
  317. /* Check if upgrade or downgrade */
  318. if (build < plus->built) { /* lower level request, currently only GV_BUILD_NONE */
  319. dig_free_plus_areas(plus);
  320. dig_spidx_free_areas(plus);
  321. dig_free_plus_isles(plus);
  322. dig_spidx_free_isles(plus);
  323. dig_free_plus_nodes(plus);
  324. dig_spidx_free_nodes(plus);
  325. dig_free_plus_lines(plus);
  326. dig_spidx_free_lines(plus);
  327. }
  328. else {
  329. /* Note: Do not use OGR_L_GetFeatureCount (it may scan all features) */
  330. OGR_L_ResetReading(Map->fInfo.ogr.layer);
  331. count = iFeature = 0;
  332. while ((hFeature = OGR_L_GetNextFeature(Map->fInfo.ogr.layer)) != NULL) {
  333. iFeature++;
  334. count++;
  335. G_debug(3, " Feature %d", iFeature);
  336. hGeom = OGR_F_GetGeometryRef(hFeature);
  337. if (hGeom == NULL) {
  338. G_warning(_("Feature %d without geometry ignored"), iFeature);
  339. OGR_F_Destroy(hFeature);
  340. continue;
  341. }
  342. FID = (int)OGR_F_GetFID(hFeature);
  343. if (FID == OGRNullFID) {
  344. G_warning(_("OGR feature %d without ID ignored"), iFeature);
  345. OGR_F_Destroy(hFeature);
  346. continue;
  347. }
  348. G_debug(4, " FID = %d", FID);
  349. reset_parts(&parts);
  350. add_part(&parts, FID);
  351. add_geometry(Map, hGeom, FID, &parts);
  352. OGR_F_Destroy(hFeature);
  353. } /* while */
  354. }
  355. free_parts(&parts);
  356. Map->plus.built = build;
  357. return 1;
  358. }
  359. #endif