geos.c 13 KB

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