build_sfa.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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 (set_initial_query(pg_info, TRUE) != 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. wkb_data = PQgetvalue(pg_info->res, iFeature, 0);
  286. G_progress(iFeature + 1, 1e4);
  287. /* cache feature (lines) */
  288. if (SF_NONE == cache_feature(wkb_data, FALSE, FALSE,
  289. &(pg_info->cache), &fparts)) {
  290. G_warning(_("Feature %d without geometry skipped"),
  291. iFeature + 1);
  292. continue;
  293. }
  294. /* register all parts */
  295. reset_parts(&parts);
  296. add_part(&parts, fid);
  297. for (ipart = 0; ipart < fparts.n_parts; ipart++) {
  298. if (fparts.nlines[ipart] < 1) {
  299. G_warning(_("Feature %d without geometry skipped"), fid);
  300. continue;
  301. }
  302. npoints += pg_info->cache.lines[ipart]->n_points;
  303. G_debug(4, "Feature: fid = %d part = %d", fid, ipart);
  304. if (fparts.n_parts > 1)
  305. add_part(&parts, ipart);
  306. add_geometry_pg(&(Map->plus), pg_info, &fparts, ipart,
  307. fid, build, &parts);
  308. if (fparts.n_parts > 1)
  309. del_part(&parts);
  310. }
  311. /* read next feature from cache */
  312. pg_info->cache.lines_next = 0;
  313. }
  314. G_progress(1, 1);
  315. G_message(_("%d primitives registered"), Map->plus.n_lines);
  316. G_message(_("%d vertices registered"), npoints);
  317. Map->plus.built = GV_BUILD_BASE;
  318. PQclear(pg_info->res);
  319. pg_info->res = NULL;
  320. /* free allocated space */
  321. free_parts(&parts);
  322. }
  323. #endif /* HAVE_POSTGRES */
  324. #ifdef HAVE_OGR
  325. /*!
  326. \brief Recursively add geometry (OGR) to topology
  327. */
  328. int add_geometry_ogr(struct Plus_head *plus,
  329. struct Format_info_ogr *ogr_info,
  330. OGRGeometryH hGeom, int FID, int build,
  331. struct geom_parts *parts)
  332. {
  333. int i, ret, npoints, line;
  334. int area, isle, outer_area;
  335. int lines[1];
  336. double area_size, x, y;
  337. int eType, nRings, iPart, nParts, nPoints;
  338. struct bound_box box;
  339. struct P_line *Line;
  340. struct Format_info_offset *offset;
  341. OGRGeometryH hGeom2, hRing;
  342. G_debug(4, "add_geometry_ogr() FID = %d", FID);
  343. offset = &(ogr_info->offset);
  344. /* allocate space in cache */
  345. if (!ogr_info->cache.lines) {
  346. ogr_info->cache.lines_alloc = 1;
  347. ogr_info->cache.lines = (struct line_pnts **) G_malloc(sizeof(struct line_pnts *));
  348. ogr_info->cache.lines_types = (int *) G_malloc(sizeof(int));
  349. ogr_info->cache.lines[0] = Vect_new_line_struct();
  350. ogr_info->cache.lines_types[0] = -1;
  351. }
  352. npoints = outer_area = 0;
  353. eType = wkbFlatten(OGR_G_GetGeometryType(hGeom));
  354. G_debug(4, "OGR type = %d", eType);
  355. switch (eType) {
  356. case wkbPoint:
  357. G_debug(4, "Point");
  358. ogr_info->cache.lines_types[0] = GV_POINT;
  359. Vect_reset_line(ogr_info->cache.lines[0]);
  360. Vect_append_point(ogr_info->cache.lines[0], OGR_G_GetX(hGeom, 0),
  361. OGR_G_GetY(hGeom, 0), OGR_G_GetZ(hGeom, 0));
  362. add_line(plus, offset, GV_POINT, ogr_info->cache.lines[0], FID, parts);
  363. npoints += ogr_info->cache.lines[0]->n_points;
  364. break;
  365. case wkbLineString:
  366. G_debug(4, "LineString");
  367. ogr_info->cache.lines_types[0] = GV_LINE;
  368. nPoints = OGR_G_GetPointCount(hGeom);
  369. Vect_reset_line(ogr_info->cache.lines[0]);
  370. for (i = 0; i < nPoints; i++) {
  371. Vect_append_point(ogr_info->cache.lines[0],
  372. OGR_G_GetX(hGeom, i), OGR_G_GetY(hGeom, i),
  373. OGR_G_GetZ(hGeom, i));
  374. }
  375. add_line(plus, offset, GV_LINE, ogr_info->cache.lines[0], FID, parts);
  376. npoints += ogr_info->cache.lines[0]->n_points;
  377. break;
  378. case wkbPolygon:
  379. G_debug(4, "Polygon");
  380. nRings = OGR_G_GetGeometryCount(hGeom);
  381. G_debug(4, "Number of rings: %d", nRings);
  382. /* alloc space for islands if needed */
  383. if (nRings > ogr_info->cache.lines_alloc) {
  384. ogr_info->cache.lines_alloc += nRings;
  385. ogr_info->cache.lines = (struct line_pnts **) G_realloc(ogr_info->cache.lines,
  386. ogr_info->cache.lines_alloc *
  387. sizeof(struct line_pnts *));
  388. ogr_info->cache.lines_types = (int *) G_realloc(ogr_info->cache.lines_types,
  389. ogr_info->cache.lines_alloc * sizeof(int));
  390. for (i = ogr_info->cache.lines_alloc - nRings; i < ogr_info->cache.lines_alloc; i++) {
  391. ogr_info->cache.lines[i] = Vect_new_line_struct();
  392. ogr_info->cache.lines_types[i] = -1;
  393. }
  394. }
  395. /* go thru rings */
  396. for (iPart = 0; iPart < nRings; iPart++) {
  397. ogr_info->cache.lines_types[iPart] = GV_BOUNDARY;
  398. hRing = OGR_G_GetGeometryRef(hGeom, iPart);
  399. nPoints = OGR_G_GetPointCount(hRing);
  400. G_debug(4, " ring %d : nPoints = %d", iPart, nPoints);
  401. Vect_reset_line(ogr_info->cache.lines[iPart]);
  402. for (i = 0; i < nPoints; i++) {
  403. Vect_append_point(ogr_info->cache.lines[iPart],
  404. OGR_G_GetX(hRing, i), OGR_G_GetY(hRing, i),
  405. OGR_G_GetZ(hRing, i));
  406. }
  407. npoints += ogr_info->cache.lines[iPart]->n_points;
  408. /* register boundary */
  409. add_part(parts, iPart);
  410. line = add_line(plus, offset, GV_BOUNDARY, ogr_info->cache.lines[iPart], FID, parts);
  411. del_part(parts);
  412. if (build < GV_BUILD_AREAS)
  413. continue;
  414. /* add area (each inner ring is also area) */
  415. dig_line_box(ogr_info->cache.lines[iPart], &box);
  416. dig_find_area_poly(ogr_info->cache.lines[iPart], &area_size);
  417. if (area_size > 0) /* area clockwise */
  418. lines[0] = line;
  419. else
  420. lines[0] = -line;
  421. area = dig_add_area(plus, 1, lines, &box);
  422. /* each area is also isle */
  423. lines[0] = -lines[0]; /* island is counter clockwise */
  424. isle = dig_add_isle(plus, 1, lines, &box);
  425. if (build < GV_BUILD_ATTACH_ISLES)
  426. continue;
  427. if (iPart == 0) { /* outer ring */
  428. outer_area = area;
  429. }
  430. else { /* inner ring */
  431. struct P_isle *Isle;
  432. Isle = plus->Isle[isle];
  433. Isle->area = outer_area;
  434. dig_area_add_isle(plus, outer_area, isle);
  435. }
  436. }
  437. if (build >= GV_BUILD_CENTROIDS) {
  438. /* create virtual centroid */
  439. ret = Vect_get_point_in_poly_isl((const struct line_pnts *) ogr_info->cache.lines[0],
  440. (const struct line_pnts **) ogr_info->cache.lines + 1,
  441. nRings - 1, &x, &y);
  442. if (ret < -1) {
  443. G_warning(_("Unable to calculate centroid for area %d"),
  444. outer_area);
  445. }
  446. else {
  447. struct P_area *Area;
  448. struct P_topo_c *topo;
  449. G_debug(4, " Centroid: %f, %f", x, y);
  450. Vect_reset_line(ogr_info->cache.lines[0]);
  451. Vect_append_point(ogr_info->cache.lines[0], x, y, 0.0);
  452. line = add_line(plus, offset, GV_CENTROID, ogr_info->cache.lines[0],
  453. FID, parts);
  454. Line = plus->Line[line];
  455. topo = (struct P_topo_c *)Line->topo;
  456. topo->area = outer_area;
  457. /* register centroid to area */
  458. Area = plus->Area[outer_area];
  459. Area->centroid = line;
  460. }
  461. }
  462. break;
  463. case wkbMultiPoint:
  464. case wkbMultiLineString:
  465. case wkbMultiPolygon:
  466. case wkbGeometryCollection:
  467. nParts = OGR_G_GetGeometryCount(hGeom);
  468. G_debug(4, "%d geoms -> next level", nParts);
  469. /* alloc space for parts if needed */
  470. if (nParts > ogr_info->cache.lines_alloc) {
  471. ogr_info->cache.lines_alloc += nParts;
  472. ogr_info->cache.lines = (struct line_pnts **) G_realloc(ogr_info->cache.lines,
  473. ogr_info->cache.lines_alloc *
  474. sizeof(struct line_pnts *));
  475. ogr_info->cache.lines_types = (int *) G_realloc(ogr_info->cache.lines_types,
  476. ogr_info->cache.lines_alloc * sizeof(int));
  477. for (i = ogr_info->cache.lines_alloc - nParts; i < ogr_info->cache.lines_alloc; i++) {
  478. ogr_info->cache.lines[i] = Vect_new_line_struct();
  479. ogr_info->cache.lines_types[i] = -1;
  480. }
  481. }
  482. /* go thru all parts */
  483. for (i = 0; i < nParts; i++) {
  484. add_part(parts, i);
  485. hGeom2 = OGR_G_GetGeometryRef(hGeom, i);
  486. npoints += add_geometry_ogr(plus, ogr_info, hGeom2,
  487. FID, build, parts);
  488. del_part(parts);
  489. }
  490. break;
  491. default:
  492. G_warning(_("OGR feature type %d not supported"), eType);
  493. break;
  494. }
  495. return npoints;
  496. }
  497. void build_ogr(struct Map_info *Map, int build)
  498. {
  499. int iFeature, FID, npoints;
  500. struct Format_info_ogr *ogr_info;
  501. OGRFeatureH hFeature;
  502. OGRGeometryH hGeom;
  503. struct geom_parts parts;
  504. ogr_info = &(Map->fInfo.ogr);
  505. /* initialize data structures */
  506. init_parts(&parts);
  507. /* Note: Do not use OGR_L_GetFeatureCount (it may scan all features) */
  508. OGR_L_ResetReading(ogr_info->layer);
  509. npoints = iFeature = 0;
  510. G_message(_("Registering primitives..."));
  511. while ((hFeature = OGR_L_GetNextFeature(ogr_info->layer)) != NULL) {
  512. G_debug(3, " Feature %d", iFeature);
  513. G_progress(++iFeature, 1e4);
  514. hGeom = OGR_F_GetGeometryRef(hFeature);
  515. if (hGeom == NULL) {
  516. G_warning(_("Feature %d without geometry skipped"), iFeature);
  517. OGR_F_Destroy(hFeature);
  518. continue;
  519. }
  520. FID = (int) OGR_F_GetFID(hFeature);
  521. if (FID == OGRNullFID) {
  522. G_warning(_("OGR feature %d without ID skipped"), iFeature);
  523. OGR_F_Destroy(hFeature);
  524. continue;
  525. }
  526. G_debug(4, " FID = %d", FID);
  527. reset_parts(&parts);
  528. add_part(&parts, FID);
  529. npoints += add_geometry_ogr(&(Map->plus), ogr_info, hGeom,
  530. FID, build, &parts);
  531. OGR_F_Destroy(hFeature);
  532. } /* while */
  533. G_progress(1, 1);
  534. G_message(_("%d primitives registered"), Map->plus.n_lines);
  535. G_message(_("%d vertices registered"), npoints);
  536. Map->plus.built = GV_BUILD_BASE;
  537. free_parts(&parts);
  538. }
  539. #endif /* HAVE_OGR */
  540. /*!
  541. \brief Build pseudo-topology (for simple features) - internal use only
  542. See Vect_build_ogr() and Vect_build_pg() for implemetation issues.
  543. Build levels:
  544. - GV_BUILD_NONE
  545. - GV_BUILD_BASE
  546. - GV_BUILD_ATTACH_ISLES
  547. - GV_BUILD_CENTROIDS
  548. - GV_BUILD_ALL
  549. \param Map pointer to Map_info structure
  550. \param build build level
  551. \return 1 on success
  552. \return 0 on error
  553. */
  554. int Vect__build_sfa(struct Map_info *Map, int build)
  555. {
  556. struct Plus_head *plus;
  557. plus = &(Map->plus);
  558. /* check if upgrade or downgrade */
  559. if (build < plus->built) {
  560. /* -> downgrade */
  561. Vect__build_downgrade(Map, build);
  562. return 1;
  563. }
  564. /* -> upgrade */
  565. if (plus->built < GV_BUILD_BASE) {
  566. if (Map->format == GV_FORMAT_OGR ||
  567. Map->format == GV_FORMAT_OGR_DIRECT) {
  568. #ifdef HAVE_OGR
  569. build_ogr(Map, build);
  570. #else
  571. G_fatal_error(_("GRASS is not compiled with OGR support"));
  572. #endif
  573. }
  574. else if (Map->format == GV_FORMAT_POSTGIS) {
  575. #ifdef HAVE_POSTGRES
  576. build_pg(Map, build);
  577. #else
  578. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  579. #endif
  580. }
  581. else {
  582. G_fatal_error(_("%s: Native format unsupported"),
  583. "Vect__build_sfa()");
  584. }
  585. }
  586. plus->built = build;
  587. return 1;
  588. }
  589. /*!
  590. \brief Dump feature index to file
  591. \param Map pointer to Map_info struct
  592. \param out file for output (stdout/stderr for example)
  593. \return 1 on success
  594. \return 0 on error
  595. */
  596. int Vect_fidx_dump(const struct Map_info *Map, FILE *out)
  597. {
  598. int i;
  599. const struct Format_info_offset *offset;
  600. if (Map->format != GV_FORMAT_OGR &&
  601. Map->format != GV_FORMAT_POSTGIS) {
  602. G_warning(_("Feature index is built only for non-native formats. "
  603. "Nothing to dump."));
  604. return 0;
  605. }
  606. if (Map->format == GV_FORMAT_OGR)
  607. offset = &(Map->fInfo.ogr.offset);
  608. else
  609. offset = &(Map->fInfo.pg.offset);
  610. fprintf(out, "---------- FEATURE INDEX DUMP ----------\n");
  611. fprintf(out, "format: %s\n", Vect_maptype_info(Map));
  612. if (Vect_maptype(Map) == GV_FORMAT_POSTGIS &&
  613. Map->fInfo.pg.toposchema_name)
  614. fprintf(out, "topology: PostGIS\n");
  615. else
  616. fprintf(out, "topology: pseudo\n");
  617. fprintf(out, "feature type: %s\n",
  618. Vect_get_finfo_geometry_type(Map));
  619. fprintf(out, "number of features: %d\n\noffset : value (fid or part idx):\n",
  620. Vect_get_num_lines(Map));
  621. for (i = 0; i < offset->array_num; i++) {
  622. fprintf(out, "%6d : %d\n", i, offset->array[i]);
  623. }
  624. return 1;
  625. }