[GRASS-SVN] r47932 - grass/trunk/vector/v.select
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Aug 29 04:57:54 EDT 2011
Author: martinl
Date: 2011-08-29 01:57:54 -0700 (Mon, 29 Aug 2011)
New Revision: 47932
Added:
grass/trunk/vector/v.select/copy_tabs.c
grass/trunk/vector/v.select/select.c
grass/trunk/vector/v.select/write.c
Modified:
grass/trunk/vector/v.select/main.c
grass/trunk/vector/v.select/proto.h
Log:
v.select: code reorganization
Added: grass/trunk/vector/v.select/copy_tabs.c
===================================================================
--- grass/trunk/vector/v.select/copy_tabs.c (rev 0)
+++ grass/trunk/vector/v.select/copy_tabs.c 2011-08-29 08:57:54 UTC (rev 47932)
@@ -0,0 +1,71 @@
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/dbmi.h>
+#include <grass/glocale.h>
+
+#include "proto.h"
+
+void copy_tabs(struct Map_info *In, struct Map_info *Out,
+ int nfields, int *fields, int *ncats, int **cats)
+{
+ int i, ttype, ntabs;
+
+ struct field_info *IFi, *OFi;
+
+ ntabs = 0;
+
+ G_message(_("Writing attributes..."));
+
+ /* Number of output tabs */
+ for (i = 0; i < Vect_get_num_dblinks(In); i++) {
+ int f, j;
+
+ IFi = Vect_get_dblink(In, i);
+
+ for (j = 0; j < nfields; j++) { /* find field */
+ if (fields[j] == IFi->number) {
+ f = j;
+ break;
+ }
+ }
+ if (ncats[f] > 0)
+ ntabs++;
+ }
+
+ if (ntabs > 1)
+ ttype = GV_MTABLE;
+ else
+ ttype = GV_1TABLE;
+
+ for (i = 0; i < nfields; i++) {
+ int ret;
+
+ if (fields[i] == 0)
+ continue;
+
+ /* Make a list of categories */
+ IFi = Vect_get_field(In, fields[i]);
+ if (!IFi) { /* no table */
+ G_warning(_("No table for layer %d"), fields[i]);
+ continue;
+ }
+
+ OFi =
+ Vect_default_field_info(Out, IFi->number, IFi->name, ttype);
+
+ ret =
+ db_copy_table_by_ints(IFi->driver, IFi->database, IFi->table,
+ OFi->driver,
+ Vect_subst_var(OFi->database, Out),
+ OFi->table, IFi->key, cats[i],
+ ncats[i]);
+
+ if (ret == DB_FAILED) {
+ G_warning(_("Unable to copy table for layer %d"), fields[i]);
+ }
+ else {
+ Vect_map_add_dblink(Out, OFi->number, OFi->name, OFi->table,
+ IFi->key, OFi->database, OFi->driver);
+ }
+ }
+}
Property changes on: grass/trunk/vector/v.select/copy_tabs.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:eol-style
+ native
Modified: grass/trunk/vector/v.select/main.c
===================================================================
--- grass/trunk/vector/v.select/main.c 2011-08-29 08:38:18 UTC (rev 47931)
+++ grass/trunk/vector/v.select/main.c 2011-08-29 08:57:54 UTC (rev 47932)
@@ -1,12 +1,12 @@
/****************************************************************************
*
- * MODULE: v.select - select features from one map by features in another map.
+ * MODULE: v.select
* AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
* Glynn Clements <glynn gclements.plus.com>
* Markus Neteler <neteler itc.it>
* Martin Landa <landa.martin gmail.com> (GEOS support)
- * PURPOSE:
+ * PURPOSE: Select features from one map by features in another map.
* COPYRIGHT: (C) 2003-2011 by the GRASS Development Team
*
* This program is free software under the GNU General
@@ -19,7 +19,6 @@
#include <string.h>
#include <stdio.h>
#include <grass/gis.h>
-#include <grass/dbmi.h>
#include <grass/vector.h>
#include <grass/glocale.h>
@@ -27,21 +26,19 @@
int main(int argc, char *argv[])
{
- int i, iopt;
+ int iopt;
int operator;
- int aline, nalines, nskipped, is_ogr;
- int ltype, itype[2], ifield[2];
- int **cats, *ncats, nfields, *fields;
+ int nskipped, is_ogr;
+ int itype[2], ifield[2];
+
+ int *ALines; /* List of lines: 0 do not output, 1 - write to output */
+ int **cats, *ncats, *fields, nfields;
+
struct GModule *module;
struct GParm parm;
struct GFlag flag;
struct Map_info In[2], Out;
- struct field_info *IFi, *OFi;
- struct line_pnts *APoints, *BPoints;
- struct line_cats *ACats;
- int *ALines; /* List of lines: 0 do not output, 1 - write to output */
- struct ilist *BoundList, *LList;
- struct boxlist *List, *TmpList;
+ struct field_info *IFi;
G_gisinit(argv[0]);
@@ -82,7 +79,7 @@
else if (parm.operator->answer[0] == 'r')
operator = OP_RELATE;
else
- G_fatal_error(_("Unknown operator"));
+ G_fatal_error(_("Unknown operator '%s'"), parm.operator->answer);
if (operator == OP_RELATE && !parm.relate->answer) {
G_fatal_error(_("Required parameter <%s> not set"),
@@ -99,18 +96,13 @@
Vect_set_open_level(2);
Vect_open_old(&(In[iopt]), parm.input[iopt]->answer, "");
}
-
+
+ /* Alloc space for input lines array */
+ ALines = (int *)G_calloc(Vect_get_num_lines(&(In[0])) + 1, sizeof(int));
+
/* Read field info */
IFi = Vect_get_field(&(In[0]), ifield[0]);
- APoints = Vect_new_line_struct();
- BPoints = Vect_new_line_struct();
- ACats = Vect_new_cats_struct();
- List = Vect_new_boxlist(1);
- TmpList = Vect_new_boxlist(1);
- BoundList = Vect_new_list();
- LList = Vect_new_list();
-
/* Open output */
Vect_open_new(&Out, parm.output->answer, Vect_is_3d(&(In[0])));
Vect_set_map_name(&Out, _("Output from v.select"));
@@ -118,297 +110,13 @@
Vect_copy_head_data(&(In[0]), &Out);
Vect_hist_copy(&(In[0]), &Out);
Vect_hist_command(&Out);
-
- nskipped = 0;
- nalines = Vect_get_num_lines(&(In[0]));
-
-#ifdef HAVE_GEOS
- initGEOS(G_message, G_fatal_error);
- GEOSGeometry *AGeom = NULL;
-#else
- void *AGeom = NULL;
-#endif
-
- /* Alloc space for input lines array */
- ALines = (int *)G_calloc(nalines + 1, sizeof(int));
-
- /* Lines in A. Go through all lines and mark those that meets condition */
- if (itype[0] & (GV_POINTS | GV_LINES)) {
- G_message(_("Processing features..."));
-
- for (aline = 1; aline <= nalines; aline++) {
- struct bound_box abox;
-
- G_debug(3, "aline = %d", aline);
- G_percent(aline, nalines, 2); /* must be before any continue */
-
- /* Check category */
- if (!flag.cat->answer && Vect_get_line_cat(&(In[0]), aline, ifield[0]) < 0) {
- nskipped++;
- continue;
- }
-
- /* Read line and check type */
- if (operator != OP_OVERLAP) {
-#ifdef HAVE_GEOS
- AGeom = Vect_read_line_geos(&(In[0]), aline, <ype);
-#endif
- if (!(ltype & (GV_POINT | GV_LINE)))
- continue;
-
- if (!AGeom)
- G_fatal_error(_("Unable to read line id %d from vector map <%s>"),
- aline, Vect_get_full_name(&(In[0])));
- }
- else {
- ltype = Vect_read_line(&(In[0]), APoints, NULL, aline);
- }
-
- if (!(ltype & itype[0]))
- continue;
-
- Vect_get_line_box(&(In[0]), aline, &abox);
-
- /* Check if this line overlaps any feature in B */
- /* x Lines in B */
- if (itype[1] & (GV_POINTS | GV_LINES)) {
- int i;
- int found = 0;
-
- /* Lines */
- Vect_select_lines_by_box(&(In[1]), &abox, itype[1], List);
- for (i = 0; i < List->n_values; i++) {
- int bline;
-
- bline = List->id[i];
- G_debug(3, " bline = %d", bline);
-
- /* Check category */
- if (!flag.cat->answer && Vect_get_line_cat(&(In[1]), bline, ifield[1]) < 0) {
- nskipped++;
- continue;
- }
-
- if (operator != OP_OVERLAP) {
-#ifdef HAVE_GEOS
- if(line_relate_geos(&(In[1]), AGeom,
- bline, operator, parm.relate->answer)) {
-
- found = 1;
- break;
- }
-#endif
- }
- else {
- Vect_read_line(&(In[1]), BPoints, NULL, bline);
-
- if (Vect_line_check_intersection(APoints, BPoints, 0)) {
- found = 1;
- break;
- }
- }
- }
-
- if (found) {
- ALines[aline] = 1;
- continue; /* Go to next A line */
- }
- }
-
- /* x Areas in B. */
- if (itype[1] & GV_AREA) {
- int i;
-
- Vect_select_areas_by_box(&(In[1]), &abox, List);
- for (i = 0; i < List->n_values; i++) {
- int barea;
-
- barea = List->id[i];
- G_debug(3, " barea = %d", barea);
-
- if (Vect_get_area_cat(&(In[1]), barea, ifield[1]) < 0) {
- nskipped++;
- continue;
- }
-
- if (operator != OP_OVERLAP) {
-#ifdef HAVE_GEOS
- if(area_relate_geos(&(In[1]), AGeom,
- barea, operator, parm.relate->answer)) {
- ALines[aline] = 1;
- break;
- }
-#endif
- }
- else {
- if (line_overlap_area(&(In[0]), aline, &(In[1]), barea, List->box[i])) {
- ALines[aline] = 1;
- break;
- }
- }
- }
- }
- if (operator != OP_OVERLAP) {
-#ifdef HAVE_GEOS
- GEOSGeom_destroy(AGeom);
-#endif
- AGeom = NULL;
- }
- }
- }
- /* Areas in A. */
- if (itype[0] & GV_AREA) {
- int aarea, naareas;
-
- G_message(_("Processing areas..."));
-
- naareas = Vect_get_num_areas(&(In[0]));
-
- for (aarea = 1; aarea <= naareas; aarea++) {
- struct bound_box abox;
-
- G_percent(aarea, naareas, 2); /* must be before any continue */
-
- if (Vect_get_area_cat(&(In[0]), aarea, ifield[0]) < 0) {
- nskipped++;
- continue;
- }
-
- Vect_get_area_box(&(In[0]), aarea, &abox);
- abox.T = PORT_DOUBLE_MAX;
- abox.B = -PORT_DOUBLE_MAX;
-
- if (operator != OP_OVERLAP) {
-#ifdef HAVE_GEOS
- AGeom = Vect_read_area_geos(&(In[0]), aarea);
-#endif
- if (!AGeom)
- G_fatal_error(_("Unable to read area id %d from vector map <%s>"),
- aline, Vect_get_full_name(&(In[0])));
- }
-
- /* x Lines in B */
- if (itype[1] & (GV_POINTS | GV_LINES)) {
- Vect_select_lines_by_box(&(In[1]), &abox, itype[1], List);
-
- for (i = 0; i < List->n_values; i++) {
- int bline;
-
- bline = List->id[i];
-
- if (!flag.cat->answer && Vect_get_line_cat(&(In[1]), bline, ifield[1]) < 0) {
- nskipped++;
- continue;
- }
-
- if (operator != OP_OVERLAP) {
-#ifdef HAVE_GEOS
- if(line_relate_geos(&(In[1]), AGeom,
- bline, operator, parm.relate->answer)) {
- add_aarea(&(In[0]), aarea, ALines);
- break;
- }
-#endif
- }
- else {
- if (line_overlap_area(&(In[1]), bline, &(In[0]), aarea, abox)) {
- add_aarea(&(In[0]), aarea, ALines);
- continue;
- }
- }
- }
- }
-
- /* x Areas in B */
- if (itype[1] & GV_AREA) {
- int naisles;
- int found = 0;
-
- /* List of areas B */
-
- /* Make a list of features forming area A */
- Vect_reset_list(LList);
-
- Vect_get_area_boundaries(&(In[0]), aarea, BoundList);
- for (i = 0; i < BoundList->n_values; i++) {
- Vect_list_append(LList, abs(BoundList->value[i]));
- }
-
- naisles = Vect_get_area_num_isles(&(In[0]), aarea);
-
- for (i = 0; i < naisles; i++) {
- int j, aisle;
-
- aisle = Vect_get_area_isle(&(In[0]), aarea, i);
-
- Vect_get_isle_boundaries(&(In[0]), aisle, BoundList);
- for (j = 0; j < BoundList->n_values; j++) {
- Vect_list_append(LList, BoundList->value[j]);
- }
- }
-
- Vect_select_areas_by_box(&(In[1]), &abox, TmpList);
-
- for (i = 0; i < LList->n_values; i++) {
- int j, aline;
-
- aline = abs(LList->value[i]);
-
- for (j = 0; j < TmpList->n_values; j++) {
- int barea, bcentroid;
-
- barea = TmpList->id[j];
- G_debug(3, " barea = %d", barea);
-
- if (Vect_get_area_cat(&(In[1]), barea, ifield[1]) < 0) {
- nskipped++;
- continue;
- }
-
- /* Check if any centroid of area B is in area A.
- * This test is important in if area B is completely within area A */
- bcentroid = Vect_get_area_centroid(&(In[1]), barea);
- Vect_read_line(&(In[1]), BPoints, NULL, bcentroid);
-
- if (operator != OP_OVERLAP) {
-#ifdef HAVE_GEOS
- if(area_relate_geos(&(In[1]), AGeom,
- barea, operator, parm.relate->answer)) {
- found = 1;
- break;
- }
-#endif
- }
- else {
- if (Vect_point_in_area(BPoints->x[0], BPoints->y[0], &(In[0]),
- aarea, abox)) {
- found = 1;
- break;
- }
-
- /* Check intersectin of lines from List with area B */
- if (line_overlap_area(&(In[0]), aline,
- &(In[1]), barea, List->box[j])) {
- found = 1;
- break;
- }
- }
- }
- if (found) {
- add_aarea(&(In[0]), aarea, ALines);
- break;
- }
- }
- }
- if (operator != OP_OVERLAP) {
-#ifdef HAVE_GEOS
- GEOSGeom_destroy(AGeom);
-#endif
- AGeom = NULL;
- }
- }
- }
+ /* Select features */
+ nskipped = select_lines(&(In[0]), itype[0], ifield[0],
+ &(In[1]), itype[1], ifield[1],
+ flag.cat->answer ? 1 : 0, operator,
+ parm.relate->answer,
+ ALines);
Vect_close(&(In[1]));
@@ -416,8 +124,14 @@
finishGEOS();
#endif
- /* Write lines */
is_ogr = Vect_maptype(&Out) == GV_FORMAT_OGR_DIRECT;
+
+ nfields = Vect_cidx_get_num_fields(&(In[0]));
+ cats = (int **)G_malloc(nfields * sizeof(int *));
+ ncats = (int *)G_malloc(nfields * sizeof(int));
+ fields = (int *)G_malloc(nfields * sizeof(int));
+
+ /* Write lines */
if (!flag.table->answer && is_ogr) {
/* Copy attributes for OGR output */
if (!IFi)
@@ -426,118 +140,25 @@
Vect_map_add_dblink(&Out, IFi->number, IFi->name, IFi->table, IFi->key,
IFi->database, IFi->driver);
}
-
- nfields = Vect_cidx_get_num_fields(&(In[0]));
- cats = (int **)G_malloc(nfields * sizeof(int *));
- ncats = (int *)G_malloc(nfields * sizeof(int));
- fields = (int *)G_malloc(nfields * sizeof(int));
- for (i = 0; i < nfields; i++) {
- ncats[i] = 0;
- cats[i] =
- (int *)G_malloc(Vect_cidx_get_num_cats_by_index(&(In[0]), i) *
- sizeof(int));
- fields[i] = Vect_cidx_get_field_number(&(In[0]), i);
- }
+ write_lines(&(In[0]), IFi, ALines,
+ &Out, flag.table->answer ? 1 : 0, flag.reverse->answer ? 1 : 0,
+ nfields, fields, ncats, cats);
- G_message(_("Writing selected features..."));
- for (aline = 1; aline <= nalines; aline++) {
- int atype;
-
- G_debug(4, "aline = %d ALines[aline] = %d", aline, ALines[aline]);
- G_percent(aline, nalines, 2);
-
- if ((!flag.reverse->answer && !(ALines[aline])) ||
- (flag.reverse->answer && ALines[aline]))
- continue;
-
- atype = Vect_read_line(&(In[0]), APoints, ACats, aline);
- Vect_write_line(&Out, atype, APoints, ACats);
-
- if (!(flag.table->answer) && (IFi != NULL)) {
- for (i = 0; i < ACats->n_cats; i++) {
- int f, j;
-
- for (j = 0; j < nfields; j++) { /* find field */
- if (fields[j] == ACats->field[i]) {
- f = j;
- break;
- }
- }
- cats[f][ncats[f]] = ACats->cat[i];
- ncats[f]++;
- }
- }
- }
-
/* Copy tables */
if (!flag.table->answer && !is_ogr) {
- int ttype, ntabs = 0;
-
- G_message(_("Writing attributes..."));
-
- /* Number of output tabs */
- for (i = 0; i < Vect_get_num_dblinks(&(In[0])); i++) {
- int f, j;
-
- IFi = Vect_get_dblink(&(In[0]), i);
-
- for (j = 0; j < nfields; j++) { /* find field */
- if (fields[j] == IFi->number) {
- f = j;
- break;
- }
- }
- if (ncats[f] > 0)
- ntabs++;
- }
-
- if (ntabs > 1)
- ttype = GV_MTABLE;
- else
- ttype = GV_1TABLE;
-
- for (i = 0; i < nfields; i++) {
- int ret;
-
- if (fields[i] == 0)
- continue;
-
- /* Make a list of categories */
- IFi = Vect_get_field(&(In[0]), fields[i]);
- if (!IFi) { /* no table */
- G_warning(_("Layer %d - no table"), fields[i]);
- continue;
- }
-
- OFi =
- Vect_default_field_info(&Out, IFi->number, IFi->name, ttype);
-
- ret =
- db_copy_table_by_ints(IFi->driver, IFi->database, IFi->table,
- OFi->driver,
- Vect_subst_var(OFi->database, &Out),
- OFi->table, IFi->key, cats[i],
- ncats[i]);
-
- if (ret == DB_FAILED) {
- G_warning(_("Layer %d - unable to copy table"), fields[i]);
- }
- else {
- Vect_map_add_dblink(&Out, OFi->number, OFi->name, OFi->table,
- IFi->key, OFi->database, OFi->driver);
- }
- }
+ copy_tabs(&(In[0]), &Out,
+ nfields, fields, ncats, cats);
}
-
+
Vect_close(&(In[0]));
Vect_build(&Out);
Vect_close(&Out);
if (nskipped > 0) {
- G_warning(_("%d features without category skipped"), nskipped);
+ G_warning(_("%d features without category skipped"), nskipped);
}
-
+
G_done_msg(_("%d features written to output."), Vect_get_num_lines(&Out));
exit(EXIT_SUCCESS);
Modified: grass/trunk/vector/v.select/proto.h
===================================================================
--- grass/trunk/vector/v.select/proto.h 2011-08-29 08:38:18 UTC (rev 47931)
+++ grass/trunk/vector/v.select/proto.h 2011-08-29 08:57:54 UTC (rev 47932)
@@ -23,6 +23,10 @@
/* args.c */
void parse_options(struct GParm *, struct GFlag *);
+/* copy_tabs.c */
+void copy_tabs(struct Map_info *, struct Map_info *,
+ int, int *, int *, int **);
+
#ifdef HAVE_GEOS
/* geos.c */
int line_relate_geos(struct Map_info *, const GEOSGeometry *,
@@ -31,8 +35,17 @@
int, int, const char *);
#endif
+/* select.c */
+int select_lines(struct Map_info *, int, int,
+ struct Map_info *, int, int,
+ int, int, const char *, int *);
+
/* overlap.c */
void add_aarea(struct Map_info *, int, int *);
int line_overlap_area(struct Map_info *, int, struct Map_info *, int, struct bound_box);
+/* write.c */
+void write_lines(struct Map_info *, struct field_info *, int *,
+ struct Map_info *, int, int,
+ int, int *, int *, int **);
#endif /* PROTO_H */
Added: grass/trunk/vector/v.select/select.c
===================================================================
--- grass/trunk/vector/v.select/select.c (rev 0)
+++ grass/trunk/vector/v.select/select.c 2011-08-29 08:57:54 UTC (rev 47932)
@@ -0,0 +1,324 @@
+#include <grass/gis.h>
+#include <grass/vector.h>
+#include <grass/glocale.h>
+
+#include "proto.h"
+
+int select_lines(struct Map_info *aIn, int atype, int afield,
+ struct Map_info *bIn, int btype, int bfield,
+ int cat_flag, int operator, const char *relate,
+ int *ALines)
+{
+ int i;
+ int nalines, aline, nskipped, ltype;
+
+ struct line_pnts *APoints, *BPoints;
+ struct ilist *BoundList, *LList;
+ struct boxlist *List, *TmpList;
+
+#ifdef HAVE_GEOS
+ initGEOS(G_message, G_fatal_error);
+ GEOSGeometry *AGeom = NULL;
+#else
+ void *AGeom = NULL;
+#endif
+
+ nskipped = 0;
+ APoints = Vect_new_line_struct();
+ BPoints = Vect_new_line_struct();
+ List = Vect_new_boxlist(1);
+ TmpList = Vect_new_boxlist(1);
+ BoundList = Vect_new_list();
+ LList = Vect_new_list();
+
+ nalines = Vect_get_num_lines(aIn);
+
+ /* Lines in A. Go through all lines and mark those that meets condition */
+ if (atype & (GV_POINTS | GV_LINES)) {
+ G_message(_("Processing features..."));
+
+ for (aline = 1; aline <= nalines; aline++) {
+ struct bound_box abox;
+
+ G_debug(3, "aline = %d", aline);
+ G_percent(aline, nalines, 2); /* must be before any continue */
+
+ /* Check category */
+ if (!cat_flag && Vect_get_line_cat(aIn, aline, afield) < 0) {
+ nskipped++;
+ continue;
+ }
+
+ /* Read line and check type */
+ if (operator != OP_OVERLAP) {
+#ifdef HAVE_GEOS
+ AGeom = Vect_read_line_geos(aIn, aline, <ype);
+#endif
+ if (!(ltype & (GV_POINT | GV_LINE)))
+ continue;
+
+ if (!AGeom)
+ G_fatal_error(_("Unable to read line id %d from vector map <%s>"),
+ aline, Vect_get_full_name(aIn));
+ }
+ else {
+ ltype = Vect_read_line(aIn, APoints, NULL, aline);
+ }
+
+ if (!(ltype & atype))
+ continue;
+
+ Vect_get_line_box(aIn, aline, &abox);
+
+ /* Check if this line overlaps any feature in B */
+ /* x Lines in B */
+ if (btype & (GV_POINTS | GV_LINES)) {
+ int i;
+ int found = 0;
+
+ /* Lines */
+ Vect_select_lines_by_box(bIn, &abox, btype, List);
+ for (i = 0; i < List->n_values; i++) {
+ int bline;
+
+ bline = List->id[i];
+ G_debug(3, " bline = %d", bline);
+
+ /* Check category */
+ if (!cat_flag &&
+ Vect_get_line_cat(bIn, bline, bfield) < 0) {
+ nskipped++;
+ continue;
+ }
+
+ if (operator != OP_OVERLAP) {
+#ifdef HAVE_GEOS
+ if(line_relate_geos(bIn, AGeom,
+ bline, operator, relate)) {
+
+ found = 1;
+ break;
+ }
+#endif
+ }
+ else {
+ Vect_read_line(bIn, BPoints, NULL, bline);
+
+ if (Vect_line_check_intersection(APoints, BPoints, 0)) {
+ found = 1;
+ break;
+ }
+ }
+ }
+
+ if (found) {
+ ALines[aline] = 1;
+ continue; /* Go to next A line */
+ }
+ }
+
+ /* x Areas in B. */
+ if (btype & GV_AREA) {
+ int i;
+
+ Vect_select_areas_by_box(bIn, &abox, List);
+ for (i = 0; i < List->n_values; i++) {
+ int barea;
+
+ barea = List->id[i];
+ G_debug(3, " barea = %d", barea);
+
+ if (Vect_get_area_cat(bIn, barea, bfield) < 0) {
+ nskipped++;
+ continue;
+ }
+
+ if (operator != OP_OVERLAP) {
+#ifdef HAVE_GEOS
+ if(area_relate_geos(bIn, AGeom,
+ barea, operator, relate)) {
+ ALines[aline] = 1;
+ break;
+ }
+#endif
+ }
+ else {
+ if (line_overlap_area(aIn, aline, bIn, barea, List->box[i])) {
+ ALines[aline] = 1;
+ break;
+ }
+ }
+ }
+ }
+ if (operator != OP_OVERLAP) {
+#ifdef HAVE_GEOS
+ GEOSGeom_destroy(AGeom);
+#endif
+ AGeom = NULL;
+ }
+ }
+ }
+
+ /* Areas in A. */
+ if (atype & GV_AREA) {
+ int aarea, naareas;
+
+ G_message(_("Processing areas..."));
+
+ naareas = Vect_get_num_areas(aIn);
+
+ for (aarea = 1; aarea <= naareas; aarea++) {
+ struct bound_box abox;
+
+ G_percent(aarea, naareas, 2); /* must be before any continue */
+
+ if (Vect_get_area_cat(aIn, aarea, afield) < 0) {
+ nskipped++;
+ continue;
+ }
+
+ Vect_get_area_box(aIn, aarea, &abox);
+ abox.T = PORT_DOUBLE_MAX;
+ abox.B = -PORT_DOUBLE_MAX;
+
+ if (operator != OP_OVERLAP) {
+#ifdef HAVE_GEOS
+ AGeom = Vect_read_area_geos(aIn, aarea);
+#endif
+ if (!AGeom)
+ G_fatal_error(_("Unable to read area id %d from vector map <%s>"),
+ aline, Vect_get_full_name(aIn));
+ }
+
+ /* x Lines in B */
+ if (btype & (GV_POINTS | GV_LINES)) {
+ Vect_select_lines_by_box(bIn, &abox, btype, List);
+
+ for (i = 0; i < List->n_values; i++) {
+ int bline;
+
+ bline = List->id[i];
+
+ if (!cat_flag &&
+ Vect_get_line_cat(bIn, bline, bfield) < 0) {
+ nskipped++;
+ continue;
+ }
+
+ if (operator != OP_OVERLAP) {
+#ifdef HAVE_GEOS
+ if(line_relate_geos(bIn, AGeom,
+ bline, operator, relate)) {
+ add_aarea(aIn, aarea, ALines);
+ break;
+ }
+#endif
+ }
+ else {
+ if (line_overlap_area(bIn, bline, aIn, aarea, abox)) {
+ add_aarea(aIn, aarea, ALines);
+ continue;
+ }
+ }
+ }
+ }
+
+ /* x Areas in B */
+ if (btype & GV_AREA) {
+ int naisles;
+ int found = 0;
+
+ /* List of areas B */
+
+ /* Make a list of features forming area A */
+ Vect_reset_list(LList);
+
+ Vect_get_area_boundaries(aIn, aarea, BoundList);
+ for (i = 0; i < BoundList->n_values; i++) {
+ Vect_list_append(LList, abs(BoundList->value[i]));
+ }
+
+ naisles = Vect_get_area_num_isles(aIn, aarea);
+
+ for (i = 0; i < naisles; i++) {
+ int j, aisle;
+
+ aisle = Vect_get_area_isle(aIn, aarea, i);
+
+ Vect_get_isle_boundaries(aIn, aisle, BoundList);
+ for (j = 0; j < BoundList->n_values; j++) {
+ Vect_list_append(LList, BoundList->value[j]);
+ }
+ }
+
+ Vect_select_areas_by_box(bIn, &abox, TmpList);
+
+ for (i = 0; i < LList->n_values; i++) {
+ int j, aline;
+
+ aline = abs(LList->value[i]);
+
+ for (j = 0; j < TmpList->n_values; j++) {
+ int barea, bcentroid;
+
+ barea = TmpList->id[j];
+ G_debug(3, " barea = %d", barea);
+
+ if (Vect_get_area_cat(bIn, barea, bfield) < 0) {
+ nskipped++;
+ continue;
+ }
+
+ /* Check if any centroid of area B is in area A.
+ * This test is important in if area B is completely within area A */
+ bcentroid = Vect_get_area_centroid(bIn, barea);
+ Vect_read_line(bIn, BPoints, NULL, bcentroid);
+
+ if (operator != OP_OVERLAP) {
+#ifdef HAVE_GEOS
+ if(area_relate_geos(bIn, AGeom,
+ barea, operator, relate)) {
+ found = 1;
+ break;
+ }
+#endif
+ }
+ else {
+ if (Vect_point_in_area(BPoints->x[0], BPoints->y[0], aIn,
+ aarea, abox)) {
+ found = 1;
+ break;
+ }
+
+ /* Check intersectin of lines from List with area B */
+ if (line_overlap_area(aIn, aline,
+ bIn, barea, List->box[j])) {
+ found = 1;
+ break;
+ }
+ }
+ }
+ if (found) {
+ add_aarea(aIn, aarea, ALines);
+ break;
+ }
+ }
+ }
+ if (operator != OP_OVERLAP) {
+#ifdef HAVE_GEOS
+ GEOSGeom_destroy(AGeom);
+#endif
+ AGeom = NULL;
+ }
+ }
+ }
+
+ Vect_destroy_line_struct(APoints);
+ Vect_destroy_line_struct(BPoints);
+ Vect_destroy_list(BoundList);
+ Vect_destroy_list(LList);
+ Vect_destroy_boxlist(List);
+ Vect_destroy_boxlist(TmpList);
+
+ return nskipped;
+}
Property changes on: grass/trunk/vector/v.select/select.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:eol-style
+ native
Added: grass/trunk/vector/v.select/write.c
===================================================================
--- grass/trunk/vector/v.select/write.c (rev 0)
+++ grass/trunk/vector/v.select/write.c 2011-08-29 08:57:54 UTC (rev 47932)
@@ -0,0 +1,53 @@
+#include <grass/vector.h>
+#include <grass/glocale.h>
+
+void write_lines(struct Map_info *In, struct field_info *IFi, int *ALines,
+ struct Map_info *Out, int table_flag, int reverse_flag,
+ int nfields, int *fields, int *ncats, int **cats)
+{
+ int i, f, j, aline, nalines;
+ int atype;
+
+ struct line_pnts *APoints;
+ struct line_cats *ACats;
+
+ APoints = Vect_new_line_struct();
+ ACats = Vect_new_cats_struct();
+
+ for (i = 0; i < nfields; i++) {
+ ncats[i] = 0;
+ cats[i] =
+ (int *)G_malloc(Vect_cidx_get_num_cats_by_index(&(In[0]), i) *
+ sizeof(int));
+ fields[i] = Vect_cidx_get_field_number(&(In[0]), i);
+ }
+
+ nalines = Vect_get_num_lines(In);
+ G_message(_("Writing selected features..."));
+ for (aline = 1; aline <= nalines; aline++) {
+ G_debug(3, "aline = %d ALines[aline] = %d", aline, ALines[aline]);
+ G_percent(aline, nalines, 2);
+ if ((!reverse_flag && !(ALines[aline])) ||
+ (reverse_flag && ALines[aline]))
+ continue;
+
+ atype = Vect_read_line(&(In[0]), APoints, ACats, aline);
+ Vect_write_line(Out, atype, APoints, ACats);
+
+ if (!table_flag && (IFi != NULL)) {
+ for (i = 0; i < ACats->n_cats; i++) {
+ for (j = 0; j < nfields; j++) { /* find field */
+ if (fields[j] == ACats->field[i]) {
+ f = j;
+ break;
+ }
+ }
+ cats[f][ncats[f]] = ACats->cat[i];
+ ncats[f]++;
+ }
+ }
+ }
+
+ Vect_destroy_line_struct(APoints);
+ Vect_destroy_cats_struct(ACats);
+}
Property changes on: grass/trunk/vector/v.select/write.c
___________________________________________________________________
Added: svn:mime-type
+ text/x-csrc
Added: svn:eol-style
+ native
More information about the grass-commit
mailing list