build_ogr.c 9.5 KB

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