Skip to content

Commit

Permalink
Added raster support to browser geopackage data items
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Aug 9, 2017
1 parent 030b234 commit 9430a90
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/providers/ogr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ QT5_WRAP_CPP(OGR_MOC_SRCS ${OGR_MOC_HDRS})

INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}/src/core
${CMAKE_SOURCE_DIR}/src/core/raster
${CMAKE_SOURCE_DIR}/src/core/geometry
${CMAKE_SOURCE_DIR}/src/core/metadata
${CMAKE_SOURCE_DIR}/src/core/symbology
Expand Down
75 changes: 57 additions & 18 deletions src/providers/ogr/qgsgeopackagedataitems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
#include "qgsgeopackageconnection.h"
#include "qgslogger.h"
#include "qgssettings.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgsnewgeopackagelayerdialog.h"

#include <QAction>
#include <QMessageBox>
Expand Down Expand Up @@ -117,7 +120,9 @@ void QgsGeoPackageRootItem::newConnection()

void QgsGeoPackageRootItem::createDatabase()
{
// TODO
QgsNewGeoPackageLayerDialog dialog( nullptr );
dialog.setCrs( QgsProject::instance()->defaultCrsForNewLayers() );
dialog.exec();
}


Expand All @@ -132,10 +137,11 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
{
QVector<QgsDataItem *> children;

// Vector layers
QgsVectorLayer layer( mPath, QStringLiteral( "ogr_tmp" ), QStringLiteral( "ogr" ) );
if ( ! layer.isValid( ) )
{
children.append( new QgsErrorItem( this, tr( "Layer is not a valid GeoPackage layer" ), mPath + "/error" ) );
QgsDebugMsgLevel( tr( "Layer is not a valid GeoPackage Vector layer %1" ).arg( mPath ), 3 );
}
else
{
Expand All @@ -146,23 +152,34 @@ QVector<QgsDataItem *> QgsGeoPackageConnectionItem::createChildren()
QString name = pieces[1];
QString featuresCount = pieces[2];
QString geometryType = pieces[3];
QgsGeoPackageLayerItem::LayerType layerType;
QgsLayerItem::LayerType layerType;
layerType = layerTypeFromDb( geometryType );
if ( layerType != QgsGeoPackageLayerItem::LayerType::NoType )
if ( layerType != QgsLayerItem::LayerType::NoType )
{
// URI: '/path/gdal_sample_v1.2_no_extensions.gpkg|layerid=7|geometrytype=Point'
QString uri = QStringLiteral( "%1|layerid=%2|geometrytype=%3" ).arg( mPath, layerId, QgsGeoPackageLayerItem::layerTypeAsString( layerTypeFromDb( geometryType ) ) );
QgsGeoPackageLayerItem *item = new QgsGeoPackageLayerItem( this, name, mPath, uri, layerType );
QgsDebugMsg( QStringLiteral( "Adding GPKG item %1 %2 %3" ).arg( name, uri, geometryType ) );
// example URI: '/path/gdal_sample_v1.2_no_extensions.gpkg|layerid=7'
QString uri = QStringLiteral( "%1|layerid=%2" ).arg( mPath, layerId );
QgsGeoPackageVectorLayerItem *item = new QgsGeoPackageVectorLayerItem( this, name, mPath, uri, layerType );
QgsDebugMsg( QStringLiteral( "Adding GPKG Vector item %1 %2 %3" ).arg( name, uri, geometryType ) );
children.append( item );
}
else
{
children.append( new QgsErrorItem( this, tr( "Layer is not a supported GeoPackage layer geometry type: %1" ).arg( geometryType ), mPath + "/error" ) );
QgsDebugMsgLevel( QStringLiteral( "Layer type is not a supported GeoPackage Vector layer %1" ).arg( mPath ), 3 );
}

}
}
// Raster layers
QgsRasterLayer rlayer( mPath, QStringLiteral( "gdal_tmp" ), QStringLiteral( "gdal" ), false );
Q_FOREACH ( const QString &uri, rlayer.dataProvider()->subLayers( ) )
{
QStringList pieces = uri.split( ':' );
QString name = pieces.value( pieces.length() - 1 );
QgsDebugMsg( QStringLiteral( "Adding GPKG Raster item %1 %2 %3" ).arg( name, uri ) );
QgsGeoPackageRasterLayerItem *item = new QgsGeoPackageRasterLayerItem( this, name, mPath, uri );
children.append( item );

}
return children;

}
Expand All @@ -184,38 +201,47 @@ QList<QAction *> QgsGeoPackageConnectionItem::actions()
{
QList<QAction *> lst;

QAction *actionRemoveConnection = new QAction( tr( "Remove connection" ), this );
// TODO: implement layer deletion

//QAction *actionRemoveConnection = new QAction( tr( "Remove connection" ), this );
// connect( actionDeleteLayer, &QAction::triggered, this, &QgsGeoPackageLayerItem::deleteLayer );
lst.append( actionRemoveConnection );
//lst.append( actionRemoveConnection );
return lst;
}
#endif

QgsLayerItem::LayerType QgsGeoPackageConnectionItem::layerTypeFromDb( const QString &geometryType )
{
if ( QString::compare( geometryType, QStringLiteral( "Point" ), Qt::CaseInsensitive ) == 0 || QString::compare( geometryType, QStringLiteral( "MultiPoint" ), Qt::CaseInsensitive ) == 0 )
if ( geometryType.contains( QStringLiteral( "Point" ), Qt::CaseInsensitive ) )
{
return QgsLayerItem::LayerType::Point;
}
else if ( QString::compare( geometryType, QStringLiteral( "Polygon" ), Qt::CaseInsensitive ) == 0 || QString::compare( geometryType, QStringLiteral( "MultiPolygon" ), Qt::CaseInsensitive ) == 0 )
else if ( geometryType.contains( QStringLiteral( "Polygon" ), Qt::CaseInsensitive ) )
{
return QgsLayerItem::LayerType::Polygon;
}
else if ( QString::compare( geometryType, QStringLiteral( "LineString" ), Qt::CaseInsensitive ) == 0 || QString::compare( geometryType, QStringLiteral( "MultiLineString" ), Qt::CaseInsensitive ) == 0 )
else if ( geometryType.contains( QStringLiteral( "LineString" ), Qt::CaseInsensitive ) )
{
return QgsLayerItem::LayerType::Line;
}
else if ( geometryType.contains( QStringLiteral( "Collection" ), Qt::CaseInsensitive ) )
{
return QgsLayerItem::LayerType::Vector;
}
else if ( geometryType.contains( QStringLiteral( "Table" ), Qt::CaseInsensitive ) )
{
return QgsLayerItem::LayerType::Table;
}
// To be moved in a parent class that would also work for gdal and rasters
else if ( QString::compare( geometryType, QStringLiteral( "Raster" ), Qt::CaseInsensitive ) == 0 )
else if ( geometryType.contains( QStringLiteral( "Raster" ), Qt::CaseInsensitive ) )
{
return QgsLayerItem::LayerType::Raster;
}
return QgsLayerItem::LayerType::NoType;
}

#ifdef HAVE_GUI
QList<QAction *> QgsGeoPackageLayerItem::actions()
QList<QAction *> QgsGeoPackageAbstractLayerItem::actions()
{
QList<QAction *> lst;

Expand All @@ -227,8 +253,21 @@ QList<QAction *> QgsGeoPackageLayerItem::actions()
}
#endif

QgsGeoPackageLayerItem::QgsGeoPackageLayerItem( QgsDataItem *parent, QString name, QString path, QString uri, QgsLayerItem::LayerType layerType )
: QgsLayerItem( parent, name, path, uri, layerType, QStringLiteral( "ogr" ) )
QgsGeoPackageAbstractLayerItem::QgsGeoPackageAbstractLayerItem( QgsDataItem *parent, QString name, QString path, QString uri, QgsLayerItem::LayerType layerType, QString providerKey )
: QgsLayerItem( parent, name, path, uri, layerType, providerKey )
{
setState( Populated ); // no children are expected
}


QgsGeoPackageVectorLayerItem::QgsGeoPackageVectorLayerItem( QgsDataItem *parent, QString name, QString path, QString uri, LayerType layerType )
: QgsGeoPackageAbstractLayerItem( parent, name, path, uri, layerType, QStringLiteral( "ogr" ) )
{

}

QgsGeoPackageRasterLayerItem::QgsGeoPackageRasterLayerItem( QgsDataItem *parent, QString name, QString path, QString uri )
: QgsGeoPackageAbstractLayerItem( parent, name, path, uri, QgsLayerItem::LayerType::Raster, QStringLiteral( "gdal" ) )
{

}
29 changes: 25 additions & 4 deletions src/providers/ogr/qgsgeopackagedataitems.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
#include "qgsdataitemprovider.h"
#include "qgsdataprovider.h"

class QgsGeoPackageLayerItem : public QgsLayerItem
/**
* \brief The QgsGeoPackageAbstractLayerItem class is the base class for GeoPackage raster and vector layers
*/
class QgsGeoPackageAbstractLayerItem : public QgsLayerItem
{
Q_OBJECT
public:
QgsGeoPackageLayerItem( QgsDataItem *parent, QString name, QString path, QString uri, LayerType layerType );

protected:
QgsGeoPackageAbstractLayerItem( QgsDataItem *parent, QString name, QString path, QString uri, LayerType layerType, QString providerKey );

#ifdef HAVE_GUI
QList<QAction *> actions() override;
Expand All @@ -35,6 +39,23 @@ class QgsGeoPackageLayerItem : public QgsLayerItem
#endif
};


class QgsGeoPackageRasterLayerItem : public QgsGeoPackageAbstractLayerItem
{
Q_OBJECT
public:
QgsGeoPackageRasterLayerItem( QgsDataItem *parent, QString name, QString path, QString uri );
};


class QgsGeoPackageVectorLayerItem : public QgsGeoPackageAbstractLayerItem
{
Q_OBJECT
public:
QgsGeoPackageVectorLayerItem( QgsDataItem *parent, QString name, QString path, QString uri, LayerType layerType );
};


class QgsGeoPackageConnectionItem : public QgsDataCollectionItem
{
Q_OBJECT
Expand Down Expand Up @@ -90,7 +111,7 @@ class QgsGeoPackageRootItem : public QgsDataCollectionItem
};


//! Provider for geopackage root data item
//! Provider for geopackage data item
class QgsGeoPackageDataItemProvider : public QgsDataItemProvider
{
public:
Expand Down

0 comments on commit 9430a90

Please sign in to comment.