geos.c 14 KB

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