Esempio n. 1
0
void TestQgsHistogram::fromLayer()
{
  QgsHistogram h;

  QVERIFY( !h.setValues( 0, QString() ) );

  QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?field=col1:real" ), QStringLiteral( "layer" ), QStringLiteral( "memory" ) );
  QVERIFY( layer->isValid() );
  QgsFeatureList features;
  for ( int i = 1; i <= 10; ++i )
  {
    QgsFeature f( layer->dataProvider()->fields(), i );
    f.setAttribute( QStringLiteral( "col1" ), i );
    features << f;
  }
  layer->dataProvider()->addFeatures( features );

  QVERIFY( !h.setValues( layer, QString() ) );
  QVERIFY( h.setValues( layer, QString( "col1" ) ) );
  QList<int>counts = h.counts( 5 );
  QList<int> expected;
  expected << 2 << 2 << 2 << 2 << 2;
  QCOMPARE( counts, expected );

  delete layer;
}
Esempio n. 2
0
    void testSnapOnIntersection()
    {
      // testing with a layer with two crossing linestrings
      // (0,1)  x  x (1,1)
      //         \/
      //         /\    .
      // (0,0)  x  x (1,0)
      QgsVectorLayer* vl = new QgsVectorLayer( QStringLiteral( "LineString" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );
      QgsPolyline polyline1, polyline2;
      polyline1 << QgsPoint( 0, 0 ) << QgsPoint( 1, 1 );
      polyline2 << QgsPoint( 1, 0 ) << QgsPoint( 0, 1 );
      QgsFeature f1;
      QgsGeometry f1g = QgsGeometry::fromPolyline( polyline1 ) ;
      f1.setGeometry( f1g );
      QgsFeature f2;
      QgsGeometry f2g = QgsGeometry::fromPolyline( polyline2 );
      f2.setGeometry( f2g );
      QgsFeatureList flist;
      flist << f1 << f2;
      vl->dataProvider()->addFeatures( flist );

      QVERIFY( vl->dataProvider()->featureCount() == 2 );

      QgsMapSettings mapSettings;
      mapSettings.setOutputSize( QSize( 100, 100 ) );
      mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) );
      QVERIFY( mapSettings.hasValidSettings() );

      QgsSnappingUtils u;
      u.setMapSettings( mapSettings );
      QgsSnappingConfig snappingConfig = u.config();
      snappingConfig.setMode( QgsSnappingConfig::AdvancedConfiguration );
      QgsSnappingConfig::IndividualLayerSettings layerSettings( true, QgsSnappingConfig::Vertex, 0.1, QgsTolerance::ProjectUnits );
      snappingConfig.setIndividualLayerSettings( vl, layerSettings );
      u.setConfig( snappingConfig );

      // no snapping on intersections by default - should find nothing
      QgsPointLocator::Match m = u.snapToMap( QgsPoint( 0.45, 0.5 ) );
      QVERIFY( !m.isValid() );

      snappingConfig.setIntersectionSnapping( true );
      u.setConfig( snappingConfig );

      QgsPointLocator::Match m2 = u.snapToMap( QgsPoint( 0.45, 0.5 ) );
      QVERIFY( m2.isValid() );
      QCOMPARE( m2.type(), QgsPointLocator::Vertex );
      QCOMPARE( m2.point(), QgsPoint( 0.5, 0.5 ) );

      delete vl;
    }
void QgsOfflineEditingPluginGui::updateLayerList( bool filterEditableLayers )
{
  ui_layerList->clear();

  QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
  for ( QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin() ; layer_it != mapLayers.end(); ++layer_it )
  {
    if ( layer_it.value()->type() == QgsMapLayer::VectorLayer )
    {
      QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( layer_it.value() );

      bool showLayer = true;
      if ( filterEditableLayers )
      {
        int cap = layer->dataProvider()->capabilities();
        showLayer = ( cap & QgsVectorDataProvider::AddFeatures ) &&
                    ( cap & QgsVectorDataProvider::DeleteFeatures ) &&
                    ( cap & QgsVectorDataProvider::ChangeAttributeValues ) &&
                    ( cap & QgsVectorDataProvider::AddAttributes ) &&
                    ( cap & QgsVectorDataProvider::ChangeGeometries );
      }
      if ( showLayer )
      {
        QListWidgetItem* item = new QListWidgetItem( layer->name(), ui_layerList );
        item->setData( Qt::UserRole, QVariant( layer_it.key() ) );
      }
    }
  }
}
bool QgsZonalStatisticsDialog::prefixIsValid( const QString& prefix ) const
{
  QgsVectorLayer* vl = polygonLayer();
  if ( !vl )
  {
    return false;
  }
  QgsVectorDataProvider* dp = vl->dataProvider();
  if ( !dp )
  {
    return false;
  }

  const QgsFields& providerFields = dp->fields();
  QString currentFieldName;

  for ( int idx = 0; idx < providerFields.count(); ++idx )
  {
    currentFieldName = providerFields[idx].name();
    if ( currentFieldName == ( prefix + "mean" ) || currentFieldName == ( prefix + "sum" ) || currentFieldName == ( prefix + "count" ) )
    {
      return false;
    }
  }
  return true;
}
void QgsZonalStatisticsDialog::insertAvailableLayers()
{
  //insert available raster layers
  //enter available layers into the combo box
  QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
  QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin();

  for ( ; layer_it != mapLayers.end(); ++layer_it )
  {
    QgsRasterLayer* rl = dynamic_cast<QgsRasterLayer*>( layer_it.value() );
    if ( rl )
    {
      QgsRasterDataProvider* rp = rl->dataProvider();
      if ( rp && rp->name() == "gdal" )
      {
        mRasterLayerComboBox->addItem( rl->name(), QVariant( rl->id() ) );
      }
    }
    else
    {
      QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer_it.value() );
      if ( vl && vl->geometryType() == QGis::Polygon )
      {
        QgsVectorDataProvider* provider  = vl->dataProvider();
        if ( provider->capabilities() & QgsVectorDataProvider::AddAttributes )
        {
          mPolygonLayerComboBox->addItem( vl->name(), QVariant( vl->id() ) );
        }
      }
    }
  }
}
void RgLineVectorLayerSettingsWidget::on_mcbLayers_selectItem()
{
  mcbDirection->clear();
  mcbSpeed->clear();

  mcbDirection->insertItem( 0, tr( "Always use default" ) );
  mcbSpeed->insertItem( 0, tr( "Always use default" ) );

  QgsVectorLayer* vl = selectedLayer();
  if ( !vl )
    return;

  QgsVectorDataProvider* provider = vl->dataProvider();
  if ( !provider )
    return;

  const QgsFields& fields = provider->fields();
  for ( int idx = 0; idx < fields.count(); ++idx )
  {
    const QgsField& currentField = fields[idx];
    QVariant currentType = currentField.type();
    if ( currentType == QVariant::Int || currentType == QVariant::String )
    {
      mcbDirection->insertItem( 1, currentField.name() );
    }
    if ( currentType == QVariant::Int || currentType == QVariant::Double )
    {
      mcbSpeed->insertItem( 1, currentField.name() );
    }
  }

} // RgDSettingsDlg::on_mcbLayers_selectItem()
void QgsLegendLayerFile::addToPopupMenu( QMenu& theMenu, QAction* toggleEditingAction )
{
  QgsMapLayer* lyr = layer();

  // zoom to layer extent
  theMenu.addAction( QgisApp::getThemeIcon( "/mActionZoomToLayer.png" ),
                     tr( "&Zoom to layer extent" ), legend(), SLOT( legendLayerZoom() ) );

  // show in overview
  QAction* showInOverviewAction = theMenu.addAction( tr( "&Show in overview" ), this, SLOT( showInOverview() ) );
  showInOverviewAction->setCheckable( true );
  showInOverviewAction->blockSignals( true );
  showInOverviewAction->setChecked( mLyr.isInOverview() );
  showInOverviewAction->blockSignals( false );

  // remove from canvas
  theMenu.addAction( QgisApp::getThemeIcon( "/mActionRemove.png" ),
                     tr( "&Remove" ), legend(), SLOT( legendLayerRemove() ) );

  theMenu.addSeparator();

  if ( lyr->type() == QgsMapLayer::VectorLayer )
  {
    QgsVectorLayer* vlayer = dynamic_cast<QgsVectorLayer*>( lyr );

    // attribute table
    theMenu.addAction( tr( "&Open attribute table" ), this, SLOT( table() ) );

    // editing
    int cap = vlayer->dataProvider()->capabilities();
    if ( cap & QgsVectorDataProvider::EditingCapabilities )
    {
      if ( toggleEditingAction )
      {
        theMenu.addAction( toggleEditingAction );
      }
    }

    // save as shapefile
    theMenu.addAction( tr( "Save as shapefile..." ), this, SLOT( saveAsShapefile() ) );

    QAction* saveSelectionAction = theMenu.addAction( tr( "Save selection as shapefile..." ), this, SLOT( saveSelectionAsShapefile() ) );
    if ( vlayer->selectedFeatureCount() == 0 )
    {
      saveSelectionAction->setEnabled( false );
    }

    theMenu.addSeparator();
  }
  else if ( lyr->type() == QgsMapLayer::RasterLayer )
  {
    // TODO: what was this for?
    //QgsRasterLayer* rlayer = dynamic_cast<QgsRasterLayer*>(lyr);
    //theMenu.addAction(tr("&Convert to..."), rlayer, SLOT(convertTo()));
  }

  // properties goes on bottom of menu for consistency with normal ui standards
  // e.g. kde stuff
  theMenu.addAction( tr( "&Properties" ), legend(), SLOT( legendLayerShowProperties() ) );
}
Esempio n. 8
0
QgsVectorLayer* RgExportDlg::mapLayer() const
{
  QgsVectorLayer* myLayer = NULL;
  QString layerId = mcbLayers->itemData( mcbLayers->currentIndex() ).toString();

  if ( layerId == "-1" )
  {
    // create a temporary layer
    myLayer = new QgsVectorLayer( QString( "LineString?crs=epsg:4326&memoryid=%1" ).arg( QUuid::createUuid().toString() ), "shortest path", "memory" );

    QgsVectorDataProvider *prov = myLayer->dataProvider();
    if ( prov == NULL )
      return NULL;

    QList<QgsField> attrList;
    attrList.append( QgsField( "length", QVariant::Double, "", 20, 8 ) );
    attrList.append( QgsField( "time", QVariant::Double, "", 20, 8 ) );
    prov->addAttributes( attrList );
    myLayer->updateFields();
    QList<QgsMapLayer *> myList;
    myList << myLayer;
    QgsMapLayerRegistry::instance()->addMapLayers( myList );
  }
  else
  {
    // return selected layer
    myLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
  }

  return myLayer;
} // QgsVectorLayer* RgExportDlg::vectorLayer() const
bool QgsZonalStatisticsDialog::prefixIsValid( const QString& prefix ) const
{
  QgsVectorLayer* vl = polygonLayer();
  if ( !vl )
  {
    return false;
  }
  QgsVectorDataProvider* dp = vl->dataProvider();
  if ( !dp )
  {
    return false;
  }

  QgsFieldMap providerFieldMap = dp->fields();
  QgsFieldMap::const_iterator it = providerFieldMap.constBegin();
  QString currentFieldName;

  for ( ; it != providerFieldMap.constEnd(); ++it )
  {
    currentFieldName = it.value().name();
    if ( currentFieldName == ( prefix + "mean" ) || currentFieldName == ( prefix + "sum" ) || currentFieldName == ( prefix + "count" ) )
    {
      return false;
    }
  }
  return true;
}
Esempio n. 10
0
bool QgsZonalStatisticsDialog::prefixIsValid( const QString& prefix ) const
{
    QgsVectorLayer* vl = polygonLayer();
    if ( !vl )
    {
        return false;
    }
    QgsVectorDataProvider* dp = vl->dataProvider();
    if ( !dp )
    {
        return false;
    }

    QString currentFieldName;

    Q_FOREACH ( const QgsField& field, dp->fields() )
    {
        currentFieldName = field.name();
        if ( currentFieldName == ( prefix + "mean" ) || currentFieldName == ( prefix + "sum" ) || currentFieldName == ( prefix + "count" ) )
        {
            return false;
        }
    }
    return true;
}
Esempio n. 11
0
QgsRectangle QgsInterpolationDialog::boundingBoxOfLayers()
{
  int nLayers = mLayersTreeWidget->topLevelItemCount();
  QgsRectangle combinedLayerExtent;

  for ( int i = 0; i < nLayers; ++i )
  {
    QString layerName = mLayersTreeWidget->topLevelItem( i )->text( 0 );
    QgsVectorLayer* theVectorLayer = vectorLayerFromName( layerName );
    if ( !theVectorLayer )
    {
      continue;
    }

    QgsVectorDataProvider* theProvider = theVectorLayer->dataProvider();
    if ( !theProvider )
    {
      continue;
    }

    //update extent
    QgsRectangle currentLayerExtent = theVectorLayer->extent();
    if ( combinedLayerExtent.isEmpty() )
    {
      combinedLayerExtent = currentLayerExtent;
    }
    else
    {
      combinedLayerExtent.combineExtentWith( &currentLayerExtent );
    }
  }
  return combinedLayerExtent;
}
int QgsUniqueValueRenderer::readXML( const QDomNode& rnode, QgsVectorLayer& vl )
{
  mGeometryType = vl.geometryType();
  QDomNode classnode = rnode.namedItem( "classificationfield" );
  QString classificationField = classnode.toElement().text();

  QgsVectorDataProvider* theProvider = vl.dataProvider();
  if ( !theProvider )
  {
    return 1;
  }

  int classificationId = vl.fieldNameIndex( classificationField );
  if ( classificationId == -1 )
  {
    //go on. Because with joins, it might be the joined layer is not loaded yet
  }
  setClassificationField( classificationId );

  QDomNode symbolnode = rnode.namedItem( "symbol" );
  while ( !symbolnode.isNull() )
  {
    QgsSymbol* msy = new QgsSymbol( mGeometryType );
    msy->readXML( symbolnode, &vl );
    insertValue( msy->lowerValue(), msy );
    symbolnode = symbolnode.nextSibling();
  }
  updateSymbolAttributes();
  vl.setRenderer( this );
  return 0;
}
Esempio n. 13
0
void TestQgsGeometrySnapper::snapPolygonToPolygon()
{
  QgsVectorLayer* rl = new QgsVectorLayer( QStringLiteral( "Polygon" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );
  QgsFeature ff( 0 );
  QgsGeometry refGeom = QgsGeometry::fromWkt( QStringLiteral( "Polygon((0 0, 10 0, 10 10, 0 10, 0 0))" ) );
  ff.setGeometry( refGeom );
  QgsFeatureList flist;
  flist << ff;
  rl->dataProvider()->addFeatures( flist );

  QgsGeometry polygonGeom = QgsGeometry::fromWkt( QStringLiteral( "Polygon((0.1 -0.1, 10.1 0, 9.9 10.1, 0 10, 0.1 -0.1))" ) );
  QgsGeometrySnapper snapper( rl );
  QgsGeometry result = snapper.snapGeometry( polygonGeom, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((0 0, 10 0, 10 10, 0 10, 0 0))" ) );

  QgsGeometry polygonGeom2 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((0.1 -0.1, 10.1 0, 0 10, 0.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom2, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((0 0, 10 0, 0 10, 0 0))" ) );

  // insert new vertex
  QgsGeometry polygonGeom3 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((0.1 -0.1, 20.5 0.5, 20 10, 0 9.9, 0.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom3, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((0 0, 10 0, 20.5 0.5, 20 10, 10 10, 0 10, 0 0))" ) );

  // remove vertex
  QgsGeometry polygonGeom4 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((0.1 -0.1, 10.1 0, 9.9 10.1, 5 10, 0 10, 0.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom4, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((0 0, 10 0, 10 10, 0 10, 0 0))" ) );
}
const RgGraphDirector* RoadGraphPlugin::director() const
{
  QString layerId;
  QgsVectorLayer *layer = NULL;
  QMap< QString, QgsMapLayer* > mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
  QMap< QString, QgsMapLayer* >::const_iterator it;
  for ( it = mapLayers.begin(); it != mapLayers.end(); ++it )
  {
    if ( it.value()->name() != mSettings->mLayer )
      continue;
    layerId = it.key();
    layer = dynamic_cast< QgsVectorLayer* >( it.value() );
    break;
  }
  if ( layer == NULL )
    return NULL;

  QgsVectorDataProvider *provider = dynamic_cast< QgsVectorDataProvider* >( layer->dataProvider() );
  if ( provider == NULL )
    return NULL;

  RgLineVectorLayerDirector * director =
    new RgLineVectorLayerDirector( layerId,
                                   provider->fieldNameIndex( mSettings->mDirection ),
                                   mSettings->mFirstPointToLastPointDirectionVal,
                                   mSettings->mLastPointToFirstPointDirectionVal,
                                   mSettings->mBothDirectionVal,
                                   mSettings->mDefaultDirection,
                                   mSettings->mSpeedUnitName,
                                   provider->fieldNameIndex( mSettings->mSpeed ),
                                   mSettings->mDefaultSpeed );

  return director;
}
Esempio n. 15
0
void QgsGrassPlugin::resetEditActions()
{
  QgsDebugMsg( "Entered" );

  QgsGrassProvider* grassProvider = 0;
  QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( qGisInterface->activeLayer() );
  if ( vectorLayer )
  {
    grassProvider = dynamic_cast<QgsGrassProvider*>( vectorLayer->dataProvider() );
  }
  if ( grassProvider && vectorLayer->editBuffer() )
  {
    mAddFeatureAction->setVisible( false );
    mAddPointAction->setVisible( true );
    mAddLineAction->setVisible( true );
    mAddBoundaryAction->setVisible( true );
    mAddCentroidAction->setVisible( true );
    mAddAreaAction->setVisible( true );
  }
  else
  {
    mAddFeatureAction->setVisible( true );
    mAddPointAction->setVisible( false );
    mAddLineAction->setVisible( false );
    mAddBoundaryAction->setVisible( false );
    mAddCentroidAction->setVisible( false );
    mAddAreaAction->setVisible( false );
  }
}
bool QgsUniqueValueRenderer::writeXML( QDomNode & layer_node, QDomDocument & document, const QgsVectorLayer& vl ) const
{
  const QgsVectorDataProvider* theProvider = vl.dataProvider();
  if ( !theProvider )
  {
    return false;
  }

  QString classificationFieldName;
  const QgsFields& fields = vl.pendingFields();
  if ( mClassificationField >= 0 && mClassificationField < fields.count() )
  {
    classificationFieldName = fields[ mClassificationField ].name();
  }

  bool returnval = true;
  QDomElement uniquevalue = document.createElement( "uniquevalue" );
  layer_node.appendChild( uniquevalue );
  QDomElement classificationfield = document.createElement( "classificationfield" );
  QDomText classificationfieldtxt = document.createTextNode( classificationFieldName );
  classificationfield.appendChild( classificationfieldtxt );
  uniquevalue.appendChild( classificationfield );
  for ( QMap<QString, QgsSymbol*>::const_iterator it = mSymbols.begin(); it != mSymbols.end(); ++it )
  {
    if ( !( it.value()->writeXML( uniquevalue, document, &vl ) ) )
    {
      returnval = false;
    }
  }
  return returnval;
}
Esempio n. 17
0
void RgLineVectorLayerSettingsWidget::on_mcbLayers_selectItem()
{
  mcbDirection->clear();
  mcbSpeed->clear();

  mcbDirection->insertItem( 0, tr( "Always use default" ) );
  mcbSpeed->insertItem( 0, tr( "Always use default" ) );

  QgsVectorLayer* vl = selectedLayer();
  if ( !vl )
    return;

  QgsVectorDataProvider* provider = vl->dataProvider();
  if ( !provider )
    return;

  const QgsFieldMap& fields = provider->fields();
  QgsFieldMap::const_iterator it;
  for ( it = fields.constBegin(); it != fields.constEnd(); ++it )
  {
    QgsField currentField = it.value();
    QVariant currentType = currentField.type();
    if ( currentType == QVariant::Int || currentType == QVariant::String )
    {
      mcbDirection->insertItem( 1, currentField.name() );
    }
    if ( currentType == QVariant::Int || currentType == QVariant::Double )
    {
      mcbSpeed->insertItem( 1, currentField.name() );
    }
  }

} // RgDSettingsDlg::on_mcbLayers_selectItem()
Esempio n. 18
0
QgsTransaction* QgsTransaction::create( const QStringList& layerIds )
{
  if ( layerIds.isEmpty() )
  {
    return 0;
  }

  QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerIds.first() ) );
  if ( !layer )
  {
    return 0;
  }

  QString connStr = QgsDataSourceURI( layer->source() ).connectionInfo();
  QString providerKey = layer->dataProvider()->name();
  QgsTransaction* ts = QgsTransaction::create( connStr, providerKey );
  if ( !ts )
  {
    return 0;
  }

  Q_FOREACH ( const QString& layerId, layerIds )
  {
    if ( !ts->addLayer( layerId ) )
    {
      delete ts;
      return 0;
    }
  }
  return ts;
}
void QgsAttributeTypeLoadDialog::loadDataToValueMap()
{
  mValueMap.clear();
  int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
  int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentText() );
  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
  if ( vLayer == NULL )
  {
    return;
  }

  QgsVectorDataProvider* dataProvider = vLayer->dataProvider();
  dataProvider->enableGeometrylessFeatures( true );

  QgsAttributeList attributeList = QgsAttributeList();
  attributeList.append( idx );
  attributeList.append( idx2 );
  vLayer->select( attributeList, QgsRectangle(), false );

  QgsFeature f;
  while ( vLayer->nextFeature( f ) )
  {
    QVariant val = f.attributeMap()[idx];
    if ( val.isValid() && !val.isNull() && !val.toString().isEmpty() )
    {
      mValueMap.insert( f.attributeMap()[idx2].toString(), val );
    }
  }
  dataProvider->enableGeometrylessFeatures( false );
}
Esempio n. 20
0
QgsVectorLayer* RgExportDlg::mapLayer() const
{
  QgsVectorLayer* myLayer = NULL;
  QString layerId = mcbLayers->itemData( mcbLayers->currentIndex() ).toString();

  if ( layerId == QString( "-1" ) )
  {
    // create a temporary layer
    myLayer = new QgsVectorLayer( "LineString", "shortest path", "memory" );

    QgsVectorDataProvider *prov = myLayer->dataProvider();
    if ( prov == NULL )
      return NULL;

    QList<QgsField> attrList;
    attrList.append( QgsField( "one", QVariant::Int ) );
    prov->addAttributes( attrList );
    QList<QgsMapLayer *> myList;
    myList << myLayer;
    QgsMapLayerRegistry::instance()->addMapLayers( myList );

  }
  else
  {
    // retrun selected layer
    myLayer = dynamic_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
  }

  return myLayer;
} // QgsVectorLayer* RgExportDlg::vectorLayer() const
Esempio n. 21
0
void QgsVectorLayerProperties::on_mButtonAddJoin_clicked()
{
  QgsAddJoinDialog d( layer );
  if ( d.exec() == QDialog::Accepted )
  {
    QgsVectorJoinInfo info;
    info.targetField = d.targetField();
    info.joinLayerId = d.joinedLayerId();
    info.joinField = d.joinField();
    info.memoryCache = d.cacheInMemory();
    if ( layer )
    {
      //create attribute index if possible
      if ( d.createAttributeIndex() )
      {
        QgsVectorLayer* joinLayer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( info.joinLayerId ) );
        if ( joinLayer )
        {
          joinLayer->dataProvider()->createAttributeIndex( info.joinField );
        }
      }

      layer->addJoin( info );
      loadRows(); //update attribute tab
      addJoinToTreeWidget( info );
      pbnQueryBuilder->setEnabled( layer && layer->dataProvider() && layer->dataProvider()->supportsSubsetString() &&
                                   !layer->isEditable() && layer->vectorJoins().size() < 1 );
    }
  }
}
Esempio n. 22
0
void TestQgsGeometrySnapper::snapPointToLine()
{
  QgsVectorLayer* rl = new QgsVectorLayer( QStringLiteral( "Linestring" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );

  // closed linestring
  QgsGeometry refGeom = QgsGeometry::fromWkt( QStringLiteral( "LineString(0 0, 10 0, 10 10, 0 10, 0 0)" ) );
  QgsFeature ff( 0 );
  ff.setGeometry( refGeom );
  QgsFeatureList flist;
  flist << ff;
  rl->dataProvider()->addFeatures( flist );

  QgsGeometry pointGeom = QgsGeometry::fromWkt( QStringLiteral( "Point(0.1 -0.1)" ) );
  QgsGeometrySnapper snapper( rl );
  QgsGeometry result = snapper.snapGeometry( pointGeom, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Point (0 0)" ) );

  pointGeom = QgsGeometry::fromWkt( QStringLiteral( "Point(10.6 -0.1)" ) );
  result = snapper.snapGeometry( pointGeom, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Point (10 0)" ) );

  pointGeom = QgsGeometry::fromWkt( QStringLiteral( "Point(0.5 0.5)" ) );
  result = snapper.snapGeometry( pointGeom, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Point (0 0)" ) );
}
Esempio n. 23
0
void TestQgsGeometrySnapper::snapLineToPoint()
{
  QgsVectorLayer* rl = new QgsVectorLayer( QStringLiteral( "Point" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );

  QgsGeometry refGeom = QgsGeometry::fromWkt( QStringLiteral( "Point(0 0)" ) );
  QgsFeature ff( 0 );
  ff.setGeometry( refGeom );
  QgsGeometry refGeom2 = QgsGeometry::fromWkt( QStringLiteral( "Point(10 0)" ) );
  QgsFeature ff2( 2 );
  ff2.setGeometry( refGeom2 );
  QgsFeatureList flist;
  flist << ff << ff2;
  rl->dataProvider()->addFeatures( flist );

  QgsGeometry lineGeom = QgsGeometry::fromWkt( QStringLiteral( "LineString(0.1 -0.1, 10.1 0, 10 10, 0 10)" ) );
  QgsGeometrySnapper snapper( rl );
  QgsGeometry result = snapper.snapGeometry( lineGeom, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "LineString (0 0, 10 0, 10 10, 0 10)" ) );

  QgsGeometry lineGeom2 = QgsGeometry::fromWkt( QStringLiteral( "LineString(0.1 -0.1, 10.1 0, 0 10)" ) );
  result = snapper.snapGeometry( lineGeom2, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "LineString (0 0, 10 0, 0 10)" ) );

  // insert new vertex
  QgsGeometry lineGeom3 = QgsGeometry::fromWkt( QStringLiteral( "LineString(0.1 -0.1, 20.0 0.0, 20 10, 0 10)" ) );
  result = snapper.snapGeometry( lineGeom3, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "LineString (0 0, 10 0, 20 0, 20 10, 0 10)" ) );
}
void QgsAttributeTypeLoadDialog::fillComboBoxes( int layerIndex )
{
  //clear comboboxes first
  keyComboBox->clear();
  valueComboBox->clear();

  if ( layerIndex < 0 )
  {
    return;
  }

  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentText() );
  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
  if ( vLayer == NULL )
  {
    return;
  }
  QMap<QString, int> fieldMap = vLayer->dataProvider()->fieldNameMap();
  QMap<QString, int>::iterator it = fieldMap.begin();
  for ( ; it != fieldMap.end(); it++ )
  {
    keyComboBox->addItem( it.key(), it.value() );
    valueComboBox->addItem( it.key(), it.value() );
  }

}
bool QgsGraduatedSymbolRenderer::writeXML( QDomNode & layer_node, QDomDocument & document, const QgsVectorLayer& vl ) const
{
  bool returnval = true;
  QDomElement graduatedsymbol = document.createElement( "graduatedsymbol" );
  layer_node.appendChild( graduatedsymbol );

  //
  // Mode field first ...
  //

  QString modeValue = "";
  if ( mMode == QgsGraduatedSymbolRenderer::Empty )
  {
    modeValue == "Empty";
  }
  else if ( QgsGraduatedSymbolRenderer::Quantile )
  {
    modeValue = "Quantile";
  }
  else //default
  {
    modeValue = "Equal Interval";
  }
  QDomElement modeElement = document.createElement( "mode" );
  QDomText modeText = document.createTextNode( modeValue );
  modeElement.appendChild( modeText );
  graduatedsymbol.appendChild( modeElement );



  //
  // classification field now ...
  //

  QDomElement classificationfield = document.createElement( "classificationfield" );

  const QgsVectorDataProvider* theProvider = vl.dataProvider();
  if ( !theProvider )
  {
    return false;
  }

  QString classificationFieldName;
  if ( vl.pendingFields().contains( mClassificationField ) )
  {
    classificationFieldName = vl.pendingFields()[ mClassificationField ].name();
  }

  QDomText classificationfieldtxt = document.createTextNode( classificationFieldName );
  classificationfield.appendChild( classificationfieldtxt );
  graduatedsymbol.appendChild( classificationfield );
  for ( QList<QgsSymbol*>::const_iterator it = mSymbols.begin(); it != mSymbols.end(); ++it )
  {
    if ( !( *it )->writeXML( graduatedsymbol, document, &vl ) )
    {
      returnval = false;
    }
  }
  return returnval;
}
bool QgsGeometryCheckerResultTab::exportErrorsDo( const QString& file )
{
  QList< QPair<QString, QString> > attributes;
  attributes.append( qMakePair( QString( "FeatureID" ), QString( "String;10;" ) ) );
  attributes.append( qMakePair( QString( "ErrorDesc" ), QString( "String;80;" ) ) );

  QLibrary ogrLib( QgsProviderRegistry::instance()->library( "ogr" ) );
  if ( !ogrLib.load() )
  {
    return false;
  }
  typedef bool ( *createEmptyDataSourceProc )( const QString&, const QString&, const QString&, QGis::WkbType, const QList< QPair<QString, QString> >&, const QgsCoordinateReferenceSystem * );
  createEmptyDataSourceProc createEmptyDataSource = ( createEmptyDataSourceProc ) cast_to_fptr( ogrLib.resolve( "createEmptyDataSource" ) );
  if ( !createEmptyDataSource )
  {
    return false;
  }
  if ( !createEmptyDataSource( file, "ESRI Shapefile", mFeaturePool->getLayer()->dataProvider()->encoding(), QGis::WKBPoint, attributes, &mFeaturePool->getLayer()->crs() ) )
  {
    return false;
  }
  QgsVectorLayer* layer = new QgsVectorLayer( file, QFileInfo( file ).baseName(), "ogr" );
  if ( !layer->isValid() )
  {
    delete layer;
    return false;
  }

  int fieldFeatureId = layer->fieldNameIndex( "FeatureID" );
  int fieldErrDesc = layer->fieldNameIndex( "ErrorDesc" );
  for ( int row = 0, nRows = ui.tableWidgetErrors->rowCount(); row < nRows; ++row )
  {
    QgsGeometryCheckError* error = ui.tableWidgetErrors->item( row, 0 )->data( Qt::UserRole ).value<QgsGeometryCheckError*>();

    QgsFeature f( layer->pendingFields() );
    f.setAttribute( fieldFeatureId, error->featureId() );
    f.setAttribute( fieldErrDesc, error->description() );
    f.setGeometry( new QgsGeometry( error->location().clone() ) );
    layer->dataProvider()->addFeatures( QgsFeatureList() << f );
  }

  // Remove existing layer with same uri
  QStringList toRemove;
  foreach ( QgsMapLayer* maplayer, QgsMapLayerRegistry::instance()->mapLayers() )
  {
    if ( dynamic_cast<QgsVectorLayer*>( maplayer ) &&
         static_cast<QgsVectorLayer*>( maplayer )->dataProvider()->dataSourceUri() == layer->dataProvider()->dataSourceUri() )
    {
      toRemove.append( maplayer->id() );
    }
  }
  if ( !toRemove.isEmpty() )
  {
    QgsMapLayerRegistry::instance()->removeMapLayers( toRemove );
  }

  QgsMapLayerRegistry::instance()->addMapLayers( QList<QgsMapLayer*>() << layer );
  return true;
}
Esempio n. 27
0
void RgShortestPathWidget::exportPath()
{
  RgExportDlg dlg( this );
  if ( !dlg.exec() )
    return;

  QgsVectorLayer *vl = dlg.mapLayer();
  if ( vl == NULL )
    return;

  QgsPoint p1, p2;
  QgsGraph *path = getPath( p1, p2 );
  if ( path == NULL )
    return;

  QgsCoordinateTransform ct( mPlugin->iface()->mapCanvas()->mapSettings().destinationCrs(),
                             vl->crs() );

  int startVertexIdx = path->findVertex( p1 );
  int stopVertexIdx  = path->findVertex( p2 );

  double time = 0.0;
  double cost = 0.0;

  Unit timeUnit = Unit::byName( mPlugin->timeUnitName() );
  Unit distanceUnit = Unit::byName( mPlugin->distanceUnitName() );

  QgsPolyline p;
  while ( startVertexIdx != stopVertexIdx )
  {
    if ( stopVertexIdx < 0 )
      break;

    QgsGraphArcIdList l = path->vertex( stopVertexIdx ).inArc();
    if ( l.empty() )
      break;
    const QgsGraphArc& e = path->arc( l.front() );

    cost += e.property( 0 ).toDouble();
    time += e.property( 1 ).toDouble();

    p.push_front( ct.transform( path->vertex( e.inVertex() ).point() ) );
    stopVertexIdx = e.outVertex();
  }
  p.push_front( ct.transform( p1 ) );

  QgsFeature f;
  f.initAttributes( vl->pendingFields().count() );
  f.setGeometry( QgsGeometry::fromPolyline( p ) );
  f.setAttribute( 0, cost / distanceUnit.multipler() );
  f.setAttribute( 1, time / timeUnit.multipler() );
  QgsFeatureList features;
  features << f;
  vl->dataProvider()->addFeatures( features );
  vl->updateExtents();

  mPlugin->iface()->mapCanvas()->update();
  delete path;
}
Esempio n. 28
0
void TestQgsGeometrySnapper::snapPolygonToLine()
{
  QgsVectorLayer* rl = new QgsVectorLayer( QStringLiteral( "Linestring" ), QStringLiteral( "x" ), QStringLiteral( "memory" ) );

  // closed linestring
  QgsGeometry refGeom = QgsGeometry::fromWkt( QStringLiteral( "LineString(0 0, 10 0, 10 10, 0 10, 0 0)" ) );
  QgsFeature ff( 0 );
  ff.setGeometry( refGeom );
  // unclosed linestring
  QgsGeometry refGeom2 = QgsGeometry::fromWkt( QStringLiteral( "LineString(100 0, 110 0, 110 10, 100 10)" ) );
  QgsFeature ff2( 2 );
  ff2.setGeometry( refGeom2 );
  QgsFeatureList flist;
  flist << ff << ff2;
  rl->dataProvider()->addFeatures( flist );


  // snapping to closed linestring
  QgsGeometry polygonGeom = QgsGeometry::fromWkt( QStringLiteral( "Polygon((0.1 -0.1, 10.1 0, 9.9 10.1, 0 10, 0.1 -0.1))" ) );
  QgsGeometrySnapper snapper( rl );
  QgsGeometry result = snapper.snapGeometry( polygonGeom, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((0 0, 10 0, 10 10, 0 10, 0 0))" ) );

  QgsGeometry polygonGeom2 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((0.1 -0.1, 10.1 0, 0 10, 0.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom2, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((0 0, 10 0, 0 10, 0 0))" ) );

  // insert new vertex
  QgsGeometry polygonGeom3 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((0.1 -0.1, 20.5 0.5, 20 10, 0 9.9, 0.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom3, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((0 0, 10 0, 20.5 0.5, 20 10, 10 10, 0 10, 0 0))" ) );

  // remove vertex
  QgsGeometry polygonGeom4 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((0.1 -0.1, 10.1 0, 9.9 10.1, 5 10, 0 10, 0.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom4, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((0 0, 10 0, 10 10, 0 10, 0 0))" ) );


  // snapping to unclosed linestring
  QgsGeometry polygonGeom5 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((100.1 -0.1, 110.1 0, 109.9 10.1, 100 10, 100.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom5, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((100 0, 110 0, 110 10, 100 10, 100 0))" ) );

  QgsGeometry polygonGeom6 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((100.1 -0.1, 110.1 0, 100 10, 100.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom6, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((100 0, 110 0, 100 10, 100 0))" ) );

  // insert new vertex
  QgsGeometry polygonGeom7 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((100.1 -0.1, 120.5 0.5, 120 10, 100 9.9, 100.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom7, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((100 0, 110 0, 120.5 0.5, 120 10, 110 10, 100 10, 100 0))" ) );

  // remove vertex
  QgsGeometry polygonGeom8 = QgsGeometry::fromWkt( QStringLiteral( "Polygon((100.1 -0.1, 110.1 0, 109.9 10.1, 105 10, 100 10, 100.1 -0.1))" ) );
  result = snapper.snapGeometry( polygonGeom8, 1 );
  QCOMPARE( result.exportToWkt(), QStringLiteral( "Polygon ((100 0, 110 0, 110 10, 100 10, 100 0))" ) );
}
Esempio n. 29
0
QgsAddJoinDialog::QgsAddJoinDialog( QgsVectorLayer* layer, QWidget * parent, Qt::WindowFlags f ): QDialog( parent, f ), mLayer( layer )
{
  setupUi( this );

  if ( !mLayer )
  {
    return;
  }

  //insert possible vector layers into mJoinLayerComboBox

  mJoinLayerComboBox->blockSignals( true );
  const QMap<QString, QgsMapLayer*>& layerList = QgsMapLayerRegistry::instance()->mapLayers();
  QMap<QString, QgsMapLayer*>::const_iterator layerIt = layerList.constBegin();
  for ( ; layerIt != layerList.constEnd(); ++layerIt )
  {
    QgsMapLayer* currentLayer = layerIt.value();
    if ( currentLayer->type() == QgsMapLayer::VectorLayer )
    {
      QgsVectorLayer* currentVectorLayer = dynamic_cast<QgsVectorLayer*>( currentLayer );
      if ( currentVectorLayer && currentVectorLayer != mLayer )
      {
        if ( currentVectorLayer->dataProvider() && currentVectorLayer->dataProvider()->supportsSubsetString() )
          mJoinLayerComboBox->addItem( currentLayer->name(), QVariant( currentLayer->id() ) );
      }
    }
  }
  mJoinLayerComboBox->blockSignals( false );
  on_mJoinLayerComboBox_currentIndexChanged( mJoinLayerComboBox->currentIndex() );

  //insert possible target fields
  QgsVectorDataProvider* provider = mLayer->dataProvider();
  if ( provider )
  {
    const QgsFields& layerFields = provider->fields();
    for ( int idx = 0; idx < layerFields.count(); ++idx )
    {
      mTargetFieldComboBox->addItem( layerFields[idx].name(), idx );
    }
  }

  mCacheInMemoryCheckBox->setChecked( true );
}
void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
{
  previewTableWidget->clearContents();

  for ( int i = previewTableWidget->rowCount() - 1; i > 0; i-- )
  {
    previewTableWidget->removeRow( i );
  }
  if ( layerComboBox->currentIndex() < 0 || fieldIndex < 0 )
  {
    //when nothing is selected there is no reason for preview
    return;
  }
  int idx = keyComboBox->itemData( keyComboBox->currentIndex() ).toInt();
  int idx2 = valueComboBox->itemData( valueComboBox->currentIndex() ).toInt();
  QgsMapLayer* dataLayer = QgsMapLayerRegistry::instance()->mapLayer( layerComboBox->currentText() );
  QgsVectorLayer* vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
  if ( vLayer == NULL )
  {
    return;
  }

  QgsVectorDataProvider* dataProvider = vLayer->dataProvider();
  dataProvider->enableGeometrylessFeatures( true );

  QgsAttributeList attributeList = QgsAttributeList();
  attributeList.append( idx );
  attributeList.append( idx2 );
  vLayer->select( attributeList, QgsRectangle(), false );

  QgsFeature f;
  QMap<QString, QVariant> valueMap;
  while ( vLayer->nextFeature( f ) )
  {
    QVariant val1 = f.attributeMap()[idx];
    QVariant val2 = f.attributeMap()[idx2];
    if ( val1.isValid() && !val1.isNull() && !val1.toString().isEmpty()
         && val2.isValid() && !val2.isNull() && !val2.toString().isEmpty() )
    {
      valueMap.insert( val1.toString(), val2.toString() );
    }
    if ( !full && valueMap.size() > 8 )
      break; //just first entries all on button
  }
  int row = 0;
  for ( QMap<QString, QVariant>::iterator mit = valueMap.begin(); mit != valueMap.end(); mit++, row++ )
  {
    previewTableWidget->insertRow( row );
    previewTableWidget->setItem( row, 0, new QTableWidgetItem( mit.value().toString() ) );
    previewTableWidget->setItem( row, 1, new QTableWidgetItem( mit.key() ) );
  }

  dataProvider->enableGeometrylessFeatures( false );
}