16 #include <grass/config.h>
17 #include <grass/gis.h>
18 #include <grass/Vect.h>
19 #include <grass/glocale.h>
23 static GEOSGeometry *Vect__read_line_geos(
struct Map_info *,
long,
int *);
24 static GEOSCoordSequence *V1_read_line_geos(
struct Map_info *,
long,
int *);
25 static GEOSCoordSequence *V2_read_line_geos(
struct Map_info *,
int);
26 static GEOSCoordSequence *read_polygon_points(
struct Map_info *,
int,
int*);
50 G_debug(3,
"Vect_read_line_geos(): line = %d", line);
53 G_fatal_error(
"Vect_read_line_geos(): %s", _(
"vector map is not opened"));
55 if (line < 1 || line > Map->plus.n_lines)
56 G_fatal_error(_(
"Vect_read_line_geos(): feature id %d is not reasonable "
57 "(max features in vector map <%s>: %d)"),
60 if (Map->format != GV_FORMAT_NATIVE)
61 G_fatal_error(
"Vect_read_line_geos(): %s", _(
"only native format supported"));
63 Line = Map->plus.Line[line];
66 _(
"Attempt to read dead line"), line);
68 return Vect__read_line_geos(Map, Line->offset, type);
85 GEOSGeometry *boundary, **holes;
87 G_debug(3,
"Vect_read_area_geos(): area = %d", area);
91 G_fatal_error(_(
"Vect_read_area_geos(): unable to read area id %d"),
96 holes = (GEOSGeometry **) G_malloc(nholes *
sizeof(GEOSGeometry *));
97 for (i = 0; i < nholes; i++) {
105 G_fatal_error(_(
"Vect_read_area_geos(): unable to read isle id %d of area id %d"),
109 return GEOSGeom_createPolygon(boundary, holes, nholes);
129 const struct line_pnts *points,
int type)
133 GEOSCoordSequence *pseq;
135 G_debug(3,
"Vect_line_to_geos(): type = %d", type);
140 if (!(type & (GV_POINT |
GV_LINES)))
143 if (type == GV_POINT) {
144 if (points->n_points != 1)
149 if (points->n_points < 2)
154 pseq = GEOSCoordSeq_create(points->n_points, with_z ? 3 : 2);
156 for (i = 0; i < points->n_points; i++) {
157 GEOSCoordSeq_setX(pseq, i, points->x[i]);
158 GEOSCoordSeq_setY(pseq, i, points->y[i]);
160 GEOSCoordSeq_setZ(pseq, i, points->z[i]);
163 if (type == GV_POINT)
164 geom = GEOSGeom_createPoint(pseq);
165 else if (type == GV_LINE)
166 geom = GEOSGeom_createLineString(pseq);
168 geom = GEOSGeom_createLineString(pseq);
169 if (GEOSisRing(geom)) {
171 geom = GEOSGeom_createLinearRing(pseq);
194 GEOSGeometry *Vect__read_line_geos(
struct Map_info *
Map,
long offset,
int *
type)
199 GEOSCoordSequence *pseq;
201 pseq = V1_read_line_geos(Map, offset, &ftype);
205 if (ftype & GV_POINT) {
206 G_debug(3,
" geos_type = point");
207 geom = GEOSGeom_createPoint(pseq);
209 else if (ftype & GV_LINE) {
210 G_debug(3,
" geos_type = linestring");
211 geom = GEOSGeom_createLineString(pseq);
214 geom = GEOSGeom_createLineString(pseq);
215 if (GEOSisRing(geom)) {
217 geom = GEOSGeom_createLinearRing(pseq);
218 G_debug(3,
" geos_type = linearring");
221 G_debug(3,
" geos_type = linestring");
245 GEOSCoordSequence *V2_read_line_geos(
struct Map_info *Map,
int line)
250 G_debug(3,
"V2_read_line_geos(): line = %d", line);
252 Line = Map->plus.Line[line];
256 _(
"Attempt to read dead line"), line);
258 return V1_read_line_geos(Map, Line->offset, &ftype);
278 GEOSCoordSequence *V1_read_line_geos(
struct Map_info *Map,
long offset,
int *type)
286 GEOSCoordSequence *pseq;
288 G_debug(3,
"V1_read_line_geos(): offset = %ld", offset);
290 Map->head.last_offset = offset;
301 return GEOSCoordSeq_create(0, (Map->head.with_z) ? 3 : 2);
312 if (!(*type & (GV_POINT |
GV_LINES)))
313 return GEOSCoordSeq_create(0, (Map->head.with_z) ? 3 : 2);
317 if (Map->head.Version_Minor == 1) {
326 G_debug(3,
" n_cats = %d", n_cats);
328 if (Map->head.Version_Minor == 1) {
329 size = (2 * PORT_INT) * n_cats;
332 size = (PORT_SHORT + PORT_INT) * n_cats;
334 dig_fseek(&(Map->dig_fp), size, SEEK_CUR);
337 if (*type & GV_POINTS) {
345 G_debug(3,
" n_points = %d dim = %d", n_points, (Map->head.with_z) ? 3 : 2);
347 pseq = GEOSCoordSeq_create(n_points, (Map->head.with_z) ? 3 : 2);
349 x = (
double *) G_malloc(n_points *
sizeof(
double));
350 y = (
double *) G_malloc(n_points *
sizeof(
double));
351 if (Map->head.with_z)
352 z = (
double *) G_malloc(n_points *
sizeof(
double));
362 if (Map->head.with_z) {
368 for (i = 0; i < n_points; i++) {
369 GEOSCoordSeq_setX(pseq, i, x[i]);
370 GEOSCoordSeq_setY(pseq, i, y[i]);
371 if (Map->head.with_z)
372 GEOSCoordSeq_setZ(pseq, i, z[i]);
401 struct Plus_head *Plus;
404 G_debug(3,
"Vect_get_area_points_geos(): area = %d", area);
407 Area = Plus->Area[area];
410 G_warning(_(
"Attempt to read points of nonexistent area id %d"), area);
414 return read_polygon_points(Map, Area->n_lines, Area->lines);
432 struct Plus_head *Plus;
435 G_debug(3,
"Vect_get_isle_points_geos(): isle = %d", isle);
438 Isle = Plus->Isle[isle];
440 return read_polygon_points(Map, Isle->n_lines, Isle->lines);
443 GEOSCoordSequence *read_polygon_points(
struct Map_info *Map,
int n_lines,
int *lines)
447 unsigned int n_points, n_points_shell;
451 GEOSCoordSequence **pseq, *pseq_shell;
453 G_debug(3,
" n_lines = %d", n_lines);
454 pseq = (GEOSCoordSequence **) G_malloc(n_lines *
sizeof(GEOSCoordSequence *));
455 dir = (
int*) G_malloc(n_lines *
sizeof(
int));
458 for (i = 0; i < n_lines; i++) {
461 G_debug(3,
" append line(%d) = %d", i, line);
466 dir[i] = GV_BACKWARD;
468 pseq[i] = V2_read_line_geos(Map, aline);
473 GEOSCoordSeq_getSize(pseq[i], &n_points);
474 G_debug(3,
" line n_points = %d", n_points);
475 n_points_shell += n_points;
479 pseq_shell = GEOSCoordSeq_create(n_points_shell, Map->head.with_z ? 3 : 2);
481 for (i = 0; i < n_lines; i++) {
482 GEOSCoordSeq_getSize(pseq[i], &n_points);
483 if (dir[i] == GV_FORWARD) {
484 for (j = 0; j < (
int) n_points; j++, k++) {
485 GEOSCoordSeq_getX(pseq[i], j, &x);
486 GEOSCoordSeq_setX(pseq_shell, k, x);
488 GEOSCoordSeq_getY(pseq[i], j, &y);
489 GEOSCoordSeq_setY(pseq_shell, k, y);
491 if (Map->head.with_z) {
492 GEOSCoordSeq_getY(pseq[i], j, &z);
493 GEOSCoordSeq_setZ(pseq_shell, k, z);
498 for (j = (
int) n_points - 1; j > -1; j--, k++) {
499 GEOSCoordSeq_getX(pseq[i], j, &x);
500 GEOSCoordSeq_setX(pseq_shell, k, x);
502 GEOSCoordSeq_getY(pseq[i], j, &y);
503 GEOSCoordSeq_setY(pseq_shell, k, y);
505 if (Map->head.with_z) {
506 GEOSCoordSeq_getY(pseq[i], j, &z);
507 GEOSCoordSeq_setZ(pseq_shell, k, z);
GEOSCoordSequence * Vect_get_area_points_geos(struct Map_info *Map, int area)
Returns the polygon array of points, i.e. outer ring (shell)
void G_free(void *buf)
Free allocated memory.
int dig_set_cur_port(struct Port_info *port)
GEOSCoordSequence * Vect_get_isle_points_geos(struct Map_info *Map, int isle)
Returns the polygon (isle) array of points (inner ring)
int Vect_get_area_num_isles(struct Map_info *Map, int area)
Returns number of isles for area.
int Vect_get_area_isle(struct Map_info *Map, int area, int isle)
Returns isle for area.
GEOSGeometry * Vect_read_line_geos(struct Map_info *Map, int line, int *type)
Read vector feature and stores it as GEOSGeometry instance.
int dig_fseek(GVFILE *file, long offset, int whence)
Set GVFILE position.
int dig_type_from_store(int stype)
Convert type from store type.
int dig__fread_port_D(double *buf, int cnt, GVFILE *fp)
GEOSGeometry * Vect_line_to_geos(struct Map_info *Map, const struct line_pnts *points, int type)
Create GEOSGeometry of given type from feature points.
int dig__fread_port_C(char *buf, int cnt, GVFILE *fp)
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_debug(int level, const char *msg,...)
Print debugging message.
GEOSGeometry * Vect_read_area_geos(struct Map_info *Map, int area)
Read vector area and stores it as GEOSGeometry instance (polygon)
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
long dig_ftell(GVFILE *file)
Get GVFILE position.
int dig__fread_port_I(int *buf, int cnt, GVFILE *fp)