GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
read_pg.c File Reference

Vector library - reading features (PostGIS format) More...

#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <grass/vector.h>
#include <grass/dbmi.h>
#include <grass/glocale.h>
#include "local_proto.h"
#include "pg_local_proto.h"
Include dependency graph for read_pg.c:

Go to the source code of this file.

Functions

SF_FeatureType get_feature (struct Map_info *Map, int fid, int type)
 Read feature geometry. More...
 
int V1_read_next_line_pg (struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c)
 Read next feature from PostGIS layer. Skip empty features (level 1 without topology). t This function implements sequential access. More...
 
int V2_read_next_line_pg (struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c)
 Read next feature from PostGIS layer on topological level (simple feature access). More...
 
int V1_read_line_pg (struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, off_t offset)
 Read feature from PostGIS layer at given offset (level 1 without topology) More...
 
int V2_read_line_pg (struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
 Read feature from PostGIS layer on topological level. More...
 
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. More...
 
int Vect__open_cursor_next_line_pg (struct Format_info_pg *pg_info, int fetch_all, int built_level)
 Create select cursor for sequential access (internal use only) More...
 
int Vect__open_cursor_line_pg (struct Format_info_pg *pg_info, int fid, int type)
 Open select cursor for random access (internal use only) More...
 
int Vect__close_cursor_pg (struct Format_info_pg *pg_info)
 Close select cursor. More...
 
int Vect__select_line_pg (struct Format_info_pg *pg_info, int fid, int type)
 Select feature (internal use only) More...
 
int Vect__execute_pg (PGconn *conn, const char *stmt)
 Execute SQL statement. More...
 
int Vect__execute_get_value_pg (PGconn *conn, const char *stmt)
 Execute SQL statement and get value. More...
 
void Vect__reallocate_cache (struct Format_info_cache *cache, int num, int incr)
 Reallocate lines cache. More...
 

Detailed Description

Vector library - reading features (PostGIS format)

Higher level functions for reading/writing/manipulating vectors.

Todo:
Currently only points, linestrings and polygons are supported, implement also other types
Todo:
Support multigeometries
Todo:
PostGIS Topology - fix category handling (read categories from feature table)

(C) 2011-2013 by the GRASS Development Team

This program is free software under the GNU General Public License (>=v2). Read the file COPYING that comes with GRASS for details.

Author
Martin Landa <landa.martin gmail.com>

Definition in file read_pg.c.

Function Documentation

◆ get_feature()

SF_FeatureType get_feature ( struct Map_info Map,
int  fid,
int  type 
)

Read feature geometry.

Geometry is stored in lines cache.

Parameters
[in,out]Mappointer to Map_info struct
fidfeature id to be read (-1 for next)
typefeature type (GV_POINT, GV_LINE, ...) - use only for topological access
Returns
simple feature type (SF_POINT, SF_LINESTRING, ...)
-1 on error

Definition at line 564 of file read_pg.c.

◆ V1_read_line_pg()

int V1_read_line_pg ( struct Map_info Map,
struct line_pnts line_p,
struct line_cats line_c,
off_t  offset 
)

Read feature from PostGIS layer at given offset (level 1 without topology)

This function implements random access on level 1.

Parameters
Mappointer to Map_info structure
[out]line_pcontainer used to store line points within (pointer line_pnts struct)
[out]line_ccontainer used to store line categories within (pointer line_cats struct)
offsetgiven offset
Returns
line type
0 dead line
-2 no more features
-1 out of memory

Definition at line 237 of file read_pg.c.

◆ V1_read_next_line_pg()

int V1_read_next_line_pg ( struct Map_info Map,
struct line_pnts line_p,
struct line_cats line_c 
)

Read next feature from PostGIS layer. Skip empty features (level 1 without topology). t This function implements sequential access.

The action of this routine can be modified by:

Parameters
Mappointer to Map_info structure
[out]line_pcontainer used to store line points within (pointer to line_pnts struct)
[out]line_ccontainer used to store line categories within (pointer line_cats struct)
Returns
feature type
-2 no more features (EOF)
-1 out of memory

Definition at line 82 of file read_pg.c.

References G_debug().

◆ V2_read_line_pg()

int V2_read_line_pg ( struct Map_info Map,
struct line_pnts line_p,
struct line_cats line_c,
int  line 
)

Read feature from PostGIS layer on topological level.

This function implements random access on level 2.

Note: Topology must be built at level >= GV_BUILD_BASE

Parameters
Mappointer to Map_info structure
[out]line_pcontainer used to store line points within (pointer line_pnts struct)
[out]line_ccontainer used to store line categories within (pointer line_cats struct)
linefeature id to read
Returns
feature type
0 dead feature
-1 on error

Definition at line 319 of file read_pg.c.

◆ V2_read_next_line_pg()

int V2_read_next_line_pg ( struct Map_info Map,
struct line_pnts line_p,
struct line_cats line_c 
)

Read next feature from PostGIS layer on topological level (simple feature access).

This function implements sequential access.

Parameters
Mappointer to Map_info structure
[out]line_pcontainer used to store line points within (pointer to line_pnts struct)
[out]line_ccontainer used to store line categories within (pointer to line_cats struct)
Returns
feature type
-2 no more features (EOF)
-1 on failure

Definition at line 112 of file read_pg.c.

◆ Vect__cache_feature_pg()

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.

This code is inspired by OGRGeometryFactory::createFromWkb() from GDAL/OGR library.

Parameters
dataHEX data
skip_polygonskip polygons (level 1)
force_typeforce GV_BOUNDARY or GV_CENTROID (used for PostGIS topology only)
[out]cachelines cache
[out]fpartsused for building pseudo-topology (or NULL)
Returns
simple feature type
SF_GEOMETRY on error

Definition at line 757 of file read_pg.c.

◆ Vect__close_cursor_pg()

int Vect__close_cursor_pg ( struct Format_info_pg pg_info)

Close select cursor.

Parameters
pg_infopointer to Format_info_pg struct
Returns
0 on success
-1 on failure

Definition at line 1408 of file read_pg.c.

References _, Format_info_pg::conn, Format_info_pg::cursor_name, DB_SQL_MAX, G_free(), G_warning(), NULL, Format_info_pg::res, and Vect__execute_pg().

Referenced by V1_rewind_pg().

◆ Vect__execute_get_value_pg()

int Vect__execute_get_value_pg ( PGconn *  conn,
const char *  stmt 
)

Execute SQL statement and get value.

Parameters
connpointer to PGconn
stmtquery
Returns
value on success
-1 on error

Definition at line 1548 of file read_pg.c.

References _, G_debug(), G_warning(), and NULL.

Referenced by Vect_sfa_get_num_features().

◆ Vect__execute_pg()

int Vect__execute_pg ( PGconn *  conn,
const char *  stmt 
)

Execute SQL statement.

See pg_local_proto.h

Parameters
connpointer to PGconn
stmtquery
Returns
0 on success
-1 on error

Definition at line 1514 of file read_pg.c.

References _, G_debug(), G_warning(), and NULL.

Referenced by V1_close_pg(), V1_delete_line_pg(), Vect__clean_grass_db_topo(), Vect__close_cursor_pg(), Vect__insert_face_pg(), Vect__open_cursor_next_line_pg(), and Vect_build_pg().

◆ Vect__open_cursor_line_pg()

int Vect__open_cursor_line_pg ( struct Format_info_pg pg_info,
int  fid,
int  type 
)

Open select cursor for random access (internal use only)

Fetch number of feature (given by CURSOR_PAGE) starting with fid.

Allocated cursor name should be freed by G_free().

Parameters
pg_infopointer to Format_info_pg struct
fidfeature id to get
typefeature type
Returns
0 on success
-1 on failure

Definition at line 1337 of file read_pg.c.

◆ Vect__open_cursor_next_line_pg()

int Vect__open_cursor_next_line_pg ( struct Format_info_pg pg_info,
int  fetch_all,
int  built_level 
)

Create select cursor for sequential access (internal use only)

Allocated cursor name should be freed by G_free().

Parameters
pg_infopointer to Format_info_pg struct
fetch_allTRUE to fetch all records
[out]cursorname
Returns
0 on success
-1 on failure

Definition at line 1234 of file read_pg.c.

References _, Format_info_pg::conn, Format_info_pg::cursor_name, DB_SQL_MAX, Format_info_pg::fid_column, G_asprintf(), G_debug(), G_free_tokens(), G_number_of_tokens(), G_tokenize(), G_warning(), Format_info_pg::geom_column, GV_BOUNDARY, GV_BUILD_CENTROIDS, GV_CENTROID, GV_LINE, GV_POINT, Format_info_pg::res, Format_info_pg::schema_name, Format_info_pg::table_name, Format_info_pg::topogeom_column, Format_info_pg::toposchema_name, Vect__execute_pg(), and Format_info_pg::where.

◆ Vect__reallocate_cache()

void Vect__reallocate_cache ( struct Format_info_cache cache,
int  num,
int  incr 
)

◆ Vect__select_line_pg()

int Vect__select_line_pg ( struct Format_info_pg pg_info,
int  fid,
int  type 
)

Select feature (internal use only)

Parameters
pg_infopointer to Format_info_pg struct
fidfeature id to get
typefeature type
Returns
0 on success
-1 on failure

Definition at line 1441 of file read_pg.c.