GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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  (C) 2013 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public License
11  (>=v2). Read the file COPYING that comes with GRASS for details.
12 
13  \author Martin Landa <landa.martin gmail.com>
14 */
15 
16 #include <grass/vector.h>
17 
18 #ifdef HAVE_POSTGRES
19 #include "pg_local_proto.h"
20 
21 static PGresult *build_stmt(const struct Plus_head *, const struct Format_info_pg *, const plus_t *, int);
22 
23 /*!
24  \brief Get area boundary points (PostGIS Topology)
25 
26  Used by Vect_build_line_area() and Vect_get_area_points().
27 
28  \param Map pointer to Map_info struct
29  \param lines array of boundary lines
30  \param n_lines number of lines in array
31  \param[out] APoints pointer to output line_pnts struct
32 
33  \return number of points
34  \return -1 on error
35 */
36 int Vect__get_area_points_pg(const struct Map_info *Map, const plus_t *lines, int n_lines,
37  struct line_pnts *APoints)
38 {
39  int i, direction;
40 
41  struct Format_info_pg *pg_info;
42 
43  PGresult *res;
44 
45  pg_info = (struct Format_info_pg *)&(Map->fInfo.pg);
46 
47  Vect_reset_line(APoints);
48 
49  res = build_stmt(&(Map->plus), pg_info, lines, n_lines);
50  if (!res)
51  return -1;
52 
53  for (i = 0; i < n_lines; i++) {
54  Vect__cache_feature_pg(PQgetvalue(res, i, 0), FALSE, FALSE,
55  &(pg_info->cache), NULL); /* do caching in readable way */
56  direction = lines[i] > 0 ? GV_FORWARD : GV_BACKWARD;
57  Vect_append_points(APoints, pg_info->cache.lines[0], direction);
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 
67 PGresult *build_stmt(const struct Plus_head *plus, const struct Format_info_pg *pg_info,
68  const plus_t *lines, int n_lines)
69 {
70  int i, line;
71  size_t stmt_id_size;
72  char *stmt, *stmt_id, buf_id[128];
73 
74  struct P_line *BLine;
75 
76  PGresult *res;
77 
78  stmt = NULL;
79  stmt_id_size = DB_SQL_MAX;
80  stmt_id = (char *) G_malloc(stmt_id_size);
81  stmt_id[0] = '\0';
82 
83  for (i = 0; i < n_lines; i++) {
84  if (strlen(stmt_id) + 100 > stmt_id_size) {
85  stmt_id_size = strlen(stmt_id) + DB_SQL_MAX;
86  stmt_id = (char *) G_realloc(stmt_id, stmt_id_size);
87  }
88  line = abs(lines[i]);
89  BLine = plus->Line[line];
90  if (i > 0)
91  strcat(stmt_id, ",");
92  sprintf(buf_id, "%d", (int) BLine->offset);
93  strcat(stmt_id, buf_id);
94  }
95  /* Not really working - why?
96  G_asprintf(&stmt, "SELECT geom FROM \"%s\".edge_data WHERE edge_id IN (%s) "
97  "ORDER BY POSITION(edge_id::text in '%s')", pg_info->toposchema_name,
98  stmt_id, stmt_id);
99  */
100  G_asprintf(&stmt, "SELECT geom FROM \"%s\".edge_data AS t "
101  "JOIN (SELECT id, row_number() over() AS id_sorter FROM "
102  "(SELECT UNNEST(ARRAY[%s]) AS id) AS y) x ON "
103  "t.edge_id in (%s) AND x.id = t.edge_id "
104  "ORDER BY x.id_sorter", pg_info->toposchema_name, stmt_id, stmt_id);
105  G_free(stmt_id);
106 
107  G_debug(2, "SQL: %s", stmt);
108  res = PQexec(pg_info->conn, stmt);
109  G_free(stmt);
110 
111  if (!res || PQresultStatus(res) != PGRES_TUPLES_OK ||
112  PQntuples(res) != n_lines) {
113  if (res)
114  PQclear(res);
115 
116  return NULL;
117  }
118 
119  return res;
120 }
121 #endif
int Vect_append_points(struct line_pnts *, const struct line_pnts *, int)
Appends points to the end of a line.
Definition: line.c:337
char * toposchema_name
Topology schema name and id.
Definition: dig_structs.h:699
#define G_malloc(n)
Definition: defs/gis.h:112
#define GV_FORWARD
Line direction indicator forward/backward.
Definition: dig_defines.h:178
int plus_t
plus_t size
Definition: dig_structs.h:41
off_t offset
Offset in coor file for line.
Definition: dig_structs.h:1593
PGconn * conn
PGconn object (generated by PQconnectdb)
Definition: dig_structs.h:663
Vector geometry.
Definition: dig_structs.h:1574
struct P_line ** Line
Array of vector geometries.
Definition: dig_structs.h:887
#define GV_BACKWARD
Definition: dig_defines.h:179
#define DB_SQL_MAX
Definition: dbmi.h:142
int n_points
Number of points.
Definition: dig_structs.h:1692
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
struct Format_info fInfo
Format info for non-native formats.
Definition: dig_structs.h:1415
#define NULL
Definition: ccmath.h:32
Feature geometry info - coordinates.
Definition: dig_structs.h:1675
Basic topology-related info.
Definition: dig_structs.h:784
Non-native format info (PostGIS)
Definition: dig_structs.h:602
struct Format_info_cache cache
Lines cache for reading feature.
Definition: dig_structs.h:683
#define FALSE
Definition: gis.h:63
struct Format_info_pg pg
PostGIS info.
Definition: dig_structs.h:726
struct Plus_head plus
Plus info (topology, version, ...)
Definition: dig_structs.h:1286
Vector map info.
Definition: dig_structs.h:1259
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:757
int Vect__get_area_points_pg(const 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:36
PGresult * res
Definition: dig_structs.h:664
struct line_pnts ** lines
Lines array.
Definition: dig_structs.h:473
#define G_realloc(p, n)
Definition: defs/gis.h:114
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:130