void QgsOgrFeatureIterator::getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature & f, int attindex ) { OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, attindex ); if ( ! fldDef ) { QgsDebugMsg( "ogrFet->GetFieldDefnRef(attindex) returns NULL" ); return; } QVariant value; if ( OGR_F_IsFieldSet( ogrFet, attindex ) ) { switch ( P->mAttributeFields[attindex].type() ) { case QVariant::String: value = QVariant( P->mEncoding->toUnicode( OGR_F_GetFieldAsString( ogrFet, attindex ) ) ); break; case QVariant::Int: value = QVariant( OGR_F_GetFieldAsInteger( ogrFet, attindex ) ); break; case QVariant::Double: value = QVariant( OGR_F_GetFieldAsDouble( ogrFet, attindex ) ); break; //case QVariant::DateTime: value = QVariant(QDateTime::fromString(str)); break; default: assert( NULL && "unsupported field type" ); } } else { value = QVariant( QString::null ); } f.setAttribute( attindex, value ); }
static int _setAtt(S57_geo *geoData, OGRFeatureH hFeature) { int field_count = OGR_F_GetFieldCount(hFeature); for (int field_index=0; field_index<field_count; ++field_index) { if (OGR_F_IsFieldSet(hFeature, field_index)) { const char *propName = OGR_Fld_GetNameRef(OGR_F_GetFieldDefnRef(hFeature,field_index)); const char *propValue = OGR_F_GetFieldAsString(hFeature, field_index); S57_setAtt(geoData, propName, propValue); /* debug if (0 == g_strcmp0(S57_getName(geoData), "M_NPUB")) { PRINTF("DEBUG: M_NPUB-%i: %s --> %s\n", field_index, propName, propValue); } if (0 == g_strcmp0(S57_getName(geoData), "C_AGGR")) { PRINTF("DEBUG: C_AGGR-%i: %s --> %s\n", field_index, propName, propValue); } if (0 == g_strcmp0(S57_getName(geoData), "C_ASSO")) { PRINTF("DEBUG: C_ASSO-%i: %s --> %s\n", field_index, propName, propValue); } */ } } // optimisation: direct link to the value of Att (GString) // save the search in attList GString *scamin = S57_getAttVal(geoData, "SCAMIN"); if ((NULL!=scamin) && (NULL!=scamin->str)){ S57_setScamin(geoData, S52_atof(scamin->str)); } return TRUE; }
QgsFields QgsOgrUtils::readOgrFields( OGRFeatureH ogrFet, QTextCodec* encoding ) { QgsFields fields; if ( !ogrFet ) return fields; int fieldCount = OGR_F_GetFieldCount( ogrFet ); for ( int i = 0; i < fieldCount; ++i ) { OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, i ); if ( !fldDef ) { fields.append( QgsField() ); continue; } QString name = encoding ? encoding->toUnicode( OGR_Fld_GetNameRef( fldDef ) ) : QString::fromUtf8( OGR_Fld_GetNameRef( fldDef ) ); QVariant::Type varType; switch ( OGR_Fld_GetType( fldDef ) ) { case OFTInteger: varType = QVariant::Int; break; #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 2000000 case OFTInteger64: varType = QVariant::LongLong; break; #endif case OFTReal: varType = QVariant::Double; break; #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1400 case OFTDate: varType = QVariant::Date; break; case OFTTime: varType = QVariant::Time; break; case OFTDateTime: varType = QVariant::DateTime; break; case OFTString: #endif default: varType = QVariant::String; // other unsupported, leave it as a string } fields.append( QgsField( name, varType ) ); } return fields; }
void QgsOgrFeatureIterator::getFeatureAttribute( OGRFeatureH ogrFet, QgsFeature & f, int attindex ) { OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, attindex ); if ( ! fldDef ) { QgsDebugMsg( "ogrFet->GetFieldDefnRef(attindex) returns NULL" ); return; } QVariant value; if ( OGR_F_IsFieldSet( ogrFet, attindex ) ) { switch ( mSource->mFields.at( attindex ).type() ) { case QVariant::String: value = QVariant( mSource->mEncoding->toUnicode( OGR_F_GetFieldAsString( ogrFet, attindex ) ) ); break; case QVariant::Int: value = QVariant( OGR_F_GetFieldAsInteger( ogrFet, attindex ) ); break; #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 2000000 case QVariant::LongLong: value = QVariant( OGR_F_GetFieldAsInteger64( ogrFet, attindex ) ); break; #endif case QVariant::Double: value = QVariant( OGR_F_GetFieldAsDouble( ogrFet, attindex ) ); break; case QVariant::Date: case QVariant::DateTime: { int year, month, day, hour, minute, second, tzf; OGR_F_GetFieldAsDateTime( ogrFet, attindex, &year, &month, &day, &hour, &minute, &second, &tzf ); if ( mSource->mFields.at( attindex ).type() == QVariant::Date ) value = QDate( year, month, day ); else value = QDateTime( QDate( year, month, day ), QTime( hour, minute, second ) ); } break; default: assert( 0 && "unsupported field type" ); } } else { value = QVariant( QString::null ); } f.setAttribute( attindex, value ); }
QVariant QgsOgrUtils::getOgrFeatureAttribute( OGRFeatureH ogrFet, const QgsFields& fields, int attIndex, QTextCodec* encoding , bool* ok ) { if ( !ogrFet || attIndex < 0 || attIndex >= fields.count() ) { if ( ok ) *ok = false; return QVariant(); } OGRFieldDefnH fldDef = OGR_F_GetFieldDefnRef( ogrFet, attIndex ); if ( ! fldDef ) { if ( ok ) *ok = false; QgsDebugMsg( "ogrFet->GetFieldDefnRef(attindex) returns NULL" ); return QVariant(); } QVariant value; if ( ok ) *ok = true; if ( OGR_F_IsFieldSet( ogrFet, attIndex ) ) { switch ( fields.at( attIndex ).type() ) { case QVariant::String: { if ( encoding ) value = QVariant( encoding->toUnicode( OGR_F_GetFieldAsString( ogrFet, attIndex ) ) ); else value = QVariant( QString::fromUtf8( OGR_F_GetFieldAsString( ogrFet, attIndex ) ) ); break; } case QVariant::Int: value = QVariant( OGR_F_GetFieldAsInteger( ogrFet, attIndex ) ); break; #if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 2000000 case QVariant::LongLong: value = QVariant( OGR_F_GetFieldAsInteger64( ogrFet, attIndex ) ); break; #endif case QVariant::Double: value = QVariant( OGR_F_GetFieldAsDouble( ogrFet, attIndex ) ); break; case QVariant::Date: case QVariant::DateTime: case QVariant::Time: { int year, month, day, hour, minute, second, tzf; OGR_F_GetFieldAsDateTime( ogrFet, attIndex, &year, &month, &day, &hour, &minute, &second, &tzf ); if ( fields.at( attIndex ).type() == QVariant::Date ) value = QDate( year, month, day ); else if ( fields.at( attIndex ).type() == QVariant::Time ) value = QTime( hour, minute, second ); else value = QDateTime( QDate( year, month, day ), QTime( hour, minute, second ) ); } break; default: Q_ASSERT_X( false, "QgsOgrUtils::getOgrFeatureAttribute", "unsupported field type" ); if ( ok ) *ok = false; } } else { value = QVariant( QString::null ); } return value; }
QString QgsShapeFile::getFeatureClass() { // scan the whole layer to try to determine the geometry // type. qApp->processEvents(); isMulti = scanGeometries(); OGRFeatureH feat; // skip features without geometry while (( feat = OGR_L_GetNextFeature( ogrLayer ) ) != NULL ) { if ( OGR_F_GetGeometryRef( feat ) ) break; } if ( feat ) { OGRGeometryH geom = OGR_F_GetGeometryRef( feat ); if ( geom ) { /* OGR doesn't appear to report geometry type properly * for a layer containing both polygon and multipolygon * entities * // get the feature type from the layer OGRFeatureDefn * gDef = ogrLayer->GetLayerDefn(); OGRwkbGeometryType gType = gDef->GetGeomType(); geom_type = QGis::qgisFeatureTypes[gType]; */ //geom_type = QString(geom->getGeometryName()); //geom_type = "GEOMETRY"; QgsDebugMsg( "Preparing to escape " + geom_type ); char * esc_str = new char[geom_type.length()*2+1]; PQescapeString( esc_str, geom_type.toUtf8(), geom_type.length() ); geom_type = QString( esc_str ); QgsDebugMsg( "After escaping, geom_type is : " + geom_type ); delete[] esc_str; QString file( fileName ); file.replace( file.length() - 3, 3, "dbf" ); // open the dbf file std::ifstream dbf( file.toUtf8(), std::ios::in | std::ios::binary ); // read header DbaseHeader dbh; dbf.read(( char * )&dbh, sizeof( dbh ) ); // Check byte order if ( htonl( 1 ) == 1 ) { /* DbaseHeader is stored in little-endian format. * The num_recs, size_hdr and size_rec fields must be byte-swapped when read * on a big-endian processor. Currently only size_hdr is used. */ unsigned char *byte = reinterpret_cast<unsigned char *>( &dbh.size_hdr ); unsigned char t = *byte; *byte = *( byte + 1 ); *( byte + 1 ) = t; } Fda fda; QString str_type = "varchar("; for ( int field_count = 0, bytes_read = sizeof( dbh ); bytes_read < dbh.size_hdr - 1; field_count++, bytes_read += sizeof( fda ) ) { dbf.read(( char * )&fda, sizeof( fda ) ); switch ( fda.field_type ) { case 'N': if (( int )fda.field_decimal > 0 ) column_types.push_back( "float" ); else column_types.push_back( "int" ); break; case 'F': column_types.push_back( "float" ); break; case 'D': column_types.push_back( "date" ); break; case 'C': str_type = QString( "varchar(%1)" ).arg( fda.field_length ); column_types.push_back( str_type ); break; case 'L': column_types.push_back( "boolean" ); break; default: column_types.push_back( "varchar(256)" ); break; } } dbf.close(); int numFields = OGR_F_GetFieldCount( feat ); for ( int n = 0; n < numFields; n++ ) { QString s = codec->toUnicode( OGR_Fld_GetNameRef( OGR_F_GetFieldDefnRef( feat, n ) ) ); column_names.push_back( s ); } } else valid = false; OGR_F_Destroy( feat ); } else valid = false; OGR_L_ResetReading( ogrLayer ); return valid ? geom_type : QString::null; }
QString QgsShapeFile::getFeatureClass() { // scan the whole layer to try to determine the geometry // type. qApp->processEvents(); isMulti = scanGeometries(); OGRFeatureH feat; // skip features without geometry while (( feat = OGR_L_GetNextFeature( ogrLayer ) ) != NULL ) { if ( OGR_F_GetGeometryRef( feat ) ) break; } if ( feat ) { OGRGeometryH geom = OGR_F_GetGeometryRef( feat ); if ( geom ) { /* OGR doesn't appear to report geometry type properly * for a layer containing both polygon and multipolygon * entities * // get the feature type from the layer OGRFeatureDefn * gDef = ogrLayer->GetLayerDefn(); OGRwkbGeometryType gType = gDef->GetGeomType(); geom_type = QGis::qgisFeatureTypes[gType]; */ //geom_type = QString(geom->getGeometryName()); //geom_type = "GEOMETRY"; QgsDebugMsg( "Preparing to escape " + geom_type ); char * esc_str = new char[geom_type.length()*2+1]; PQescapeString( esc_str, geom_type.toUtf8(), geom_type.length() ); geom_type = QString( esc_str ); QgsDebugMsg( "After escaping, geom_type is : " + geom_type ); delete[] esc_str; int numFields = OGR_F_GetFieldCount( feat ); for ( int n = 0; n < numFields; n++ ) { OGRFieldDefnH fld = OGR_F_GetFieldDefnRef( feat, n ); column_names.push_back( codec->toUnicode( OGR_Fld_GetNameRef( fld ) ) ); switch ( OGR_Fld_GetType( fld ) ) { case OFTInteger: column_types.push_back( "int" ); break; case OFTReal: column_types.push_back( "float" ); break; case OFTString: column_types.push_back( QString( "varchar(%1)" ).arg( OGR_Fld_GetWidth( fld ) ) ); break; case OFTDate: column_types.push_back( "date" ); break; case OFTTime: column_types.push_back( "time" ); break; case OFTDateTime: column_types.push_back( "timestamp" ); break; default: column_types.push_back( "varchar(256)" ); break; } } } else valid = false; OGR_F_Destroy( feat ); } else valid = false; OGR_L_ResetReading( ogrLayer ); return valid ? geom_type : QString::null; }