build_sfa.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*!
  2. \file lib/vector/Vlib/build_sfa.c
  3. \brief Vector library - Building pseudo-topology for simple feature access
  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. (C) 2001-2012 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. \author Radim Blazek
  12. \author Piero Cavalieri
  13. \author Various updates for GRASS 7 by Martin Landa <landa.martin gmail.com>
  14. */
  15. #include <stdlib.h>
  16. #include <grass/gis.h>
  17. #include <grass/vector.h>
  18. #include <grass/glocale.h>
  19. /*!
  20. \brief This structure keeps info about geometry parts above current
  21. geometry, path to curent geometry in the feature. First 'part' number
  22. however is feature id */
  23. struct geom_parts
  24. {
  25. int *part;
  26. int a_parts;
  27. int n_parts;
  28. };
  29. static void init_parts(struct geom_parts *);
  30. static void reset_parts(struct geom_parts *);
  31. static void free_parts(struct geom_parts *);
  32. static void add_part(struct geom_parts *, int);
  33. static void del_part(struct geom_parts *);
  34. static void add_parts_to_offset(struct Format_info_offset *,
  35. struct geom_parts *);
  36. static int add_line(struct Plus_head *, struct Format_info_offset *,
  37. int, struct line_pnts *,
  38. int, struct geom_parts *);
  39. #ifdef HAVE_POSTGRES
  40. #include "pg_local_proto.h"
  41. static int add_geometry_pg(struct Plus_head *,
  42. struct Format_info_pg *,
  43. struct feat_parts *, int,
  44. int, int,
  45. struct geom_parts *);
  46. static void build_pg(struct Map_info *, int);
  47. #endif
  48. #ifdef HAVE_OGR
  49. #include <ogr_api.h>
  50. static int add_geometry_ogr(struct Plus_head *,
  51. struct Format_info_ogr *,
  52. OGRGeometryH, int, int,
  53. struct geom_parts *);
  54. static void build_ogr(struct Map_info *, int);
  55. #endif
  56. /*!
  57. \brief Init parts
  58. */
  59. void init_parts(struct geom_parts * parts)
  60. {
  61. G_zero(parts, sizeof(struct geom_parts));
  62. }
  63. /*!
  64. \brief Reset parts
  65. */
  66. void reset_parts(struct geom_parts * parts)
  67. {
  68. parts->n_parts = 0;
  69. }
  70. /*!
  71. \brief Free parts
  72. */
  73. void free_parts(struct geom_parts * parts)
  74. {
  75. G_free(parts->part);
  76. G_zero(parts, sizeof(struct geom_parts));
  77. }
  78. /*!
  79. \brief Add new part number to parts
  80. */
  81. void add_part(struct geom_parts *parts, int part)
  82. {
  83. if (parts->a_parts == parts->n_parts) {
  84. parts->a_parts += 10;
  85. parts->part = (int *) G_realloc((void *)parts->part,
  86. parts->a_parts * sizeof(int));
  87. }
  88. parts->part[parts->n_parts] = part;
  89. parts->n_parts++;
  90. }
  91. /*!
  92. \brief Remove last part
  93. */
  94. void del_part(struct geom_parts *parts)
  95. {
  96. parts->n_parts--;
  97. }
  98. /*!
  99. \brief Add parts to offset
  100. */
  101. void add_parts_to_offset(struct Format_info_offset *offset,
  102. struct geom_parts *parts)
  103. {
  104. int i, j;
  105. if (offset->array_num + parts->n_parts >= offset->array_alloc) {
  106. offset->array_alloc += parts->n_parts + 1000;
  107. offset->array = (int *)G_realloc(offset->array,
  108. offset->array_alloc * sizeof(int));
  109. }
  110. j = offset->array_num;
  111. for (i = 0; i < parts->n_parts; i++) {
  112. G_debug(4, "add offset %d", parts->part[i]);
  113. offset->array[j] = parts->part[i];
  114. j++;
  115. }
  116. offset->array_num += parts->n_parts;
  117. }
  118. /*!
  119. \brief Add line to support structures
  120. */
  121. int add_line(struct Plus_head *plus, struct Format_info_offset *offset,
  122. int type, struct line_pnts *Points,
  123. int FID, struct geom_parts *parts)
  124. {
  125. int line;
  126. long offset_value;
  127. struct bound_box box;
  128. if (type != GV_CENTROID) {
  129. /* beginning in the offset array */
  130. offset_value = offset->array_num;
  131. }
  132. else {
  133. /* TODO : could be used to statore category ? */
  134. /* because centroids are read from topology, not from layer */
  135. offset_value = FID;
  136. }
  137. G_debug(4, "Register line: FID = %d offset = %ld", FID, offset_value);
  138. dig_line_box(Points, &box);
  139. line = dig_add_line(plus, type, Points, &box, offset_value);
  140. G_debug(4, "Line registered with line = %d", line);
  141. /* Set box */
  142. if (line == 1)
  143. Vect_box_copy(&(plus->box), &box);
  144. else
  145. Vect_box_extend(&(plus->box), &box);
  146. if (type != GV_BOUNDARY) {
  147. dig_cidx_add_cat(plus, 1, (int)FID, line, type);
  148. }
  149. else {
  150. dig_cidx_add_cat(plus, 0, 0, line, type);
  151. }
  152. /* because centroids are read from topology, not from layer */
  153. if (type != GV_CENTROID)
  154. add_parts_to_offset(offset, parts);
  155. return line;
  156. }
  157. #ifdef HAVE_POSTGRES
  158. /*!
  159. \brief Recursively add geometry (PostGIS) to topology
  160. */
  161. int add_geometry_pg(struct Plus_head *plus,
  162. struct Format_info_pg *pg_info,
  163. struct feat_parts *fparts, int ipart,
  164. int FID, int build, struct geom_parts *parts)
  165. {
  166. int line, i, idx, area, isle, outer_area, ret;
  167. int lines[1];
  168. double area_size, x, y;
  169. SF_FeatureType ftype;
  170. struct bound_box box;
  171. struct Format_info_offset *offset;
  172. struct line_pnts *line_i;
  173. ftype = fparts->ftype[ipart];
  174. G_debug(4, "add_geometry_pg() FID = %d ftype = %d", FID, ftype);
  175. offset = &(pg_info->offset);
  176. outer_area = 0;
  177. switch(ftype) {
  178. case SF_POINT:
  179. G_debug(4, "Point");
  180. line_i = pg_info->cache.lines[fparts->idx[ipart]];
  181. add_line(plus, offset, GV_POINT, line_i,
  182. FID, parts);
  183. break;
  184. case SF_LINESTRING:
  185. G_debug(4, "LineString");
  186. line_i = pg_info->cache.lines[fparts->idx[ipart]];
  187. add_line(plus, offset, GV_LINE, line_i,
  188. FID, parts);
  189. break;
  190. case SF_POLYGON:
  191. G_debug(4, "Polygon");
  192. /* register boundaries */
  193. idx = fparts->idx[ipart];
  194. for (i = 0; i < fparts->nlines[ipart]; i++) {
  195. line_i = pg_info->cache.lines[idx++];
  196. G_debug(4, "part %d", i);
  197. add_part(parts, i);
  198. line = add_line(plus, offset, GV_BOUNDARY,
  199. line_i, FID, parts);
  200. del_part(parts);
  201. if (build < GV_BUILD_AREAS)
  202. continue;
  203. /* add area (each inner ring is also area) */
  204. dig_line_box(line_i, &box);
  205. dig_find_area_poly(line_i, &area_size);
  206. if (area_size > 0) /* area clockwise */
  207. lines[0] = line;
  208. else
  209. lines[0] = -line;
  210. area = dig_add_area(plus, 1, lines, &box);
  211. /* each area is also isle */
  212. lines[0] = -lines[0]; /* island is counter clockwise */
  213. isle = dig_add_isle(plus, 1, lines, &box);
  214. if (build < GV_BUILD_ATTACH_ISLES)
  215. continue;
  216. if (i == 0) { /* outer ring */
  217. outer_area = area;
  218. }
  219. else { /* inner ring */
  220. struct P_isle *Isle;
  221. Isle = plus->Isle[isle];
  222. Isle->area = outer_area;
  223. dig_area_add_isle(plus, outer_area, isle);
  224. }
  225. }
  226. if (build >= GV_BUILD_CENTROIDS) {
  227. /* create virtual centroid */
  228. ret = Vect_get_point_in_poly_isl((const struct line_pnts *) pg_info->cache.lines[fparts->idx[ipart]],
  229. (const struct line_pnts **) &pg_info->cache.lines[fparts->idx[ipart]] + 1,
  230. fparts->nlines[ipart] - 1, &x, &y);
  231. if (ret < -1) {
  232. G_warning(_("Unable to calculate centroid for area %d"),
  233. outer_area);
  234. }
  235. else {
  236. struct P_area *Area;
  237. struct P_topo_c *topo;
  238. struct P_line *Line;
  239. struct line_pnts *line_c;
  240. G_debug(4, " Centroid: %f, %f", x, y);
  241. line_c = Vect_new_line_struct();
  242. Vect_append_point(line_c, x, y, 0.0);
  243. line = add_line(plus, offset, GV_CENTROID, line_c, FID, parts);
  244. Line = plus->Line[line];
  245. topo = (struct P_topo_c *)Line->topo;
  246. topo->area = outer_area;
  247. /* register centroid to area */
  248. Area = plus->Area[outer_area];
  249. Area->centroid = line;
  250. Vect_destroy_line_struct(line_c);
  251. }
  252. }
  253. break;
  254. default:
  255. G_warning(_("Feature type %d not supported"), ftype);
  256. break;
  257. }
  258. return 0;
  259. }
  260. /*!
  261. \brief Build pseudo-topology for PostGIS layers
  262. */
  263. void build_pg(struct Map_info *Map, int build)
  264. {
  265. int iFeature, ipart, fid, nrecords, npoints;
  266. char *wkb_data;
  267. struct Format_info_pg *pg_info;
  268. struct feat_parts fparts;
  269. struct geom_parts parts;
  270. pg_info = &(Map->fInfo.pg);
  271. /* initialize data structures */
  272. init_parts(&parts);
  273. G_zero(&fparts, sizeof(struct feat_parts));
  274. /* get all features */
  275. if (Vect__open_cursor_next_line_pg(pg_info, TRUE, Map->plus.built) != 0)
  276. return;
  277. /* scan records */
  278. npoints = 0;
  279. nrecords = PQntuples(pg_info->res);
  280. G_debug(4, "build_pg(): nrecords = %d", nrecords);
  281. G_message(_("Registering primitives..."));
  282. for (iFeature = 0; iFeature < nrecords; iFeature++) {
  283. /* get feature id */
  284. fid = atoi(PQgetvalue(pg_info->res, iFeature, 1));
  285. if (fid < 1)
  286. continue; /* PostGIS Topology: skip features with negative
  287. * fid (isles, universal face, ...) */
  288. wkb_data = PQgetvalue(pg_info->res, iFeature, 0);
  289. G_progress(iFeature + 1, 1e4);
  290. /* cache feature (lines) */
  291. if (SF_NONE == Vect__cache_feature_pg(wkb_data, FALSE, FALSE,
  292. &(pg_info->cache), &fparts)) {
  293. G_warning(_("Feature %d without geometry skipped"),
  294. iFeature + 1);
  295. continue;
  296. }
  297. /* register all parts */
  298. reset_parts(&parts);
  299. add_part(&parts, fid);
  300. for (ipart = 0; ipart < fparts.n_parts; ipart++) {
  301. if (fparts.nlines[ipart] < 1) {
  302. G_warning(_("Feature %d without geometry skipped"), fid);
  303. continue;
  304. }
  305. npoints += pg_info->cache.lines[ipart]->n_points;
  306. G_debug(4, "Feature: fid = %d part = %d", fid, ipart);
  307. if (fparts.n_parts > 1)
  308. add_part(&parts, ipart);
  309. add_geometry_pg(&(Map->plus), pg_info, &fparts, ipart,
  310. fid, build, &parts);
  311. if (fparts.n_parts > 1)
  312. del_part(&parts);
  313. }
  314. /* read next feature from cache */
  315. pg_info->cache.lines_next = 0;
  316. }
  317. G_progress(1, 1);
  318. G_message(n_("One primitive registered", "%d primitives registered", Map->plus.n_lines), Map->plus.n_lines);
  319. G_message(n_("One vertex registered", "%d vertices registered", npoints), npoints);
  320. Map->plus.built = GV_BUILD_BASE;
  321. PQclear(pg_info->res);
  322. pg_info->res = NULL;
  323. /* free allocated space */
  324. free_parts(&parts);
  325. }
  326. #endif /* HAVE_POSTGRES */
  327. #ifdef HAVE_OGR
  328. /*!
  329. \brief Recursively add geometry (OGR) to topology
  330. */
  331. int add_geometry_ogr(struct Plus_head *plus,
  332. struct Format_info_ogr *ogr_info,
  333. OGRGeometryH hGeom, int FID, int build,
  334. struct geom_parts *parts)
  335. {
  336. int i, ret, npoints, line;
  337. int area, isle, outer_area;
  338. int lines[1];
  339. double area_size, x, y;
  340. int eType, nRings, iPart, nParts, nPoints;
  341. struct bound_box box;
  342. struct P_line *Line;
  343. struct Format_info_offset *offset;
  344. OGRGeometryH hGeom2, hRing;
  345. G_debug(4, "add_geometry_ogr() FID = %d", FID);
  346. offset = &(ogr_info->offset);
  347. /* allocate space in cache */
  348. if (!ogr_info->cache.lines) {
  349. ogr_info->cache.lines_alloc = 1;
  350. ogr_info->cache.lines = (struct line_pnts **) G_malloc(sizeof(struct line_pnts *));
  351. ogr_info->cache.lines_types = (int *) G_malloc(sizeof(int));
  352. ogr_info->cache.lines[0] = Vect_new_line_struct();
  353. ogr_info->cache.lines_types[0] = -1;
  354. }
  355. npoints = outer_area = 0;
  356. eType = wkbFlatten(OGR_G_GetGeometryType(hGeom));
  357. G_debug(4, "OGR type = %d", eType);
  358. switch (eType) {
  359. case wkbPoint:
  360. G_debug(4, "Point");
  361. ogr_info->cache.lines_types[0] = GV_POINT;
  362. Vect_reset_line(ogr_info->cache.lines[0]);
  363. Vect_append_point(ogr_info->cache.lines[0], OGR_G_GetX(hGeom, 0),
  364. OGR_G_GetY(hGeom, 0), OGR_G_GetZ(hGeom, 0));
  365. add_line(plus, offset, GV_POINT, ogr_info->cache.lines[0], FID, parts);
  366. npoints += ogr_info->cache.lines[0]->n_points;
  367. break;
  368. case wkbLineString:
  369. G_debug(4, "LineString");
  370. ogr_info->cache.lines_types[0] = GV_LINE;
  371. nPoints = OGR_G_GetPointCount(hGeom);
  372. Vect_reset_line(ogr_info->cache.lines[0]);
  373. for (i = 0; i < nPoints; i++) {
  374. Vect_append_point(ogr_info->cache.lines[0],
  375. OGR_G_GetX(hGeom, i), OGR_G_GetY(hGeom, i),
  376. OGR_G_GetZ(hGeom, i));
  377. }
  378. add_line(plus, offset, GV_LINE, ogr_info->cache.lines[0], FID, parts);
  379. npoints += ogr_info->cache.lines[0]->n_points;
  380. break;
  381. case wkbPolygon:
  382. G_debug(4, "Polygon");
  383. nRings = OGR_G_GetGeometryCount(hGeom);
  384. G_debug(4, "Number of rings: %d", nRings);
  385. /* alloc space for islands if needed */
  386. if (nRings > ogr_info->cache.lines_alloc) {
  387. ogr_info->cache.lines_alloc += nRings;
  388. ogr_info->cache.lines = (struct line_pnts **) G_realloc(ogr_info->cache.lines,
  389. ogr_info->cache.lines_alloc *
  390. sizeof(struct line_pnts *));
  391. ogr_info->cache.lines_types = (int *) G_realloc(ogr_info->cache.lines_types,
  392. ogr_info->cache.lines_alloc * sizeof(int));
  393. for (i = ogr_info->cache.lines_alloc - nRings; i < ogr_info->cache.lines_alloc; i++) {
  394. ogr_info->cache.lines[i] = Vect_new_line_struct();
  395. ogr_info->cache.lines_types[i] = -1;
  396. }
  397. }
  398. /* go through rings */
  399. for (iPart = 0; iPart < nRings; iPart++) {
  400. ogr_info->cache.lines_types[iPart] = GV_BOUNDARY;
  401. hRing = OGR_G_GetGeometryRef(hGeom, iPart);
  402. nPoints = OGR_G_GetPointCount(hRing);
  403. G_debug(4, " ring %d : nPoints = %d", iPart, nPoints);
  404. Vect_reset_line(ogr_info->cache.lines[iPart]);
  405. for (i = 0; i < nPoints; i++) {
  406. Vect_append_point(ogr_info->cache.lines[iPart],
  407. OGR_G_GetX(hRing, i), OGR_G_GetY(hRing, i),
  408. OGR_G_GetZ(hRing, i));
  409. }
  410. npoints += ogr_info->cache.lines[iPart]->n_points;
  411. /* register boundary */
  412. add_part(parts, iPart);
  413. line = add_line(plus, offset, GV_BOUNDARY, ogr_info->cache.lines[iPart], FID, parts);
  414. del_part(parts);
  415. if (build < GV_BUILD_AREAS)
  416. continue;
  417. /* add area (each inner ring is also area) */
  418. dig_line_box(ogr_info->cache.lines[iPart], &box);
  419. dig_find_area_poly(ogr_info->cache.lines[iPart], &area_size);
  420. if (area_size > 0) /* area clockwise */
  421. lines[0] = line;
  422. else
  423. lines[0] = -line;
  424. area = dig_add_area(plus, 1, lines, &box);
  425. /* each area is also isle */
  426. lines[0] = -lines[0]; /* island is counter clockwise */
  427. isle = dig_add_isle(plus, 1, lines, &box);
  428. if (build < GV_BUILD_ATTACH_ISLES)
  429. continue;
  430. if (iPart == 0) { /* outer ring */
  431. outer_area = area;
  432. }
  433. else { /* inner ring */
  434. struct P_isle *Isle;
  435. Isle = plus->Isle[isle];
  436. Isle->area = outer_area;
  437. dig_area_add_isle(plus, outer_area, isle);
  438. }
  439. }
  440. if (build >= GV_BUILD_CENTROIDS) {
  441. /* create virtual centroid */
  442. ret = Vect_get_point_in_poly_isl((const struct line_pnts *) ogr_info->cache.lines[0],
  443. (const struct line_pnts **) ogr_info->cache.lines + 1,
  444. nRings - 1, &x, &y);
  445. if (ret < -1) {
  446. G_warning(_("Unable to calculate centroid for area %d"),
  447. outer_area);
  448. }
  449. else {
  450. struct P_area *Area;
  451. struct P_topo_c *topo;
  452. G_debug(4, " Centroid: %f, %f", x, y);
  453. Vect_reset_line(ogr_info->cache.lines[0]);
  454. Vect_append_point(ogr_info->cache.lines[0], x, y, 0.0);
  455. line = add_line(plus, offset, GV_CENTROID, ogr_info->cache.lines[0],
  456. FID, parts);
  457. Line = plus->Line[line];
  458. topo = (struct P_topo_c *)Line->topo;
  459. topo->area = outer_area;
  460. /* register centroid to area */
  461. Area = plus->Area[outer_area];
  462. Area->centroid = line;
  463. }
  464. }
  465. break;
  466. case wkbMultiPoint:
  467. case wkbMultiLineString:
  468. case wkbMultiPolygon:
  469. case wkbGeometryCollection:
  470. nParts = OGR_G_GetGeometryCount(hGeom);
  471. G_debug(4, "%d geoms -> next level", nParts);
  472. /* alloc space for parts if needed */
  473. if (nParts > ogr_info->cache.lines_alloc) {
  474. ogr_info->cache.lines_alloc += nParts;
  475. ogr_info->cache.lines = (struct line_pnts **) G_realloc(ogr_info->cache.lines,
  476. ogr_info->cache.lines_alloc *
  477. sizeof(struct line_pnts *));
  478. ogr_info->cache.lines_types = (int *) G_realloc(ogr_info->cache.lines_types,
  479. ogr_info->cache.lines_alloc * sizeof(int));
  480. for (i = ogr_info->cache.lines_alloc - nParts; i < ogr_info->cache.lines_alloc; i++) {
  481. ogr_info->cache.lines[i] = Vect_new_line_struct();
  482. ogr_info->cache.lines_types[i] = -1;
  483. }
  484. }
  485. /* go through all parts */
  486. for (i = 0; i < nParts; i++) {
  487. add_part(parts, i);
  488. hGeom2 = OGR_G_GetGeometryRef(hGeom, i);
  489. npoints += add_geometry_ogr(plus, ogr_info, hGeom2,
  490. FID, build, parts);
  491. del_part(parts);
  492. }
  493. break;
  494. default:
  495. G_warning(_("OGR feature type %d not supported"), eType);
  496. break;
  497. }
  498. return npoints;
  499. }
  500. void build_ogr(struct Map_info *Map, int build)
  501. {
  502. int iFeature, FID, npoints, nskipped;
  503. struct Format_info_ogr *ogr_info;
  504. OGRFeatureH hFeature;
  505. OGRGeometryH hGeom;
  506. struct geom_parts parts;
  507. ogr_info = &(Map->fInfo.ogr);
  508. /* initialize data structures */
  509. init_parts(&parts);
  510. /* Note: Do not use OGR_L_GetFeatureCount (it may scan all features) */
  511. OGR_L_ResetReading(ogr_info->layer);
  512. if (ogr_info->where)
  513. /* set attribute filter if where sql statement defined */
  514. OGR_L_SetAttributeFilter(ogr_info->layer, ogr_info->where);
  515. npoints = iFeature = nskipped = 0;
  516. G_message(_("Registering primitives..."));
  517. while ((hFeature = OGR_L_GetNextFeature(ogr_info->layer)) != NULL) {
  518. G_debug(3, " Feature %d", iFeature);
  519. G_progress(++iFeature, 1e4);
  520. hGeom = OGR_F_GetGeometryRef(hFeature);
  521. if (hGeom == NULL) {
  522. G_debug(3, "Feature %d without geometry skipped", iFeature);
  523. OGR_F_Destroy(hFeature);
  524. nskipped++;
  525. continue;
  526. }
  527. FID = (int) OGR_F_GetFID(hFeature);
  528. if (FID == OGRNullFID) {
  529. G_debug(3, "OGR feature %d without ID skipped", iFeature);
  530. OGR_F_Destroy(hFeature);
  531. nskipped++;
  532. continue;
  533. }
  534. G_debug(4, " FID = %d", FID);
  535. reset_parts(&parts);
  536. add_part(&parts, FID);
  537. npoints += add_geometry_ogr(&(Map->plus), ogr_info, hGeom,
  538. FID, build, &parts);
  539. OGR_F_Destroy(hFeature);
  540. } /* while */
  541. G_progress(1, 1);
  542. G_message(n_("One primitive registered", "%d primitives registered", Map->plus.n_lines), Map->plus.n_lines);
  543. G_message(n_("One vertex registered", "%d vertices registered", npoints), npoints);
  544. if (nskipped > 0)
  545. G_warning(n_("One feature without geometry skipped", "%d features without geometry skipped", nskipped), nskipped);
  546. Map->plus.built = GV_BUILD_BASE;
  547. free_parts(&parts);
  548. }
  549. #endif /* HAVE_OGR */
  550. /*!
  551. \brief Build pseudo-topology (for simple features) - internal use only
  552. See Vect_build_ogr() and Vect_build_pg() for implementation issues.
  553. Build levels:
  554. - GV_BUILD_NONE
  555. - GV_BUILD_BASE
  556. - GV_BUILD_ATTACH_ISLES
  557. - GV_BUILD_CENTROIDS
  558. - GV_BUILD_ALL
  559. \param Map pointer to Map_info structure
  560. \param build build level
  561. \return 1 on success
  562. \return 0 on error
  563. */
  564. int Vect__build_sfa(struct Map_info *Map, int build)
  565. {
  566. struct Plus_head *plus;
  567. plus = &(Map->plus);
  568. /* check if upgrade or downgrade */
  569. if (build < plus->built) {
  570. /* -> downgrade */
  571. Vect__build_downgrade(Map, build);
  572. return 1;
  573. }
  574. /* -> upgrade */
  575. if (plus->built < GV_BUILD_BASE) {
  576. if (Map->format == GV_FORMAT_OGR ||
  577. Map->format == GV_FORMAT_OGR_DIRECT) {
  578. #ifdef HAVE_OGR
  579. build_ogr(Map, build);
  580. #else
  581. G_fatal_error(_("GRASS is not compiled with OGR support"));
  582. #endif
  583. }
  584. else if (Map->format == GV_FORMAT_POSTGIS) {
  585. #ifdef HAVE_POSTGRES
  586. build_pg(Map, build);
  587. #else
  588. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  589. #endif
  590. }
  591. else {
  592. G_fatal_error(_("%s: Native format unsupported"),
  593. "Vect__build_sfa()");
  594. }
  595. }
  596. plus->built = build;
  597. return 1;
  598. }
  599. /*!
  600. \brief Dump feature index to file
  601. \param Map pointer to Map_info struct
  602. \param out file for output (stdout/stderr for example)
  603. \return 1 on success
  604. \return 0 on error
  605. */
  606. int Vect_fidx_dump(const struct Map_info *Map, FILE *out)
  607. {
  608. int i;
  609. const struct Format_info_offset *offset;
  610. if (Map->format != GV_FORMAT_OGR &&
  611. Map->format != GV_FORMAT_POSTGIS) {
  612. G_warning(_("Feature index is built only for non-native formats. "
  613. "Nothing to dump."));
  614. return 0;
  615. }
  616. if (Map->format == GV_FORMAT_OGR)
  617. offset = &(Map->fInfo.ogr.offset);
  618. else
  619. offset = &(Map->fInfo.pg.offset);
  620. fprintf(out, "---------- FEATURE INDEX DUMP ----------\n");
  621. fprintf(out, "format: %s\n", Vect_maptype_info(Map));
  622. if (Vect_maptype(Map) == GV_FORMAT_POSTGIS &&
  623. Map->fInfo.pg.toposchema_name)
  624. fprintf(out, "topology: PostGIS\n");
  625. else
  626. fprintf(out, "topology: pseudo\n");
  627. fprintf(out, "feature type: %s\n",
  628. Vect_get_finfo_geometry_type(Map));
  629. fprintf(out, "number of features: %d\n\noffset : value (fid or part idx):\n",
  630. Vect_get_num_lines(Map));
  631. for (i = 0; i < offset->array_num; i++) {
  632. fprintf(out, "%6d : %d\n", i, offset->array[i]);
  633. }
  634. return 1;
  635. }