[GRASS-SVN] r70262 - in grass/trunk: include/vect lib/vector/Vlib lib/vector/diglib vector/v.external

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 5 03:30:58 PST 2017


Author: martinl
Date: 2017-01-05 03:30:58 -0800 (Thu, 05 Jan 2017)
New Revision: 70262

Modified:
   grass/trunk/include/vect/dig_structs.h
   grass/trunk/lib/vector/Vlib/build_sfa.c
   grass/trunk/lib/vector/Vlib/read_pg.c
   grass/trunk/lib/vector/diglib/frmt.c
   grass/trunk/vector/v.external/args.c
   grass/trunk/vector/v.external/local_proto.h
   grass/trunk/vector/v.external/main.c
   grass/trunk/vector/v.external/v.external.html
Log:
v.external: implement where statement option (requires distclean)

Modified: grass/trunk/include/vect/dig_structs.h
===================================================================
--- grass/trunk/include/vect/dig_structs.h	2017-01-05 11:10:21 UTC (rev 70261)
+++ grass/trunk/include/vect/dig_structs.h	2017-01-05 11:30:58 UTC (rev 70262)
@@ -527,6 +527,10 @@
       \brief OGR layer name
     */
     char *layer_name;
+    /*!
+      \brief SQL where statement (to filter features)
+    */
+    char *where;
 #ifdef HAVE_OGR
     /*!
       \brief Pointer to OGRDriver
@@ -614,6 +618,10 @@
     */
     char    *table_name;
     /*!
+      \brief SQL where statement (fo filter features)
+    */
+    char *where;
+    /*!
       \brief FID column
     */
     char    *fid_column;        

Modified: grass/trunk/lib/vector/Vlib/build_sfa.c
===================================================================
--- grass/trunk/lib/vector/Vlib/build_sfa.c	2017-01-05 11:10:21 UTC (rev 70261)
+++ grass/trunk/lib/vector/Vlib/build_sfa.c	2017-01-05 11:30:58 UTC (rev 70262)
@@ -625,6 +625,9 @@
 
     /* Note: Do not use OGR_L_GetFeatureCount (it may scan all features) */
     OGR_L_ResetReading(ogr_info->layer);
+    if (ogr_info->where)
+        /* set attribute filter if where sql statement defined */
+        OGR_L_SetAttributeFilter(ogr_info->layer, ogr_info->where);
     npoints = iFeature = nskipped = 0;
     G_message(_("Registering primitives..."));
     while ((hFeature = OGR_L_GetNextFeature(ogr_info->layer)) != NULL) {

Modified: grass/trunk/lib/vector/Vlib/read_pg.c
===================================================================
--- grass/trunk/lib/vector/Vlib/read_pg.c	2017-01-05 11:10:21 UTC (rev 70261)
+++ grass/trunk/lib/vector/Vlib/read_pg.c	2017-01-05 11:30:58 UTC (rev 70262)
@@ -1245,10 +1245,17 @@
     if (!pg_info->toposchema_name) {
         /* simple feature access (geom, fid) */
         /* TODO: start_fid */
-        sprintf(stmt,
-                "DECLARE %s CURSOR FOR SELECT %s,%s FROM \"%s\".\"%s\" ORDER BY %s",
-                pg_info->cursor_name, pg_info->geom_column, pg_info->fid_column, pg_info->schema_name,
-                pg_info->table_name, pg_info->fid_column);
+        if (pg_info->where)
+            /* set attribute filter if where sql statement defined */
+            sprintf(stmt,
+                    "DECLARE %s CURSOR FOR SELECT %s,%s FROM \"%s\".\"%s\" WHERE %s ORDER BY %s",
+                    pg_info->cursor_name, pg_info->geom_column, pg_info->fid_column, pg_info->schema_name,
+                    pg_info->table_name, pg_info->where, pg_info->fid_column);
+        else
+            sprintf(stmt,
+                    "DECLARE %s CURSOR FOR SELECT %s,%s FROM \"%s\".\"%s\" ORDER BY %s",
+                    pg_info->cursor_name, pg_info->geom_column, pg_info->fid_column, pg_info->schema_name,
+                    pg_info->table_name, pg_info->fid_column);
     }
     else {
         /* topology access (geom,id,fid,type) */
@@ -1666,7 +1673,7 @@
         Vect_append_point(line_p, list.box[found].E, list.box[found].N, 0.0);
     }
     if (line_c) {
-        Vect_cat_set(line_c, 1, topo->area);
+        Vect_cat_set(line_c, 1, Line->offset);
     }
     
     return GV_CENTROID;

Modified: grass/trunk/lib/vector/diglib/frmt.c
===================================================================
--- grass/trunk/lib/vector/diglib/frmt.c	2017-01-05 11:10:21 UTC (rev 70261)
+++ grass/trunk/lib/vector/diglib/frmt.c	2017-01-05 11:30:58 UTC (rev 70262)
@@ -111,6 +111,8 @@
                 finfo->ogr.dsn = G_store(ptr);
             if (G_strcasecmp(buf1, "LAYER") == 0)
                 finfo->ogr.layer_name = G_store(ptr);
+            if (G_strcasecmp(buf1, "WHERE") == 0)
+                finfo->ogr.where = G_store(ptr);
         }
 #endif
 #ifdef HAVE_POSTGRES
@@ -123,6 +125,8 @@
                 finfo->pg.table_name = G_store(ptr);
             if (G_strcasecmp(buf1, "FID") == 0)
                 finfo->pg.fid_column = G_store(ptr);
+            if (G_strcasecmp(buf1, "WHERE") == 0)
+                finfo->pg.where = G_store(ptr);
         }
 #endif
     }

Modified: grass/trunk/vector/v.external/args.c
===================================================================
--- grass/trunk/vector/v.external/args.c	2017-01-05 11:10:21 UTC (rev 70261)
+++ grass/trunk/vector/v.external/args.c	2017-01-05 11:30:58 UTC (rev 70262)
@@ -32,7 +32,9 @@
     options->layer->required = NO;
     options->layer->key_desc = "name";
     options->layer->gisprompt = "old,datasource_layer,datasource_layer";
-        
+
+    options->where = G_define_standard_option(G_OPT_DB_WHERE);
+
     options->output = G_define_standard_option(G_OPT_V_OUTPUT);
     options->output->required = NO;
     options->output->description = _("Name for output GRASS vector map (default: input layer)");

Modified: grass/trunk/vector/v.external/local_proto.h
===================================================================
--- grass/trunk/vector/v.external/local_proto.h	2017-01-05 11:10:21 UTC (rev 70261)
+++ grass/trunk/vector/v.external/local_proto.h	2017-01-05 11:30:58 UTC (rev 70262)
@@ -2,7 +2,7 @@
 #define V_EXTERNAL_LOCAL_PROTO_H
 
 struct _options {
-    struct Option *dsn, *output, *layer;
+    struct Option *dsn, *output, *layer, *where;
 };
 
 struct _flags {

Modified: grass/trunk/vector/v.external/main.c
===================================================================
--- grass/trunk/vector/v.external/main.c	2017-01-05 11:10:21 UTC (rev 70261)
+++ grass/trunk/vector/v.external/main.c	2017-01-05 11:30:58 UTC (rev 70262)
@@ -8,7 +8,7 @@
  *               
  * PURPOSE:      Create a new vector as a link to OGR layer
  *               
- * COPYRIGHT:    (C) 2003-2016 by the GRASS Development Team
+ * COPYRIGHT:    (C) 2003-2017 by the GRASS Development Team
  *
  *               This program is free software under the GNU General
  *               Public License (>=v2). Read the file COPYING that
@@ -175,7 +175,7 @@
         if (schema_name)
             fprintf(fd, "schema: %s\n", schema_name);
         fprintf(fd, "table: %s\n", table_name);
-
+          
         G_free(table_name);
         G_free(schema_name);
     }
@@ -184,6 +184,9 @@
         fprintf(fd, "dsn: %s\n", dsn);
         fprintf(fd, "layer: %s\n", layer);
     }
+    if (options.where->answer)
+        fprintf(fd, "where: %s\n", options.where->answer);
+    
     fclose(fd);
     
     if (!flags.topo->answer) {

Modified: grass/trunk/vector/v.external/v.external.html
===================================================================
--- grass/trunk/vector/v.external/v.external.html	2017-01-05 11:10:21 UTC (rev 70261)
+++ grass/trunk/vector/v.external/v.external.html	2017-01-05 11:30:58 UTC (rev 70262)
@@ -8,6 +8,20 @@
 PostGIS feature table can be linked also using built-in <em>GRASS-PostGIS
 data driver</em> (requires GRASS to be built with PostgreSQL support).
 
+<h2>NOTES</h2>
+
+<p>The simple feature data model used by OGR (or PostGIS) is very
+different from the topological format used by GRASS. Instead of true
+topology, so called 'pseudo topology' is created for data linked by
+<em>v.external</em>. User should learn the difference between those
+two formats, because <b>some modules</b> working correctly with GRASS native
+data, <b>can produce wrong results</b> with input vector maps created
+by <em>v.external</em>.
+
+<p>See <em><a href="v.db.connect.html">v.db.connect</a></em> for an
+example of maintaining attributes in external DBMS in also writable
+mode.
+
 <h3>Supported OGR vector formats</h3>
 
 To list supported OGR formats, type
@@ -17,7 +31,7 @@
 </pre></div>
 
 For details see
-<a href="http://www.gdal.org/ogr_formats.html">OGR web site</a>.
+<a href="http://www.gdal.org/ogr_formats.html">GDAL web site</a>.
 
 <h2>EXAMPLES</h2>
 
@@ -30,8 +44,37 @@
 v.external input=/home/user/shape_data layer=test_shape output=grass_map 
 </pre></div>
 
+<h3>PostGIS layers</h3>
+
+By default, PostGIS links are created by built-in PostGIS support,
+ie. using <em>GRASS-PostGIS data driver</em>. If the environment
+variable <tt>GRASS_VECTOR_OGR</tt> exists, or GRASS is compiled
+without PostgreSQL support then GRASS will use OGR-PostgreSQL driver
+for creating a link.
+
+<p>List of layers for given data source can be printed by <b>-l</b>
+flag.
+
+<div class="code"><pre>
+v.external input="PG:host=localhost user=postgres dbname=postgis" -l
+
+...
+polymap
+...
+</pre></div>
+
+<div class="code"><pre>
+v.external input="PG:host=localhost user=postgres dbname=postgis" layer=polymap
+</pre></div>
+
+Note: Authentication details (user password) can be preferably defined
+by <em><a href="db.login.html">db.login</a></em>.
+
 <h3>MapInfo files</h3>
 
+Assuming that 'mapinfo_test' MapInfo file is located in the current
+directory (".").
+
 <div class="code"><pre>
 v.external input=./ layer=mapinfo_test output=grass_map
 </pre></div>
@@ -50,34 +93,23 @@
 v.external input=input/2000/56015/ layer=CompleteChain,PIP output=t56015_all
 </pre></div>
 
-<h3>PostGIS layers</h3>
+<h3>Linking subset of features</h3>
 
-PostGIS links are by default created by built-in PostGIS support,
-ie. using <em>GRASS-PostGIS data driver</em>. If the environment
-variable <tt>GRASS_VECTOR_OGR</tt> exists, or GRASS is compiled
-without PostgreSQL support then GRASS will use OGR-PostgreSQL driver
-for creating a link.
+By <b>where</b> option only subset of features can be linked. In the
+example below only one feature (in this case a vector tile) will be
+linked as a new GRASS vector map.
 
 <div class="code"><pre>
-v.external input="PG:host=localhost user=postgres dbname=postgis" layer=polymap
+v.external in="PG:dbname=tiles" layer=wrs2_descending where="pr=191026" 
+...
+Number of areas: 1
+...
 </pre></div>
 
-<h2>NOTES</h2>
-
-The simple feature data model used by OGR (or PostGIS) is very
-different from the topological format used by GRASS. Instead of true
-topology, so called 'pseudo topology' is created for data linked by
-<em>v.external</em>. User should learn the difference between those
-two formats, because some modules working correctly with GRASS native
-data, can produce wrong results with input vector maps created
-by <em>v.external</em>.
-
-<p>See <em><a href="v.db.connect.html">v.db.connect</a></em> for an example of
-maintaining attributes in external DBMS in also writable mode.
-
 <h2>REFERENCES</h2>
 
-<a href="http://www.gdal.org/ogr__api_8h.html">OGR vector library C API</a> documentation
+<a href="http://www.gdal.org/ogr__api_8h.html">OGR vector library C
+API</a> documentation
 
 <h2>SEE ALSO</h2>
 
@@ -92,7 +124,7 @@
 </em>
 
 <p>
-<a href="http://www.gdal.org/">OGR Library</a>
+<a href="http://www.gdal.org/">GDAL Library</a>
 <br>
 <a href="http://postgis.org/">PostGIS</a>
 
@@ -104,7 +136,8 @@
 
 Radim Blazek, ITC-Irst, Trento, Italy
 <br>
-PostGIS support by Martin Landa, Czech Technical University in Prague, Czech Republic
+PostGIS support by Martin Landa, GeoForAll (OSGeoREL) Lab, Czech
+Technical University in Prague, Czech Republic
 
 <p>
 <i>Last changed: $Date$</i>



More information about the grass-commit mailing list