26#include "local_proto.h"
29#include "pg_local_proto.h"
32#define SEP "-----------------------------------\n"
34#if !defined HAVE_POSTGRES
37 G_fatal_error(
_(
"Requested format is not compiled in this version"));
74 int area,
isle, n_lines;
93 G_debug(3,
" area/isle = %d -> skip", area);
99 G_debug(3,
" n_lines = %d", n_lines);
101 G_debug(3,
" unable to build area with line %d",
iline);
111 G_warning(
_(
"Area of size = 0.0 (less than 4 vertices) ignored"));
129 G_debug(3,
" -> area %d", area);
151static int sort_by_size(
const void *a,
const void *
b)
153 BOX_SIZE *
as = (BOX_SIZE *)a;
154 BOX_SIZE *
bs = (BOX_SIZE *)
b;
156 if (
as->size <
bs->size)
159 return (
as->size >
bs->size);
193 G_debug(3,
"Vect_isle_find_area () island = %d",
isle);
197 G_warning(
_(
"Request to find area outside nonexistent isle"));
209 Line = plus->
Line[line];
211 Node = plus->
Node[topo->
N1];
221 G_debug(3,
"%d areas overlap island boundary point",
List->n_values);
236 for (i = 0; i <
List->n_values; i++) {
241 G_debug(3,
" isle not completely inside area box");
254 if (
List->n_values > 1) {
255 if (
List->n_values == 2) {
269 for (i = 0; i <
List->n_values; i++) {
273 Area = plus->
Area[area];
278 G_debug(3,
" area inside isolated isle");
294 G_debug(3,
" isle not completely inside area box");
299 G_debug(3,
" poly = %d", poly);
321 G_debug(1,
"slow version of Vect_isle_find_area()");
332 G_debug(3,
" first area size = %f (n points = %d)",
343 G_debug(3,
" area size = %f (n points = %d)", size,
352 G_warning(
_(
"Larger bbox but smaller area!!!"));
393 G_debug(3,
"\tisle = %d -> area outside = %d",
isle, area);
396 if (
Isle->area > 0) {
398 "Attempt to attach isle %d to more areas "
399 "(=>topology is not clean)",
427 G_debug(3,
"Vect_attach_isles()");
435 G_debug(3,
" number of isles to attach = %d",
List->n_values);
437 for (i = 0; i <
List->n_values; i++) {
450 G_debug(3,
"Outer area is fully inside search box");
501 static int first = 1;
509 G_debug(3,
"Vect_attach_centroids()");
519 G_debug(3,
"\tnumber of centroids to reattach = %d",
List->n_values);
520 for (i = 0; i <
List->n_values; i++) {
536 G_debug(3,
"Centroid's area is fully inside search box");
555 G_debug(3,
"\tfirst centroid -> attach to area");
562 G_debug(3,
"\tduplicate centroid -> do not attach to area");
610 G_message(
_(
"Checking for topological errors..."));
618 for (line = 1; line <= nlines; line++) {
647 G_warning(
_(
"Number of boundaries of length zero: %d"),
665 for (line = 1; line <= nlines; line++) {
677 if (topo->
left == 0 || topo->
right == 0) {
678 G_debug(3,
"line = %d left = %d right = %d", line, topo->
left,
685 G_warning(
_(
"Skipping further checks because of incorrect boundaries"));
692 for (area = 1; area <=
nareas; area++) {
700 for (i = 0; i <
List->n_values; i++) {
701 line =
List->value[i];
755 return Map->plus.built;
779 for (line = 1; line <= plus->
n_lines; line++) {
780 Line = plus->
Line[line];
795 for (line = 1; line <= plus->
n_lines; line++) {
796 Line = plus->
Line[line];
874 if (!
Map->plus.Spidx_built) {
877 _(
"Unable to open spatial index file for vector map <%s>"),
885 G_message(
_(
"Building topology for vector map <%s>..."), map_name);
939 for (line = 1; line <= nlines; line++) {
946 if (topo->
left == 0 || topo->
right == 0) {
947 G_debug(3,
"line = %d left = %d right = %d", line,
957 else if (topo->
area < 0)
982 G_warning(
_(
"Number of centroids exceeds number of areas: %d > %d"),
1015 G_debug(1,
"Vect_save_topo()");
1018 plus = &(
Map->plus);
1024 G_warning(
_(
"Unable to create topo file for vector map <%s>"),
1054 int i,
j, line,
isle;
1063 plus = &(
Map->plus);
1065 fprintf(
out,
"---------- TOPOLOGY DUMP ----------\n");
1068 G_free((
void *)map_name);
1073 Map->fInfo.pg.toposchema_name) {
1089 fprintf(
out,
"N,S,E,W,T,B: %f, %f, %f, %f, %f, %f\n",
box.N,
box.S,
box.E,
1096 for (i = 1; i <= plus->
n_nodes; i++) {
1101 fprintf(
out,
"node = %d, n_lines = %d, xyz = %f, %f, %f\n", i,
1103 for (
j = 0;
j <
Node->n_lines;
j++) {
1104 line =
Node->lines[
j];
1109 fprintf(
out,
" line = %3d, type = %d, angle = %f (%.4f)\n", line,
1118 for (i = 1; i <= plus->
n_lines; i++) {
1124 fprintf(
out,
"line = %d, type = %d, offset = %lu\n", i,
Line->type,
1125 (
unsigned long)
Line->offset);
1130 fprintf(
out,
"line = %d, type = %d, offset = %lu, area = %d\n", i,
1137 out,
"line = %d, type = %d, offset = %lu, n1 = %d, n2 = %d\n",
1144 "line = %d, type = %d, offset = %lu, n1 = %d, n2 = %d, "
1145 "left = %d, right = %d\n",
1153 "line = %d, type = %d, offset = %lu, e1 = %d, e2 = %d, "
1154 "e3 = %d, left = %d, right = %d\n",
1155 i, Line->
type, (
unsigned long)Line->
offset, topo->
E[0],
1161 fprintf(
out,
"line = %d, type = %d, offset = %lu, volume = %d", i,
1170 for (i = 1; i <= plus->
n_areas; i++) {
1174 Area = plus->
Area[i];
1176 fprintf(
out,
"area = %d, n_lines = %d, n_isles = %d centroid = %d\n", i,
1194 for (i = 1; i <= plus->
n_isles; i++) {
1198 Isle = plus->
Isle[i];
1226 if (
Map->level < 2) {
1228 "vector map is not opened at topology level 2"));
1230 if (!
Map->plus.Spidx_built) {
1247 G_debug(3,
"Vect_build_sidx_from_topo(): name=%s", map_name);
1248 G_free((
void *)map_name);
1250 G_warning(
_(
"%s is no longer supported"),
"Vect_build_sidx_from_topo()");
1268 G_debug(1,
"Vect_save_spatial_index()");
1270 plus = &(
Map->plus);
1273 G_warning(
_(
"Spatial index not available, can not be saved"));
1286 _(
"Unable to create spatial index file for vector map <%s>"),
1295 G_warning(
_(
"Error writing out spatial index file"));
1319 if (!(
Map->plus.Spidx_built)) {
1323 fprintf(
out,
"---------- SPATIAL INDEX DUMP ----------\n");
void Vect__build_downgrade(struct Map_info *Map, int build)
Downgrade build level (for internal use only)
int Vect_build_sidx_from_topo(struct Map_info *Map)
Create spatial index from topology if necessary (not longer supported)
int Vect_attach_isle(struct Map_info *Map, int isle, const struct bound_box *box)
(Re)Attach isle to area
int Vect_isle_find_area(struct Map_info *Map, int isle, const struct bound_box *box)
Find area outside island.
int Vect_topo_dump(struct Map_info *Map, FILE *out)
Dump topology to file.
int Vect_build(struct Map_info *Map)
Build topology for vector map.
int Vect_build_partial(struct Map_info *Map, int build)
Build partial topology for vector map.
int Vect_build_line_area(struct Map_info *Map, int iline, int side)
Build area on given side of line (GV_LEFT or GV_RIGHT)
int Vect_build_sidx(struct Map_info *Map)
Create spatial index if necessary.
int Vect_sidx_dump(struct Map_info *Map, FILE *out)
Dump spatial index to file.
int Vect_attach_centroids(struct Map_info *Map, const struct bound_box *box)
(Re)Attach centroids in given bounding box to areas
int Vect_attach_isles(struct Map_info *Map, const struct bound_box *box)
(Re)Attach isles in given bounding box to areas
int Vect_topo_check(struct Map_info *Map, struct Map_info *Err)
Extensive tests for correct topology.
int Vect_get_built(struct Map_info *Map)
Return current highest built level (part)
int Vect_save_sidx(struct Map_info *Map)
Save spatial index file for vector map.
int Vect_save_topo(struct Map_info *Map)
Save topology file for vector map.
AMI_err name(char **stream_name)
void G_free(void *)
Free allocated memory.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
void void G_verbose_message(const char *,...) __attribute__((format(printf
void G_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
plus_t Vect_get_num_lines(struct Map_info *)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
int Vect_select_isles_by_box(struct Map_info *, const struct bound_box *, struct boxlist *)
Select isles with bounding boxes by box.
double Vect_line_length(const struct line_pnts *)
Calculate line length, 3D-length in case of 3D vector line.
plus_t Vect_get_num_areas(struct Map_info *)
Get number of areas in vector map.
int Vect_area_alive(struct Map_info *, int)
Check if area is alive or dead (topological level required)
int Vect_build_pg(struct Map_info *, int)
Build topology for PostGIS layer.
int Vect_get_line_type(struct Map_info *, int)
Get line type.
int Vect_get_area_boundaries(struct Map_info *, int, struct ilist *)
Creates list of boundaries for given area.
struct boxlist * Vect_new_boxlist(int)
Creates and initializes a struct boxlist.
const char * Vect_get_name(struct Map_info *)
Get name of vector map.
int Vect_get_area_points(struct Map_info *, int, struct line_pnts *)
Returns polygon array of points (outer ring) of given area.
int Vect_build_nat(struct Map_info *, int)
Build topology.
void Vect_destroy_list(struct ilist *)
Frees all memory associated with a struct ilist, including the struct itself.
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_check_line_breaks(struct Map_info *, int, struct Map_info *)
Check for and count intersecting lines, do not break.
int Vect_read_line(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
int Vect_point_in_area_outer_ring(double, double, struct Map_info *, int, struct bound_box *)
Determines if a point (X,Y) is inside an area outer ring. Islands are not considered.
int Vect_line_alive(struct Map_info *, int)
Check if feature is alive or dead (topological level required)
plus_t Vect_get_num_primitives(struct Map_info *, int)
Get number of primitives in vector map.
int Vect_get_area_box(struct Map_info *, int, struct bound_box *)
Get bounding box of area.
int Vect_select_areas_by_box(struct Map_info *, const struct bound_box *, struct boxlist *)
Select areas with bounding boxes by box.
int Vect_build_ogr(struct Map_info *, int)
Build pseudo-topology (simple features) for OGR layer.
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
struct ilist * Vect_new_list(void)
Creates and initializes a struct ilist.
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
off_t Vect_write_line(struct Map_info *, int, const struct line_pnts *, const struct line_cats *)
Writes a new feature.
int Vect_select_lines_by_box(struct Map_info *, const struct bound_box *, int, struct boxlist *)
Select lines with bounding boxes by box.
int Vect_open_sidx(struct Map_info *, int)
Open spatial index file ('sidx')
int Vect_get_area_centroid(struct Map_info *, int)
Returns centroid id for given area.
int Vect_line_prune(struct line_pnts *)
Remove duplicate points, i.e. zero length segments.
int Vect_get_line_areas(struct Map_info *, int, int *, int *)
Get area id on the left and right side of the boundary.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
int Vect_find_area(struct Map_info *, double, double)
Find the nearest area.
int Vect_get_isle_area(struct Map_info *, int)
Returns area id for isle.
int Vect_box_copy(struct bound_box *, const struct bound_box *)
Copy box B to box A.
#define GV_BUILD_NONE
Topology levels - nothing to build.
#define GV_FORMAT_POSTGIS
PostGIS format.
#define GV_POINT
Feature types used in memory on run time (may change)
#define GV_SIDX_ELEMENT
Native format, spatial index.
#define LEVEL_1
Vector level - without topology.
#define GV_BUILD_BASE
Topology levels - basic level (without areas and isles)
#define GV_BUILD_ALL
Topology levels - build everything (currently same as GV_BUILD_CENTROIDS)
#define GV_MODE_WRITE
Write vector map open mode.
#define GV_BUILD_AREAS
Topology levels - build areas.
#define GV_BUILD_CENTROIDS
Topology levels - assign centroids to areas.
#define LEVEL_2
Vector level - with 2D topology.
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
#define PORT_DOUBLE_MAX
Limits for portable types.
#define GV_TOPO_ELEMENT
Native format, topology file.
#define GV_FORMAT_OGR
OGR format.
#define GV_FORMAT_NATIVE
Geometry data formats supported by lib Don't change GV_FORMAT_* values, this order is hardcoded in li...
void dig_spidx_free_nodes(struct Plus_head *)
Free spatial index for nodes.
void dig_spidx_free_areas(struct Plus_head *)
Reset spatial index for areas.
int dig_add_isle(struct Plus_head *, int, plus_t *, struct bound_box *)
Allocate space for new island and create boundary info from array.
int dig__byte_order_out(void)
Get byte order.
int dig_area_add_isle(struct Plus_head *, int, int)
Add isle to area if does not exist yet.
void dig_free_plus_nodes(struct Plus_head *)
Free Plus->Node structure.
int dig_Wr_spidx(struct gvfile *, struct Plus_head *)
Write spatial index to file.
void dig_free_plus_isles(struct Plus_head *)
Free Plus->Isle structure.
int dig_area_del_isle(struct Plus_head *, int, int)
Delete isle from area.
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
int dig_dump_spidx(FILE *, const struct Plus_head *)
Dump spatial index.
void dig_cidx_sort(struct Plus_head *)
int dig_add_area(struct Plus_head *, int, plus_t *, struct bound_box *)
Allocate space for new area and create boundary info from array.
void dig_cidx_free(struct Plus_head *)
void dig_spidx_free_lines(struct Plus_head *)
Free spatial index for lines.
plus_t dig_line_get_area(struct Plus_head *, plus_t, int)
Get area number on line side.
int dig_cidx_init(struct Plus_head *)
Initialize Plus_head structure (cidx)
void dig_free_plus_lines(struct Plus_head *)
Free Plus->Line structure.
int dig_find_area_poly(struct line_pnts *, double *)
int dig_build_area_with_line(struct Plus_head *, plus_t, int, plus_t **)
Build topo for area from lines.
void dig_spidx_free_isles(struct Plus_head *)
Reset spatial index for isles.
void dig_file_init(struct gvfile *file)
Initialize gvfile structure.
void dig_free_plus_areas(struct Plus_head *)
Free Plus->Area structure.
int dig_write_plus_file(struct gvfile *, struct Plus_head *)
Writes topo structure to topo file.
int dig_line_box(const struct line_pnts *, struct bound_box *)
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
struct bound_box box
Region (bbox) constraint.
int format
Map format (native, ogr, postgis)
plus_t n_isles
Number of islands inside.
plus_t * isles
1st generation interior islands
plus_t n_lines
Number of boundary lines.
plus_t * lines
List of boundary lines.
plus_t centroid
Number of first centroid within area.
plus_t * lines
List of boundary lines.
plus_t n_lines
Number of boundary lines.
plus_t area
Area it exists w/in, if any.
off_t offset
Offset in coor file for line.
void * topo
Topology info.
Topological feature - node.
plus_t left
Area number to the left, negative for isle.
plus_t right
Area number to the right, negative for isle.
plus_t area
Area number, negative for duplicate centroid.
plus_t left
Volume number to the left, negative for hole.
plus_t E[3]
Array of edges.
plus_t right
Volume number to the right, negative for hole.
plus_t volume
Volume number, negative for duplicate kernel.
Basic topology-related info.
struct gvfile spidx_fp
Spatial index file pointer.
plus_t n_klines
Current number of kernels.
int Spidx_built
Spatial index built?
int with_z
2D/3D vector data
struct P_line ** Line
Array of vector geometries.
plus_t n_lines
Current number of lines.
int Spidx_new
Build new spatial index.
plus_t n_plines
Current number of points.
plus_t n_nodes
Current number of topological features derived from vector geometries.
plus_t n_blines
Current number of boundaries.
struct P_area ** Area
Array of areas.
int spidx_with_z
2D/3D spatial index
plus_t n_clines
Current number of centroids.
int cidx_up_to_date
Category index to be updated.
struct Version_info topo
Version info for topology file.
plus_t n_isles
Current number of isles.
struct Port_info spidx_port
Portability information for spatial index.
struct bound_box box
Bounding box of features.
struct P_isle ** Isle
Array of isles.
struct P_node ** Node
Array of nodes.
struct Port_info port
Portability information.
plus_t n_areas
Current number of areas.
int built
Highest level of topology currently available.
plus_t n_flines
Current number of faces.
plus_t n_llines
Current number of lines.
List of bounding boxes with id.
FILE * file
File descriptor.
Feature geometry info - coordinates.
int Vect__get_area_points(struct Map_info *Map, const plus_t *lines, int n_lines, struct line_pnts *BPoints)
Get area boundary points (internal use only)
char * Vect__get_element_path(char *file_path, struct Map_info *Map, const char *element)
Get map element full path (internal use only)
char * Vect__get_path(char *path, struct Map_info *Map)
Get map directory name (internal use only)