geos.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 type feature type (see supported types)
  92. \return pointer to GEOSGeometry instance
  93. \return NULL on error
  94. */
  95. GEOSGeometry *Vect_line_to_geos(struct Map_info *Map,
  96. const struct line_pnts *points, int type)
  97. {
  98. int i, with_z;
  99. GEOSGeometry *geom;
  100. GEOSCoordSequence *pseq;
  101. G_debug(3, "Vect_line_to_geos(): type = %d", type);
  102. with_z = Vect_is_3d(Map);
  103. /* read only points / lines / boundaries */
  104. if (!(type & (GV_POINT | GV_LINES)))
  105. return NULL;
  106. if (type == GV_POINT) {
  107. if (points->n_points != 1)
  108. /* point is not valid */
  109. return NULL;
  110. }
  111. else {
  112. if (points->n_points < 2)
  113. /* line/boundary is not valid */
  114. return NULL;
  115. }
  116. pseq = GEOSCoordSeq_create(points->n_points, with_z ? 3 : 2);
  117. for (i = 0; i < points->n_points; i++) {
  118. GEOSCoordSeq_setX(pseq, i, points->x[i]);
  119. GEOSCoordSeq_setY(pseq, i, points->y[i]);
  120. if (with_z)
  121. GEOSCoordSeq_setZ(pseq, i, points->z[i]);
  122. }
  123. if (type == GV_POINT)
  124. geom = GEOSGeom_createPoint(pseq);
  125. else if (type == GV_LINE)
  126. geom = GEOSGeom_createLineString(pseq);
  127. else { /* boundary */
  128. geom = GEOSGeom_createLineString(pseq);
  129. if (GEOSisRing(geom)) {
  130. /* GEOSGeom_destroy(geom); */
  131. geom = GEOSGeom_createLinearRing(pseq);
  132. }
  133. }
  134. /* GEOSCoordSeq_destroy(pseq); */
  135. return geom;
  136. }
  137. /*!
  138. \brief Read line from coor file
  139. You should free allocated memory by GEOSGeom_destroy().
  140. \param Map pointer to Map_info
  141. \param offset line offset
  142. \param[out] type feature type or NULL
  143. \return pointer to GEOSGeometry
  144. \return NULL on error
  145. \return NULL dead line
  146. \return NULL end of file
  147. */
  148. GEOSGeometry *Vect__read_line_geos(struct Map_info *Map, long offset, int *type)
  149. {
  150. int ftype;
  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. /* GEOSCoordSeq_destroy(pseq); */
  176. if (type)
  177. *type = ftype;
  178. return geom;
  179. }
  180. /*!
  181. \brief Read line from coor file into GEOSCoordSequence
  182. You should free allocated memory by GEOSCoordSeq_destroy().
  183. \param Map pointer to Map_info
  184. \param line line id
  185. \return pointer to GEOSCoordSequence
  186. \return empty GEOSCoordSequence for dead line or unsuppored feature type
  187. \return NULL end of file
  188. */
  189. GEOSCoordSequence *V2_read_line_geos(struct Map_info *Map, int line)
  190. {
  191. int ftype;
  192. struct P_line *Line;
  193. G_debug(3, "V2_read_line_geos(): line = %d", line);
  194. Line = Map->plus.Line[line];
  195. if (Line == NULL)
  196. G_fatal_error("V2_read_line_geos(): %s %d",
  197. _("Attempt to read dead line"), line);
  198. return V1_read_line_geos(Map, Line->offset, &ftype);
  199. }
  200. /*!
  201. \brief Read feature from coor file into GEOSCoordSequence
  202. Note: Function reads only points, lines and boundaries, other
  203. feature types are ignored (empty coord array is returned)!
  204. You should free allocated memory by GEOSCoordSeq_destroy().
  205. \param Map pointer to Map_info
  206. \param offset line offset
  207. \param[out] type feature type
  208. \return pointer to GEOSCoordSequence
  209. \return empty GEOSCoordSequence for dead line or unsuppored feature type
  210. \return NULL end of file
  211. */
  212. GEOSCoordSequence *V1_read_line_geos(struct Map_info *Map, long offset, int *type)
  213. {
  214. int i, n_points;
  215. int do_cats, n_cats;
  216. char rhead, nc;
  217. long size;
  218. double *x, *y, *z;
  219. GEOSCoordSequence *pseq;
  220. G_debug(3, "V1_read_line_geos(): offset = %ld", offset);
  221. Map->head.last_offset = offset;
  222. /* reads must set in_head, but writes use default */
  223. dig_set_cur_port(&(Map->head.port));
  224. dig_fseek(&(Map->dig_fp), offset, 0);
  225. if (0 >= dig__fread_port_C(&rhead, 1, &(Map->dig_fp)))
  226. return NULL; /* end of file */
  227. if (!(rhead & 0x01)) /* dead line */
  228. return GEOSCoordSeq_create(0, (Map->head.with_z) ? 3 : 2);
  229. if (rhead & 0x02) /* categories exists */
  230. do_cats = 1; /* do not return here let file offset moves forward to next */
  231. else /* line */
  232. do_cats = 0;
  233. rhead >>= 2;
  234. *type = dig_type_from_store((int) rhead);
  235. /* read only points / lines / boundaries */
  236. if (!(*type & (GV_POINT | GV_LINES)))
  237. return GEOSCoordSeq_create(0, (Map->head.with_z) ? 3 : 2);
  238. /* skip categories */
  239. if (do_cats) {
  240. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  241. if (0 >= dig__fread_port_I(&n_cats, 1, &(Map->dig_fp)))
  242. return NULL;
  243. }
  244. else { /* coor format 5.0 */
  245. if (0 >= dig__fread_port_C(&nc, 1, &(Map->dig_fp)))
  246. return NULL;
  247. n_cats = (int) nc;
  248. }
  249. G_debug(3, " n_cats = %d", n_cats);
  250. if (Map->head.Version_Minor == 1) { /* coor format 5.1 */
  251. size = (2 * PORT_INT) * n_cats;
  252. }
  253. else { /* coor format 5.0 */
  254. size = (PORT_SHORT + PORT_INT) * n_cats;
  255. }
  256. dig_fseek(&(Map->dig_fp), size, SEEK_CUR);
  257. }
  258. if (*type & GV_POINTS) {
  259. n_points = 1;
  260. }
  261. else {
  262. if (0 >= dig__fread_port_I(&n_points, 1, &(Map->dig_fp)))
  263. return NULL;
  264. }
  265. G_debug(3, " n_points = %d dim = %d", n_points, (Map->head.with_z) ? 3 : 2);
  266. pseq = GEOSCoordSeq_create(n_points, (Map->head.with_z) ? 3 : 2);
  267. x = (double *) G_malloc(n_points * sizeof(double));
  268. y = (double *) G_malloc(n_points * sizeof(double));
  269. if (Map->head.with_z)
  270. z = (double *) G_malloc(n_points * sizeof(double));
  271. else
  272. z = NULL;
  273. if (0 >= dig__fread_port_D(x, n_points, &(Map->dig_fp)))
  274. return NULL; /* end of file */
  275. if (0 >= dig__fread_port_D(y, n_points, &(Map->dig_fp)))
  276. return NULL; /* end of file */
  277. if (Map->head.with_z) {
  278. if (0 >= dig__fread_port_D(z, n_points, &(Map->dig_fp)))
  279. return NULL; /* end of file */
  280. }
  281. for (i = 0; i < n_points; i++) {
  282. GEOSCoordSeq_setX(pseq, i, x[i]);
  283. GEOSCoordSeq_setY(pseq, i, y[i]);
  284. if (Map->head.with_z)
  285. GEOSCoordSeq_setZ(pseq, i, z[i]);
  286. }
  287. G_debug(3, " off = %ld", (long) dig_ftell(&(Map->dig_fp)));
  288. G_free((void *) x);
  289. G_free((void *) y);
  290. if (z)
  291. G_free((void *) z);
  292. return pseq;
  293. }
  294. /*!
  295. \brief Returns the polygon array of points, i.e. outer ring (shell)
  296. You should free allocated memory by GEOSCoordSeq_destroy().
  297. See also Vect_get_area_points().
  298. \param Map pointer to Map_info
  299. \param area area id
  300. \return pointer to GEOSCoordSequence
  301. \return empty GEOSCoordSequence for dead area
  302. \return NULL on error
  303. */
  304. GEOSCoordSequence *Vect_get_area_points_geos(struct Map_info *Map, int area)
  305. {
  306. struct Plus_head *Plus;
  307. struct P_area *Area;
  308. G_debug(3, "Vect_get_area_points_geos(): area = %d", area);
  309. Plus = &(Map->plus);
  310. Area = Plus->Area[area];
  311. if (Area == NULL) { /* dead area */
  312. G_warning(_("Attempt to read points of nonexistent area id %d"), area);
  313. return NULL; /* error , because we should not read dead areas */
  314. }
  315. return read_polygon_points(Map, Area->n_lines, Area->lines);
  316. }
  317. /*!
  318. \brief Returns the polygon (isle) array of points (inner ring)
  319. You should free allocated memory by GEOSCoordSeq_destroy().
  320. See also Vect_get_isle_points().
  321. \param Map pointer to Map_info
  322. \param isle isel id
  323. \return pointer to GEOSGeometry
  324. \return NULL on error or dead line
  325. */
  326. GEOSCoordSequence *Vect_get_isle_points_geos(struct Map_info *Map, int isle)
  327. {
  328. struct Plus_head *Plus;
  329. struct P_isle *Isle;
  330. G_debug(3, "Vect_get_isle_points_geos(): isle = %d", isle);
  331. Plus = &(Map->plus);
  332. Isle = Plus->Isle[isle];
  333. return read_polygon_points(Map, Isle->n_lines, Isle->lines);
  334. }
  335. GEOSCoordSequence *read_polygon_points(struct Map_info *Map, int n_lines, int *lines)
  336. {
  337. int i, j, k;
  338. int line, aline;
  339. unsigned int n_points, n_points_shell;
  340. double x, y, z;
  341. int *dir;
  342. GEOSCoordSequence **pseq, *pseq_shell;
  343. G_debug(3, " n_lines = %d", n_lines);
  344. pseq = (GEOSCoordSequence **) G_malloc(n_lines * sizeof(GEOSCoordSequence *));
  345. dir = (int*) G_malloc(n_lines * sizeof(int));
  346. n_points_shell = 0;
  347. for (i = 0; i < n_lines; i++) {
  348. line = lines[i];
  349. aline = abs(line);
  350. G_debug(3, " append line(%d) = %d", i, line);
  351. if (line > 0)
  352. dir[i] = GV_FORWARD;
  353. else
  354. dir[i] = GV_BACKWARD;
  355. pseq[i] = V2_read_line_geos(Map, aline);
  356. if (!(pseq[i])) {
  357. G_fatal_error(_("Unable to read feature id %d"), aline);
  358. }
  359. GEOSCoordSeq_getSize(pseq[i], &n_points);
  360. G_debug(3, " line n_points = %d", n_points);
  361. n_points_shell += n_points;
  362. }
  363. /* create shell (outer ring) */
  364. pseq_shell = GEOSCoordSeq_create(n_points_shell, Map->head.with_z ? 3 : 2);
  365. k = 0;
  366. for (i = 0; i < n_lines; i++) {
  367. GEOSCoordSeq_getSize(pseq[i], &n_points);
  368. if (dir[i] == GV_FORWARD) {
  369. for (j = 0; j < (int) n_points; j++, k++) {
  370. GEOSCoordSeq_getX(pseq[i], j, &x);
  371. GEOSCoordSeq_setX(pseq_shell, k, x);
  372. GEOSCoordSeq_getY(pseq[i], j, &y);
  373. GEOSCoordSeq_setY(pseq_shell, k, y);
  374. if (Map->head.with_z) {
  375. GEOSCoordSeq_getY(pseq[i], j, &z);
  376. GEOSCoordSeq_setZ(pseq_shell, k, z);
  377. }
  378. }
  379. }
  380. else { /* GV_BACKWARD */
  381. for (j = (int) n_points - 1; j > -1; j--, k++) {
  382. GEOSCoordSeq_getX(pseq[i], j, &x);
  383. GEOSCoordSeq_setX(pseq_shell, k, x);
  384. GEOSCoordSeq_getY(pseq[i], j, &y);
  385. GEOSCoordSeq_setY(pseq_shell, k, y);
  386. if (Map->head.with_z) {
  387. GEOSCoordSeq_getY(pseq[i], j, &z);
  388. GEOSCoordSeq_setZ(pseq_shell, k, z);
  389. }
  390. }
  391. }
  392. }
  393. G_free((void *) pseq);
  394. G_free((void *) dir);
  395. return pseq_shell;
  396. }
  397. #endif /* HAVE_GEOS */