From 7e2a2aab5a46629ce2c31f912e99750e0f2cb1dd Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Sat, 19 Jun 2010 15:13:47 +0000 Subject: [PATCH] +ability to choose ogr layer by index number --- CHANGELOG | 2 ++ bindings/python/mapnik/__init__.py | 3 ++- plugins/input/ogr/ogr_datasource.cpp | 37 ++++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 130b452fe..f0deb03ae 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -14,6 +14,8 @@ For a complete change history, see the SVN log. Mapnik Trunk ------------ +- Added support for choosing OGR layer by index number using 'layer_by_index' parameter (r1904) + - Added support for reading jpeg images (in addition to png/tiff) for image symbolizers (#518) - Made libjpeg dependency optional at compile time and added mapnik2.has_jpeg() method to check for support in python (#545). diff --git a/bindings/python/mapnik/__init__.py b/bindings/python/mapnik/__init__.py index b42a44ac3..6b2652cd6 100644 --- a/bindings/python/mapnik/__init__.py +++ b/bindings/python/mapnik/__init__.py @@ -493,9 +493,10 @@ def Ogr(**keywords): Required keyword arguments: file -- path to OGR supported dataset - layer -- layer to use within datasource + layer -- name of layer to use within datasource (optional if layer_by_index is used) Optional keyword arguments: + layer_by_index -- choose layer by index number instead of by layer name. base -- path prefix (default None) encoding -- file encoding (default 'utf-8') multiple_geometries -- boolean, direct the Mapnik wkb reader to interpret as multigeometries (default False) diff --git a/plugins/input/ogr/ogr_datasource.cpp b/plugins/input/ogr/ogr_datasource.cpp index a7e83e1ad..7aa67971b 100644 --- a/plugins/input/ogr/ogr_datasource.cpp +++ b/plugins/input/ogr/ogr_datasource.cpp @@ -67,6 +67,8 @@ ogr_datasource::ogr_datasource(parameters const& params) boost::optional file = params.get("file"); if (!file) throw datasource_exception("missing parameter"); + boost::optional layer_idx = params.get("layer_by_index"); + multiple_geometries_ = *params_.get("multiple_geometries",false); boost::optional base = params.get("base"); @@ -88,7 +90,30 @@ ogr_datasource::ogr_datasource(parameters const& params) } // initialize layer + boost::optional layer = params.get("layer"); + + if (layer_idx && !layer) + { + OGRLayer *ogr_layer = dataset_->GetLayer(*layer_idx); + if (ogr_layer) + { + OGRFeatureDefn* def = ogr_layer->GetLayerDefn(); + if (def != 0) { + layerName_ = def->GetName(); + layer_ = ogr_layer; + } + /*else + { + throw datasource_exception("No layers found!"); + }*/ + } + /*else + { + throw datasource_exception("No layers found!"); + }*/ + } + if (!layer) { std::string s ("missing parameter, available layers are: "); @@ -105,12 +130,14 @@ ogr_datasource::ogr_datasource(parameters const& params) s += "No layers found!"; } } - throw datasource_exception(s); + throw datasource_exception(s); + } + else + { + layerName_ = *layer; + layer_ = dataset_->GetLayerByName (layerName_.c_str()); + if (! layer_) throw datasource_exception("cannot find in dataset"); } - - layerName_ = *layer; - layer_ = dataset_->GetLayerByName (layerName_.c_str()); - if (! layer_) throw datasource_exception("cannot find in dataset"); // initialize envelope OGREnvelope envelope;