QgsGraph* RgShortestPathWidget::getPath( QgsPoint& p1, QgsPoint& p2 )
{
  if ( mFrontPointLineEdit->text().isNull() || mBackPointLineEdit->text().isNull() )
  {
    QMessageBox::critical( this, tr( "Point not selected" ), tr( "First, select start and stop points." ) );
    return NULL;
  }

  QgsGraphBuilder builder(
    mPlugin->iface()->mapCanvas()->mapRenderer()->destinationCrs(),
    mPlugin->iface()->mapCanvas()->mapRenderer()->hasCrsTransformEnabled(),
    mPlugin->topologyToleranceFactor() );
  {
    const QgsGraphDirector *director = mPlugin->director();
    if ( director == NULL )
    {
      QMessageBox::critical( this, tr( "Plugin isn't configured" ), tr( "Plugin isn't configured!" ) );
      return NULL;
    }
    connect( director, SIGNAL( buildProgress( int, int ) ), mPlugin->iface()->mainWindow(), SLOT( showProgress( int, int ) ) );
    connect( director, SIGNAL( buildMessage( QString ) ), mPlugin->iface()->mainWindow(), SLOT( showStatusMessage( QString ) ) );

    QVector< QgsPoint > points;
    QVector< QgsPoint > tiedPoint;

    points.push_back( mFrontPoint );
    points.push_back( mBackPoint );
    director->makeGraph( &builder, points, tiedPoint );

    p1 = tiedPoint[ 0 ];
    p2 = tiedPoint[ 1 ];
    // not need
    delete director;
  }

  if ( p1 == QgsPoint( 0.0, 0.0 ) )
  {
    QMessageBox::critical( this, tr( "Tie point failed" ), tr( "Start point doesn't tie to the road!" ) );
    return NULL;
  }
  if ( p2 == QgsPoint( 0.0, 0.0 ) )
  {
    QMessageBox::critical( this, tr( "Tie point failed" ), tr( "Stop point doesn't tie to the road!" ) );
    return NULL;
  }

  QgsGraph *graph = builder.graph();

  QVector< int > pointIdx( 0, 0 );
  QVector< double > pointCost( 0, 0.0 );

  int startVertexIdx = graph->findVertex( p1 );

  int criterionNum = 0;
  if ( mCriterionName->currentIndex() > 0 )
    criterionNum = 1;

  QgsGraph* shortestpathTree = QgsGraphAnalyzer::shortestTree( graph, startVertexIdx, criterionNum );

  delete graph;

  if ( shortestpathTree->findVertex( p2 ) == -1 )
  {
    QMessageBox::critical( this, tr( "Path not found" ), tr( "Path not found" ) );
    return NULL;
  }
  return shortestpathTree;
}
Example #2
0
QgsGraph* RgShortestPathWidget::getPath( QgsPoint& p1, QgsPoint& p2 )
{
  if ( mFrontPointLineEdit->text().isNull() || mBackPointLineEdit->text().isNull() )
  {
    QMessageBox::critical( this, tr( "Point not selected" ), tr( "First, select start and stop points." ) );
    return nullptr;
  }

  QgsGraphBuilder builder(
    mPlugin->iface()->mapCanvas()->mapSettings().destinationCrs(),
    mPlugin->iface()->mapCanvas()->mapSettings().hasCrsTransformEnabled(),
    mPlugin->topologyToleranceFactor() );
  {
    const QgsGraphDirector *director = mPlugin->director();
    if ( !director )
    {
      QMessageBox::critical( this, tr( "Plugin isn't configured" ), tr( "Plugin isn't configured! Please go to the Vector menu, Road Graph, Settings option to configure it." ) );
      return nullptr;
    }
    connect( director, SIGNAL( buildProgress( int, int ) ), mPlugin->iface()->mainWindow(), SLOT( showProgress( int, int ) ) );
    connect( director, SIGNAL( buildMessage( QString ) ), mPlugin->iface()->mainWindow(), SLOT( showStatusMessage( QString ) ) );

    QVector< QgsPoint > points;
    QVector< QgsPoint > tiedPoint;

    points.push_back( mFrontPoint );
    points.push_back( mBackPoint );
    director->makeGraph( &builder, points, tiedPoint );

    p1 = tiedPoint[ 0 ];
    p2 = tiedPoint[ 1 ];
    // not need
    delete director;
  }

  if ( p1 == QgsPoint( 0.0, 0.0 ) )
  {
    QMessageBox::critical( this, tr( "Tie point failed" ), tr( "Start point doesn't tie to the road!" ) );
    return nullptr;
  }
  if ( p2 == QgsPoint( 0.0, 0.0 ) )
  {
    QMessageBox::critical( this, tr( "Tie point failed" ), tr( "Stop point doesn't tie to the road!" ) );
    return nullptr;
  }

  QgsGraph *graph = builder.graph();

  int startVertexIdx = graph->findVertex( p1 );
  if ( startVertexIdx < 0 )
  {
    mPlugin->iface()->messageBar()->pushMessage(
      tr( "Cannot calculate path" ),
      tr( "Could not find start vertex. Please check your input data." ),
      QgsMessageBar::WARNING,
      mPlugin->iface()->messageTimeout()
    );

    delete graph;
    return nullptr;
  }

  int criterionNum = 0;
  if ( mCriterionName->currentIndex() > 0 )
    criterionNum = 1;

  if ( graph->vertexCount() == 0 )
  {
    mPlugin->iface()->messageBar()->pushMessage(
      tr( "Cannot calculate path" ),
      tr( "The created graph is empty. Please check your input data." ),
      QgsMessageBar::WARNING,
      mPlugin->iface()->messageTimeout()
    );

    delete graph;
    return nullptr;
  }


  QgsGraph* shortestpathTree = QgsGraphAnalyzer::shortestTree( graph, startVertexIdx, criterionNum );
  delete graph;

  if ( shortestpathTree->findVertex( p2 ) == -1 )
  {
    delete shortestpathTree;
    QMessageBox::critical( this, tr( "Path not found" ), tr( "Path not found" ) );
    return nullptr;
  }
  return shortestpathTree;
}