Пример #1
0
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;
}
Пример #2
0
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;
}