Example #1
0
bool QgsVectorLayerEditUtils::deleteVertex( QgsFeatureId atFeatureId, int atVertex )
{
  if ( !L->hasGeometryType() )
    return false;

  QgsGeometry geometry;
  if ( !cache()->geometry( atFeatureId, geometry ) )
  {
    // it's not in cache: let's fetch it from layer
    QgsFeature f;
    if ( !L->getFeatures( QgsFeatureRequest().setFilterFid( atFeatureId ).setSubsetOfAttributes( QgsAttributeList() ) ).nextFeature( f ) || !f.constGeometry() )
      return false; // geometry not found

    geometry = *f.constGeometry();
  }

  if ( !geometry.deleteVertex( atVertex ) )
    return false;

  if ( geometry.geometry() && geometry.geometry()->nCoordinates() == 0 )
  {
    //last vertex deleted, set geometry to null
    geometry.setGeometry( 0 );
  }


  L->editBuffer()->changeGeometry( atFeatureId, &geometry );
  return true;
}
Example #2
0
QgsVectorLayer::EditResult QgsVectorLayerEditUtils::deleteVertexV2( QgsFeatureId featureId, int vertex )
{
    if ( !L->hasGeometryType() )
        return QgsVectorLayer::InvalidLayer;

    QgsGeometry geometry;
    if ( !cache()->geometry( featureId, geometry ) )
    {
        // it's not in cache: let's fetch it from layer
        QgsFeature f;
        if ( !L->getFeatures( QgsFeatureRequest().setFilterFid( featureId ).setSubsetOfAttributes( QgsAttributeList() ) ).nextFeature( f ) || !f.hasGeometry() )
            return QgsVectorLayer::FetchFeatureFailed; // geometry not found

        geometry = f.geometry();
    }

    if ( !geometry.deleteVertex( vertex ) )
        return QgsVectorLayer::EditFailed;

    if ( geometry.geometry() && geometry.geometry()->nCoordinates() == 0 )
    {
        //last vertex deleted, set geometry to null
        geometry.setGeometry( nullptr );
    }

    L->editBuffer()->changeGeometry( featureId, geometry );
    return !geometry.isEmpty() ? QgsVectorLayer::Success : QgsVectorLayer::EmptyGeometry;
}
Example #3
0
    void testEmptyGeometries()
    {
      QgsVectorLayer *vlEmptyGeom = new QgsVectorLayer( "Polygon", "x", "memory" );
      QgsFeature ff( 0 );
      QgsGeometry g;
      g.setGeometry( new QgsPolygonV2() );
      ff.setGeometry( g );
      QgsFeatureList flist;
      flist << ff;
      vlEmptyGeom->dataProvider()->addFeatures( flist );

      QgsPointLocator loc( vlEmptyGeom, QgsCoordinateReferenceSystem(), nullptr );

      QgsPointLocator::Match m1 = loc.nearestVertex( QgsPointXY( 2, 2 ), std::numeric_limits<double>::max() );
      QVERIFY( !m1.isValid() );

      QgsPointLocator::Match m2 = loc.nearestEdge( QgsPointXY( 2, 2 ), std::numeric_limits<double>::max() );
      QVERIFY( !m2.isValid() );

      delete vlEmptyGeom;
    }