Beispiel #1
0
void TestQgsProject::testReadPath()
{
  QgsProject *prj = new QgsProject;
  // this is a bit hacky as we do not really load such project
  QString prefix;
#if defined(Q_OS_WIN)
  prefix = "C:";
#endif
  prj->setFileName( prefix + "/home/qgis/a-project-file.qgs" ); // not expected to exist
  // make sure we work with relative paths!
  prj->writeEntry( QStringLiteral( "Paths" ), QStringLiteral( "Absolute" ), false );

  QCOMPARE( prj->readPath( "./x.shp" ), QString( prefix + "/home/qgis/x.shp" ) );
  QCOMPARE( prj->readPath( "../x.shp" ), QString( prefix + "/home/x.shp" ) );

  // TODO: old style (seems QGIS < 1.3) - needs existing project file and existing file
  // QCOMPARE( prj->readPath( "x.shp" ), QString( "/home/qgis/x.shp" ) );

  // VSI: /vsizip, /vsitar, /vsigzip, *.zip, *.gz, *.tgz, ...

  QCOMPARE( prj->readPath( "./x.gz" ), QString( prefix + "/home/qgis/x.gz" ) );
  QCOMPARE( prj->readPath( "/vsigzip/./x.gz" ), QString( "/vsigzip/%1/home/qgis/x.gz" ).arg( prefix ) ); // not sure how useful this really is...

  delete prj;
}
Beispiel #2
0
void TestQgsProject::testLayerFlags()
{
  QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
  QString layerPath = dataDir + "/points.shp";
  QgsVectorLayer *layer1 = new QgsVectorLayer( layerPath, QStringLiteral( "points 1" ), QStringLiteral( "ogr" ) );
  QgsVectorLayer *layer2 = new QgsVectorLayer( layerPath, QStringLiteral( "points 2" ), QStringLiteral( "ogr" ) );

  QgsProject prj;
  prj.addMapLayer( layer1 );
  prj.addMapLayer( layer2 );

  layer2->setFlags( layer2->flags() & ~QgsMapLayer::Removable );

  QString layer2id = layer2->id();

  QTemporaryFile f;
  QVERIFY( f.open() );
  f.close();
  prj.setFileName( f.fileName() );
  prj.write();

  // test reading required layers back
  QgsProject prj2;
  prj2.setFileName( f.fileName() );
  QVERIFY( prj2.read() );
  QgsMapLayer *layer = prj.mapLayer( layer2id );
  QVERIFY( layer );
  QVERIFY( !layer->flags().testFlag( QgsMapLayer::Removable ) );
}
Beispiel #3
0
void TestQgsProject::testLocalFiles()
{
  QTemporaryFile f;
  QVERIFY( f.open() );
  f.close();
  QgsProject prj;
  QFileInfo info( f.fileName() );
  prj.setFileName( f.fileName() );
  prj.write();
  QString shpPath = info.dir().path() + '/' + info.baseName() + ".shp";
  QString layerPath = "file://" + shpPath;
  QFile f2( shpPath );
  QVERIFY( f2.open( QFile::ReadWrite ) );
  f2.close();
  QgsPathResolver resolver( f.fileName( ) );
  QCOMPARE( resolver.writePath( layerPath ), QString( "./" + info.baseName() + ".shp" ) ) ;

}
Beispiel #4
0
void PIHMgis::runImportProject(){
  QString projFile = QFileDialog::getOpenFileName(NULL, "Choose pihmgis project file to import", QString::null, "PIHM GIS project (*.pihmgis)");
  if (!projFile.isEmpty()) {
  typedef enum {Skip, Vector, Raster, Path, String, Int, Double} What;
  typedef struct {
    What what;
    const char* prop;
  } Action;
  Action lines[] = {  //{Skip, "/model/mesh" }; // Excel semi-generated
    {Skip, NULL},   // date
    { Skip, "projDir"},   // line 2,
    { Raster, "dem" },   // line 3, source DEM
    { Raster, "fill" },   // line 4, sink filled DEM
    { Skip, "fill" },   // line 5,
    { Raster, "fdr" },   // line 6,
    { Raster, "fac" },   // line 7,
    { Skip, "fac" },   // line 8, See 7
    { Raster, "strgrid" },   // line 9, Streams raster
    { Int, "facThreshold" },   // line 10, Threshold for stream acc
    { Skip, "strgrid" },   // line 11, See 9
    { Skip, "fdr" },   // line 12, See 6
    { Raster, "lnk" },   // line 13,
    { Skip, "strgrid" },   // line 14, See 11
    { Skip, "fdr" },   // line 15, See 6
    { Vector, "strline" },   // line 16, Streams shapefile
    { Skip, "lnk" },   // line 17,
    { Skip, "fdr" },   // line 18,
    { Raster, "catgrid" },   // line 19,
    { Skip, "catgrid" },   // line 20,
    { Vector, "catpoly" },   // line 21,
    { Skip, "catpoly" },   // line 22, See 21
    { Vector, "catdiss" },   // line 23,
    { Skip, "catdiss" },   // line 24, See 23
    { Vector, "catline" },   // line 25,
    { Skip, "catline" },   // line 26,
    { Vector, "catsimple" },   // line 27,
    { Double, "cattol" },   // line 28, line simplification tolerance for diss polylines?
    { Vector, "strline" },   // line 29,
    { Vector, "strsimple" },   // line 30,
    { Double, "strtol" },   // line 31, line simplification tolerance for streams?
    { Skip, "catsimple" },   // line 32,
    { Vector, "catsplit" },   // line 33,
    { Skip, "strsimple" },   // line 34,
    { Vector, "strsplit" },   // line 35,
    { Skip, "catsplit" },   // line 36, See 33
    { Skip, "strsplit" },   // line 37, See 35
    { Vector, "merge" },   // line 38,
    { Skip, "merge" },   // line 39,
    { Skip, "merge" },   // line 40,
    { Path, "pslg" },   // line 41, Planar Straight Line Graph
    { Skip, "pslg" },   // line 42,
    { Double, "triangle/angle" },   // line 43, triangulation parameter
    { Double, "triangle/area" },   // line 44, triangulation parameter
    { String, "triangle/other" },   // line 45, triangulation parameter
    { Path, "ele" },   // line 46,
    { Path, "node" },   // line 47,
    { Vector, "TIN" },   // line 48, TIN/ele shp file
    { Path, "/model/mesh" },   // line 49,
    { String, "ID" },   // line 50, file ID
    { Skip, NULL },   // line 51,
    { Skip, NULL },   // line 52,
    { Skip, NULL },   // line 53,
    { Path, "Precip" },   // line 54,
    { Path, "Temp" },   // line 55,
    { Path, "Humid" },   // line 56,
    { Path, "Wind" },   // line 57,
    { Path, "G" },   // line 58,
    { Path, "Rn" },   // line 59,
    { Path, "P" },   // line 60,
    { Path, "Soil" },   // line 61,
    { Path, "Geol" },   // line 62,
    { Path, "LC" },   // line 63,
    { Path, "MF" },   // line 64,
    { Path, "MP" },   // line 65,
    { Path, "ISIC" },   // line 66,
    { Path, "SnowIC" },   // line 67,
    { Path, "OverlandIC" },   // line 68,
    { Path, "UnSatIC" },   // line 69,
    { Path, "SatIC" },   // line 70,
    { Path, "BC" },   // line 71,
    { Path, "Source" },   // line 72,
    { Skip, NULL },   // line 73,
    { Path, "/model/att" },   // line 74,
    { Skip, "strsplit" },   // line 75, input stream
    { Skip, "ele" },   // line 76,
    { Skip, "node" },   // line 77,
    { Path, "neigh" },   // line 78,
    { Skip, NULL },   // line 79,
    { Path, "/model/riv" },   // line 80, .riv file
    { Path, "rivdec" },   // line 81, For spatial plots
    { Path, "/model/para" },   // line 82,
    { Skip, NULL },   // line 83,
    { Skip, NULL },   // line 84,
    { Skip, NULL },   // line 85,
    { Skip, NULL },   // line 86,
    { Path, "soil" },   // line 87, in
    { Path, "/model/soil" },   // line 88, out
    { Path, "geol" },   // line 89, in
    { Path, "/model/geol" },   // line 90, out
    { Path, "nlcd" },   // line 91, NLCD source?
    { Path, "/model/lc" },   // line 92,
    { Path, "/model/calib" },   // line 93,
    { Path, "/model/init" },   // line 94,
    { Skip, NULL },   // line 95,
    { Skip, NULL },   // line 96,
    { Skip, NULL },   // line 97,
    { Skip, NULL },   // line 98,
    { Skip, NULL },   // line 99,
    { Double, "res" },   // line 100, DEM resolution
    { Double, "step0" },   // line 101, prep steps, adjustments
    { Double, "step1" },   // line 102,
    { Double, "step2" },   // line 103,
    { Double, "step3" },   // line 104,
    { Double, "step4" },   // line 105,
    { Double, "step5" },   // line 106,
    { Double, "step6" },   // line 107,
    { Double, "step7" },   // line 108,
    { Double, "step8" },   // line 109, adjustments
    { Double, "scale" },   // line 110,
    { Double, "start" },   // line 111, from .para file
    { Double, "finish" },   // line 112, from .para file
    { Skip, NULL },   // line 113,
    { Skip, NULL },   // line 114,
    { Skip, NULL },   // line 115,
    { Skip, NULL },   // line 116,
    { Skip, NULL },   // line 117,
    { Skip, NULL },   // line 118,
    { Skip, NULL },   // line 119,
    { Skip, NULL },   // line 120,
    { Skip, NULL },   // line 121,
  };
  int lines_size = sizeof(lines)/sizeof(lines[0]);
  QFile file(projFile);
  if (file.open(QFile::ReadOnly)) {
    QTextStream in(&file);
    in.readLine();   // date
    QString oldDir = in.readLine();
    QRegExp rx("[\\/]"); // file could have been created on other platform, can't rely on QFileInfo
    qDebug("oldDir = %s", qPrintable(oldDir));
    oldDir = oldDir.left(oldDir.lastIndexOf(rx));
    int oldDirLength = oldDir.length();
    qDebug("oldDir = %s, oldDirLength = %d", qPrintable(oldDir), oldDirLength);
    QFileInfo fi(projFile);
    QString projDir = fi.absoluteDir().absPath();
    qDebug("new projDir is %s", qPrintable(projDir));
    QgsProject *p = QgsProject::instance();
    QString s = projFile.left(projFile.length() - 7) + "qgs";
    p->setFileName(s);   // instead of pihmgis
    p->writeEntry("Paths", "/Absolute", false);
    p->writeEntry("pihm", "projDir", QString("."));
    bool ok;
    double d;
    int i;
    for( int idx=2; idx<lines_size && !in.atEnd(); ++idx) {
      s = in.readLine();
      switch (lines[idx].what) {
      case Int:
        i = s.toInt(&ok);
        if (ok)
          p->writeEntry("pihm", lines[idx].prop, i);
        break;
      case Double:
        d = s.toDouble(&ok);
        if (ok)
          p->writeEntry("pihm", lines[idx].prop, d);
        break;
      case Path:
      {
        qDebug("Path Was %s", qPrintable(s));
        s = ".." + s.mid(oldDirLength);
        qDebug("Truncated version is %s", qPrintable(s));
        QFileInfo fi(projDir, s);
        s = p->writePath(fi.canonicalFilePath());
        qDebug("Became %s", qPrintable(s));
      }       // no break!! Don't move
      case String:
        p->writeEntry("pihm", lines[idx].prop, s);
        break;
      case Raster:
      {
        qDebug("Raster Was %s", qPrintable(s));
        s = ".." + s.mid(oldDirLength);
        qDebug("Truncated version is %s", qPrintable(s));
        QFileInfo fi(projDir, s);
        s = fi.canonicalFilePath();
        qDebug("Became %s", qPrintable(s));
      }
        p->writeEntry("pihm", lines[idx].prop, p->writePath(s));
        mQGisIface->addRasterLayer(s);
        break;
      case Vector:
      {
        qDebug("Vector Was %s", qPrintable(s));
        QFileInfo fi(projDir, ".." + s.mid(oldDirLength));
        s = fi.canonicalFilePath();
        qDebug("Became %s", qPrintable(s));
      }
        p->writeEntry("pihm", lines[idx].prop, p->writePath(s));
        mQGisIface->addVectorLayer(s, QString::null, "ogr");
        break;
      }
    }
  }
  }
}