osgEarth 2.1.1
|
A FeatureSource that reads features from an OGR driver.
This FeatureSource does NOT support styling.
Definition at line 48 of file FeatureSourceOGR.cpp.
OGRFeatureSource::OGRFeatureSource | ( | const OGRFeatureOptions & | options | ) | [inline] |
Definition at line 51 of file FeatureSourceOGR.cpp.
: FeatureSource( options ), _dsHandle( 0L ), _layerHandle( 0L ), _ogrDriverHandle( 0L ), _options( options ), _featureCount(-1), _needsSync(false), _writable(false) { _geometry = _options.geometry().valid() ? _options.geometry().get() : _options.geometryConfig().isSet() ? parseGeometry( *_options.geometryConfig() ) : _options.geometryUrl().isSet() ? parseGeometryUrl( *_options.geometryUrl() ) : 0L; }
virtual OGRFeatureSource::~OGRFeatureSource | ( | ) | [inline, virtual] |
Destruct the object, cleaning up and OGR handles.
Definition at line 68 of file FeatureSourceOGR.cpp.
{ OGR_SCOPED_LOCK; if ( _layerHandle ) { if (_needsSync) { OGR_L_SyncToDisk( _layerHandle ); // for writing only const char* name = OGR_FD_GetName( OGR_L_GetLayerDefn( _layerHandle ) ); std::stringstream buf; buf << "REPACK " << name; std::string bufStr; bufStr = buf.str(); OGR_DS_ExecuteSQL( _dsHandle, bufStr.c_str(), 0L, 0L ); } _layerHandle = 0L; } if ( _dsHandle ) { OGRReleaseDataSource( _dsHandle ); _dsHandle = 0L; } }
FeatureCursor* OGRFeatureSource::createFeatureCursor | ( | const Symbology::Query & | query | ) | [inline, virtual] |
Creates a cursor that iterates over all the features corresponding to the specified query.
Caller takes ownership of the returned object.
Implements osgEarth::Features::FeatureSource.
Definition at line 260 of file FeatureSourceOGR.cpp.
{ if ( _geometry.valid() ) { return new GeometryFeatureCursor( _geometry.get(), getFeatureProfile(), _options.filters() ); //getFilters() ); } else { OGR_SCOPED_LOCK; // Each cursor requires its own DS handle so that multi-threaded access will work. // The cursor impl will dispose of the new DS handle. OGRDataSourceH dsHandle = OGROpenShared( _source.c_str(), 0, &_ogrDriverHandle ); if ( dsHandle ) { OGRLayerH layerHandle = OGR_DS_GetLayer( dsHandle, 0 ); return new FeatureCursorOGR( dsHandle, layerHandle, getFeatureProfile(), query, _options.filters() ); } else { return 0L; } } }
const FeatureProfile* OGRFeatureSource::createFeatureProfile | ( | ) | [inline, virtual] |
Called once at startup to create the profile for this feature set. Successful profile creation implies that the datasource opened succesfully.
Implements osgEarth::Features::FeatureSource.
Definition at line 109 of file FeatureSourceOGR.cpp.
{ FeatureProfile* result = 0L; // see if we have a custom profile. osg::ref_ptr<const Profile> profile; if ( _options.profile().isSet() ) { profile = Profile::create( *_options.profile() ); } if ( _geometry.valid() ) { // if the user specified explicit geometry/profile, use that: GeoExtent ex; if ( profile.valid() ) { ex = profile->getExtent(); } if ( !ex.isValid() ) { // default to WGS84 Lat/Long ex = osgEarth::Registry::instance()->getGlobalGeodeticProfile()->getExtent(); } result = new FeatureProfile( ex ); } else if ( !_source.empty() ) { // otherwise, assume we're loading from the URL: OGR_SCOPED_LOCK; // load up the driver, defaulting to shapefile if unspecified. std::string driverName = _options.ogrDriver().value(); if ( driverName.empty() ) driverName = "ESRI Shapefile"; _ogrDriverHandle = OGRGetDriverByName( driverName.c_str() ); // attempt to open the dataset: int openMode = _options.openWrite().isSet() && _options.openWrite().value() ? 1 : 0; _dsHandle = OGROpenShared( _source.c_str(), openMode, &_ogrDriverHandle ); if ( _dsHandle ) { if (openMode == 1) _writable = true; _layerHandle = OGR_DS_GetLayer( _dsHandle, 0 ); // default to layer 0 for now if ( _layerHandle ) { GeoExtent extent; // if the user provided a profile, user that: if ( profile.valid() ) { result = new FeatureProfile( profile->getExtent() ); } else { // extract the SRS and Extent: OGRSpatialReferenceH srHandle = OGR_L_GetSpatialRef( _layerHandle ); if ( srHandle ) { osg::ref_ptr<SpatialReference> srs = SpatialReference::createFromHandle( srHandle, false ); if ( srs.valid() ) { // extract the full extent of the layer: OGREnvelope env; if ( OGR_L_GetExtent( _layerHandle, &env, 1 ) == OGRERR_NONE ) { GeoExtent extent( srs.get(), env.MinX, env.MinY, env.MaxX, env.MaxY ); // got enough info to make the profile! result = new FeatureProfile( extent ); } } } } // assuming we successfully opened the layer, build a spatial index if requested. if ( _options.buildSpatialIndex() == true ) { OE_INFO << LC << "Building spatial index for " << getName() << std::endl; std::stringstream buf; const char* name = OGR_FD_GetName( OGR_L_GetLayerDefn( _layerHandle ) ); buf << "CREATE SPATIAL INDEX ON " << name; std::string bufStr; bufStr = buf.str(); OGR_DS_ExecuteSQL( _dsHandle, bufStr.c_str(), 0L, 0L ); } //Get the feature count _featureCount = OGR_L_GetFeatureCount( _layerHandle, 1 ); initSchema(); OGRwkbGeometryType wkbType = OGR_FD_GetGeomType( OGR_L_GetLayerDefn( _layerHandle ) ); if ( wkbType == wkbPolygon || wkbType == wkbPolygon25D ) { _geometryType = Geometry::TYPE_POLYGON; } else if ( wkbType == wkbLineString || wkbType == wkbLineString25D ) { _geometryType = Geometry::TYPE_LINESTRING; } else if ( wkbType == wkbLinearRing ) { _geometryType = Geometry::TYPE_RING; } else if ( wkbType == wkbPoint || wkbType == wkbPoint25D ) { _geometryType = Geometry::TYPE_POINTSET; } else if ( wkbType == wkbGeometryCollection || wkbType == wkbGeometryCollection25D || wkbType == wkbMultiPoint || wkbType == wkbMultiPoint25D || wkbType == wkbMultiLineString || wkbType == wkbMultiLineString25D || wkbType == wkbMultiPolygon || wkbType == wkbMultiPolygon25D ) { _geometryType = Geometry::TYPE_MULTI; } } } else { OE_INFO << LC << "failed to open dataset \"" << _source << "\"" << std::endl; } } else { OE_INFO << LC << "Feature Source: no valid source data available" << std::endl; } return result; }
virtual bool OGRFeatureSource::deleteFeature | ( | FeatureID | fid | ) | [inline, virtual] |
Deletes the feature with the given FID
Reimplemented from osgEarth::Features::FeatureSource.
Definition at line 296 of file FeatureSourceOGR.cpp.
{ if (_writable && _layerHandle) { if (OGR_L_DeleteFeature( _layerHandle, fid ) == OGRERR_NONE) { _needsSync = true; return true; } } return false; }
Gets the Feature with the given FID
Reimplemented from osgEarth::Features::FeatureSource.
Definition at line 314 of file FeatureSourceOGR.cpp.
{ Feature* result = NULL; OGRFeatureH handle = OGR_L_GetFeature( _layerHandle, fid); if (handle) { result = OgrUtils::createFeature( handle ); OGR_F_Destroy( handle ); } return result; }
virtual int OGRFeatureSource::getFeatureCount | ( | ) | const [inline, virtual] |
Gets the number of features in this FeatureSource
Reimplemented from osgEarth::Features::FeatureSource.
Definition at line 309 of file FeatureSourceOGR.cpp.
{ return _featureCount; }
virtual osgEarth::Symbology::Geometry::Type OGRFeatureSource::getGeometryType | ( | ) | const [inline, virtual] |
Gets the Geometry type of the FeatureSource
Reimplemented from osgEarth::Features::FeatureSource.
Definition at line 420 of file FeatureSourceOGR.cpp.
{ return _geometryType; }
const FeatureSchema& OGRFeatureSource::getSchema | ( | ) | const [inline, virtual] |
Gets the FeatureSchema for this FeatureSource. If the schema doesn't publish a source, this might be empty.
Reimplemented from osgEarth::Features::FeatureSource.
Definition at line 331 of file FeatureSourceOGR.cpp.
{ return _schema; }
void OGRFeatureSource::initialize | ( | const std::string & | referenceURI | ) | [inline, virtual] |
Initialize the FeatureSource. The profile should be computed and set here using setProfile()
Implements osgEarth::Features::FeatureSource.
Definition at line 95 of file FeatureSourceOGR.cpp.
{ if ( _options.url().isSet() ) { _source = osgEarth::getFullPath( referenceURI, _options.url()->full() ); } else if ( _options.connection().isSet() ) { _source = _options.connection().value(); } }
void OGRFeatureSource::initSchema | ( | ) | [inline, protected] |
Definition at line 445 of file FeatureSourceOGR.cpp.
{ OGRFeatureDefnH layerDef = OGR_L_GetLayerDefn( _layerHandle ); for (int i = 0; i < OGR_FD_GetFieldCount( layerDef ); i++) { OGRFieldDefnH fieldDef = OGR_FD_GetFieldDefn( layerDef, i ); std::string name; name = std::string( OGR_Fld_GetNameRef( fieldDef ) ); OGRFieldType ogrType = OGR_Fld_GetType( fieldDef ); _schema[ name ] = OgrUtils::getAttributeType( ogrType ); } }
virtual bool OGRFeatureSource::insertFeature | ( | Feature * | feature | ) | [inline, virtual] |
Inserts the given feature into the FeatureSource
Reimplemented from osgEarth::Features::FeatureSource.
Definition at line 336 of file FeatureSourceOGR.cpp.
{ OGR_SCOPED_LOCK; OGRFeatureH feature_handle = OGR_F_Create( OGR_L_GetLayerDefn( _layerHandle ) ); if ( feature_handle ) { const AttributeTable& attrs = feature->getAttrs(); // assign the attributes: int num_fields = OGR_F_GetFieldCount( feature_handle ); for( int i=0; i<num_fields; i++ ) { OGRFieldDefnH field_handle_ref = OGR_F_GetFieldDefnRef( feature_handle, i ); std::string name = OGR_Fld_GetNameRef( field_handle_ref ); int field_index = OGR_F_GetFieldIndex( feature_handle, name.c_str() ); AttributeTable::const_iterator a = attrs.find( toLower(name) ); if ( a != attrs.end() ) { switch( OGR_Fld_GetType(field_handle_ref) ) { case OFTInteger: OGR_F_SetFieldInteger( feature_handle, field_index, a->second.getInt(0) ); break; case OFTReal: OGR_F_SetFieldDouble( feature_handle, field_index, a->second.getDouble(0.0) ); break; case OFTString: OGR_F_SetFieldString( feature_handle, field_index, a->second.getString().c_str() ); break; } } } // std::string value = feature->getAttr( name ); // if (!value.empty()) // { // switch( OGR_Fld_GetType( field_handle_ref ) ) // { // case OFTInteger: // OGR_F_SetFieldInteger( feature_handle, field_index, as<int>(value, 0) ); // break; // case OFTReal: // OGR_F_SetFieldDouble( feature_handle, field_index, as<double>(value, 0.0) ); // break; // case OFTString: // OGR_F_SetFieldString( feature_handle, field_index, value.c_str() ); // break; // } // } //} // assign the geometry: OGRFeatureDefnH def = ::OGR_L_GetLayerDefn( _layerHandle ); OGRwkbGeometryType reported_type = OGR_FD_GetGeomType( def ); OGRGeometryH ogr_geometry = OgrUtils::createOgrGeometry( feature->getGeometry(), reported_type ); if ( OGR_F_SetGeometryDirectly( feature_handle, ogr_geometry ) != OGRERR_NONE ) { OE_WARN << LC << "OGR_F_SetGeometryDirectly failed!" << std::endl; } if ( OGR_L_CreateFeature( _layerHandle, feature_handle ) != OGRERR_NONE ) { //TODO: handle error better OE_WARN << LC << "OGR_L_CreateFeature failed!" << std::endl; OGR_F_Destroy( feature_handle ); return false; } // clean up the feature OGR_F_Destroy( feature_handle ); } else { //TODO: handle error better OE_WARN << LC << "OGR_F_Create failed." << std::endl; return false; } return true; }
virtual bool OGRFeatureSource::isWritable | ( | ) | const [inline, virtual] |
Whether this FeatureSource supports inserting and deleting features
Reimplemented from osgEarth::Features::FeatureSource.
Definition at line 326 of file FeatureSourceOGR.cpp.
{ return _writable; }
Symbology::Geometry* OGRFeatureSource::parseGeometry | ( | const Config & | geomConf | ) | [inline, protected] |
Definition at line 428 of file FeatureSourceOGR.cpp.
{ return OgrUtils::createGeometryFromWKT( geomConf.value() ); }
Symbology::Geometry* OGRFeatureSource::parseGeometryUrl | ( | const std::string & | geomUrl | ) | [inline, protected] |
Definition at line 434 of file FeatureSourceOGR.cpp.
{ std::string wkt; if ( HTTPClient::readString( geomUrl, wkt ) == HTTPClient::RESULT_OK ) { Config conf( "geometry", wkt ); return parseGeometry( conf ); } return 0L; }
OGRDataSourceH OGRFeatureSource::_dsHandle [private] |
Definition at line 464 of file FeatureSourceOGR.cpp.
int OGRFeatureSource::_featureCount [private] |
Definition at line 469 of file FeatureSourceOGR.cpp.
osg::ref_ptr<Symbology::Geometry> OGRFeatureSource::_geometry [private] |
Definition at line 467 of file FeatureSourceOGR.cpp.
Geometry::Type OGRFeatureSource::_geometryType [private] |
Definition at line 473 of file FeatureSourceOGR.cpp.
OGRLayerH OGRFeatureSource::_layerHandle [private] |
Definition at line 465 of file FeatureSourceOGR.cpp.
bool OGRFeatureSource::_needsSync [private] |
Definition at line 470 of file FeatureSourceOGR.cpp.
OGRSFDriverH OGRFeatureSource::_ogrDriverHandle [private] |
Definition at line 466 of file FeatureSourceOGR.cpp.
const OGRFeatureOptions OGRFeatureSource::_options [private] |
Reimplemented from osgEarth::Features::FeatureSource.
Definition at line 468 of file FeatureSourceOGR.cpp.
FeatureSchema OGRFeatureSource::_schema [private] |
Definition at line 472 of file FeatureSourceOGR.cpp.
std::string OGRFeatureSource::_source [private] |
Definition at line 463 of file FeatureSourceOGR.cpp.
bool OGRFeatureSource::_writable [private] |
Definition at line 471 of file FeatureSourceOGR.cpp.