fix null feature handling in ogr input - closes #1542

This commit is contained in:
Dane Springmeyer 2012-10-20 20:16:17 -07:00
parent f7333542f2
commit 28c35055e4
4 changed files with 29 additions and 78 deletions

View File

@ -23,7 +23,6 @@
#include "ogr_datasource.hpp"
#include "ogr_featureset.hpp"
#include "ogr_index_featureset.hpp"
#include "ogr_feature_ptr.hpp"
#include <gdal_version.h>
@ -394,10 +393,10 @@ boost::optional<mapnik::datasource::geometry_t> ogr_datasource::get_geometry_typ
// only new either reset of setNext
//layer->ResetReading();
layer->SetNextByIndex(0);
ogr_feature_ptr feat(layer->GetNextFeature());
if ((*feat) != NULL)
OGRFeature *poFeature;
while ((poFeature = layer->GetNextFeature()) != NULL)
{
OGRGeometry* geom = (*feat)->GetGeometryRef();
OGRGeometry* geom = poFeature->GetGeometryRef();
if (geom && ! geom->IsEmpty())
{
switch (wkbFlatten(geom->getGeometryType()))
@ -422,6 +421,8 @@ boost::optional<mapnik::datasource::geometry_t> ogr_datasource::get_geometry_typ
break;
}
}
OGRFeature::DestroyFeature( poFeature );
break;
}
}
break;

View File

@ -1,54 +0,0 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef OGR_FEATURE_PTR_HPP
#define OGR_FEATURE_PTR_HPP
// ogr
#include <ogrsf_frmts.h>
class ogr_feature_ptr
{
public:
ogr_feature_ptr(OGRFeature* const feat)
: feat_(feat)
{
}
~ogr_feature_ptr()
{
if (feat_ != NULL)
{
OGRFeature::DestroyFeature(feat_);
}
}
OGRFeature* operator*()
{
return feat_;
}
private:
OGRFeature* feat_;
};
#endif // OGR_FEATURE_PTR_HPP

View File

@ -35,7 +35,6 @@
// ogr
#include "ogr_featureset.hpp"
#include "ogr_converter.hpp"
#include "ogr_feature_ptr.hpp"
using mapnik::query;
using mapnik::box2d;
@ -84,23 +83,24 @@ ogr_featureset::~ogr_featureset()
feature_ptr ogr_featureset::next()
{
ogr_feature_ptr feat (layer_.GetNextFeature());
while ((*feat) != NULL)
OGRFeature *poFeature;
while ((poFeature = layer_.GetNextFeature()) != NULL)
{
// ogr feature ids start at 0, so add one to stay
// consistent with other mapnik datasources that start at 1
const int feature_id = ((*feat)->GetFID() + 1);
const int feature_id = (poFeature->GetFID() + 1);
feature_ptr feature(feature_factory::create(ctx_,feature_id));
OGRGeometry* geom = (*feat)->GetGeometryRef();
OGRGeometry* geom = poFeature->GetGeometryRef();
if (geom && ! geom->IsEmpty())
{
ogr_converter::convert_geometry(geom, feature);
}
else
{
MAPNIK_LOG_DEBUG(ogr) << "ogr_featureset: Feature with null geometry=" << (*feat)->GetFID();
MAPNIK_LOG_DEBUG(ogr) << "ogr_featureset: Feature with null geometry="
<< poFeature->GetFID();
OGRFeature::DestroyFeature( poFeature );
continue;
}
@ -117,20 +117,20 @@ feature_ptr ogr_featureset::next()
{
case OFTInteger:
{
feature->put( fld_name, (*feat)->GetFieldAsInteger(i));
feature->put( fld_name, poFeature->GetFieldAsInteger(i));
break;
}
case OFTReal:
{
feature->put( fld_name, (*feat)->GetFieldAsDouble(i));
feature->put( fld_name, poFeature->GetFieldAsDouble(i));
break;
}
case OFTString:
case OFTWideString: // deprecated !
{
UnicodeString ustr = tr_->transcode((*feat)->GetFieldAsString(i));
UnicodeString ustr = tr_->transcode(poFeature->GetFieldAsString(i));
feature->put( fld_name, ustr);
break;
}
@ -166,6 +166,7 @@ feature_ptr ogr_featureset::next()
}
}
}
OGRFeature::DestroyFeature( poFeature );
return feature;
}

View File

@ -40,7 +40,6 @@
#include "ogr_index_featureset.hpp"
#include "ogr_converter.hpp"
#include "ogr_index.hpp"
#include "ogr_feature_ptr.hpp"
using mapnik::query;
using mapnik::box2d;
@ -92,24 +91,27 @@ feature_ptr ogr_index_featureset<filterT>::next()
int pos = *itr_++;
layer_.SetNextByIndex (pos);
ogr_feature_ptr feat (layer_.GetNextFeature());
if ((*feat) == NULL)
OGRFeature *poFeature = layer_.GetNextFeature();
if (poFeature == NULL)
{
continue;
return feature_ptr();
}
// ogr feature ids start at 0, so add one to stay
// consistent with other mapnik datasources that start at 1
int feature_id = ((*feat)->GetFID() + 1);
int feature_id = (poFeature->GetFID() + 1);
feature_ptr feature(feature_factory::create(ctx_,feature_id));
OGRGeometry* geom=(*feat)->GetGeometryRef();
OGRGeometry* geom=poFeature->GetGeometryRef();
if (geom && !geom->IsEmpty())
{
ogr_converter::convert_geometry (geom, feature);
}
else
{
MAPNIK_LOG_DEBUG(ogr) << "ogr_index_featureset: Feature with null geometry=" << (*feat)->GetFID();
MAPNIK_LOG_DEBUG(ogr) << "ogr_index_featureset: Feature with null geometry="
<< poFeature->GetFID();
OGRFeature::DestroyFeature( poFeature );
continue;
}
@ -124,20 +126,20 @@ feature_ptr ogr_index_featureset<filterT>::next()
{
case OFTInteger:
{
feature->put(fld_name,(*feat)->GetFieldAsInteger (i));
feature->put(fld_name,poFeature->GetFieldAsInteger (i));
break;
}
case OFTReal:
{
feature->put(fld_name,(*feat)->GetFieldAsDouble (i));
feature->put(fld_name,poFeature->GetFieldAsDouble (i));
break;
}
case OFTString:
case OFTWideString: // deprecated !
{
UnicodeString ustr = tr_->transcode((*feat)->GetFieldAsString (i));
UnicodeString ustr = tr_->transcode(poFeature->GetFieldAsString (i));
feature->put(fld_name,ustr);
break;
}
@ -167,6 +169,7 @@ feature_ptr ogr_index_featureset<filterT>::next()
}
}
}
OGRFeature::DestroyFeature( poFeature );
return feature;
}