[GRASS-SVN] r63690 - in grass/branches/releasebranch_7_0: . vector/v.external vector/v.in.ogr

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Dec 22 13:19:29 PST 2014


Author: martinl
Date: 2014-12-22 13:19:29 -0800 (Mon, 22 Dec 2014)
New Revision: 63690

Modified:
   grass/branches/releasebranch_7_0/
   grass/branches/releasebranch_7_0/vector/v.external/args.c
   grass/branches/releasebranch_7_0/vector/v.external/list.c
   grass/branches/releasebranch_7_0/vector/v.in.ogr/main.c
   grass/branches/releasebranch_7_0/vector/v.in.ogr/v.in.ogr.html
Log:
v.in.ogr: support GDAL RFC41 (Support for multiple geometry fields in OGR)
v.external: -t print also geometry column (useful when layer has multiple geometries)
fix compilation of v.in.ogr and v.external agaist GDAL < 1.11
v.in.ogr: add example from multiple geometry columns to the manual
          (merge r63604,63608-63609,63618,63685,63689 from trunk)



Property changes on: grass/branches/releasebranch_7_0
___________________________________________________________________
Modified: svn:mergeinfo
   - /grass/trunk:60817,61096,61141,61994,62105,62179-62180,62182,62190,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62691,62705,62709,62723,62730,62739,62741,62743,62746,62750-62752,62757,62762,62785,62798,62800-62801,62803,62805,62812,62822,62824,62828,62831,62838,62845,62847,62850,62856,62879,62881,62886,62904,62907-62908,62910,62912,62914,62916,62918,62920,62925,62932-62933,62935,62940,62942,62944-62946,62949,62955-62956,62958,62960,62962,62964,62966-62968,62970,62973,62975,62977,62981,62983,62985,62987,62989,62991,62993,62995,62997,62999-63000,63003,63005,63007,63009,63011,63013,63015,63017,63020,63022,63024,63026,63028-63031,63033,63035,63037,63040,63043-63044,63047,63049,63051,63053,63055,63057,63060,63062-63064,63066,63068,63070-63071,63074,
 63076,63079,63081,63083,63085,63087,63089,63091,63093,63095,63098,63100,63102,63105,63107,63109,63111,63113-63114,63116,63119,63121,63123,63125,63130,63132-63133,63135,63137,63140,63143,63145,63147,63149,63151,63153-63154,63157,63160,63165,63170,63173,63175,63187,63192-63193,63196,63199-63200,63202,63209,63216,63220-63221,63224,63227,63240,63246,63250,63255,63259,63261,63275-63276,63279,63281,63283,63287,63290,63292,63297,63302,63304-63307,63315,63319,63327,63330,63332,63339,63342,63345,63362,63367,63379,63389,63391,63393,63402,63408-63409,63416-63417,63425,63427,63429,63431,63433,63448,63451,63453,63457,63459,63464-63470,63473,63482,63497,63505,63508,63510,63515,63521-63524,63526,63536-63537,63551-63552,63554,63556,63558-63559,63562,63570,63576,63582,63589-63593,63602,63616,63624,63626,63628,63630,63634,63639,63642,63644,63650,63652,63656,63679
   + /grass/trunk:60817,61096,61141,61994,62105,62179-62180,62182,62190,62403,62422,62424,62437,62466,62469,62487,62491,62494,62501,62506,62508-62509,62515,62518-62519,62521,62526,62533,62539,62541,62555,62562,62566,62570,62573,62575,62585,62588,62597,62603,62606,62608-62609,62614,62618,62628,62632,62638,62642,62648-62649,62652,62654-62657,62666,62691,62705,62709,62723,62730,62739,62741,62743,62746,62750-62752,62757,62762,62785,62798,62800-62801,62803,62805,62812,62822,62824,62828,62831,62838,62845,62847,62850,62856,62879,62881,62886,62904,62907-62908,62910,62912,62914,62916,62918,62920,62925,62932-62933,62935,62940,62942,62944-62946,62949,62955-62956,62958,62960,62962,62964,62966-62968,62970,62973,62975,62977,62981,62983,62985,62987,62989,62991,62993,62995,62997,62999-63000,63003,63005,63007,63009,63011,63013,63015,63017,63020,63022,63024,63026,63028-63031,63033,63035,63037,63040,63043-63044,63047,63049,63051,63053,63055,63057,63060,63062-63064,63066,63068,63070-63071,63074,
 63076,63079,63081,63083,63085,63087,63089,63091,63093,63095,63098,63100,63102,63105,63107,63109,63111,63113-63114,63116,63119,63121,63123,63125,63130,63132-63133,63135,63137,63140,63143,63145,63147,63149,63151,63153-63154,63157,63160,63165,63170,63173,63175,63187,63192-63193,63196,63199-63200,63202,63209,63216,63220-63221,63224,63227,63240,63246,63250,63255,63259,63261,63275-63276,63279,63281,63283,63287,63290,63292,63297,63302,63304-63307,63315,63319,63327,63330,63332,63339,63342,63345,63362,63367,63379,63389,63391,63393,63402,63408-63409,63416-63417,63425,63427,63429,63431,63433,63448,63451,63453,63457,63459,63464-63470,63473,63482,63497,63505,63508,63510,63515,63521-63524,63526,63536-63537,63551-63552,63554,63556,63558-63559,63562,63570,63576,63582,63589-63593,63602,63604,63608-63609,63616,63618,63624,63626,63628,63630,63634,63639,63642,63644,63650,63652,63656,63679,63685,63689

Modified: grass/branches/releasebranch_7_0/vector/v.external/args.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.external/args.c	2014-12-22 21:16:25 UTC (rev 63689)
+++ grass/branches/releasebranch_7_0/vector/v.external/args.c	2014-12-22 21:19:29 UTC (rev 63690)
@@ -51,7 +51,7 @@
     flags->tlist->key = 't';
     flags->tlist->label = _("List available layers including feature type "
 			    "in data source and exit");
-    flags->tlist->description = _("Format: layer name,type,projection check");
+    flags->tlist->description = _("Format: layer name,type,projection check,geometry");
     flags->tlist->guisection = _("Print");
     flags->tlist->suppress_required = YES;
 

Modified: grass/branches/releasebranch_7_0/vector/v.external/list.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.external/list.c	2014-12-22 21:16:25 UTC (rev 63689)
+++ grass/branches/releasebranch_7_0/vector/v.external/list.c	2014-12-22 21:19:29 UTC (rev 63690)
@@ -278,14 +278,18 @@
     for (i = 0; i < nlayers; i++) {
 	Ogr_layer = OGR_DS_GetLayer(Ogr_ds, i);
 	Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
-	Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn);
+#if GDAL_VERSION_NUM < 1110000      
+        Ogr_geom_type = OGR_FD_GetGeomType(Ogr_featuredefn);
+#endif
 	layer_name = (char *) OGR_FD_GetName(Ogr_featuredefn);
 
 	if (fd) {
 	    if (print_types) {
-		int proj_same;
+                int proj_same, igeom;
 		OGRSpatialReferenceH Ogr_projection;
-
+#if GDAL_VERSION_NUM >= 1110000
+                OGRGeomFieldDefnH Ogr_geomdefn;
+#endif
 		/* projection check */
 		Ogr_projection = OGR_L_GetSpatialRef(Ogr_layer);
 		proj_same = 0;
@@ -304,9 +308,24 @@
 			proj_same = 0;
 		}
 		G_suppress_warnings(FALSE);
-		fprintf(fd, "%s,%s,%d\n", layer_name,
-			feature_type(OGRGeometryTypeToName(Ogr_geom_type)),
-			proj_same);
+#if GDAL_VERSION_NUM >= 1110000
+                for (igeom = 0; igeom < OGR_FD_GetGeomFieldCount(Ogr_featuredefn); igeom++) {
+                    Ogr_geomdefn = OGR_FD_GetGeomFieldDefn(Ogr_featuredefn, igeom);
+                    if (!Ogr_geomdefn) {
+                        G_warning(_("Invalid geometry column %d"), igeom);
+                        continue;
+                    }
+
+                    Ogr_geom_type = OGR_GFld_GetType(Ogr_geomdefn);
+                    fprintf(fd, "%s,%s,%d,%s\n", layer_name,
+                            feature_type(OGRGeometryTypeToName(Ogr_geom_type)),
+                            proj_same, OGR_GFld_GetNameRef(Ogr_geomdefn));
+                }
+#else
+                    fprintf(fd, "%s,%s,%d,\n", layer_name,
+                            feature_type(OGRGeometryTypeToName(Ogr_geom_type)),
+                            proj_same);
+#endif
 	    }
 	    else {
 		fprintf(fd, "%s\n", layer_name);

Modified: grass/branches/releasebranch_7_0/vector/v.in.ogr/main.c
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.in.ogr/main.c	2014-12-22 21:16:25 UTC (rev 63689)
+++ grass/branches/releasebranch_7_0/vector/v.in.ogr/main.c	2014-12-22 21:19:29 UTC (rev 63690)
@@ -52,7 +52,7 @@
     struct _param {
 	struct Option *dsn, *out, *layer, *spat, *where,
 	    *min_area;
-        struct Option *snap, *type, *outloc, *cnames, *encoding, *key;
+        struct Option *snap, *type, *outloc, *cnames, *encoding, *key, *geom;
     } param;
     struct _flag {
 	struct Flag *list, *no_clean, *force2d, *notab,
@@ -62,7 +62,7 @@
 
     char *desc;
 
-    int i, j, layer, nogeom, ncnames;
+    int i, j, layer, nogeom, ncnames, igeom;
     double xmin, ymin, xmax, ymax;
     int ncols = 0, type;
     double min_area, snap;
@@ -240,6 +240,12 @@
         _("If not given, categories are generated as unique values and stored in 'cat' column");
     param.key->guisection = _("Attributes");
 
+    param.geom = G_define_standard_option(G_OPT_DB_COLUMN);
+    param.geom->key = "geometry";
+    param.geom->label = _("Name of geometry column");
+    param.geom->description = _("If not given, all geometry columns from the input are used");
+    param.geom->guisection = _("Selection");
+
     flag.formats = G_define_flag();
     flag.formats->key = 'f';
     flag.formats->description = _("List supported OGR formats and exit");
@@ -427,6 +433,20 @@
     if (Ogr_ds == NULL)
 	G_fatal_error(_("Unable to open data source <%s>"), dsn);
 
+    if (param.geom->answer) {
+#if GDAL_VERSION_NUM >= 1110000
+        if (!OGR_DS_TestCapability(Ogr_ds, ODsCCreateGeomFieldAfterCreateLayer)) {
+            G_warning(_("Option <%s> will be ignored. OGR doesn't support it for selected format (%s)."),
+                      param.geom->key, OGR_Dr_GetName(OGR_DS_GetDriver(Ogr_ds)));
+            param.geom->answer = NULL;
+        }
+#else
+        G_warning(_("Option <%s> will be ignored. Multiple geometry fields are supported by GDAL >= 1.11"),
+                  param.geom->key);
+        param.geom->answer = NULL;
+#endif
+    }
+
     /* check encoding for given driver */
     if (param.encoding->answer) {
         const char *ogr_driver;
@@ -816,7 +836,15 @@
 
 	Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layer_id);
 	Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
-
+        igeom = -1;
+#if GDAL_VERSION_NUM >= 1110000
+        if (param.geom->answer) {
+            igeom = OGR_FD_GetGeomFieldIndex(Ogr_featuredefn, param.geom->answer);
+            if (igeom < 0)
+                G_fatal_error(_("Geometry column <%s> not found in OGR layer <%s>"),
+                              param.geom->answer, OGR_L_GetName(Ogr_layer));
+        }
+#endif
 	n_features = feature_count = 0;
 
 	n_features = OGR_L_GetFeatureCount(Ogr_layer, 1);
@@ -827,15 +855,27 @@
 		  layer_names[layer]);
 	while ((Ogr_feature = OGR_L_GetNextFeature(Ogr_layer)) != NULL) {
 	    G_percent(feature_count++, n_features, 1);	/* show something happens */
-	    /* Geometry */
-	    Ogr_geometry = OGR_F_GetGeometryRef(Ogr_feature);
-	    if (Ogr_geometry != NULL) {
-		if (!flag.no_clean->answer)
-		    poly_count(Ogr_geometry, (type & GV_BOUNDARY));
-		if (OGR_G_GetCoordinateDimension(Ogr_geometry) > 2)
-		    input3d = 1;
-	    }
-	    OGR_F_Destroy(Ogr_feature);
+
+            /* Geometry */
+#if GDAL_VERSION_NUM >= 1110000
+            for (i = 0; i < OGR_FD_GetGeomFieldCount(Ogr_featuredefn); i++) {
+                if (igeom > -1 && i != igeom)
+                    continue; /* use only geometry defined via param.geom */
+            
+                Ogr_geometry = OGR_F_GetGeomFieldRef(Ogr_feature, i);
+#else
+                Ogr_geometry = OGR_F_GetGeometryRef(Ogr_feature);
+#endif
+                if (Ogr_geometry != NULL) {
+                    if (!flag.no_clean->answer)
+                        poly_count(Ogr_geometry, (type & GV_BOUNDARY));
+                    if (OGR_G_GetCoordinateDimension(Ogr_geometry) > 2)
+                        input3d = 1;
+                }
+#if GDAL_VERSION_NUM >= 1110000                
+            }
+#endif
+            OGR_F_Destroy(Ogr_feature);
 	}
 	G_percent(1, 1, 1);
     }
@@ -897,6 +937,12 @@
 	Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layer_id);
 	Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
 
+        igeom = -1;
+#if GDAL_VERSION_NUM >= 1110000
+        if (param.geom->answer)
+            igeom = OGR_FD_GetGeomFieldIndex(Ogr_featuredefn, param.geom->answer);
+#endif
+        
         if (param.key->answer) {
             const char *fid_column;
             fid_column = OGR_L_GetFIDColumn(Ogr_layer);
@@ -1077,34 +1123,44 @@
 	    db_begin_transaction(driver);
 	}
 
-	/* Import feature */
+	/* Import features */
 	cat = 1;
 	nogeom = 0;
-	OGR_L_ResetReading(Ogr_layer);
-	n_features = feature_count = 0;
+	feature_count = 0;
 
-	n_features = OGR_L_GetFeatureCount(Ogr_layer, 1);
-
+	n_features = OGR_L_GetFeatureCount(Ogr_layer, TRUE);
 	G_important_message(_("Importing %d features (OGR layer <%s>)..."),
 			    n_features, layer_names[layer]);
-
+        
+        OGR_L_ResetReading(Ogr_layer);
 	while ((Ogr_feature = OGR_L_GetNextFeature(Ogr_layer)) != NULL) {
 	    G_percent(feature_count++, n_features, 1);	/* show something happens */
-	    /* Geometry */
-	    Ogr_geometry = OGR_F_GetGeometryRef(Ogr_feature);
-	    if (Ogr_geometry == NULL) {
-		nogeom++;
-	    }
-	    else {
-                if (key_idx > -1)
-                    cat = OGR_F_GetFieldAsInteger(Ogr_feature, key_idx);
-                else if (key_idx == -1)
-                    cat = OGR_F_GetFID(Ogr_feature);
-                
-		geom(Ogr_geometry, Out, layer + 1, cat, min_area, type,
-		     flag.no_clean->answer);
-	    }
 
+            /* Geometry */
+#if GDAL_VERSION_NUM >= 1110000
+            for (i = 0; i < OGR_FD_GetGeomFieldCount(Ogr_featuredefn); i++) {
+                if (igeom > -1 && i != igeom)
+                    continue; /* use only geometry defined via param.geom */
+            
+                Ogr_geometry = OGR_F_GetGeomFieldRef(Ogr_feature, i);
+#else
+                Ogr_geometry = OGR_F_GetGeometryRef(Ogr_feature);
+#endif                
+                if (Ogr_geometry == NULL) {
+                    nogeom++;
+                }
+                else {
+                    if (key_idx > -1)
+                        cat = OGR_F_GetFieldAsInteger(Ogr_feature, key_idx);
+                    else if (key_idx == -1)
+                        cat = OGR_F_GetFID(Ogr_feature);
+                    
+                    geom(Ogr_geometry, Out, layer + 1, cat, min_area, type,
+                         flag.no_clean->answer);
+                }
+#if GDAL_VERSION_NUM >= 1110000              
+            }
+#endif
 	    /* Attributes */
 	    if (!flag.notab->answer) {
 		sprintf(buf, "insert into %s values ( %d", Fi->table, cat);
@@ -1335,6 +1391,14 @@
 	    G_message(_("Finding centroids for OGR layer <%s>..."), layer_names[layer]);
 	    layer_id = layers[layer];
 	    Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layer_id);
+            Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
+
+            igeom = -1;
+#if GDAL_VERSION_NUM >= 1110000
+            if (param.geom->answer)
+                igeom = OGR_FD_GetGeomFieldIndex(Ogr_featuredefn, param.geom->answer);
+#endif
+            
 	    n_features = OGR_L_GetFeatureCount(Ogr_layer, 1);
 	    OGR_L_ResetReading(Ogr_layer);
 
@@ -1344,12 +1408,22 @@
 		cat++;
 		G_percent(cat, n_features, 2);
 		/* Geometry */
-		Ogr_geometry = OGR_F_GetGeometryRef(Ogr_feature);
-		if (Ogr_geometry != NULL) {
-		    centroid(Ogr_geometry, Centr, &si, layer + 1, cat,
-			     min_area, type);
-		}
-
+#if GDAL_VERSION_NUM >= 1110000
+                for (i = 0; i < OGR_FD_GetGeomFieldCount(Ogr_featuredefn); i++) {
+                    if (igeom > -1 && i != igeom)
+                        continue; /* use only geometry defined via param.geom */
+            
+                    Ogr_geometry = OGR_F_GetGeomFieldRef(Ogr_feature, i);
+#else
+                    Ogr_geometry = OGR_F_GetGeometryRef(Ogr_feature);
+#endif
+                    if (Ogr_geometry != NULL) {
+                        centroid(Ogr_geometry, Centr, &si, layer + 1, cat,
+                                 min_area, type);
+                    }
+#if GDAL_VERSION_NUM >= 1110000
+                }
+#endif
 		OGR_F_Destroy(Ogr_feature);
 	    }
 	}

Modified: grass/branches/releasebranch_7_0/vector/v.in.ogr/v.in.ogr.html
===================================================================
--- grass/branches/releasebranch_7_0/vector/v.in.ogr/v.in.ogr.html	2014-12-22 21:16:25 UTC (rev 63689)
+++ grass/branches/releasebranch_7_0/vector/v.in.ogr/v.in.ogr.html	2014-12-22 21:19:29 UTC (rev 63690)
@@ -149,6 +149,13 @@
 numbers are generated starting with 1 and stored in the column named
 "cat".
 
+<p>
+Starting with GDAL 1.11 the library supports multiple geometry columns
+in OGR. By default <em>v.in.ogr</em> reads all geometry columns from
+given layer. The user can choose desired geometry column
+by <b>geometry</b> option,
+see <a href="#multiple-geometry-columns">example bellow</a>.
+
 <h2>EXAMPLES</h2>
 
 The command imports various vector formats:
@@ -269,6 +276,35 @@
 </pre></div>
 -->
 
+<h3>Multiple geometry columns</h3>
+
+This example shows how to work with data which contain multiple
+geometry per feature. The number of geometry columns per feature can
+be checked by <em><a href="v.external.html">v.external</a></em>
+together with <b>-t</b> flag.
+                  
+<div class="code"><pre>
+v.external -t input=20141130_ST_UKSH.xml.gz
+...
+Okresy,point,1,DefinicniBod
+Okresy,multipolygon,1,OriginalniHranice
+Okresy,multipolygon,1,GeneralizovaneHranice
+...
+</pre></div>      
+
+In our example layer "Okresy" has three geometry columns:
+"DefinicniBod", "OriginalniHranice" and
+"GeneralizovanaHranice". By default <em>v.in.ogr</em> reads data from
+all three geometry columns. The user can specify desired geometry
+column by <b>geometry</b> option, in this case the module will read
+geometry only from the specified geometry column. In the example bellow,
+the output vector map will contain only geometry saved in
+"OriginalniHranice" geometry column.
+
+<div class="code"><pre>
+v.in.ogr input=20141130_ST_UKSH.xml.gz layer=Okresy geometry=OriginalniHranice
+</pre></div>      
+
 <h2>WARNINGS</h2>
 
 If a message like "WARNING: Area size 1.3e-06, area not
@@ -323,11 +359,13 @@
 
 <h2>AUTHORS</h2>
 
-Radim Blazek, ITC-irst, Trento, Italy
+Original author: Radim Blazek, ITC-irst, Trento, Italy
 <br>
 Location and spatial extent support by Markus Neteler and Paul Kelly
 <br>
-Markus Metz
+Various improvements by Markus Metz
+<br>
+Multiple geometry columns support by Martin Landa, OSGeoREL, Czech Technical University in Prague, Czech Republic
 
 <p>
 <i>Last changed: $Date$</i>



More information about the grass-commit mailing list