Skip to content

Commit

Permalink
Apply patch #2948 from Stefan Ziegler (PDF map for georeferencer)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@14192 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Sep 5, 2010
1 parent 505b766 commit cf8ae13
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 37 deletions.
35 changes: 35 additions & 0 deletions src/plugins/georeferencer/qgsgeorefconfigdialog.cpp
Expand Up @@ -15,6 +15,7 @@
/* $Id$ */
#include <QCloseEvent>
#include <QSettings>
#include <QSizeF>

#include "qgsgeorefconfigdialog.h"

Expand All @@ -24,6 +25,35 @@ QgsGeorefConfigDialog::QgsGeorefConfigDialog( QWidget *parent ) :
setupUi( this );

readSettings();

mPaperSizeComboBox->addItem( tr( "A5 (148x210 mm)" ), QSizeF( 148, 210 ) );
mPaperSizeComboBox->addItem( tr( "A4 (210x297 mm)" ), QSizeF( 210, 297 ) );
mPaperSizeComboBox->addItem( tr( "A3 (297x420 mm)" ), QSizeF( 297, 420 ) );
mPaperSizeComboBox->addItem( tr( "A2 (420x594 mm)" ), QSizeF( 420, 594 ) );
mPaperSizeComboBox->addItem( tr( "A1 (594x841 mm)" ), QSizeF( 594, 841 ) );
mPaperSizeComboBox->addItem( tr( "A0 (841x1189 mm)" ), QSizeF( 841, 1189 ) );
mPaperSizeComboBox->addItem( tr( "B5 (176 x 250 mm)" ), QSizeF( 176, 250 ) );
mPaperSizeComboBox->addItem( tr( "B4 (250 x 353 mm)" ), QSizeF( 250, 353 ) );
mPaperSizeComboBox->addItem( tr( "B3 (353 x 500 mm)" ), QSizeF( 353, 500 ) );
mPaperSizeComboBox->addItem( tr( "B2 (500 x 707 mm)" ), QSizeF( 500, 707 ) );
mPaperSizeComboBox->addItem( tr( "B1 (707 x 1000 mm)" ), QSizeF( 707, 1000 ) );
mPaperSizeComboBox->addItem( tr( "B0 (1000 x 1414 mm)" ), QSizeF( 1000, 1414 ) );
// North american formats
mPaperSizeComboBox->addItem( tr( "Legal (8.5x14 inches)" ), QSizeF( 215.9, 355.6 ) );
mPaperSizeComboBox->addItem( tr( "ANSI A (Letter; 8.5x11 inches)" ), QSizeF( 215.9, 279.4 ) );
mPaperSizeComboBox->addItem( tr( "ANSI B (Tabloid; 11x17 inches)" ), QSizeF( 279.4, 431.8 ) );
mPaperSizeComboBox->addItem( tr( "ANSI C (17x22 inches)" ), QSizeF( 431.8, 558.8 ) );
mPaperSizeComboBox->addItem( tr( "ANSI D (22x34 inches)" ), QSizeF( 558.8, 863.6 ) );
mPaperSizeComboBox->addItem( tr( "ANSI E (34x44 inches)" ), QSizeF( 863.6, 1117.6 ) );
mPaperSizeComboBox->addItem( tr( "Arch A (9x12 inches)" ), QSizeF( 228.6, 304.8 ) );
mPaperSizeComboBox->addItem( tr( "Arch B (12x18 inches)" ), QSizeF( 304.8, 457.2 ) );
mPaperSizeComboBox->addItem( tr( "Arch C (18x24 inches)" ), QSizeF( 457.2, 609.6 ) );
mPaperSizeComboBox->addItem( tr( "Arch D (24x36 inches)" ), QSizeF( 609.6, 914.4 ) );
mPaperSizeComboBox->addItem( tr( "Arch E (36x48 inches)" ), QSizeF( 914.4, 1219.2 ) );
mPaperSizeComboBox->addItem( tr( "Arch E1 (30x42 inches)" ) , QSizeF( 762, 1066.8 ) );

mPaperSizeComboBox->setCurrentIndex( 2 ); //A3

}

void QgsGeorefConfigDialog::changeEvent( QEvent *e )
Expand Down Expand Up @@ -109,4 +139,9 @@ void QgsGeorefConfigDialog::writeSettings()
}
s.setValue( "/Plugin-GeoReferencer/Config/LeftMarginPDF", mLeftMarginSpinBox->value() );
s.setValue( "/Plugin-GeoReferencer/Config/RightMarginPDF", mRightMarginSpinBox->value() );

s.setValue( "/Plugin-GeoReferencer/Config/WidthPDFMap", mPaperSizeComboBox->itemData( mPaperSizeComboBox->currentIndex() ).toSizeF().width() );
s.setValue( "/Plugin-GeoReferencer/Config/HeightPDFMap", mPaperSizeComboBox->itemData( mPaperSizeComboBox->currentIndex() ).toSizeF().height() );

}

31 changes: 27 additions & 4 deletions src/plugins/georeferencer/qgsgeorefconfigdialogbase.ui
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>249</width>
<height>344</height>
<width>309</width>
<height>468</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -108,14 +108,14 @@
</layout>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QCheckBox" name="mShowDockedCheckBox">
<property name="text">
<string>Show Georeferencer window docked</string>
</property>
</widget>
</item>
<item row="4" column="0">
<item row="5" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -125,6 +125,29 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>PDF map</string>
</property>
<layout class="QGridLayout" name="gridLayout_5">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Paper size</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="mPaperSizeComboBox"/>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
Expand Down
128 changes: 123 additions & 5 deletions src/plugins/georeferencer/qgsgeorefplugingui.cpp
Expand Up @@ -28,6 +28,9 @@
#include <QPushButton>
#include <QSettings>
#include <QTextStream>
#include <QPen>
#include <QStringList>
#include <QList>

#include "qgisinterface.h"
#include "qgslegendinterface.h"
Expand Down Expand Up @@ -62,7 +65,6 @@

#include "qgsgeorefplugingui.h"


QgsGeorefDockWidget::QgsGeorefDockWidget( const QString & title, QWidget * parent, Qt::WindowFlags flags )
: QDockWidget( title, parent, flags )
{
Expand Down Expand Up @@ -310,7 +312,7 @@ bool QgsGeorefPluginGui::getTransformSettings()
}

d.getTransformSettings( mTransformParam, mResamplingMethod, mCompressionMethod,
mModifiedRasterFileName, mProjection, mPdfOutputFile, mUseZeroForTrans, mLoadInQgis, mUserResX, mUserResY );
mModifiedRasterFileName, mProjection, mPdfOutputMapFile, mPdfOutputFile, mUseZeroForTrans, mLoadInQgis, mUserResX, mUserResY );
mTransformParamLabel->setText( tr( "Transform: " ) + convertTransformEnumToString( mTransformParam ) );
mGeorefTransform.selectTransformParametrisation( mTransformParam );
mGCPListWidget->setGeorefTransform( &mGeorefTransform );
Expand Down Expand Up @@ -1219,6 +1221,12 @@ bool QgsGeorefPluginGui::georeference()
{
writePDFReportFile( mPdfOutputFile, mGeorefTransform );
}
if ( success && !mPdfOutputMapFile.isEmpty() )
{
writePDFMapFile( mPdfOutputMapFile, mGeorefTransform );
}


}
else // Helmert, Polinom 1, Polinom 2, Polinom 3
{
Expand All @@ -1243,6 +1251,10 @@ bool QgsGeorefPluginGui::georeference()
{
writePDFReportFile( mPdfOutputFile, mGeorefTransform );
}
if ( !mPdfOutputMapFile.isEmpty() )
{
writePDFMapFile( mPdfOutputMapFile, mGeorefTransform );
}
return true;
}
}
Expand Down Expand Up @@ -1328,6 +1340,112 @@ bool QgsGeorefPluginGui::calculateMeanError( double& error ) const
return true;
}

bool QgsGeorefPluginGui::writePDFMapFile( const QString& fileName, const QgsGeorefTransform& transform )
{
if ( !mCanvas )
{
return false;
}

QgsMapRenderer* canvasRenderer = mCanvas->mapRenderer();
if ( !canvasRenderer )
{
return false;
}

QgsRasterLayer *rlayer = ( QgsRasterLayer* ) mCanvas->layer( 0 );
if ( !rlayer )
{
return false;
}
double mapRatio = rlayer->extent().width() / rlayer->extent().height();

QPrinter printer;
printer.setOutputFormat( QPrinter::PdfFormat );
printer.setOutputFileName( fileName );

QSettings s;
double paperWidth = s.value( "/Plugin-GeoReferencer/Config/WidthPDFMap" ).toDouble();
double paperHeight = s.value( "/Plugin-GeoReferencer/Config/HeightPDFMap" ).toDouble();

//create composition
QgsComposition* composition = new QgsComposition( mCanvas->mapRenderer() );
if ( mapRatio >= 1 )
{
composition->setPaperSize( paperHeight, paperWidth );
}
else
{
composition->setPaperSize( paperWidth, paperHeight );
}
composition->setPrintResolution( 300 );
printer.setPaperSize( QSizeF( composition->paperWidth(), composition->paperHeight() ), QPrinter::Millimeter );

double leftMargin = 8;
double topMargin = 8;
double contentWidth = composition->paperWidth() - 2 * leftMargin;
double contentHeight = composition->paperHeight() - 2 * topMargin;
double contentRatio = contentWidth / contentHeight;

//composer map
QgsComposerMap* composerMap = new QgsComposerMap( composition, leftMargin, topMargin, contentWidth, contentHeight );
composerMap->setKeepLayerSet( true );
QStringList list;
list.append( canvasRenderer->layerSet()[0] );
composerMap->setLayerSet( list );

double xcenter = rlayer->extent().center().x();
double ycenter = rlayer->extent().center().y();

QgsRectangle rect;
if ( mapRatio > contentRatio )
{
rect = QgsRectangle( 0, ycenter - ( rlayer->extent().width() / contentRatio ) / 2, rlayer->extent().width(), ycenter + ( rlayer->extent().width() / contentRatio ) / 2 );
}
if ( mapRatio <= contentRatio )
{
rect = QgsRectangle( xcenter - ( rlayer->extent().height() * contentRatio / 2 ), -1 * rlayer->extent().height(), xcenter + ( rlayer->extent().height() * contentRatio / 2 ), 0 );
}

composerMap->setNewExtent( rect );
composition->addItem( composerMap );
printer.setFullPage( true );
printer.setColorMode( QPrinter::Color );

QString residualUnits;
if ( s.value( "/Plugin-GeoReferencer/Config/ResidualUnits" ) == "mapUnits" && mGeorefTransform.providesAccurateInverseTransformation() )
{
residualUnits = tr( "map units" );
}
else
{
residualUnits = tr( "pixels" );
}

//residual plot
QgsResidualPlotItem* resPlotItem = new QgsResidualPlotItem( composition );
composition->addItem( resPlotItem );
resPlotItem->setSceneRect( QRectF( leftMargin, topMargin, contentWidth, contentHeight ) );
resPlotItem->setExtent( rect );
resPlotItem->setGCPList( mPoints );
resPlotItem->setConvertScaleToMapUnits( residualUnits == tr( "map units" ) );

printer.setResolution( composition->printResolution() );
QPainter p( &printer );
composition->setPlotStyle( QgsComposition::Print );
QRectF paperRectMM = printer.pageRect( QPrinter::Millimeter );
QRectF paperRectPixel = printer.pageRect( QPrinter::DevicePixel );
composition->render( &p, paperRectPixel, paperRectMM );

delete resPlotItem;
delete composerMap;
delete composition;

return true;
}



bool QgsGeorefPluginGui::writePDFReportFile( const QString& fileName, const QgsGeorefTransform& transform )
{
if ( !mCanvas )
Expand Down Expand Up @@ -1475,7 +1593,7 @@ bool QgsGeorefPluginGui::writePDFReportFile( const QString& fileName, const QgsG
gcpTable->setHeaderFont( tableHeaderFont );
gcpTable->setContentFont( tableContentFont );
QStringList gcpHeader;
gcpHeader << "id" << "enabled" << "pixelX" << "pixelY" << "mapX" << "mapY" << "resX[" + residualUnits + "]" << "resY[" + residualUnits + "]" << "resTot[" + residualUnits + "]";
gcpHeader << "id" << "enabled" << "pixelX" << "pixelY" << "mapX" << "mapY" << "resX [" + residualUnits + "]" << "resY [" + residualUnits + "]" << "resTot [" + residualUnits + "]";
gcpTable->setHeaderLabels( gcpHeader );

QgsGCPList::const_iterator gcpIt = mPoints.constBegin();
Expand All @@ -1494,8 +1612,8 @@ bool QgsGeorefPluginGui::writePDFReportFile( const QString& fileName, const QgsG
{
currentGCPStrings << tr( "no" );
}
currentGCPStrings << QString::number(( *gcpIt )->pixelCoords().x(), 'f', 2 ) << QString::number(( *gcpIt )->pixelCoords().y(), 'f', 2 ) << QString::number(( *gcpIt )->mapCoords().x(), 'f', 2 )\
<< QString::number(( *gcpIt )->mapCoords().y(), 'f', 2 ) << QString::number( residual.x() ) << QString::number( residual.y() ) << QString::number( residualTot );
currentGCPStrings << QString::number(( *gcpIt )->pixelCoords().x(), 'f', 0 ) << QString::number(( *gcpIt )->pixelCoords().y(), 'f', 0 ) << QString::number(( *gcpIt )->mapCoords().x(), 'f', 3 )\
<< QString::number(( *gcpIt )->mapCoords().y(), 'f', 3 ) << QString::number( residual.x() ) << QString::number( residual.y() ) << QString::number( residualTot );
gcpTable->addRow( currentGCPStrings );
}

Expand Down
6 changes: 4 additions & 2 deletions src/plugins/georeferencer/qgsgeorefplugingui.h
Expand Up @@ -103,7 +103,7 @@ class QgsGeorefPluginGui : public QMainWindow, private Ui::QgsGeorefPluginGuiBas
void updateMouseCoordinatePrecision();

// when one Layer is removed
void layerWillBeRemoved ( QString theLayerId );
void layerWillBeRemoved( QString theLayerId );
void extentsChanged(); // Use for need add again Raster (case above)

bool updateGeorefTransform();
Expand All @@ -127,7 +127,7 @@ class QgsGeorefPluginGui : public QMainWindow, private Ui::QgsGeorefPluginGuiBas
void setupConnections();

// Mapcanvas Plugin
void addRaster(QString file);
void addRaster( QString file );

// settings
void readSettings();
Expand All @@ -142,6 +142,7 @@ class QgsGeorefPluginGui : public QMainWindow, private Ui::QgsGeorefPluginGuiBas
bool georeference();
bool writeWorldFile( QgsPoint origin, double pixelXSize, double pixelYSize, double rotation );
bool writePDFReportFile( const QString& fileName, const QgsGeorefTransform& transform );
bool writePDFMapFile( const QString& fileName, const QgsGeorefTransform& transform );
void updateTransformParamLabel();

// gdal script
Expand Down Expand Up @@ -201,6 +202,7 @@ class QgsGeorefPluginGui : public QMainWindow, private Ui::QgsGeorefPluginGuiBas
QString mGCPpointsFileName;
QString mProjection;
QString mPdfOutputFile;
QString mPdfOutputMapFile;
double mUserResX, mUserResY; // User specified target scale

QgsGeorefTransform::TransformParametrisation mTransformParam;
Expand Down
12 changes: 5 additions & 7 deletions src/plugins/georeferencer/qgsresidualplotitem.cpp
Expand Up @@ -44,12 +44,10 @@ void QgsResidualPlotItem::paint( QPainter* painter, const QStyleOptionGraphicsIt
double widthMM = rect().width();
double heightMM = rect().height();

QPen enabledPen( QColor( 0, 0, 255, 255 ) );
enabledPen.setWidthF( 0.4 );
QPen disabledPen( QColor( 0, 0, 255, 127 ) );
disabledPen.setWidthF( 0.3 );
QBrush enabledBrush( QColor( 255, 0, 0, 255 ) );
QBrush disabledBrush( QColor( 255, 0, 0, 127 ) );
QPen enabledPen( QColor( 255, 0, 0, 255 ), 0.3, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin );
QPen disabledPen( QColor( 255, 0, 0, 85 ), 0.2, Qt::SolidLine, Qt::FlatCap, Qt::MiterJoin );
QBrush enabledBrush( QColor( 255, 255, 255, 255 ) );
QBrush disabledBrush( QColor( 255, 255, 255, 127 ) );

//draw all points and collect minimal mm/pixel ratio
double minMMPixelRatio = DBL_MAX;
Expand All @@ -74,7 +72,7 @@ void QgsResidualPlotItem::paint( QPainter* painter, const QStyleOptionGraphicsIt
painter->setPen( disabledPen );
painter->setBrush( disabledBrush );
}
painter->drawRect( QRectF( gcpItemMMX - 1, gcpItemMMY - 1, 2, 2 ) );
painter->drawRect( QRectF( gcpItemMMX - 0.5, gcpItemMMY - 0.5, 1, 1 ) );
drawText( painter, gcpItemMMX + 2, gcpItemMMY + 2, QString::number(( *gcpIt )->id() ), QFont() );

mmPixelRatio = maxMMToPixelRatioForGCP( *gcpIt, gcpItemMMX, gcpItemMMY );
Expand Down

0 comments on commit cf8ae13

Please sign in to comment.