39 static void init_parts(
struct geom_parts *);
40 static void reset_parts(
struct geom_parts *);
41 static void free_parts(
struct geom_parts *);
42 static void add_part(
struct geom_parts *,
int);
43 static void del_part(
struct geom_parts *);
48 int,
struct geom_parts *);
51 #include "pg_local_proto.h" 53 static int add_geometry_pg(
struct Plus_head *,
55 struct feat_parts *,
int,
58 static void build_pg(
struct Map_info *,
int);
64 static int add_geometry_ogr(
struct Plus_head *,
66 OGRGeometryH,
int,
int,
69 static void build_ogr(
struct Map_info *,
int);
75 void init_parts(
struct geom_parts * parts)
77 G_zero(parts,
sizeof(
struct geom_parts));
83 void reset_parts(
struct geom_parts * parts)
91 void free_parts(
struct geom_parts * parts)
94 G_zero(parts,
sizeof(
struct geom_parts));
100 void add_part(
struct geom_parts *parts,
int part)
102 if (parts->a_parts == parts->n_parts) {
103 parts->a_parts += 10;
104 parts->part = (
int *)
G_realloc((
void *)parts->part,
105 parts->a_parts *
sizeof(int));
107 parts->part[parts->n_parts] = part;
114 void del_part(
struct geom_parts *parts)
123 struct geom_parts *parts)
133 for (i = 0; i < parts->n_parts; i++) {
134 G_debug(4,
"add offset %d", parts->part[i]);
135 offset->
array[j] = parts->part[i];
146 int FID,
struct geom_parts *parts)
162 G_debug(4,
"Register line: FID = %d offset = %ld", FID, offset_value);
164 line =
dig_add_line(plus, type, Points, &box, offset_value);
165 G_debug(4,
"Line registered with line = %d", line);
182 add_parts_to_offset(offset, parts);
191 int add_geometry_pg(
struct Plus_head *plus,
193 struct feat_parts *fparts,
int ipart,
194 int FID,
int build,
struct geom_parts *parts)
196 int line, i, idx, area, isle, outer_area, ret;
198 double area_size,
x, y;
204 ftype = fparts->ftype[ipart];
206 G_debug(4,
"add_geometry_pg() FID = %d ftype = %d", FID, ftype);
208 offset = &(pg_info->
offset);
215 line_i = pg_info->
cache.
lines[fparts->idx[ipart]];
216 add_line(plus, offset,
GV_POINT, line_i,
221 line_i = pg_info->
cache.
lines[fparts->idx[ipart]];
222 add_line(plus, offset,
GV_LINE, line_i,
229 idx = fparts->idx[ipart];
230 for (i = 0; i < fparts->nlines[ipart]; i++) {
253 lines[0] = -lines[0];
266 Isle = plus->
Isle[isle];
267 Isle->
area = outer_area;
277 fparts->nlines[ipart] - 1, &x, &y);
279 G_warning(
_(
"Unable to calculate centroid for area %d"),
288 G_debug(4,
" Centroid: %f, %f", x, y);
291 line = add_line(plus, offset,
GV_CENTROID, line_c, FID, parts);
293 Line = plus->
Line[line];
295 topo->
area = outer_area;
298 Area = plus->
Area[outer_area];
305 G_warning(
_(
"Feature type %d not supported"), ftype);
315 void build_pg(
struct Map_info *Map,
int build)
317 int iFeature, ipart, fid, nrecords, npoints;
322 struct feat_parts fparts;
323 struct geom_parts parts;
329 G_zero(&fparts,
sizeof(
struct feat_parts));
337 nrecords = PQntuples(pg_info->res);
338 G_debug(4,
"build_pg(): nrecords = %d", nrecords);
340 for (iFeature = 0; iFeature < nrecords; iFeature++) {
342 fid = atoi(PQgetvalue(pg_info->res, iFeature, 1));
347 wkb_data = PQgetvalue(pg_info->res, iFeature, 0);
353 &(pg_info->cache), &fparts)) {
354 G_warning(
_(
"Feature %d without geometry skipped"),
362 for (ipart = 0; ipart < fparts.n_parts; ipart++) {
363 if (fparts.nlines[ipart] < 1) {
364 G_warning(
_(
"Feature %d without geometry skipped"), fid);
368 npoints += pg_info->cache.lines[ipart]->n_points;
370 G_debug(4,
"Feature: fid = %d part = %d", fid, ipart);
372 if (fparts.n_parts > 1)
374 add_geometry_pg(&(Map->
plus), pg_info, &fparts, ipart,
376 if (fparts.n_parts > 1)
381 pg_info->cache.lines_next = 0;
386 G_message(
n_(
"One vertex registered",
"%d vertices registered", npoints), npoints);
390 PQclear(pg_info->res);
402 int add_geometry_ogr(
struct Plus_head *plus,
404 OGRGeometryH hGeom,
int FID,
int build,
405 struct geom_parts *parts)
407 int i, ret, npoints, line;
408 int area, isle, outer_area;
410 double area_size,
x, y;
411 int eType, nRings, iPart, nParts, nPoints;
417 OGRGeometryH hGeom2, hRing;
419 G_debug(4,
"add_geometry_ogr() FID = %d", FID);
421 offset = &(ogr_info->
offset);
433 npoints = outer_area = 0;
434 eType = wkbFlatten(OGR_G_GetGeometryType(hGeom));
435 G_debug(4,
"OGR type = %d", eType);
444 OGR_G_GetY(hGeom, 0), OGR_G_GetZ(hGeom, 0));
453 nPoints = OGR_G_GetPointCount(hGeom);
455 for (i = 0; i < nPoints; i++) {
457 OGR_G_GetX(hGeom, i), OGR_G_GetY(hGeom, i),
458 OGR_G_GetZ(hGeom, i));
467 nRings = OGR_G_GetGeometryCount(hGeom);
468 G_debug(4,
"Number of rings: %d", nRings);
479 for (i = ogr_info->
cache.
lines_alloc - nRings; i < ogr_info->cache.lines_alloc; i++) {
486 for (iPart = 0; iPart < nRings; iPart++) {
488 hRing = OGR_G_GetGeometryRef(hGeom, iPart);
489 nPoints = OGR_G_GetPointCount(hRing);
490 G_debug(4,
" ring %d : nPoints = %d", iPart, nPoints);
493 for (i = 0; i < nPoints; i++) {
495 OGR_G_GetX(hRing, i), OGR_G_GetY(hRing, i),
496 OGR_G_GetZ(hRing, i));
520 lines[0] = -lines[0];
533 Isle = plus->
Isle[isle];
534 Isle->
area = outer_area;
546 G_warning(
_(
"Unable to calculate centroid for area %d"),
553 G_debug(4,
" Centroid: %f, %f", x, y);
559 Line = plus->
Line[line];
561 topo->
area = outer_area;
564 Area = plus->
Area[outer_area];
571 case wkbMultiLineString:
572 case wkbMultiPolygon:
573 case wkbGeometryCollection:
574 nParts = OGR_G_GetGeometryCount(hGeom);
575 G_debug(4,
"%d geoms -> next level", nParts);
586 for (i = ogr_info->
cache.
lines_alloc - nParts; i < ogr_info->cache.lines_alloc; i++) {
593 for (i = 0; i < nParts; i++) {
595 hGeom2 = OGR_G_GetGeometryRef(hGeom, i);
596 npoints += add_geometry_ogr(plus, ogr_info, hGeom2,
603 G_warning(
_(
"OGR feature type %d not supported"), eType);
610 void build_ogr(
struct Map_info *Map,
int build)
612 int iFeature, FID, npoints, nskipped;
616 OGRFeatureH hFeature;
619 struct geom_parts parts;
627 OGR_L_ResetReading(ogr_info->layer);
630 OGR_L_SetAttributeFilter(ogr_info->layer, ogr_info->where);
631 npoints = iFeature = nskipped = 0;
633 while ((hFeature = OGR_L_GetNextFeature(ogr_info->layer)) !=
NULL) {
634 G_debug(3,
" Feature %d", iFeature);
638 hGeom = OGR_F_GetGeometryRef(hFeature);
640 G_debug(3,
"Feature %d without geometry skipped", iFeature);
641 OGR_F_Destroy(hFeature);
646 FID = (int) OGR_F_GetFID(hFeature);
647 if (FID == OGRNullFID) {
648 G_debug(3,
"OGR feature %d without ID skipped", iFeature);
649 OGR_F_Destroy(hFeature);
657 npoints += add_geometry_ogr(&(Map->
plus), ogr_info, hGeom,
660 OGR_F_Destroy(hFeature);
665 G_message(
n_(
"One vertex registered",
"%d vertices registered", npoints), npoints);
668 G_warning(
n_(
"One feature without geometry skipped",
"%d features without geometry skipped", nskipped), nskipped);
701 if (build < plus->
built) {
712 build_ogr(Map, build);
719 build_pg(Map, build);
721 G_fatal_error(
_(
"GRASS is not compiled with PostgreSQL support"));
726 "Vect__build_sfa()");
751 G_warning(
_(
"Feature index is built only for non-native formats. " 752 "Nothing to dump."));
761 fprintf(out,
"---------- FEATURE INDEX DUMP ----------\n");
766 fprintf(out,
"topology: PostGIS\n");
768 fprintf(out,
"topology: pseudo\n");
769 fprintf(out,
"feature type: %s\n",
771 fprintf(out,
"number of features: %d\n\noffset : value (fid or part idx):\n",
773 for (i = 0; i < offset->
array_num; i++) {
774 fprintf(out,
"%6d : %d\n", i, offset->
array[i]);
const char * Vect_maptype_info(const struct Map_info *)
Gets vector map format (as string)
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int built
Highest level of topology currently available.
plus_t area
Area number, negative for duplicate centroid.
int Vect__build_sfa(struct Map_info *Map, int build)
Build pseudo-topology (for simple features) - internal use only.
struct P_line ** Line
Array of vector geometries.
int Vect_box_copy(struct bound_box *, const struct bound_box *)
Copy box B to box A.
plus_t Vect_get_num_lines(const struct Map_info *)
Fetch number of features (points, lines, boundaries, centroids) in vector map.
#define GV_BUILD_BASE
Topology levels - basic level (without areas and isles)
#define n_(strs, strp, num)
struct P_area ** Area
Array of areas.
int dig_line_box(const struct line_pnts *, struct bound_box *)
int n_points
Number of points.
int dig_area_add_isle(struct Plus_head *, int, int)
Add isle to area if does not exist yet.
#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.
void G_free(void *)
Free allocated memory.
struct Format_info fInfo
Format info for non-native formats.
void G_progress(long, int)
Print progress info messages.
struct P_isle ** Isle
Array of isles.
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.
plus_t centroid
Number of first centroid within area.
#define GV_POINT
Feature types used in memory on run time (may change)
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)
plus_t n_lines
Current number of lines.
int dig_cidx_add_cat(struct Plus_head *, int, int, int, int)
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.
void G_message(const char *,...) __attribute__((format(printf
Feature geometry info - coordinates.
Basic topology-related info.
int Vect_fidx_dump(const struct Map_info *Map, FILE *out)
Dump feature index to file.
#define GV_BUILD_AREAS
Topology levels - build areas.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
int Vect_maptype(const struct Map_info *)
Gets vector map format.
void * topo
Topology info.
int Vect_append_point(struct line_pnts *, double, double, double)
Appends one point to the end of a line.
#define GV_FORMAT_OGR_DIRECT
OGR format (direct access)
struct Plus_head plus
Plus info (topology, version, ...)
int Vect_box_extend(struct bound_box *, const struct bound_box *)
Extend box A by box B.
struct bound_box box
Bounding box of features.
int dig_add_area(struct Plus_head *, int, plus_t *, struct bound_box *)
Allocate space for new area and create boundary info from array.
#define GV_BUILD_ATTACH_ISLES
Topology levels - attach islands to areas.
#define GV_FORMAT_POSTGIS
PostGIS format.
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.
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
void G_warning(const char *,...) __attribute__((format(printf
const char * Vect_get_finfo_geometry_type(const struct Map_info *)
Get geometry type as string (relevant only for non-native formats)
int format
Map format (native, ogr, postgis)
#define GV_BUILD_CENTROIDS
Topology levels - assign centroids to areas.
void add_part(SYMBOL *s, SYMBPART *p)
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
plus_t area
Area it exists w/in, if any.
int G_debug(int, const char *,...) __attribute__((format(printf
void Vect_reset_line(struct line_pnts *)
Reset line.
SF_FeatureType
Simple feature types.
int dig_find_area_poly(struct line_pnts *, double *)
void Vect__build_downgrade(struct Map_info *, int)
Downgrade build level (for internal use only)