⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qgsogrprovider.cpp

📁 一个非常好的GIS开源新版本
💻 CPP
📖 第 1 页 / 共 3 页
字号:
{  return "[OGR] " +     longName + " (" + glob.lower() + " " + glob.upper() + ");;";} // createFileFilter_QGISEXTERN QString fileVectorFilters(){  static QString myFileFilters;  // if we've already built the supported vector string, just return what  // we've already built  if ( ! ( myFileFilters.isEmpty() || myFileFilters.isNull() ) )  {    return myFileFilters;  }  // register ogr plugins  OGRRegisterAll();  // first get the GDAL driver manager  OGRSFDriverH driver;          // current driver  QString driverName;           // current driver name  // Grind through all the drivers and their respective metadata.  // We'll add a file filter for those drivers that have a file  // extension defined for them; the others, welll, even though  // theoreticaly we can open those files because there exists a  // driver for them, the user will have to use the "All Files" to  // open datasets with no explicitly defined file name extension.  QgsDebugMsg( QString("Driver count: %1").arg( OGRGetDriverCount() ) );  for (int i = 0; i < OGRGetDriverCount(); ++i)  {    driver = OGRGetDriver(i);    Q_CHECK_PTR(driver);    if (!driver)    {      QgsLogger::warning("unable to get driver " + QString::number(i));      continue;    }    driverName = OGR_Dr_GetName(driver);    if (driverName.startsWith("ESRI"))    {      myFileFilters += createFileFilter_("ESRI Shapefiles", "*.shp");    }    else if (driverName.startsWith("UK"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("SDTS"))    {      myFileFilters += createFileFilter_( "Spatial Data Transfer Standard",        "*catd.ddf" );    }    else if (driverName.startsWith("TIGER"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("S57"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("MapInfo"))    {      myFileFilters += createFileFilter_("MapInfo", "*.mif *.tab");      // XXX needs file filter extension    }    else if (driverName.startsWith("DGN"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("VRT"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("AVCBin"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("REC"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("Memory"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("Jis"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("GML"))    {      // XXX not yet supported; post 0.1 release task      myFileFilters += createFileFilter_( "Geography Markup Language",        "*.gml" );    }    else if (driverName.startsWith("CSV"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("PostgreSQL"))    {      // XXX needs file filter extension    }    else if (driverName.startsWith("GRASS"))     {       // XXX needs file filter extension     }     else if (driverName.startsWith("KML"))     {       // XXX needs file filter extension     }     else if (driverName.startsWith("Interlis 1"))     {       // XXX needs file filter extension     }     else if (driverName.startsWith("Interlis 2"))     {       // XXX needs file filter extension     }     else if (driverName.startsWith("SQLite"))     {       // XXX needs file filter extension     }     else if (driverName.startsWith("MySQL"))     {       // XXX needs file filter extension     }     else    {      // NOP, we don't know anything about the current driver      // with regards to a proper file filter string      QgsLogger::debug("fileVectorFilters, unknown driver: " + driverName);    }  }                           // each loaded GDAL driver  // can't forget the default case  myFileFilters += "All files (*.*)";  return myFileFilters;} // fileVectorFilters() constQString QgsOgrProvider::fileVectorFilters() const{  return fileVectorFilters();} // QgsOgrProvider::fileVectorFilters() const/** * Class factory to return a pointer to a newly created  * QgsOgrProvider object */QGISEXTERN QgsOgrProvider * classFactory(const QString *uri){  return new QgsOgrProvider(*uri);}/** Required key function (used to map the plugin to a data store type)*/QGISEXTERN QString providerKey(){  return TEXT_PROVIDER_KEY;}/** * Required description function  */QGISEXTERN QString description(){  return TEXT_PROVIDER_DESCRIPTION;} /** * Required isProvider function. Used to determine if this shared library * is a data provider plugin */QGISEXTERN bool isProvider(){  return true;}/**Creates an empty data source@param uri location to store the file(s)@param format data format (e.g. "ESRI Shapefile"@param vectortype point/line/polygon or multitypes@param attributes a list of name/type pairs for the initial attributes@return true in case of success*/QGISEXTERN bool createEmptyDataSource(const QString& uri,                                      const QString& format,                                      const QString& encoding,                                      QGis::WKBTYPE vectortype,                                      const std::list<std::pair<QString, QString> >& attributes){  OGRSFDriverH driver;  OGRRegisterAll();  driver = OGRGetDriverByName(format);  if(driver == NULL)  {    return false;  }  OGRDataSourceH dataSource;  dataSource = OGR_Dr_CreateDataSource(driver,QFile::encodeName(uri).constData(), NULL);  if(dataSource == NULL)  {    return false;  }  //consider spatial reference system  OGRSpatialReferenceH reference = NULL;  QgsSpatialRefSys mySpatialRefSys;  mySpatialRefSys.validate();  QString myWKT = mySpatialRefSys.toWkt();  if( !myWKT.isNull()  &&  myWKT.length() != 0 )  {    reference = OSRNewSpatialReference(myWKT.toLocal8Bit().data());  }  // Map the qgis geometry type to the OGR geometry type  OGRwkbGeometryType OGRvectortype = wkbUnknown;  switch (vectortype)  {  case QGis::WKBPoint:    OGRvectortype = wkbPoint;    break;  case QGis::WKBLineString:    OGRvectortype = wkbLineString;    break;  case QGis::WKBPolygon:    OGRvectortype = wkbPolygon;    break;  case QGis::WKBMultiPoint:    OGRvectortype = wkbMultiPoint;    break;  case QGis::WKBMultiLineString:    OGRvectortype = wkbMultiLineString;    break;  case QGis::WKBMultiPolygon:    OGRvectortype = wkbMultiPolygon;    break;  default:    {      QgsLogger::debug("Unknown vector type of: ", (int)(vectortype), 1,         __FILE__, __FUNCTION__, __LINE__);      return false;      break;    }  }  OGRLayerH layer;	  layer = OGR_DS_CreateLayer(dataSource,QFile::encodeName(QFileInfo(uri).baseName()).constData(), reference, OGRvectortype, NULL);  if(layer == NULL)  {    return false;  }  //create the attribute fields  QTextCodec* codec=QTextCodec::codecForName(encoding.toLocal8Bit().data());  for(std::list<std::pair<QString, QString> >::const_iterator it= attributes.begin(); it != attributes.end(); ++it)  {    if(it->second == "Real")    {      OGRFieldDefnH field = OGR_Fld_Create(codec->fromUnicode(it->first).data(), OFTReal);      OGR_Fld_SetPrecision(field,3);      OGR_Fld_SetWidth(field,32);      if( OGR_L_CreateField(layer,field,TRUE) != OGRERR_NONE)      {        QgsLogger::warning("creation of OFTReal field failed");      }    }    else if(it->second == "Integer")    {      OGRFieldDefnH field = OGR_Fld_Create(codec->fromUnicode(it->first).data(), OFTInteger);      OGR_Fld_SetWidth(field,10); // limit to 10.  otherwise OGR sets it to 11 and recognizes as OFTDouble later      if(OGR_L_CreateField(layer,field,TRUE) != OGRERR_NONE)      {        QgsLogger::warning("creation of OFTInteger field failed");      }    }    else if(it->second == "String")    {      OGRFieldDefnH field = OGR_Fld_Create(codec->fromUnicode(it->first).data(), OFTString);      if(OGR_L_CreateField(layer,field,TRUE) != OGRERR_NONE)      {        QgsLogger::warning("creation of OFTString field failed");      }    }  }  OGR_DS_Destroy(dataSource);  QgsDebugMsg( QString("GDAL Version number %1").arg( GDAL_VERSION_NUM ) );#if GDAL_VERSION_NUM >= 1310  if(reference)  {    OSRRelease( reference );  }#endif //GDAL_VERSION_NUM  return true;}QgsSpatialRefSys QgsOgrProvider::getSRS(){  QgsDebugMsg("QgsOgrProvider::getSRS()");  QgsSpatialRefSys srs;  OGRSpatialReferenceH mySpatialRefSys = OGR_L_GetSpatialRef(ogrLayer);  if (mySpatialRefSys == NULL)  {    QgsDebugMsg("no spatial reference found");   }  else  {    // get the proj4 text    char * ppszProj4;    OSRExportToProj4(mySpatialRefSys, &ppszProj4 );    QgsDebugMsg(ppszProj4);     char    *pszWKT = NULL;    OSRExportToWkt(mySpatialRefSys, &pszWKT );    QString myWKTString = QString(pszWKT);    OGRFree(pszWKT);      // create SRS from WKT    srs.createFromWkt( myWKTString );  }  return srs;}void QgsOgrProvider::getUniqueValues(int index, QStringList &uniqueValues){  QgsField fld = mAttributeFields[index];  QFileInfo fi( dataSourceUri() );  if( !fi.exists() )    return;    QString sql = QString("SELECT DISTINCT %1 FROM %2 ORDER BY %1").arg( fld.name() ).arg( fi.baseName() );  uniqueValues.clear();  OGRLayerH l = OGR_DS_ExecuteSQL(ogrDataSource, sql.ascii(), NULL, "SQL");  if(l==0)    return;  OGRFeatureH f;  while( f=OGR_L_GetNextFeature(l) )  {    uniqueValues.append( mEncoding->toUnicode(OGR_F_GetFieldAsString(f, 0)) );    OGR_F_Destroy(f);  }  OGR_DS_ReleaseResultSet(l, ogrDataSource);}QVariant QgsOgrProvider::minValue(int index){  QgsField fld = mAttributeFields[index];  QFileInfo fi( dataSourceUri() );  if( !fi.exists() )    return QVariant();    QString sql = QString("SELECT MIN(%1) FROM %2").arg( fld.name() ).arg( fi.baseName() );  OGRLayerH l = OGR_DS_ExecuteSQL(ogrDataSource, sql.ascii(), NULL, "SQL");  if(l==0)    return QVariant();  OGRFeatureH f = OGR_L_GetNextFeature(l);  if(f==0)  {    OGR_DS_ReleaseResultSet(l, ogrDataSource);    return QVariant();  }  QString str = mEncoding->toUnicode( OGR_F_GetFieldAsString(f,0) );  OGR_F_Destroy(f);  QVariant value;   switch (fld.type())  {    case QVariant::String: value = QVariant(str); break;    case QVariant::Int: value = QVariant(str.toInt()); break;    case QVariant::Double: value = QVariant(str.toDouble()); break;    //case QVariant::DateTime: value = QVariant(QDateTime::fromString(str)); break;    default: assert(NULL && "unsupported field type");  }    OGR_DS_ReleaseResultSet(l, ogrDataSource);  return value;}QVariant QgsOgrProvider::maxValue(int index){  QgsField fld = mAttributeFields[index];  QFileInfo fi( dataSourceUri() );  if( !fi.exists() )    return QVariant();    QString sql = QString("SELECT MAX(%1) FROM %2").arg( fld.name() ).arg( fi.baseName() );  OGRLayerH l = OGR_DS_ExecuteSQL(ogrDataSource, sql.ascii(), NULL, "SQL");  if(l==0)    return QVariant();  OGRFeatureH f = OGR_L_GetNextFeature(l);  if(f==0)  {    OGR_DS_ReleaseResultSet(l, ogrDataSource);    return QVariant();  }  QString str = mEncoding->toUnicode( OGR_F_GetFieldAsString(f,0) );  OGR_F_Destroy(f);  QVariant value;   switch (fld.type())  {    case QVariant::String: value = QVariant(str); break;    case QVariant::Int: value = QVariant(str.toInt()); break;    case QVariant::Double: value = QVariant(str.toDouble()); break;    //case QVariant::DateTime: value = QVariant(QDateTime::fromString(str)); break;    default: assert(NULL && "unsupported field type");  }    OGR_DS_ReleaseResultSet(l, ogrDataSource);  return value;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -