+ability to choose ogr layer by index number

This commit is contained in:
Dane Springmeyer 2010-06-19 15:13:47 +00:00
parent 0706b4fbaa
commit 7e2a2aab5a
3 changed files with 36 additions and 6 deletions

View file

@ -14,6 +14,8 @@ For a complete change history, see the SVN log.
Mapnik Trunk 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) - 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). - Made libjpeg dependency optional at compile time and added mapnik2.has_jpeg() method to check for support in python (#545).

View file

@ -493,9 +493,10 @@ def Ogr(**keywords):
Required keyword arguments: Required keyword arguments:
file -- path to OGR supported dataset 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: Optional keyword arguments:
layer_by_index -- choose layer by index number instead of by layer name.
base -- path prefix (default None) base -- path prefix (default None)
encoding -- file encoding (default 'utf-8') encoding -- file encoding (default 'utf-8')
multiple_geometries -- boolean, direct the Mapnik wkb reader to interpret as multigeometries (default False) multiple_geometries -- boolean, direct the Mapnik wkb reader to interpret as multigeometries (default False)

View file

@ -67,6 +67,8 @@ ogr_datasource::ogr_datasource(parameters const& params)
boost::optional<std::string> file = params.get<std::string>("file"); boost::optional<std::string> file = params.get<std::string>("file");
if (!file) throw datasource_exception("missing <file> parameter"); if (!file) throw datasource_exception("missing <file> parameter");
boost::optional<unsigned> layer_idx = params.get<unsigned>("layer_by_index");
multiple_geometries_ = *params_.get<mapnik::boolean>("multiple_geometries",false); multiple_geometries_ = *params_.get<mapnik::boolean>("multiple_geometries",false);
boost::optional<std::string> base = params.get<std::string>("base"); boost::optional<std::string> base = params.get<std::string>("base");
@ -88,7 +90,30 @@ ogr_datasource::ogr_datasource(parameters const& params)
} }
// initialize layer // initialize layer
boost::optional<std::string> layer = params.get<std::string>("layer"); boost::optional<std::string> layer = params.get<std::string>("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) if (!layer)
{ {
std::string s ("missing <layer> parameter, available layers are: "); std::string s ("missing <layer> parameter, available layers are: ");
@ -105,12 +130,14 @@ ogr_datasource::ogr_datasource(parameters const& params)
s += "No layers found!"; 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 <layer> in dataset");
} }
layerName_ = *layer;
layer_ = dataset_->GetLayerByName (layerName_.c_str());
if (! layer_) throw datasource_exception("cannot find <layer> in dataset");
// initialize envelope // initialize envelope
OGREnvelope envelope; OGREnvelope envelope;