build_ogr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 <grass/config.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <grass/gis.h>
  21. #include <grass/vector.h>
  22. #include <grass/glocale.h>
  23. #ifdef HAVE_OGR
  24. #include <ogr_api.h>
  25. /*!
  26. \brief This structure keeps info about geometry parts above
  27. current geometry, path to curent geometry in the * feature. First
  28. 'part' number however is feature Id
  29. */
  30. typedef struct
  31. {
  32. int *part;
  33. int a_parts;
  34. int n_parts;
  35. } GEOM_PARTS;
  36. /*!
  37. \brief Init parts
  38. */
  39. static void init_parts(GEOM_PARTS * parts)
  40. {
  41. parts->part = NULL;
  42. parts->a_parts = parts->n_parts = 0;
  43. }
  44. /*!
  45. \brief
  46. Reset parts
  47. */
  48. static void reset_parts(GEOM_PARTS * parts)
  49. {
  50. parts->n_parts = 0;
  51. }
  52. /*!
  53. \brief Free parts
  54. */
  55. static void free_parts(GEOM_PARTS * parts)
  56. {
  57. G_free(parts->part);
  58. parts->a_parts = parts->n_parts = 0;
  59. }
  60. /*!
  61. \brief Add new part number to parts
  62. */
  63. static void add_part(GEOM_PARTS * parts, int part)
  64. {
  65. if (parts->a_parts == parts->n_parts) {
  66. parts->a_parts += 10;
  67. parts->part =
  68. (int *)G_realloc((void *)parts->part,
  69. parts->a_parts * sizeof(int));
  70. }
  71. parts->part[parts->n_parts] = part;
  72. parts->n_parts++;
  73. }
  74. /*!
  75. \brief Remove last part
  76. */
  77. static void del_part(GEOM_PARTS * parts)
  78. {
  79. parts->n_parts--;
  80. }
  81. /*!
  82. \brief Add parts to offset
  83. */
  84. static void add_parts_to_offset(struct Map_info *Map, GEOM_PARTS * parts)
  85. {
  86. int i, j;
  87. if (Map->fInfo.ogr.offset_num + parts->n_parts >=
  88. Map->fInfo.ogr.offset_alloc) {
  89. Map->fInfo.ogr.offset_alloc += parts->n_parts + 1000;
  90. Map->fInfo.ogr.offset = (int *)G_realloc(Map->fInfo.ogr.offset,
  91. Map->fInfo.ogr.offset_alloc *
  92. sizeof(int));
  93. }
  94. j = Map->fInfo.ogr.offset_num;
  95. for (i = 0; i < parts->n_parts; i++) {
  96. G_debug(4, "add offset %d", parts->part[i]);
  97. Map->fInfo.ogr.offset[j] = parts->part[i];
  98. j++;
  99. }
  100. Map->fInfo.ogr.offset_num += parts->n_parts;
  101. }
  102. /*!
  103. \brief Add line to support structures
  104. */
  105. static int add_line(struct Map_info *Map, int type, struct line_pnts *Points,
  106. int FID, GEOM_PARTS * parts)
  107. {
  108. int line;
  109. struct Plus_head *plus;
  110. long offset;
  111. struct bound_box box;
  112. plus = &(Map->plus);
  113. if (type != GV_CENTROID) {
  114. offset = Map->fInfo.ogr.offset_num; /* beginning in the offset array */
  115. }
  116. else {
  117. /* TODO : could be used to statore category ? */
  118. offset = FID; /* because centroids are read from topology, not from layer */
  119. }
  120. G_debug(4, "Register line: FID = %d offset = %ld", FID, offset);
  121. line = dig_add_line(plus, type, Points, offset);
  122. G_debug(4, "Line registered with line = %d", line);
  123. /* Set box */
  124. dig_line_box(Points, &box);
  125. if (line == 1)
  126. Vect_box_copy(&(plus->box), &box);
  127. else
  128. Vect_box_extend(&(plus->box), &box);
  129. if (type != GV_BOUNDARY) {
  130. dig_cidx_add_cat(plus, 1, (int)FID, line, type);
  131. }
  132. else {
  133. dig_cidx_add_cat(plus, 0, 0, line, type);
  134. }
  135. if (type != GV_CENTROID) /* because centroids are read from topology, not from layer */
  136. add_parts_to_offset(Map, parts);
  137. return line;
  138. }
  139. /*!
  140. \brief Recursively add geometry to topology
  141. */
  142. static int add_geometry(struct Map_info *Map, OGRGeometryH hGeom, int FID,
  143. GEOM_PARTS * parts)
  144. {
  145. struct Plus_head *plus;
  146. int i, ret;
  147. int line;
  148. int area, isle, outer_area = 0;
  149. int lines[1];
  150. static struct line_pnts **Points = NULL;
  151. static int alloc_points = 0;
  152. struct bound_box box;
  153. struct P_line *Line;
  154. double area_size, x, y;
  155. int eType, nRings, iPart, nParts, nPoints;
  156. OGRGeometryH hGeom2, hRing;
  157. G_debug(4, "add_geometry() FID = %d", FID);
  158. plus = &(Map->plus);
  159. if (!Points) {
  160. alloc_points = 1;
  161. Points = (struct line_pnts **)G_malloc(sizeof(struct line_pnts *));
  162. Points[0] = Vect_new_line_struct();
  163. }
  164. Vect_reset_line(Points[0]);
  165. eType = wkbFlatten(OGR_G_GetGeometryType(hGeom));
  166. G_debug(4, "OGR type = %d", eType);
  167. switch (eType) {
  168. case wkbPoint:
  169. G_debug(4, "Point");
  170. Vect_append_point(Points[0], OGR_G_GetX(hGeom, 0),
  171. OGR_G_GetY(hGeom, 0), OGR_G_GetZ(hGeom, 0));
  172. add_line(Map, GV_POINT, Points[0], FID, parts);
  173. break;
  174. case wkbLineString:
  175. G_debug(4, "LineString");
  176. nPoints = OGR_G_GetPointCount(hGeom);
  177. for (i = 0; i < nPoints; i++) {
  178. Vect_append_point(Points[0],
  179. OGR_G_GetX(hGeom, i), OGR_G_GetY(hGeom, i),
  180. OGR_G_GetZ(hGeom, i));
  181. }
  182. add_line(Map, GV_LINE, Points[0], FID, parts);
  183. break;
  184. case wkbPolygon:
  185. G_debug(4, "Polygon");
  186. nRings = OGR_G_GetGeometryCount(hGeom);
  187. G_debug(4, "Number of rings: %d", nRings);
  188. /* Alloc space for islands */
  189. if (nRings >= alloc_points) {
  190. Points = (struct line_pnts **)G_realloc((void *)Points,
  191. nRings *
  192. sizeof(struct line_pnts
  193. *));
  194. for (i = alloc_points; i < nRings; i++) {
  195. Points[i] = Vect_new_line_struct();
  196. }
  197. }
  198. for (iPart = 0; iPart < nRings; iPart++) {
  199. hRing = OGR_G_GetGeometryRef(hGeom, iPart);
  200. nPoints = OGR_G_GetPointCount(hRing);
  201. G_debug(4, " ring %d : nPoints = %d", iPart, nPoints);
  202. Vect_reset_line(Points[iPart]);
  203. for (i = 0; i < nPoints; i++) {
  204. Vect_append_point(Points[iPart],
  205. OGR_G_GetX(hRing, i), OGR_G_GetY(hRing, i),
  206. OGR_G_GetZ(hRing, i));
  207. }
  208. /* register line */
  209. add_part(parts, iPart);
  210. line = add_line(Map, GV_BOUNDARY, Points[iPart], FID, parts);
  211. del_part(parts);
  212. /* add area (each inner ring is also area) */
  213. dig_line_box(Points[iPart], &box);
  214. dig_find_area_poly(Points[iPart], &area_size);
  215. if (area_size > 0) /* clockwise */
  216. lines[0] = line;
  217. else
  218. lines[0] = -line;
  219. area = dig_add_area(plus, 1, lines);
  220. dig_area_set_box(plus, area, &box);
  221. /* Each area is also isle */
  222. lines[0] = -lines[0]; /* island is counter clockwise */
  223. isle = dig_add_isle(plus, 1, lines);
  224. dig_isle_set_box(plus, isle, &box);
  225. if (iPart == 0) { /* outer ring */
  226. outer_area = area;
  227. }
  228. else { /* inner ring */
  229. struct P_isle *Isle;
  230. Isle = plus->Isle[isle];
  231. Isle->area = outer_area;
  232. dig_area_add_isle(plus, outer_area, isle);
  233. }
  234. }
  235. /* create virtual centroid */
  236. ret =
  237. Vect_get_point_in_poly_isl((const struct line_pnts *) Points[0],
  238. (const struct line_pnts **) Points + 1, nRings - 1,
  239. &x, &y);
  240. if (ret < -1) {
  241. G_warning(_("Unable to calculate centroid for area %d"),
  242. outer_area);
  243. }
  244. else {
  245. struct P_area *Area;
  246. G_debug(4, " Centroid: %f, %f", x, y);
  247. Vect_reset_line(Points[0]);
  248. Vect_append_point(Points[0], x, y, 0.0);
  249. line = add_line(Map, GV_CENTROID, Points[0], FID, parts);
  250. dig_line_box(Points[0], &box);
  251. dig_line_set_box(plus, line, &box);
  252. Line = plus->Line[line];
  253. Line->left = outer_area;
  254. /* register centroid to area */
  255. Area = plus->Area[outer_area];
  256. Area->centroid = line;
  257. }
  258. break;
  259. case wkbMultiPoint:
  260. case wkbMultiLineString:
  261. case wkbMultiPolygon:
  262. case wkbGeometryCollection:
  263. nParts = OGR_G_GetGeometryCount(hGeom);
  264. G_debug(4, "%d geoms -> next level", nParts);
  265. for (i = 0; i < nParts; i++) {
  266. add_part(parts, i);
  267. hGeom2 = OGR_G_GetGeometryRef(hGeom, i);
  268. add_geometry(Map, hGeom2, FID, parts);
  269. del_part(parts);
  270. }
  271. break;
  272. default:
  273. G_warning(_("OGR feature type %d not supported"), eType);
  274. break;
  275. }
  276. return 0;
  277. }
  278. /*!
  279. \brief Build pseudo-topology for OGR layer
  280. \param Map pointer to Map_info structure
  281. \param build build level (GV_BUILD_NONE and GV_BUILD_ALL currently supported)
  282. \return 1 on success
  283. \return 0 on error
  284. */
  285. int Vect_build_ogr(struct Map_info *Map, int build)
  286. {
  287. int iFeature, count, FID;
  288. struct Plus_head *plus;
  289. GEOM_PARTS parts;
  290. OGRFeatureH hFeature;
  291. OGRGeometryH hGeom;
  292. G_debug(1, "Vect_build_ogr(): dsn=%s layer=%s, build=%d",
  293. Map->fInfo.ogr.dsn, Map->fInfo.ogr.layer_name, build);
  294. if (build != GV_BUILD_ALL && build != GV_BUILD_NONE) {
  295. G_warning(_("Partial build for OGR is not supported"));
  296. return 0;
  297. }
  298. plus = &(Map->plus);
  299. if (build == plus->built)
  300. return 1; /* do nothing */
  301. /* TODO move this init to better place (Vect_open_ ?), because in theory build may be reused on level2 */
  302. G_free((void *) Map->fInfo.ogr.offset);
  303. Map->fInfo.ogr.offset = NULL;
  304. Map->fInfo.ogr.offset_num = 0;
  305. Map->fInfo.ogr.offset_alloc = 0;
  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