geos.c 14 KB

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