geos.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*!
  2. \file geos.c
  3. \brief Vector library - GEOS support
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2009 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Martin Landa <landa.martin gmail.com>
  9. */
  10. #include <grass/config.h>
  11. #include <grass/gis.h>
  12. #include <grass/Vect.h>
  13. #include <grass/glocale.h>
  14. #ifdef HAVE_GEOS
  15. #include <geos_c.h>
  16. static GEOSGeometry *Vect__read_line_geos(struct Map_info *, long, int *);
  17. static GEOSCoordSequence *V1_read_line_geos(struct Map_info *, long, int *);
  18. static GEOSCoordSequence *V2_read_line_geos(struct Map_info *, int);
  19. /*!
  20. \brief Read vector feature and stores it as GEOSGeometry instance
  21. Supported feature types:
  22. - GV_POINT -> POINT
  23. - GV_LINE -> LINESTRING
  24. - GV_BOUNDARY -> LINESTRING / LINEARRING
  25. You should free allocated memory by GEOSGeom_destroy().
  26. \param Map pointer to Map_info structure
  27. \param line feature id
  28. \param[out] type feature type or NULL
  29. \return pointer to GEOSGeometry instance
  30. \return empty GEOSGeometry for unsupported feature type
  31. \return NULL on error
  32. */
  33. GEOSGeometry *Vect_read_line_geos(struct Map_info *Map, int line, int *type)
  34. {
  35. P_LINE *Line;
  36. G_debug(3, "Vect_read_line_geos(): line = %d", line);
  37. if (!VECT_OPEN(Map))
  38. G_fatal_error("Vect_read_line_geos(): %s", _("vector map is not opened"));
  39. if (line < 1 || line > Map->plus.n_lines)
  40. G_fatal_error(_("Vect_read_line_geos(): feature id %d is not reasonable "
  41. "(max features in vector map <%s>: %d)"),
  42. line, Vect_get_full_name(Map), Map->plus.n_lines);
  43. if (Map->format != GV_FORMAT_NATIVE)
  44. G_fatal_error("Vect_read_line_geos(): %s", _("only native format supported"));
  45. Line = Map->plus.Line[line];
  46. if (Line == NULL)
  47. G_fatal_error("Vect_read_line_geos(): %s %d",
  48. _("Attempt to read dead line"), line);
  49. return Vect__read_line_geos(Map, Line->offset, type);
  50. }
  51. /*!
  52. \brief Read vector area and stores it as GEOSGeometry instance (polygon)
  53. You should free allocated memory by GEOSGeom_destroy().
  54. \param Map pointer to Map_info structure
  55. \param area area id
  56. \return pointer to GEOSGeometry instance
  57. \return NULL on error
  58. */
  59. GEOSGeometry *Vect_read_area_geos(struct Map_info * Map, int area)
  60. {
  61. int i, nholes, isle;
  62. GEOSGeometry *boundary, **holes;
  63. G_debug(3, "Vect_read_area_geos(): area = %d", area);
  64. boundary = GEOSGeom_createLinearRing(Vect_get_area_points_geos(Map, area));
  65. if (!boundary) {
  66. G_fatal_error(_("Vect_read_area_geos(): unable to read area id %d"),
  67. area);
  68. }
  69. nholes = Vect_get_area_num_isles(Map, area);
  70. holes = (GEOSGeometry **) G_malloc(nholes * sizeof(GEOSGeometry *));
  71. for (i = 0; i < nholes; i++) {
  72. isle = Vect_get_area_isle(Map, area, i);
  73. if (isle < 1)
  74. continue;
  75. holes[i] = GEOSGeom_createLinearRing(Vect_get_isle_points_geos(Map, isle));
  76. if (!(holes[i]))
  77. G_fatal_error(_("Vect_read_area_geos(): unable to read isle id %d of area id %d"),
  78. isle, area);
  79. }
  80. return GEOSGeom_createPolygon(boundary, holes, nholes);
  81. }
  82. /*!
  83. \brief Create GEOSGeometry of given type from feature points.
  84. Supported types:
  85. - GV_POINT -> POINT
  86. - GV_LINE -> LINESTRING
  87. - GV_BOUNDARY -> LINEARRING
  88. You should free allocated memory by GEOSGeom_destroy().
  89. \param Map pointer to Map_info structure
  90. \param type feature type (see supported types)
  91. \return pointer to GEOSGeometry instance
  92. \return NULL on error
  93. */
  94. GEOSGeometry *Vect_line_to_geos(struct Map_info *Map,
  95. const struct line_pnts *points, int type)
  96. {
  97. int i, with_z;
  98. GEOSGeometry *geom;
  99. GEOSCoordSequence *pseq;
  100. G_debug(3, "Vect_line_to_geos(): type = %d", type);
  101. with_z = Vect_is_3d(Map);
  102. /* read only points / lines / boundaries */
  103. if (!(type & (GV_POINT | GV_LINES)))
  104. return NULL;
  105. if (type == GV_POINT) {
  106. if (points->n_points != 1)
  107. /* point is not valid */
  108. return NULL;
  109. }
  110. else {
  111. if (points->n_points < 2)
  112. /* line/boundary is not valid */
  113. return NULL;
  114. }
  115. pseq = GEOSCoordSeq_create(points->n_points, with_z ? 3 : 2);
  116. for (i = 0; i < points->n_points; i++) {
  117. GEOSCoordSeq_setX(pseq, i, points->x[i]);
  118. GEOSCoordSeq_setY(pseq, i, points->y[i]);
  119. if (with_z)
  120. GEOSCoordSeq_setZ(pseq, i, points->z[i]);
  121. }
  122. if (type == GV_POINT)
  123. geom = GEOSGeom_createPoint(pseq);
  124. else if (type == GV_LINE)
  125. geom = GEOSGeom_createLineString(pseq);
  126. else { /* boundary */
  127. geom = GEOSGeom_createLineString(pseq);
  128. if (GEOSisRing(geom)) {
  129. /* GEOSGeom_destroy(geom); */
  130. geom = GEOSGeom_createLinearRing(pseq);
  131. }
  132. }
  133. /* GEOSCoordSeq_destroy(pseq); */
  134. return geom;
  135. }
  136. /*!
  137. \brief Read line from coor file
  138. You should free allocated memory by GEOSGeom_destroy().
  139. \param Map pointer to Map_info
  140. \param offset line offset
  141. \param[out] type feature type or NULL
  142. \return pointer to GEOSGeometry
  143. \return NULL on error
  144. \return NULL dead line
  145. \return NULL end of file
  146. */
  147. GEOSGeometry *Vect__read_line_geos(struct Map_info *Map, long offset, int *type)
  148. {
  149. int ftype;
  150. double *x, *y, *z;
  151. GEOSGeometry *geom;
  152. GEOSCoordSequence *pseq;
  153. pseq = V1_read_line_geos(Map, offset, &ftype);
  154. if (!pseq)
  155. G_fatal_error(_("Unable to read line offset %ld"), offset);
  156. if (ftype & GV_POINT) {
  157. G_debug(3, " geos_type = point");
  158. geom = GEOSGeom_createPoint(pseq);
  159. }
  160. else if (ftype & GV_LINE) {
  161. G_debug(3, " geos_type = linestring");
  162. geom = GEOSGeom_createLineString(pseq);
  163. }
  164. else { /* boundary */
  165. geom = GEOSGeom_createLineString(pseq);
  166. if (GEOSisRing(geom)) {
  167. /* GEOSGeom_destroy(geom); */
  168. geom = GEOSGeom_createLinearRing(pseq);
  169. G_debug(3, " geos_type = linearring");
  170. }
  171. else {
  172. G_debug(3, " geos_type = linestring");
  173. }
  174. }
  175. G_free((void *) x);
  176. G_free((void *) y);
  177. if (z)
  178. G_free((void *) z);
  179. /* GEOSCoordSeq_destroy(pseq); */
  180. if (type)
  181. *type = ftype;
  182. return geom;
  183. }
  184. /*!
  185. \brief Read line from coor file into GEOSCoordSequence
  186. You should free allocated memory by GEOSCoordSeq_destroy().
  187. \param Map pointer to Map_info
  188. \param line line id
  189. \return pointer to GEOSCoordSequence
  190. \return empty GEOSCoordSequence for dead line or unsuppored feature type
  191. \return NULL end of file
  192. */
  193. GEOSCoordSequence *V2_read_line_geos(struct Map_info *Map, int line)
  194. {
  195. int ftype;
  196. P_LINE *Line;
  197. G_debug(3, "V2_read_line_geos(): line = %d", line);
  198. Line = Map->plus.Line[line];
  199. if (Line == NULL)
  200. G_fatal_error("V2_read_line_geos(): %s %d",
  201. _("Attempt to read dead line"), line);
  202. return V1_read_line_geos(Map, Line->offset, &ftype);
  203. }
  204. /*!
  205. \brief Read feature from coor file into GEOSCoordSequence
  206. Note: Function reads only points, lines and boundaries, other
  207. feature types are ignored (empty coord array is returned)!
  208. You should free allocated memory by GEOSCoordSeq_destroy().
  209. \param Map pointer to Map_info
  210. \param offset line offset
  211. \param[out] type feature type
  212. \return pointer to GEOSCoordSequence
  213. \return empty GEOSCoordSequence for dead line or unsuppored feature type
  214. \return NULL end of file
  215. */
  216. GEOSCoordSequence *V1_read_line_geos(struct Map_info *Map, long offset, int *type)
  217. {
  218. int i, n_points;
  219. char rhead;
  220. double *x, *y, *z;
  221. GEOSCoordSequence *pseq;
  222. G_debug(3, "V1_read_line_geos(): offset = %ld", offset);
  223. Map->head.last_offset = offset;
  224. /* reads must set in_head, but writes use default */
  225. dig_set_cur_port(&(Map->head.port));
  226. dig_fseek(&(Map->dig_fp), offset, 0);
  227. if (0 >= dig__fread_port_C(&rhead, 1, &(Map->dig_fp)))
  228. return NULL; /* end of file */
  229. if (!(rhead & 0x01)) /* dead line */
  230. return GEOSCoordSeq_create(0, (Map->head.with_z) ? 3 : 2);
  231. rhead >>= 2;
  232. *type = dig_type_from_store((int) rhead);
  233. /* read only points / lines / boundaries */
  234. if (!(*type & (GV_POINT | GV_LINES)))
  235. return GEOSCoordSeq_create(0, (Map->head.with_z) ? 3 : 2);
  236. if (*type & GV_POINTS) {
  237. n_points = 1;
  238. }
  239. else {
  240. if (0 >= dig__fread_port_I(&n_points, 1, &(Map->dig_fp)))
  241. return NULL;
  242. }
  243. G_debug(3, " n_points = %d dim = %d", n_points, (Map->head.with_z) ? 3 : 2);
  244. pseq = GEOSCoordSeq_create(n_points, (Map->head.with_z) ? 3 : 2);
  245. x = (double *) G_malloc(n_points * sizeof(double));
  246. y = (double *) G_malloc(n_points * sizeof(double));
  247. if (Map->head.with_z)
  248. z = (double *) G_malloc(n_points * sizeof(double));
  249. else
  250. z = NULL;
  251. if (0 >= dig__fread_port_D(x, n_points, &(Map->dig_fp)))
  252. return NULL; /* end of file */
  253. if (0 >= dig__fread_port_D(y, n_points, &(Map->dig_fp)))
  254. return NULL; /* end of file */
  255. if (Map->head.with_z) {
  256. if (0 >= dig__fread_port_D(z, n_points, &(Map->dig_fp)))
  257. return NULL; /* end of file */
  258. }
  259. for (i = 0; i < n_points; i++) {
  260. GEOSCoordSeq_setX(pseq, i, x[i]);
  261. GEOSCoordSeq_setY(pseq, i, y[i]);
  262. if (Map->head.with_z)
  263. GEOSCoordSeq_setZ(pseq, i, z[i]);
  264. }
  265. G_debug(3, " off = %ld", dig_ftell(&(Map->dig_fp)));
  266. G_free((void *) x);
  267. G_free((void *) y);
  268. if (z)
  269. G_free((void *) z);
  270. return pseq;
  271. }
  272. /*!
  273. \brief Returns the polygon array of points, i.e. outer ring (shell)
  274. You should free allocated memory by GEOSCoordSeq_destroy().
  275. See also Vect_get_area_points().
  276. \param Map pointer to Map_info
  277. \param area area id
  278. \return pointer to GEOSCoordSequence
  279. \return empty GEOSCoordSequence for dead area
  280. \return NULL on error
  281. */
  282. GEOSCoordSequence *Vect_get_area_points_geos(struct Map_info *Map, int area)
  283. {
  284. int i, j, k;
  285. int line, aline;
  286. unsigned int n_points, n_points_shell;
  287. double x, y, z;
  288. struct Plus_head *Plus;
  289. P_AREA *Area;
  290. GEOSCoordSequence **pseq, *pseq_shell;
  291. G_debug(3, "Vect_get_area_points_geos(): area = %d", area);
  292. Plus = &(Map->plus);
  293. Area = Plus->Area[area];
  294. if (Area == NULL) { /* dead area */
  295. G_warning(_("Attempt to read points of nonexistent area id %d"), area);
  296. return NULL; /* error , because we should not read dead areas */
  297. }
  298. G_debug(3, " n_lines = %d", Area->n_lines);
  299. pseq = (GEOSCoordSequence **) G_malloc(Area->n_lines * sizeof(GEOSCoordSequence *));
  300. n_points_shell = 0;
  301. for (i = 0; i < Area->n_lines; i++) {
  302. line = Area->lines[i];
  303. aline = abs(line);
  304. G_debug(3, " append line(%d) = %d", i, line);
  305. /*
  306. TODO
  307. if (line > 0)
  308. dir = GV_FORWARD;
  309. else
  310. dir = GV_BACKWARD;
  311. */
  312. pseq[i] = V2_read_line_geos(Map, aline);
  313. if (!(pseq[i])) {
  314. G_fatal_error(_("Unable to read feature id %d"), aline);
  315. }
  316. GEOSCoordSeq_getSize(pseq[i], &n_points);
  317. G_debug(3, " line n_points = %d", n_points);
  318. n_points_shell += n_points;
  319. }
  320. /* create shell (outer ring) */
  321. pseq_shell = GEOSCoordSeq_create(n_points_shell, Map->head.with_z ? 3 : 2);
  322. k = 0;
  323. for (i = 0; i < Area->n_lines; i++) {
  324. GEOSCoordSeq_getSize(pseq[i], &n_points);
  325. for (j = 0; j < (int) n_points; j++, k++) {
  326. GEOSCoordSeq_getX(pseq[i], j, &x);
  327. GEOSCoordSeq_setX(pseq_shell, k, x);
  328. GEOSCoordSeq_getY(pseq[i], j, &y);
  329. GEOSCoordSeq_setY(pseq_shell, k, y);
  330. if (Map->head.with_z) {
  331. GEOSCoordSeq_getY(pseq[i], j, &z);
  332. GEOSCoordSeq_setZ(pseq_shell, k, z);
  333. }
  334. }
  335. }
  336. G_free((void *) pseq);
  337. return pseq_shell;
  338. }
  339. /*!
  340. \brief Returns the polygon (isle) array of points (inner ring)
  341. You should free allocated memory by GEOSCoordSeq_destroy().
  342. See also Vect_get_isle_points().
  343. \param Map pointer to Map_info
  344. \param isle isel id
  345. \return pointer to GEOSGeometry
  346. \return NULL on error or dead line
  347. */
  348. GEOSCoordSequence *Vect_get_isle_points_geos(struct Map_info *Map, int isle)
  349. {
  350. int i, j, k;
  351. int line, aline;
  352. unsigned n_points, n_points_shell;
  353. double x, y, z;
  354. struct Plus_head *Plus;
  355. P_ISLE *Isle;
  356. GEOSCoordSequence **pseq, *pseq_shell;
  357. G_debug(3, "Vect_get_isle_points_geos(): isle = %d", isle);
  358. Plus = &(Map->plus);
  359. Isle = Plus->Isle[isle];
  360. G_debug(3, " n_lines = %d", Isle->n_lines);
  361. for (i = 0; i < Isle->n_lines; i++) {
  362. line = Isle->lines[i];
  363. aline = abs(line);
  364. G_debug(3, " append line(%d) = %d", i, line);
  365. pseq[i] = V2_read_line_geos(Map, aline);
  366. if (!(pseq[i])) {
  367. G_fatal_error(_("Unable to read feature id %d"), aline);
  368. }
  369. GEOSCoordSeq_getSize(pseq[i], &n_points);
  370. G_debug(3, " line n_points = %d", n_points);
  371. n_points_shell += n_points;
  372. /*
  373. TODO
  374. if (line > 0)
  375. dir = GV_FORWARD;
  376. else
  377. dir = GV_BACKWARD;
  378. */
  379. }
  380. /* create shell (outer ring) */
  381. pseq_shell = GEOSCoordSeq_create(n_points_shell, Map->head.with_z ? 3 : 2);
  382. k = 0;
  383. for (i = 0; i < Isle->n_lines; i++) {
  384. GEOSCoordSeq_getSize(pseq[i], &n_points);
  385. for (j = 0; j < (int) n_points; j++, k++) {
  386. GEOSCoordSeq_getX(pseq[i], j, &x);
  387. GEOSCoordSeq_setX(pseq_shell, k, x);
  388. GEOSCoordSeq_getY(pseq[i], j, &y);
  389. GEOSCoordSeq_setY(pseq_shell, k, y);
  390. if (Map->head.with_z) {
  391. GEOSCoordSeq_getY(pseq[i], j, &z);
  392. GEOSCoordSeq_setZ(pseq_shell, k, z);
  393. }
  394. }
  395. }
  396. G_free((void *) pseq);
  397. return pseq_shell;
  398. }
  399. #endif /* HAVE_GEOS */