QString QgsShapeFile::getFeatureClass(){ OGRFeature *feat = ogrLayer->GetNextFeature(); if(feat){ OGRGeometry *geom = feat->GetGeometryRef(); if(geom){ geom_type = QString(geom->getGeometryName()); char * esc_str = new char[geom_type.length()*2+1]; PQescapeString(esc_str, (const char *)geom_type, geom_type.length()); geom_type = QString(esc_str); delete[] esc_str; QString file(filename); file.replace(file.length()-3, 3, "dbf"); // open the dbf file std::ifstream dbf((const char*)file, std::ios::in | std::ios::binary); // read header DbaseHeader dbh; dbf.read((char *)&dbh, sizeof(dbh)); 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 = feat->GetFieldCount(); for(int n=0; n<numFields; n++) column_names.push_back(feat->GetFieldDefnRef(n)->GetNameRef()); }else valid = false; delete feat; }else valid = false; ogrLayer->ResetReading(); return valid?geom_type:NULL; }
void CLDS2_Database::Create(void) { LOG_POST_X(1, Info << "LDS2: Creating database " << m_DbFile); // Close the connection if any x_ResetDbConnection(); // Delete all data CFile dbf(m_DbFile); if ( dbf.Exists() ) { dbf.Remove(); } // Initialize connection and create tables: x_ExecuteSqls(kLDS2_CreateDB, sizeof(kLDS2_CreateDB)/sizeof(kLDS2_CreateDB[0])); }
QString Database::nextFileName() { if (m_lastFileName.isEmpty()) { Parsers::DatabaseParser parser; QFile dbf(m_dbfile); parser.parse(&dbf); parser.gc(); m_lastFileName = parser.lastFileName(); } if (m_lastFileName.isEmpty()) { m_lastFileName = "project0.ktn"; } else { QRegExp reg("project(\\d+).ktn"); if (reg.indexIn(m_lastFileName) != -1) { QStringList list = reg.capturedTexts(); m_lastFileName = QString("project%1.ktn").arg(list[1].toInt()+1); } } return m_lastFileName; }
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; }