[GRASS-SVN] r63604 - grass/trunk/vector/v.in.ogr
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri Dec 19 01:18:23 PST 2014
Author: martinl
Date: 2014-12-19 01:18:23 -0800 (Fri, 19 Dec 2014)
New Revision: 63604
Modified:
grass/trunk/vector/v.in.ogr/main.c
Log:
v.in.ogr: support GDAL RFC41 (Support for multiple geometry fields in OGR)
Modified: grass/trunk/vector/v.in.ogr/main.c
===================================================================
--- grass/trunk/vector/v.in.ogr/main.c 2014-12-19 08:24:06 UTC (rev 63603)
+++ grass/trunk/vector/v.in.ogr/main.c 2014-12-19 09:18:23 UTC (rev 63604)
@@ -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, first geometry column from the input is 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,13 @@
if (Ogr_ds == NULL)
G_fatal_error(_("Unable to open data source <%s>"), dsn);
+ if (param.geom->answer &&
+ !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;
+ }
+
/* check encoding for given driver */
if (param.encoding->answer) {
const char *ogr_driver;
@@ -816,6 +829,13 @@
Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layer_id);
Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
+ igeom = -1;
+ 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));
+ }
n_features = feature_count = 0;
@@ -827,15 +847,21 @@
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 */
+ 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);
+ 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);
}
G_percent(1, 1, 1);
}
@@ -897,6 +923,10 @@
Ogr_layer = OGR_DS_GetLayer(Ogr_ds, layer_id);
Ogr_featuredefn = OGR_L_GetLayerDefn(Ogr_layer);
+ igeom = -1;
+ if (param.geom->answer)
+ igeom = OGR_FD_GetGeomFieldIndex(Ogr_featuredefn, param.geom->answer);
+
if (param.key->answer) {
const char *fid_column;
fid_column = OGR_L_GetFIDColumn(Ogr_layer);
@@ -1077,34 +1107,40 @@
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 */
+ 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);
+
+ 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);
+ }
+ }
+
/* Attributes */
if (!flag.notab->answer) {
sprintf(buf, "insert into %s values ( %d", Fi->table, cat);
@@ -1335,6 +1371,12 @@
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 (param.geom->answer)
+ igeom = OGR_FD_GetGeomFieldIndex(Ogr_featuredefn, param.geom->answer);
+
n_features = OGR_L_GetFeatureCount(Ogr_layer, 1);
OGR_L_ResetReading(Ogr_layer);
@@ -1344,11 +1386,16 @@
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);
- }
+ 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);
+ if (Ogr_geometry != NULL) {
+ centroid(Ogr_geometry, Centr, &si, layer + 1, cat,
+ min_area, type);
+ }
+ }
OGR_F_Destroy(Ogr_feature);
}
More information about the grass-commit
mailing list