23 #include <cpl_error.h> 29 #define OGR_F_IsFieldSetAndNotNull OGR_F_IsFieldSet 36 OGR_DS_Destroy( source );
42 OGR_G_DestroyGeometry( geometry );
47 OGR_Fld_Destroy( definition );
52 OGR_F_Destroy( feature );
69 CPLPushErrorHandler( CPLQuietErrorHandler );
70 GDALDeleteDataset( driver, path.toUtf8().constData() );
82 GDALDestroyWarpOptions( options );
94 feature.
setId( OGR_F_GetFID( ogrFet ) );
97 if ( !readOgrFeatureGeometry( ogrFet, feature ) )
102 if ( !readOgrFeatureAttributes( ogrFet, fields, feature, encoding ) )
117 int fieldCount = OGR_F_GetFieldCount( ogrFet );
118 for (
int i = 0; i < fieldCount; ++i )
120 OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, i );
127 QString name = encoding ? encoding->toUnicode( OGR_Fld_GetNameRef( fldDef ) ) : QString::fromUtf8( OGR_Fld_GetNameRef( fldDef ) );
128 QVariant::Type varType;
129 switch ( OGR_Fld_GetType( fldDef ) )
132 if ( OGR_Fld_GetSubType( fldDef ) == OFSTBoolean )
133 varType = QVariant::Bool;
135 varType = QVariant::Int;
138 varType = QVariant::LongLong;
141 varType = QVariant::Double;
144 varType = QVariant::Date;
147 varType = QVariant::Time;
150 varType = QVariant::DateTime;
154 varType = QVariant::String;
163 if ( !ogrFet || attIndex < 0 || attIndex >= fields.
count() )
170 OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, attIndex );
177 QgsDebugMsg(
"ogrFet->GetFieldDefnRef(attindex) returns NULL" );
188 switch ( fields.
at( attIndex ).
type() )
190 case QVariant::String:
193 value = QVariant( encoding->toUnicode( OGR_F_GetFieldAsString( ogrFet, attIndex ) ) );
195 value = QVariant( QString::fromUtf8( OGR_F_GetFieldAsString( ogrFet, attIndex ) ) );
200 value = QVariant( OGR_F_GetFieldAsInteger( ogrFet, attIndex ) );
202 case QVariant::LongLong:
203 value = QVariant( OGR_F_GetFieldAsInteger64( ogrFet, attIndex ) );
205 case QVariant::Double:
206 value = QVariant( OGR_F_GetFieldAsDouble( ogrFet, attIndex ) );
209 case QVariant::DateTime:
212 int year, month, day, hour, minute, second, tzf;
214 OGR_F_GetFieldAsDateTime( ogrFet, attIndex, &year, &month, &day, &hour, &minute, &second, &tzf );
215 if ( fields.
at( attIndex ).
type() == QVariant::Date )
216 value = QDate( year, month, day );
217 else if ( fields.
at( attIndex ).
type() == QVariant::Time )
218 value = QTime( hour, minute, second );
220 value = QDateTime( QDate( year, month, day ), QTime( hour, minute, second ) );
224 Q_ASSERT_X(
false,
"QgsOgrUtils::getOgrFeatureAttribute",
"unsupported field type" );
231 value = QVariant( QString() );
247 for (
int idx = 0; idx < fields.
count(); ++idx )
249 QVariant value = getOgrFeatureAttribute( ogrFet, fields, idx, encoding, &ok );
263 OGRGeometryH geom = OGR_F_GetGeometryRef( ogrFet );
267 feature.
setGeometry( ogrGeometryToQgsGeometry( geom ) );
278 int memorySize = OGR_G_WkbSize( geom );
279 unsigned char *wkb =
new unsigned char[memorySize];
283 uint32_t origGeomType;
284 memcpy( &origGeomType, wkb + 1,
sizeof( uint32_t ) );
285 bool hasZ = ( origGeomType >= 1000 && origGeomType < 2000 ) || ( origGeomType >= 3000 && origGeomType < 4000 );
286 bool hasM = ( origGeomType >= 2000 && origGeomType < 3000 ) || ( origGeomType >= 3000 && origGeomType < 4000 );
289 if ( origGeomType % 1000 == 16 )
292 int nDims = 2 + hasZ + hasM;
295 unsigned char *wkbptr = wkb;
301 memcpy( wkbptr, &newMultiType,
sizeof( uint32_t ) );
306 memcpy( &numGeoms, wkb + 5,
sizeof( uint32_t ) );
310 for ( uint32_t i = 0; i < numGeoms; ++i )
316 memcpy( wkbptr, &newSingleType,
sizeof( uint32_t ) );
317 wkbptr +=
sizeof( uint32_t );
321 memcpy( &nRings, wkbptr,
sizeof( uint32_t ) );
322 wkbptr +=
sizeof( uint32_t );
324 for ( uint32_t j = 0; j < nRings; ++j )
327 memcpy( &nPoints, wkbptr,
sizeof( uint32_t ) );
328 wkbptr +=
sizeof( uint32_t ) +
sizeof(
double ) * nDims * nPoints;
332 else if ( origGeomType % 1000 == 15 )
337 memcpy( wkb + 1, &newType,
sizeof( uint32_t ) );
348 if (
string.isEmpty() )
351 QString randomFileName = QStringLiteral(
"/vsimem/%1" ).arg( QUuid::createUuid().toString() );
354 QByteArray ba =
string.toUtf8();
355 VSIFCloseL( VSIFileFromMemBuffer( randomFileName.toUtf8().constData(),
reinterpret_cast< GByte *
>( ba.data() ),
356 static_cast< vsi_l_offset >( ba.size() ), FALSE ) );
361 VSIUnlink( randomFileName.toUtf8().constData() );
365 OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS.get(), 0 );
369 VSIUnlink( randomFileName.toUtf8().constData() );
374 while ( oFeat.reset( OGR_L_GetNextFeature( ogrLayer ) ), oFeat )
376 QgsFeature feat = readOgrFeature( oFeat.get(), fields, encoding );
382 VSIUnlink( randomFileName.toUtf8().constData() );
390 if (
string.isEmpty() )
393 QString randomFileName = QStringLiteral(
"/vsimem/%1" ).arg( QUuid::createUuid().toString() );
396 QByteArray ba =
string.toUtf8();
397 VSIFCloseL( VSIFileFromMemBuffer( randomFileName.toUtf8().constData(),
reinterpret_cast< GByte *
>( ba.data() ),
398 static_cast< vsi_l_offset >( ba.size() ), FALSE ) );
403 VSIUnlink( randomFileName.toUtf8().constData() );
407 OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS.get(), 0 );
411 VSIUnlink( randomFileName.toUtf8().constData() );
417 if ( oFeat.reset( OGR_L_GetNextFeature( ogrLayer ) ), oFeat )
419 fields = readOgrFields( oFeat.get(), encoding );
423 VSIUnlink( randomFileName.toUtf8().constData() );
432 for (
qgssize i = 0; stringList[i]; ++i )
434 strings.append( QString::fromUtf8( stringList[i] ) );
bool isValid() const
Returns the validity of this feature.
void CORE_EXPORT operator()(OGRFeatureH feature)
Destroys an OGR feature, using the correct gdal calls.
static bool readOgrFeatureAttributes(OGRFeatureH ogrFet, const QgsFields &fields, QgsFeature &feature, QTextCodec *encoding)
Reads all attributes from an OGR feature into a QgsFeature.
void setFields(const QgsFields &fields, bool initAttributes=false)
Assign a field map with the feature to allow attribute access by attribute name.
void fromWkb(unsigned char *wkb, int length)
Set the geometry, feeding in the buffer containing OGC Well-Known Binary and the buffer's length...
QList< QgsFeature > QgsFeatureList
static bool readOgrFeatureGeometry(OGRFeatureH ogrFet, QgsFeature &feature)
Reads the geometry from an OGR feature into a QgsFeature.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
void CORE_EXPORT operator()(OGRDataSourceH source)
Destroys an OGR data source, using the correct gdal calls.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
static QgsFields readOgrFields(OGRFeatureH ogrFet, QTextCodec *encoding)
Reads an OGR feature and returns a corresponding fields collection.
#define OGR_F_IsFieldSetAndNotNull
static endian_t endian()
Returns whether this machine uses big or little endian.
int count() const
Returns number of items.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
static QgsFeatureList stringToFeatureList(const QString &string, const QgsFields &fields, QTextCodec *encoding)
Attempts to parse a string representing a collection of features using OGR.
void CORE_EXPORT fast_delete_and_close(dataset_unique_ptr &dataset, GDALDriverH driver, const QString &path)
Performs a fast close of an unwanted GDAL dataset handle by deleting the underlying data store...
std::unique_ptr< std::remove_pointer< OGRFeatureH >::type, OGRFeatureDeleter > ogr_feature_unique_ptr
Scoped OGR feature.
void CORE_EXPORT operator()(GDALDatasetH datasource)
Destroys an gdal dataset, using the correct gdal calls.
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
static QgsFeature readOgrFeature(OGRFeatureH ogrFet, const QgsFields &fields, QTextCodec *encoding)
Reads an OGR feature and converts it to a QgsFeature.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
static QgsFields stringToFields(const QString &string, QTextCodec *encoding)
Attempts to retrieve the fields from a string representing a collection of features using OGR...
Encapsulate a field in an attribute table or data source.
void setId(QgsFeatureId id)
Sets the feature ID for this feature.
static QgsGeometry ogrGeometryToQgsGeometry(OGRGeometryH geom)
Converts an OGR geometry representation to a QgsGeometry object.
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
void CORE_EXPORT operator()(GDALWarpOptions *options)
Destroys GDAL warp options, using the correct gdal calls.
void setValid(bool validity)
Sets the validity of the feature.
void clearGeometry()
Removes any geometry associated with the feature.
static QVariant getOgrFeatureAttribute(OGRFeatureH ogrFet, const QgsFields &fields, int attIndex, QTextCodec *encoding, bool *ok=nullptr)
Retrieves an attribute value from an OGR feature.
void CORE_EXPORT operator()(OGRGeometryH geometry)
Destroys an OGR geometry, using the correct gdal calls.
void CORE_EXPORT operator()(OGRFieldDefnH definition)
Destroys an OGR field definition, using the correct gdal calls.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
static Type zmType(Type type, bool hasZ, bool hasM)
Returns the modified input geometry type according to hasZ / hasM.
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
std::unique_ptr< std::remove_pointer< OGRDataSourceH >::type, OGRDataSourceDeleter > ogr_datasource_unique_ptr
Scoped OGR data source.
static QStringList cStringListToQStringList(char **stringList)
Converts a c string list to a QStringList.