📄 qgsgrass.cpp
字号:
/* _Correct_ putenv() implementation is not making copy! */ QString gisrcEnv = "GISRC=" + mGisrc; char *gisrcEnvChar = new char[gisrcEnv.length()+1]; strcpy ( gisrcEnvChar, const_cast<char *>(gisrcEnv.ascii()) ); putenv( gisrcEnvChar ); // Reinitialize GRASS G__setenv( (char *)"GISRC", const_cast<char *>(gisrcEnv.ascii()) ); #if defined(WIN32) G__setenv( (char *)"GISDBASE", const_cast<char *>(getShortPath(gisdbase).ascii()) );#else G__setenv( (char *)"GISDBASE", const_cast<char *>(gisdbase.ascii()) );#endif G__setenv( (char *)"LOCATION_NAME", const_cast<char *>(location.ascii()) ); G__setenv( (char *)"MAPSET", const_cast<char *>(mapset.ascii()) ); defaultGisdbase = gisdbase; defaultLocation = location; defaultMapset = mapset; active = true; // Close old mapset if ( mMapsetLock.length() > 0 ) { QFile file ( mMapsetLock ); file.remove(); } mMapsetLock = lock; return NULL;}QString QgsGrass::closeMapset ( ){#ifdef QGISDEBUG std::cerr << "QgsGrass::closeMapset" << std::endl;#endif if ( mMapsetLock.length() > 0 ) { QFile file ( mMapsetLock ); if ( !file.remove() ) { return QObject::tr("Cannot remove mapset lock: ") + mMapsetLock; } mMapsetLock = ""; putenv( (char *)"GISRC" ); // Reinitialize GRASS G__setenv( (char *)"GISRC", (char *)"" ); G__setenv( (char *)"GISDBASE", (char *)"" ); G__setenv( (char *)"LOCATION_NAME", (char *)"" ); G__setenv( (char *)"MAPSET", (char *)"" ); defaultGisdbase = ""; defaultLocation = ""; defaultMapset = ""; active = 0; // Delete temporary dir // To be sure that we dont delete '/' for example if ( mTmp.left(4) == "/tmp" ) { QDir dir ( mTmp ); for ( unsigned int i = 0; i < dir.count(); i++ ) { if ( dir[i] == "." || dir[i] == ".." ) continue; dir.remove(dir[i]); if ( dir.remove(dir[i]) ) { std::cerr << "Cannot remove temporary file " << dir[i].local8Bit().data() << std::endl; } } if ( !dir.rmdir(mTmp) ) { std::cerr << "Cannot remove temporary directory " << mTmp.local8Bit().data() << std::endl; } } } return NULL;}QStringList GRASS_EXPORT QgsGrass::locations ( QString gisbase ){#ifdef QGISDEBUG std::cerr << "QgsGrass::locations gisbase = " << gisbase.ascii() << std::endl;#endif QStringList list; if ( gisbase.isEmpty() ) return list; QDir d = QDir( gisbase ); d.setFilter(QDir::NoDotAndDotDot|QDir::Dirs); for ( unsigned int i = 0; i < d.count(); i++ ) { if ( QFile::exists ( gisbase + "/" + d[i] + "/PERMANENT/DEFAULT_WIND" ) ) { list.append(QString(d[i])); } } return list;}QStringList GRASS_EXPORT QgsGrass::mapsets ( QString gisbase, QString locationName ){#ifdef QGISDEBUG std::cerr << "QgsGrass::mapsets gisbase = " << gisbase.ascii() << " locationName = " << locationName.ascii() << std::endl;#endif if ( gisbase.isEmpty() || locationName.isEmpty() ) return QStringList(); return QgsGrass::mapsets ( gisbase + "/" + locationName );}QStringList GRASS_EXPORT QgsGrass::mapsets ( QString locationPath ){#ifdef QGISDEBUG std::cerr << "QgsGrass::mapsets locationPath = " << locationPath.ascii() << std::endl;#endif QStringList list; if ( locationPath.isEmpty() ) return list; QDir d = QDir( locationPath ); d.setFilter(QDir::NoDotAndDotDot|QDir::Dirs); for ( unsigned int i = 0; i < d.count(); i++ ) { if ( QFile::exists ( locationPath + "/" + d[i] + "/WIND" ) ) { list.append(d[i]); } } return list;}QStringList GRASS_EXPORT QgsGrass::vectors ( QString gisbase, QString locationName, QString mapsetName){ std::cerr << "QgsGrass::vectors()" << std::endl; if ( gisbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() ) return QStringList(); /* TODO: G_list() was added to GRASS 6.1 06-05-24, enable its use after some period (others do update) */ /* if ( QgsGrass::versionMajor() > 6 || QgsGrass::versionMinor() > 0 ) { QStringList list; char **glist = G_list( G_ELEMENT_VECTOR, gisbase.toLocal8Bit().constData(), locationName.toLocal8Bit().constData(), mapsetName.toLocal8Bit().constData() ); int i = 0; while ( glist[i] ) { list.append( QString(glist[i]) ); i++; } G_free_list ( glist ); return list; } */ return QgsGrass::vectors ( gisbase + "/" + locationName + "/" + mapsetName );}QStringList GRASS_EXPORT QgsGrass::vectors ( QString mapsetPath ){#ifdef QGISDEBUG std::cerr << "QgsGrass::vectors mapsetPath = " << mapsetPath.ascii() << std::endl;#endif QStringList list; if ( mapsetPath.isEmpty() ) return list; QDir d = QDir( mapsetPath + "/vector" ); d.setFilter(QDir::NoDotAndDotDot|QDir::Dirs); for ( unsigned int i = 0; i < d.count(); i++ ) { /* if ( QFile::exists ( mapsetPath + "/vector/" + d[i] + "/head" ) ) { list.append(d[i]); } */ list.append(d[i]); } return list;}QStringList GRASS_EXPORT QgsGrass::rasters ( QString gisbase, QString locationName, QString mapsetName){ std::cerr << "QgsGrass::rasters()" << std::endl; if ( gisbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() ) return QStringList(); /* TODO: G_list() was added to GRASS 6.1 06-05-24, enable its use after some period (others do update) */ /* if ( QgsGrass::versionMajor() > 6 || QgsGrass::versionMinor() > 0 ) { QStringList list; char **glist = G_list( G_ELEMENT_RASTER, gisbase.toLocal8Bit().constData(), locationName.toLocal8Bit().constData(), mapsetName.toLocal8Bit().constData() ); int i = 0; while ( glist[i] ) { list.append( QString(glist[i]) ); i++; } G_free_list ( glist ); return list; } */ return QgsGrass::rasters ( gisbase + "/" + locationName + "/" + mapsetName );}QStringList GRASS_EXPORT QgsGrass::rasters ( QString mapsetPath ){#ifdef QGISDEBUG std::cerr << "QgsGrass::rasters mapsetPath = " << mapsetPath.ascii() << std::endl;#endif QStringList list; if ( mapsetPath.isEmpty() ) return list; QDir d = QDir( mapsetPath + "/cellhd" ); d.setFilter(QDir::Files); for ( unsigned int i = 0; i < d.count(); i++ ) { list.append(d[i]); } return list;}QStringList GRASS_EXPORT QgsGrass::elements ( QString gisbase, QString locationName, QString mapsetName, QString element){ if ( gisbase.isEmpty() || locationName.isEmpty() || mapsetName.isEmpty() ) return QStringList(); return QgsGrass::elements ( gisbase + "/" + locationName + "/" + mapsetName, element );}QStringList GRASS_EXPORT QgsGrass::elements ( QString mapsetPath, QString element ){#ifdef QGISDEBUG std::cerr << "QgsGrass::elements mapsetPath = " << mapsetPath.ascii() << std::endl;#endif QStringList list; if ( mapsetPath.isEmpty() ) return list; QDir d = QDir( mapsetPath + "/" + element ); d.setFilter(QDir::Files); for ( unsigned int i = 0; i < d.count(); i++ ) { list.append(d[i]); } return list;}QString GRASS_EXPORT QgsGrass::regionString( struct Cell_head *window ){ QString reg; int fmt; char buf[1024]; fmt = window->proj; // TODO 3D reg = "proj:" + QString::number(window->proj) + ";" ; reg += "zone:" + QString::number(window->zone) + ";" ; G_format_northing (window->north,buf,fmt); reg += "north:" + QString(buf) + ";" ; G_format_northing (window->south,buf,fmt); reg += "south:" + QString(buf) + ";" ; G_format_easting (window->east,buf,fmt); reg += "east:" + QString(buf) + ";" ; G_format_easting (window->west,buf,fmt); reg += "west:" + QString(buf) + ";" ; reg += "cols:" + QString::number(window->cols) + ";" ; reg += "rows:" + QString::number(window->rows) + ";" ; G_format_resolution (window->ew_res,buf,fmt); reg += "e-w resol:" + QString(buf) + ";" ; G_format_resolution (window->ns_res,buf,fmt); reg += "n-s resol:" + QString(buf) + ";" ; return reg;}bool GRASS_EXPORT QgsGrass::region( QString gisbase, QString location, QString mapset, struct Cell_head *window ){ QgsGrass::setLocation( gisbase, location ); if ( G__get_window ( window, (char *)"", (char *)"WIND", mapset.toLocal8Bit().data() ) ) { return false; } return true;}bool GRASS_EXPORT QgsGrass::writeRegion( QString gisbase, QString location, QString mapset, struct Cell_head *window ){ std::cerr << "QgsGrass::writeRegion()" << std::endl; std::cerr << "n = " << window->north << " s = " << window->south << std::endl; std::cerr << "e = " << window->east << " w = " << window->west << std::endl; QgsGrass::setMapset( gisbase, location, mapset ); if ( G_put_window(window) == -1 ) { return false; } return true;}void GRASS_EXPORT QgsGrass::copyRegionExtent( struct Cell_head *source, struct Cell_head *target ){ target->north = source->north; target->south = source->south; target->east = source->east; target->west = source->west; target->top = source->top; target->bottom = source->bottom;}void GRASS_EXPORT QgsGrass::copyRegionResolution( struct Cell_head *source, struct Cell_head *target ){ target->ns_res = source->ns_res; target->ew_res = source->ew_res; target->tb_res = source->tb_res; target->ns_res3 = source->ns_res3; target->ew_res3 = source->ew_res3;}void GRASS_EXPORT QgsGrass::extendRegion( struct Cell_head *source, struct Cell_head *target ){ if ( source->north > target->north ) target->north = source->north; if ( source->south < target->south ) target->south = source->south; if ( source->east > target->east ) target->east = source->east; if ( source->west < target->west ) target->west = source->west; if ( source->top > target->top ) target->top = source->top; if ( source->bottom < target->bottom ) target->bottom = source->bottom;}bool GRASS_EXPORT QgsGrass::mapRegion( int type, QString gisbase, QString location, QString mapset, QString map, struct Cell_head *window ){#ifdef QGISDEBUG std::cerr << "QgsGrass::mapRegion()" << std::endl; std::cerr << "map = " << map.toLocal8Bit().data() << std::endl; std::cerr << "mapset = " << mapset.toLocal8Bit().data() << std::endl;#endif QgsGrass::setLocation( gisbase, location ); if ( type == Raster ) { if ( G_get_cellhd ( map.toLocal8Bit().data(), mapset.toLocal8Bit().data(), window) < 0 ) { QMessageBox::warning( 0, QObject::tr("Warning"), QObject::tr("Cannot read raster map region" )); return false; } } else if ( type == Vector ) { // Get current projection region( gisbase, location, mapset, window ); struct Map_info Map; int level = Vect_open_old_head ( &Map, map.toLocal8Bit().data(), mapset.toLocal8Bit().data()); if ( level < 2 ) { QMessageBox::warning( 0, QObject::tr("Warning"), QObject::tr("Cannot read vector map region" ) ); return false; } BOUND_BOX box; Vect_get_map_box (&Map, &box ); window->north = box.N; window->south = box.S; window->west = box.W; window->east = box.E; window->top = box.T; window->bottom = box.B; // Is this optimal ? window->ns_res = (window->north-window->south)/1000; window->ew_res = window->ns_res; if ( window->top > window->bottom ) { window->tb_res = (window->top-window->bottom)/10; } else { window->top = window->bottom + 1; window->tb_res = 1; } G_adjust_Cell_head3 ( window, 0, 0, 0 ); Vect_close (&Map); } else if ( type == Region ) { if ( G__get_window (window, (char *)"windows", map.toLocal8Bit().data(), mapset.toLocal8Bit().data() ) != NULL ) { QMessageBox::warning( 0, QObject::tr("Warning"), QObject::tr("Cannot read region" )); return false; } } return true;}// GRASS version constants have been changed on 26.4.2007// http://freegis.org/cgi-bin/viewcvs.cgi/grass6/include/version.h.in.diff?r1=1.4&r2=1.5// The following lines workaround this changeint GRASS_EXPORT QgsGrass::versionMajor(){#ifdef GRASS_VERSION_MAJOR return GRASS_VERSION_MAJOR;#else return QString(GRASS_VERSION_MAJOR).toInt();#endif}int GRASS_EXPORT QgsGrass::versionMinor(){#ifdef GRASS_VERSION_MINOR return GRASS_VERSION_MINOR;#else return QString(GRASS_VERSION_MINOR).toInt();#endif}int GRASS_EXPORT QgsGrass::versionRelease(){#ifdef GRASS_VERSION_RELEASE#define QUOTE(x) #x return QString(QUOTE(GRASS_VERSION_RELEASE)).toInt();#else return QString(GRASS_VERSION_RELEASE).toInt();#endif}QString GRASS_EXPORT QgsGrass::versionString(){ return QString(GRASS_VERSION_STRING);}bool GRASS_EXPORT QgsGrass::isMapset ( QString path ){ /* TODO: G_is_mapset() was added to GRASS 6.1 06-05-24, enable its use after some period (others do update) */ /* if ( QgsGrass::versionMajor() > 6 || QgsGrass::versionMinor() > 0 ) { if ( G_is_mapset( path.toLocal8Bit().constData() ) ) return true; } else { */ QString windf = path + "/WIND"; if ( QFile::exists ( windf ) ) return true; //} return false;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -