23 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800 24 #define TO8(x) (x).toUtf8().constData() 25 #define TO8F(x) (x).toUtf8().constData() 26 #define FROM8(x) QString::fromUtf8(x) 28 #define TO8(x) (x).toLocal8Bit().constData() 29 #define TO8F(x) QFile::encodeName( x ).constData() 30 #define FROM8(x) QString::fromLocal8Bit(x) 37 #define OGR_F_IsFieldSetAndNotNull OGR_F_IsFieldSet 72 int fieldCount = OGR_F_GetFieldCount( ogrFet );
73 for (
int i = 0; i < fieldCount; ++i )
75 OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, i );
83 QVariant::Type varType;
84 switch ( OGR_Fld_GetType( fldDef ) )
87 varType = QVariant::Int;
89 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 2000000 91 varType = QVariant::LongLong;
95 varType = QVariant::Double;
97 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1400 99 varType = QVariant::Date;
102 varType = QVariant::Time;
105 varType = QVariant::DateTime;
110 varType = QVariant::String;
119 if ( !ogrFet || attIndex < 0 || attIndex >= fields.
count() )
126 OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, attIndex );
133 QgsDebugMsg(
"ogrFet->GetFieldDefnRef(attindex) returns NULL" );
144 switch ( fields.
at( attIndex ).
type() )
146 case QVariant::String:
149 value =
QVariant( encoding->
toUnicode( OGR_F_GetFieldAsString( ogrFet, attIndex ) ) );
155 value =
QVariant( OGR_F_GetFieldAsInteger( ogrFet, attIndex ) );
157 #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 2000000 158 case QVariant::LongLong:
159 value =
QVariant( OGR_F_GetFieldAsInteger64( ogrFet, attIndex ) );
162 case QVariant::Double:
163 value =
QVariant( OGR_F_GetFieldAsDouble( ogrFet, attIndex ) );
166 case QVariant::DateTime:
169 int year, month, day, hour, minute, second, tzf;
171 OGR_F_GetFieldAsDateTime( ogrFet, attIndex, &year, &month, &day, &hour, &minute, &second, &tzf );
172 if ( fields.
at( attIndex ).
type() == QVariant::Date )
173 value =
QDate( year, month, day );
174 else if ( fields.
at( attIndex ).
type() == QVariant::Time )
175 value =
QTime( hour, minute, second );
181 Q_ASSERT_X(
false,
"QgsOgrUtils::getOgrFeatureAttribute",
"unsupported field type" );
204 for (
int idx = 0; idx < fields.
count(); ++idx )
220 OGRGeometryH geom = OGR_F_GetGeometryRef( ogrFet );
235 int memorySize = OGR_G_WkbSize( geom );
236 unsigned char *wkb =
new unsigned char[memorySize];
247 if (
string.isEmpty() )
254 VSIFCloseL( VSIFileFromMemBuffer(
TO8( randomFileName ), reinterpret_cast< GByte* >( ba.
data() ),
255 static_cast< vsi_l_offset >( ba.
size() ), FALSE ) );
257 OGRDataSourceH hDS = OGROpen(
TO8( randomFileName ),
false,
nullptr );
260 VSIUnlink(
TO8( randomFileName ) );
264 OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS, 0 );
267 OGR_DS_Destroy( hDS );
268 VSIUnlink(
TO8( randomFileName ) );
273 while (( oFeat = OGR_L_GetNextFeature( ogrLayer ) ) )
279 OGR_F_Destroy( oFeat );
282 OGR_DS_Destroy( hDS );
283 VSIUnlink(
TO8( randomFileName ) );
291 if (
string.isEmpty() )
298 VSIFCloseL( VSIFileFromMemBuffer(
TO8( randomFileName ), reinterpret_cast< GByte* >( ba.
data() ),
299 static_cast< vsi_l_offset >( ba.
size() ), FALSE ) );
301 OGRDataSourceH hDS = OGROpen(
TO8( randomFileName ),
false,
nullptr );
304 VSIUnlink(
TO8( randomFileName ) );
308 OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS, 0 );
311 OGR_DS_Destroy( hDS );
312 VSIUnlink(
TO8( randomFileName ) );
318 if (( oFeat = OGR_L_GetNextFeature( ogrLayer ) ) )
321 OGR_F_Destroy( oFeat );
324 OGR_DS_Destroy( hDS );
325 VSIUnlink(
TO8( randomFileName ) );
bool isValid() const
Returns the validity of this feature.
static QVariant getOgrFeatureAttribute(OGRFeatureH ogrFet, const QgsFields &fields, int attIndex, QTextCodec *encoding, bool *ok=0)
Retrieves an attribute value from an OGR feature.
static bool readOgrFeatureAttributes(OGRFeatureH ogrFet, const QgsFields &fields, QgsFeature &feature, QTextCodec *encoding)
Reads all attributes from an OGR feature into a QgsFeature.
void fromWkb(unsigned char *wkb, int length)
Set the geometry, feeding in the buffer containing OGC Well-Known Binary and the buffer's length...
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.
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
Return number of items.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
void setGeometry(const QgsGeometry &geom)
Set this feature's geometry from another QgsGeometry object.
static QgsFeatureList stringToFeatureList(const QString &string, const QgsFields &fields, QTextCodec *encoding)
Attempts to parse a string representing a collection of features using OGR.
QString fromUtf8(const char *str, int size)
void setFeatureId(QgsFeatureId id)
Sets the feature ID for this feature.
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 QgsGeometry * ogrGeometryToQgsGeometry(OGRGeometryH geom)
Converts an OGR geometry representation to a QgsGeometry object.
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.
Q_DECL_DEPRECATED void setFields(const QgsFields *fields, bool initAttributes=false)
Assign a field map with the feature to allow attribute access by attribute name.
void setValid(bool validity)
Sets the validity of the feature.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString toUnicode(const QByteArray &a) const