GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
area_pg.c
Go to the documentation of this file.
1/*!
2 \file lib/vector/Vlib/area_pg.c
3
4 \brief Vector library - area-related functions (PostGIS Topology)
5
6 Higher level functions for reading/writing/manipulating vectors.
7
8 SPDX-FileCopyrightText: 2013 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Martin Landa <landa.martin gmail.com>
12 */
13
14#include <grass/vector.h>
15
16#ifdef HAVE_POSTGRES
17#include "pg_local_proto.h"
18
19static PGresult *build_stmt(const struct Plus_head *,
20 const struct Format_info_pg *, const plus_t *, int);
21
22/*!
23 \brief Get area boundary points (PostGIS Topology)
24
25 Used by Vect_build_line_area() and Vect_get_area_points().
26
27 \param Map pointer to Map_info struct
28 \param lines array of boundary lines
29 \param n_lines number of lines in array
30 \param[out] APoints pointer to output line_pnts struct
31
32 \return number of points
33 \return -1 on error
34 */
35int Vect__get_area_points_pg(struct Map_info *Map, const plus_t *lines,
36 int n_lines, struct line_pnts *APoints)
37{
38 int i, direction;
39
40 struct Format_info_pg *pg_info;
41
43
44 pg_info = (struct Format_info_pg *)&(Map->fInfo.pg);
45
47
48 res = build_stmt(&(Map->plus), pg_info, lines, n_lines);
49 if (!res)
50 return -1;
51
52 for (i = 0; i < n_lines; i++) {
54 &(pg_info->cache),
55 NULL); /* do caching in readable way */
56 direction = lines[i] > 0 ? GV_FORWARD : GV_BACKWARD;
58 APoints->n_points--; /* skip last point, avoids duplicates */
59 }
60 APoints->n_points++; /* close polygon */
61
62 PQclear(res);
63
64 return APoints->n_points;
65}
66
67PGresult *build_stmt(const struct Plus_head *plus,
68 const struct Format_info_pg *pg_info, const plus_t *lines,
69 int n_lines)
70{
71 int i, line;
72 size_t stmt_id_size;
73 char *stmt, *stmt_id, buf_id[128];
74
75 struct P_line *BLine;
76
77 PGresult *res;
78
79 stmt = NULL;
81 stmt_id = (char *)G_malloc(stmt_id_size);
82 stmt_id[0] = '\0';
83
84 for (i = 0; i < n_lines; i++) {
85 if (strlen(stmt_id) + 100 > stmt_id_size) {
88 }
89 line = abs(lines[i]);
90 BLine = plus->Line[line];
91 if (i > 0)
92 strcat(stmt_id, ",");
93 snprintf(buf_id, sizeof(buf_id), "%d", (int)BLine->offset);
94 strcat(stmt_id, buf_id);
95 }
96 /* Not really working - why?
97 G_asprintf(&stmt, "SELECT geom FROM \"%s\".edge_data WHERE edge_id IN
98 (%s) " "ORDER BY POSITION(edge_id::text in '%s')",
99 pg_info->toposchema_name, stmt_id, stmt_id);
100 */
101 G_asprintf(&stmt,
102 "SELECT geom FROM \"%s\".edge_data AS t "
103 "JOIN (SELECT id, row_number() over() AS id_sorter FROM "
104 "(SELECT UNNEST(ARRAY[%s]) AS id) AS y) x ON "
105 "t.edge_id in (%s) AND x.id = t.edge_id "
106 "ORDER BY x.id_sorter",
107 pg_info->toposchema_name, stmt_id, stmt_id);
109
110 G_debug(2, "SQL: %s", stmt);
111 res = PQexec(pg_info->conn, stmt);
112 G_free(stmt);
113
114 if (!res || PQresultStatus(res) != PGRES_TUPLES_OK ||
115 PQntuples(res) != n_lines) {
116 if (res)
117 PQclear(res);
118
119 return NULL;
120 }
121
122 return res;
123}
124#endif
int Vect__get_area_points_pg(struct Map_info *Map, const plus_t *lines, int n_lines, struct line_pnts *APoints)
Get area boundary points (PostGIS Topology)
Definition area_pg.c:35
#define NULL
Definition ccmath.h:32
#define DB_SQL_MAX
Definition dbmi.h:140
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_realloc(p, n)
Definition defs/gis.h:138
#define G_malloc(n)
Definition defs/gis.h:136
int G_asprintf(char **, const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
void Vect_reset_line(struct line_pnts *)
Reset line.
Definition line.c:127
int Vect_append_points(struct line_pnts *, const struct line_pnts *, int)
Appends points to the end of a line.
Definition line.c:333
#define GV_FORWARD
Line direction indicator forward/backward.
#define GV_BACKWARD
int plus_t
plus_t size
Definition dig_structs.h:39
#define FALSE
Definition gis.h:79
SF_FeatureType Vect__cache_feature_pg(const char *data, int skip_polygon, int force_type, struct Format_info_cache *cache, struct feat_parts *fparts)
Read geometry from HEX data.
Definition read_pg.c:777
Non-native format info (PostGIS)
PGresult * res
Vector map info.
Vector geometry.
Basic topology-related info.
struct P_line ** Line
Array of vector geometries.
Feature geometry info - coordinates.