Esempio n. 1
0
 GeoVectorFilesObserver() : PluggableObserver()
 {
   GDALAllRegister_COMPAT();
 }
Esempio n. 2
0
bool ImportWorker::processFilesAndDatastore(int Step)
{
  emit stepEntered(tr("Copying files and populating datastore..."));

  int i=0;
  bool OKToContinue = true;

  while (i<m_SourcesInfos.size() && OKToContinue)
  {

    if (m_SourcesInfos[i].IsDatasetImport)
    {
      // local copy of files in dataset

      QString FullSrcFilePath = m_SourcesInfos[i].SourceURI;
      if (!m_SourcesInfos[i].CachedSourceURI.isEmpty())
        FullSrcFilePath = m_SourcesInfos[i].CachedSourceURI;

      QString FullDestFilePath = m_InputDir + "/" + m_SourcesInfos[i].RelativeDatasetPath;
      QString FullDestPath = QFileInfo(FullDestFilePath).absolutePath();
      QString DestExtension = QFileInfo(FullDestFilePath).suffix();

      QDir().mkpath(FullDestPath);

      GDALAllRegister_COMPAT();

      GDALDataset_COMPAT* SrcDS = GDALOpenRO_COMPAT(FullSrcFilePath.toStdString().c_str());

      if (SrcDS != nullptr)
      {
        GDALDriver_COMPAT *CopyDriver;

        QString DriverName = OGRGDALHelpers::getDriverFromFileExt(DestExtension);

        CopyDriver = GDALGetDriverByName_COMPAT(DriverName.toStdString().c_str());

        if (CopyDriver != nullptr)
        {
          GDALDataset_COMPAT* DestDS = GDALCopy_COMPAT(CopyDriver,SrcDS,FullDestFilePath.toStdString().c_str());

          GDALClose_COMPAT(DestDS);
        }

        GDALClose_COMPAT(SrcDS);

      }
    }

    if (m_SourcesInfos[i].IsDatastore)
    {

      QString RelativePath = m_SourcesInfos[i].RelativeDatasetPath;

      if (m_SourcesInfos[i].IsAlreadyInDataset)
        RelativePath = QDir(m_InputDir).relativeFilePath(m_SourcesInfos[i].SourceURI);


      // populate datastore

      openfluid::fluidx::DatastoreItemDescriptor* DSItem =
          new openfluid::fluidx::DatastoreItemDescriptor(m_SourcesInfos[i].DatastoreID.toStdString(),
                                                         m_InputDir.toStdString(),
                                                         RelativePath.toStdString(),
                                                         openfluid::core::UnstructuredValue::GeoVectorValue);
      DSItem->setUnitsClass(m_SourcesInfos[i].UnitsClass.toStdString());

      mp_AdvDesc->datastore().appendItem(DSItem);
    }

    i++;
  }

  emit stepCompleted(Step,getStyledText(tr("[OK]"),"green"));

  return true;
}
bool DataProcessingWorker::loadDataFromSources(int Step)
{
  emit stepEntered(tr("Loading and checking data from sources..."));


  GDALAllRegister_COMPAT();

  for (int i=0; i<m_SourcesInfos.size();i++)
  {

    QString RealURI = m_SourcesInfos[i].SourceURI;
    if (!m_SourcesInfos[i].CachedSourceURI.isEmpty())
      RealURI = m_SourcesInfos[i].CachedSourceURI;


    GDALDataset_COMPAT* DS = GDALOpenRO_COMPAT(RealURI.toStdString().c_str());

    if (DS == nullptr)
    {
      emit stepCompleted(Step,getStyledText(tr("[Error] Unable to open datasource for layer \"%1\"")
                                            .arg(m_SourcesInfos[i].LayerName),"red"));
      return false;
    }

    OGRLayer *Layer;
    OGRFeature *Feature;

    Layer = DS->GetLayerByName(m_SourcesInfos[i].LayerName.toStdString().c_str());

    // For cached layers as GeoJSON files
    // TODO do better for that!
    if (Layer == nullptr)
    {
      Layer = DS->GetLayer(0);
    }

    Layer->ResetReading();
    while((Feature = Layer->GetNextFeature()) != nullptr )
    {
      SourceUnit CurrentUnit;
      int CurrentUnitID;

      int FieldIndex = 0;
      QString FieldValue;

      // === Unit ID ===

      FieldIndex = Feature->GetFieldIndex(OGRGDAL_UNITID_FIELD);

      if (FieldIndex < 0)
      {
        GDALClose_COMPAT(DS);
        emit stepCompleted(Step,getStyledText(tr("[Error] Field for unit ID not found in layer \"%1\"")
                                              .arg(m_SourcesInfos[i].LayerName),"red"));
        return false;
      }

      CurrentUnitID = Feature->GetFieldAsInteger(FieldIndex);

      if (CurrentUnitID <= 0)
      {
        GDALClose_COMPAT(DS);
        emit stepCompleted(Step,getStyledText(tr("[Error] Wrong field format for unit ID in layer \"%1\"")
                                              .arg(m_SourcesInfos[i].LayerName),"red"));
        return false;
      }

      if (m_SourcesData[i].isUnitExists(CurrentUnitID))
      {
        GDALClose_COMPAT(DS);
        emit stepCompleted(Step,getStyledText(tr("[Error] Unit ID %2 already exist in layer \"%1\"")
                                                 .arg(m_SourcesInfos[i].LayerName)
                                                 .arg(CurrentUnitID),
                                              "red"));
        return false;
      }


      // === Process order ===

      if (m_SourcesInfos[i].UnitsPcsOrdField.isEmpty())
      {
        CurrentUnit.ProcessOrder = 1;
      }
      else
      {
        FieldIndex = Feature->GetFieldIndex(m_SourcesInfos[i].UnitsPcsOrdField.toStdString().c_str());

        if (FieldIndex < 0)
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Field for process order not found in layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName),
                                                "red"));
          return false;
        }

        int PcsOrd = Feature->GetFieldAsInteger(FieldIndex);

        if (PcsOrd <= 0)
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Wrong field format for process order in layer \"%1\"")
                                                 .arg(m_SourcesInfos[i].LayerName),
                                                "red"));
          return false;
        }

        CurrentUnit.ProcessOrder = PcsOrd;
      }


      // === To connections ===

      if (!m_SourcesInfos[i].ToConnectionsField.isEmpty())
      {
        FieldIndex = Feature->GetFieldIndex(m_SourcesInfos[i].ToConnectionsField.toStdString().c_str());

        if (FieldIndex < 0)
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Field for \"To\" connections not found in layer \"%1\"")
                                                .arg(m_SourcesInfos[i].LayerName),"red"));
          return false;
        }

        CurrentUnit.ToConnStr = QString(Feature->GetFieldAsString(FieldIndex));

        if (!OGRGDALHelpers::convertConnectionsStringToList(CurrentUnit.ToConnStr,CurrentUnit.ToConn))
        {
          emit stepCompleted(Step,getStyledText(tr("[Error] Wrong field \"%2\" format for \"To\" connections "
                                                   "in layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName)
                                                   .arg(m_SourcesInfos[i].ToConnectionsField),
                                                "red"));
          return false;
        }
      }


      // === Childof connections ===

      if (!m_SourcesInfos[i].ChildofConnectionsField.isEmpty())
      {
        FieldIndex = Feature->GetFieldIndex(m_SourcesInfos[i].ChildofConnectionsField.toStdString().c_str());

        if (FieldIndex < 0)
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,
                             getStyledText(tr("[Error] Field for \"Child Of\" connections not found in layer \"%1\"")
                                           .arg(m_SourcesInfos[i].LayerName),"red"));
          return false;
        }

        CurrentUnit.ChildofConnStr = QString(Feature->GetFieldAsString(FieldIndex));

        if (!OGRGDALHelpers::convertConnectionsStringToList(CurrentUnit.ChildofConnStr,CurrentUnit.ChildofConn))
        {
          emit stepCompleted(Step,getStyledText(tr("[Error] Wrong field \"%2\" format for \"Child Of\" connections "
                                                   "in layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName)
                                                   .arg(m_SourcesInfos[i].ChildofConnectionsField),
                                                "red"));
          return false;
        }
      }


      // === Attributes ===

      for (int j=0;j<m_SourcesInfos[i].ImportedFields.size(); j++)
      {
        FieldIndex = Feature->GetFieldIndex(m_SourcesInfos[i].ImportedFields[j].toStdString().c_str());

        if (FieldIndex <0)
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Field for attribute \"%2\" not found in layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName)
                                                   .arg(m_SourcesInfos[i].ImportedFields[j]),
                                                "red"));
          return false;
        }

        // replacing unwanted chars (space, tab) by underscore
        QString Attr;

        if (!OGRGDALHelpers::convertFieldToAttribute(Feature,FieldIndex,Attr))
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Wrong field format for attribute \"%2\" in layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName)
                                                   .arg(m_SourcesInfos[i].ImportedFields[j]),
                                                "red"));
          return false;
        }

        CurrentUnit.Attributes[m_SourcesInfos[i].ImportedFields[j]] = Attr.replace(" ","_")
                                                                          .replace("\t","_");
      }


      // === Area computed attribute ===

      if (m_SourcesInfos[i].IsAreaCompute)
      {
        if (m_SourcesInfos[i].ImportedFields.contains(m_SourcesInfos[i].AreaComputeAttribute))
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Attribute \"%2\" for computed area attribute "
                                                   "is already imported from layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName)
                                                   .arg(m_SourcesInfos[i].AreaComputeAttribute),
                                                "red"));
          return false;
        }
        else
        {
          double Area = ((OGRPolygon*)(Feature->GetGeometryRef()))->get_Area();
          CurrentUnit.Attributes[m_SourcesInfos[i].AreaComputeAttribute] = QString::number(Area,'g');
        }
      }


      // === Length computed attribute ===

      if (m_SourcesInfos[i].IsLengthCompute)
      {
        if (m_SourcesInfos[i].ImportedFields.contains(m_SourcesInfos[i].LengthComputeAttribute))
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Attribute \"%2\" for computed length attribute "
                                                   "is already imported from layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName)
                                                   .arg(m_SourcesInfos[i].LengthComputeAttribute),
                                                "red"));
          return false;
        }
        else
        {
          double Length = ((OGRLineString*)(Feature->GetGeometryRef()))->get_Length();
          CurrentUnit.Attributes[m_SourcesInfos[i].LengthComputeAttribute] = QString::number(Length,'g');
        }
      }


      // === Preprocess for computed centroid

      OGRPoint Centroid;

      if (Feature->GetGeometryRef()->Centroid(&Centroid) != OGRERR_NONE)
      {
        GDALClose_COMPAT(DS);
        emit stepCompleted(Step,
                           getStyledText(tr("[Error] Unable to compute centroid coordinates "
                                            "for geometries in layer \"%1\"."
                                            "Computing centroid coordinates should be unchecked "
                                            "in computed attributes.")
                                         .arg(m_SourcesInfos[i].LayerName),"red"));
        return false;
      }


      // === XCentroid computed attribute ===

      if (m_SourcesInfos[i].IsXCentroidCompute)
      {
        if (m_SourcesInfos[i].ImportedFields.contains(m_SourcesInfos[i].XCentroidComputeAttribute))
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Attribute \"%2\" for computed centroid X attribute "
                                                   "is already imported from layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName)
                                                   .arg(m_SourcesInfos[i].XCentroidComputeAttribute),
                                                "red"));
          return false;
        }
        else
        {
          CurrentUnit.Attributes[m_SourcesInfos[i].XCentroidComputeAttribute] = QString::number(Centroid.getX(),'g');
        }
      }


      // === YCentroid computed attribute ===

      if (m_SourcesInfos[i].IsYCentroidCompute)
      {
        if (m_SourcesInfos[i].ImportedFields.contains(m_SourcesInfos[i].YCentroidComputeAttribute))
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Attribute \"%2\" for computed centroid Y attribute "
                                                   "is already imported from layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName)
                                                   .arg(m_SourcesInfos[i].YCentroidComputeAttribute),
                                                "red"));
          return false;
        }
        else
        {
          CurrentUnit.Attributes[m_SourcesInfos[i].YCentroidComputeAttribute] = QString::number(Centroid.getY(),'g');
        }
      }


      // === ZCentroid computed attribute ===

      if (m_SourcesInfos[i].IsZCentroidCompute)
      {
        if (m_SourcesInfos[i].ImportedFields.contains(m_SourcesInfos[i].ZCentroidComputeAttribute))
        {
          GDALClose_COMPAT(DS);
          emit stepCompleted(Step,getStyledText(tr("[Error] Attribute \"%2\" for computed centroid Z attribute "
                                                   "is already imported from layer \"%1\"")
                                                   .arg(m_SourcesInfos[i].LayerName)
                                                   .arg(m_SourcesInfos[i].ZCentroidComputeAttribute),
                                                "red"));
          return false;
        }
        else
        {
          CurrentUnit.Attributes[m_SourcesInfos[i].ZCentroidComputeAttribute] = QString::number(Centroid.getZ(),'g');
        }
      }


      m_SourcesData[i].Units[CurrentUnitID] = CurrentUnit;
    }

    GDALClose_COMPAT(DS);
    DS = nullptr;
  }

  emit stepCompleted(Step,getStyledText(tr("[OK]"),"green"));
  return true;
}