geos.c 15 KB

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