38 static void init_parts(
struct geom_parts *);
39 static void reset_parts(
struct geom_parts *);
40 static void free_parts(
struct geom_parts *);
41 static void add_part(
struct geom_parts *,
int);
42 static void del_part(
struct geom_parts *);
46 struct line_pnts *,
int,
struct geom_parts *);
49 #include "pg_local_proto.h"
52 struct feat_parts *,
int,
int,
int,
54 static void build_pg(
struct Map_info *,
int);
61 OGRGeometryH,
int,
int,
struct geom_parts *);
63 static void build_ogr(
struct Map_info *,
int);
69 void init_parts(
struct geom_parts *parts)
71 G_zero(parts,
sizeof(
struct geom_parts));
77 void reset_parts(
struct geom_parts *parts)
85 void free_parts(
struct geom_parts *parts)
88 G_zero(parts,
sizeof(
struct geom_parts));
94 void add_part(
struct geom_parts *parts,
int part)
96 if (parts->a_parts == parts->n_parts) {
99 (
int *)
G_realloc((
void *)parts->part, parts->a_parts *
sizeof(int));
101 parts->part[parts->n_parts] = part;
108 void del_part(
struct geom_parts *parts)
117 struct geom_parts *parts)
127 for (i = 0; i < parts->n_parts; i++) {
128 G_debug(4,
"add offset %d", parts->part[i]);
129 offset->
array[j] = parts->part[i];
139 int type,
struct line_pnts *Points,
int FID,
140 struct geom_parts *parts)
156 G_debug(4,
"Register line: FID = %d offset = %ld", FID, offset_value);
158 line =
dig_add_line(plus, type, Points, &box, offset_value);
159 G_debug(4,
"Line registered with line = %d", line);
176 add_parts_to_offset(offset, parts);
186 struct feat_parts *fparts,
int ipart,
int FID,
int build,
187 struct geom_parts *parts)
189 int line, i, idx, area, isle, outer_area, ret;
191 double area_size,
x, y;
197 ftype = fparts->ftype[ipart];
199 G_debug(4,
"add_geometry_pg() FID = %d ftype = %d", FID, ftype);
201 offset = &(pg_info->
offset);
208 line_i = pg_info->
cache.
lines[fparts->idx[ipart]];
209 add_line(plus, offset,
GV_POINT, line_i, FID, parts);
213 line_i = pg_info->
cache.
lines[fparts->idx[ipart]];
214 add_line(plus, offset,
GV_LINE, line_i, FID, parts);
220 idx = fparts->idx[ipart];
221 for (i = 0; i < fparts->nlines[ipart]; i++) {
225 line = add_line(plus, offset,
GV_BOUNDARY, line_i, FID, parts);
243 lines[0] = -lines[0];
256 Isle = plus->
Isle[isle];
257 Isle->
area = outer_area;
269 .
lines[fparts->idx[ipart]] +
271 fparts->nlines[ipart] - 1, &
x, &y);
273 G_warning(
_(
"Unable to calculate centroid for area %d"),
285 line = add_line(plus, offset,
GV_CENTROID, line_c, FID, parts);
287 Line = plus->
Line[line];
289 topo->
area = outer_area;
292 Area = plus->
Area[outer_area];
299 G_warning(
_(
"Feature type %d not supported"), ftype);
309 void build_pg(
struct Map_info *Map,
int build)
311 int iFeature, ipart, fid, nrecords, npoints;
316 struct feat_parts fparts;
317 struct geom_parts parts;
323 G_zero(&fparts,
sizeof(
struct feat_parts));
331 nrecords = PQntuples(pg_info->
res);
332 G_debug(4,
"build_pg(): nrecords = %d", nrecords);
334 for (iFeature = 0; iFeature < nrecords; iFeature++) {
336 fid = atoi(PQgetvalue(pg_info->
res, iFeature, 1));
341 wkb_data = PQgetvalue(pg_info->
res, iFeature, 0);
347 &(pg_info->
cache), &fparts)) {
348 G_warning(
_(
"Feature %d without geometry skipped"), iFeature + 1);
355 for (ipart = 0; ipart < fparts.n_parts; ipart++) {
356 if (fparts.nlines[ipart] < 1) {
357 G_warning(
_(
"Feature %d without geometry skipped"), fid);
363 G_debug(4,
"Feature: fid = %d part = %d", fid, ipart);
365 if (fparts.n_parts > 1)
367 add_geometry_pg(&(Map->
plus), pg_info, &fparts, ipart, fid, build,
369 if (fparts.n_parts > 1)
378 G_message(
n_(
"One primitive registered",
"%d primitives registered",
381 G_message(
n_(
"One vertex registered",
"%d vertices registered", npoints),
386 PQclear(pg_info->
res);
399 OGRGeometryH hGeom,
int FID,
int build,
400 struct geom_parts *parts)
402 int i, ret, npoints, line;
403 int area, isle, outer_area;
405 double area_size,
x, y;
406 int eType, nRings, iPart, nParts, nPoints;
412 OGRGeometryH hGeom2, hRing;
414 G_debug(4,
"add_geometry_ogr() FID = %d", FID);
416 offset = &(ogr_info->
offset);
429 npoints = outer_area = 0;
430 eType = wkbFlatten(OGR_G_GetGeometryType(hGeom));
431 G_debug(4,
"OGR type = %d", eType);
440 OGR_G_GetY(hGeom, 0), OGR_G_GetZ(hGeom, 0));
449 nPoints = OGR_G_GetPointCount(hGeom);
451 for (i = 0; i < nPoints; i++) {
453 OGR_G_GetY(hGeom, i), OGR_G_GetZ(hGeom, i));
462 nRings = OGR_G_GetGeometryCount(hGeom);
463 G_debug(4,
"Number of rings: %d", nRings);
476 i < ogr_info->cache.lines_alloc; i++) {
483 for (iPart = 0; iPart < nRings; iPart++) {
485 hRing = OGR_G_GetGeometryRef(hGeom, iPart);
486 nPoints = OGR_G_GetPointCount(hRing);
487 G_debug(4,
" ring %d : nPoints = %d", iPart, nPoints);
490 for (i = 0; i < nPoints; i++) {
492 OGR_G_GetX(hRing, i), OGR_G_GetY(hRing, i),
493 OGR_G_GetZ(hRing, i));
518 lines[0] = -lines[0];
531 Isle = plus->
Isle[isle];
532 Isle->
area = outer_area;
545 G_warning(
_(
"Unable to calculate centroid for area %d"),
552 G_debug(4,
" Centroid: %f, %f",
x, y);
558 Line = plus->
Line[line];
560 topo->
area = outer_area;
563 Area = plus->
Area[outer_area];
570 case wkbMultiLineString:
571 case wkbMultiPolygon:
572 case wkbGeometryCollection:
573 nParts = OGR_G_GetGeometryCount(hGeom);
574 G_debug(4,
"%d geoms -> next level", nParts);
587 i < ogr_info->cache.lines_alloc; i++) {
594 for (i = 0; i < nParts; i++) {
596 hGeom2 = OGR_G_GetGeometryRef(hGeom, i);
598 add_geometry_ogr(plus, ogr_info, hGeom2, FID, build, parts);
604 G_warning(
_(
"OGR feature type %d not supported"), eType);
611 void build_ogr(
struct Map_info *Map,
int build)
613 int iFeature, FID, npoints, nskipped;
617 OGRFeatureH hFeature;
620 struct geom_parts parts;
628 OGR_L_ResetReading(ogr_info->
layer);
631 OGR_L_SetAttributeFilter(ogr_info->
layer, ogr_info->
where);
632 npoints = iFeature = nskipped = 0;
634 while ((hFeature = OGR_L_GetNextFeature(ogr_info->
layer)) !=
NULL) {
635 G_debug(3,
" Feature %d", iFeature);
639 hGeom = OGR_F_GetGeometryRef(hFeature);
641 G_debug(3,
"Feature %d without geometry skipped", iFeature);
642 OGR_F_Destroy(hFeature);
647 FID = (int)OGR_F_GetFID(hFeature);
648 if (FID == OGRNullFID) {
649 G_debug(3,
"OGR feature %d without ID skipped", iFeature);
650 OGR_F_Destroy(hFeature);
659 add_geometry_ogr(&(Map->
plus), ogr_info, hGeom, FID, build, &parts);
661 OGR_F_Destroy(hFeature);
665 G_message(
n_(
"One primitive registered",
"%d primitives registered",
668 G_message(
n_(
"One vertex registered",
"%d vertices registered", npoints),
672 G_warning(
n_(
"One feature without geometry skipped",
673 "%d features without geometry skipped", nskipped),
707 if (build < plus->
built) {
718 build_ogr(Map, build);
725 build_pg(Map, build);
727 G_fatal_error(
_(
"GRASS is not compiled with PostgreSQL support"));
732 "Vect__build_sfa()");
756 G_warning(
_(
"Feature index is built only for non-native formats. "
757 "Nothing to dump."));
766 fprintf(out,
"---------- FEATURE INDEX DUMP ----------\n");
770 fprintf(out,
"topology: PostGIS\n");
772 fprintf(out,
"topology: pseudo\n");
775 "number of features: %d\n\noffset : value (fid or part idx):\n",
777 for (i = 0; i < offset->
array_num; i++) {
778 fprintf(out,
"%6d : %d\n", i, offset->
array[i]);
int Vect__build_sfa(struct Map_info *Map, int build)
Build pseudo-topology (for simple features) - internal use only.
int Vect_fidx_dump(struct Map_info *Map, FILE *out)
Dump feature index to file.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
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
void G_progress(long, int)
Print progress info messages.
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_get_point_in_poly_isl(const struct line_pnts *, const struct line_pnts **, int, double *, double *)
Get point inside polygon but outside the islands specifiled in IPoints.
void Vect__build_downgrade(struct Map_info *, int)
Downgrade build level (for internal use only)
int Vect_box_extend(struct bound_box *, const struct bound_box *)
Extend box A by box B.
int Vect_maptype(struct Map_info *)
Gets vector map format.
const char * Vect_maptype_info(struct Map_info *)
Gets vector map format (as string)
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
const char * Vect_get_finfo_geometry_type(struct Map_info *)
Get geometry type as string (relevant only for non-native formats)
void Vect_reset_line(struct line_pnts *)
Reset line.
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
int Vect_box_copy(struct bound_box *, const struct bound_box *)
Copy box B to box A.
SF_FeatureType
Simple feature types.
#define GV_FORMAT_POSTGIS
PostGIS format.
#define GV_POINT
Feature types used in memory on run time (may change)
#define GV_BUILD_ATTACH_ISLES
Topology levels - attach islands to areas.
#define GV_BUILD_BASE
Topology levels - basic level (without areas and isles)
#define GV_BUILD_AREAS
Topology levels - build areas.
#define GV_BUILD_CENTROIDS
Topology levels - assign centroids to areas.
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
#define GV_FORMAT_OGR
OGR format.
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_area_add_isle(struct Plus_head *, int, int)
Add isle to area if does not exist yet.
int dig_cidx_add_cat(struct Plus_head *, int, int, int, int)
int dig_add_area(struct Plus_head *, int, plus_t *, struct bound_box *)
Allocate space for new area and create boundary info from array.
int dig_find_area_poly(struct line_pnts *, double *)
int dig_add_line(struct Plus_head *, int, const struct line_pnts *, const struct bound_box *, off_t)
Add new line to Plus_head structure.
int dig_line_box(const struct line_pnts *, struct bound_box *)
#define n_(strs, strp, num)
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.
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)
int format
Map format (native, ogr, postgis)
struct Format_info fInfo
Format info for non-native formats.
struct Plus_head plus
Plus info (topology, version, ...)
plus_t centroid
Number of first centroid within area.
plus_t area
Area it exists w/in, if any.
void * topo
Topology info.
plus_t area
Area number, negative for duplicate centroid.
Basic topology-related info.
struct P_line ** Line
Array of vector geometries.
plus_t n_lines
Current number of lines.
struct P_area ** Area
Array of areas.
struct bound_box box
Bounding box of features.
struct P_isle ** Isle
Array of isles.
int built
Highest level of topology currently available.
Feature geometry info - coordinates.
double * y
Array of Y coordinates.
int n_points
Number of points.
void add_part(SYMBOL *s, SYMBPART *p)