Skip to content

Commit

Permalink
Consistent handling of outputformat in mapserver and better error han…
Browse files Browse the repository at this point in the history
…dling

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15044 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Jan 14, 2011
1 parent 0619c4a commit d172b17
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 43 deletions.
5 changes: 2 additions & 3 deletions src/mapserver/qgis_map_serv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,9 @@ int main( int argc, char * argv[] )
else if ( requestIt->second == "GetPrint" )
{
QByteArray* printOutput = 0;
QString formatString;
try
{
printOutput = theServer->getPrint( formatString );
printOutput = theServer->getPrint( theRequestHandler->format() );
}
catch ( QgsMapServiceException& ex )
{
Expand All @@ -397,7 +396,7 @@ int main( int argc, char * argv[] )

if ( printOutput )
{
theRequestHandler->sendGetPrintResponse( printOutput, formatString );
theRequestHandler->sendGetPrintResponse( printOutput );
}
delete printOutput;
delete theRequestHandler;
Expand Down
42 changes: 23 additions & 19 deletions src/mapserver/qgsgetrequesthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,24 @@ std::map<QString, QString> QgsGetRequestHandler::parseInput()
QgsMapServerLogger::instance()->printMessage( "formatString is: " + formatString );

//remove the image/ in front of the format
if ( formatString == "image/jpeg" || formatString == "image/jpg" || formatString == "JPG" || formatString == "jpg" )
if ( formatString.compare( "image/png", Qt::CaseInsensitive ) == 0 || formatString.compare( "png", Qt::CaseInsensitive ) == 0 )
{
formatString = "PNG";
}
else if ( formatString.compare( "image/jpeg", Qt::CaseInsensitive ) == 0 || formatString.compare( "image/jpg", Qt::CaseInsensitive ) == 0 \
|| formatString.compare( "jpg", Qt::CaseInsensitive ) == 0 )
{
formatString = "JPG";
}
else if ( formatString == "image/png" || formatString == "PNG" || formatString == "png" )
else if ( formatString.compare( "svg", Qt::CaseInsensitive ) == 0 )
{
formatString = "PNG";
formatString = "SVG";
}
else if ( formatString.compare( "pdf", Qt::CaseInsensitive ) == 0 )
{
formatString = "PDF";
}

mFormat = formatString;
}
}
Expand All @@ -119,25 +129,19 @@ void QgsGetRequestHandler::sendGetMapResponse( const QString& service, QImage* i
{
if ( img )
{
if ( mFormat != "PNG" && mFormat != "JPG" )
{
sendServiceException( QgsMapServiceException( "InvalidFormat", "Output format '" + mFormat + "' is not supported in the GetMap request" ) );
return;
}

//store the image in a QByteArray and send it directly
QByteArray ba;
QBuffer buffer( &ba );
buffer.open( QIODevice::WriteOnly );
img->save( &buffer, mFormat.toLocal8Bit().data(), -1 );
QString mimetype; //official mime-type string differs sometimes
if ( mFormat == "PNG" )
{
mimetype = "image/png";
}
else if ( mFormat == "JPG" )
{
mimetype = "image/jpeg";
}
else
{
//we don't support other formats yet...
}
sendHttpResponse( &ba, mimetype );

sendHttpResponse( &ba, formatToMimeType( mFormat ) );
}
}

Expand Down Expand Up @@ -298,7 +302,7 @@ void QgsGetRequestHandler::sendServiceException( const QgsMapServiceException& e
sendHttpResponse( &ba, "text/xml" );
}

void QgsGetRequestHandler::sendGetPrintResponse( QByteArray* ba, const QString& formatString ) const
void QgsGetRequestHandler::sendGetPrintResponse( QByteArray* ba ) const
{
sendHttpResponse( ba, formatString );
sendHttpResponse( ba, formatToMimeType( mFormat ) );
}
2 changes: 1 addition & 1 deletion src/mapserver/qgsgetrequesthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ class QgsGetRequestHandler: public QgsHttpRequestHandler
void sendGetFeatureInfoResponse( const QDomDocument& infoDoc, const QString& infoFormat ) const;
void sendServiceException( const QgsMapServiceException& ex ) const;
void sendGetStyleResponse( const QDomDocument& doc ) const;
void sendGetPrintResponse( QByteArray* ba, const QString& formatString ) const;
void sendGetPrintResponse( QByteArray* ba ) const;
};
21 changes: 21 additions & 0 deletions src/mapserver/qgshttprequesthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,24 @@ void QgsHttpRequestHandler::sendHttpResponse( QByteArray* ba, const QString& for
printf( "\n" );
fwrite( ba->data(), ba->size(), 1, FCGI_stdout );
}

QString QgsHttpRequestHandler::formatToMimeType( const QString& format ) const
{
if ( format.compare( "png", Qt::CaseInsensitive ) )
{
return "image/png";
}
else if ( format.compare( "jpg", Qt::CaseInsensitive ) )
{
return "image/jpeg";
}
else if ( format.compare( "svg", Qt::CaseInsensitive ) )
{
return "image/svg+xml";
}
else if ( format.compare( "pdf", Qt::CaseInsensitive ) )
{
return "application/pdf";
}
return format;
}
3 changes: 3 additions & 0 deletions src/mapserver/qgshttprequesthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class QgsHttpRequestHandler: public QgsRequestHandler

protected:
void sendHttpResponse( QByteArray* ba, const QString& format ) const;
/**Converts format to official mimetype (e.g. 'jpg' to 'image/jpeg')
@return mime string (or the entered string if not found)*/
QString formatToMimeType( const QString& format ) const;
};

#endif
3 changes: 2 additions & 1 deletion src/mapserver/qgsrequesthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class QgsRequestHandler
virtual void sendGetFeatureInfoResponse( const QDomDocument& infoDoc, const QString& infoFormat ) const = 0;
virtual void sendServiceException( const QgsMapServiceException& ex ) const = 0;
virtual void sendGetStyleResponse( const QDomDocument& doc ) const = 0;
virtual void sendGetPrintResponse( QByteArray* ba, const QString& formatString ) const = 0;
virtual void sendGetPrintResponse( QByteArray* ba ) const = 0;
QString format() const { return mFormat; }
protected:
/**This is set by the parseInput methods of the subclasses (parameter FORMAT, e.g. 'FORMAT=PNG')*/
QString mFormat;
Expand Down
2 changes: 1 addition & 1 deletion src/mapserver/qgssoaprequesthandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ void QgsSOAPRequestHandler::sendGetStyleResponse( const QDomDocument& infoDoc )
sendHttpResponse( &ba, "text/xml" );
}

void QgsSOAPRequestHandler::sendGetPrintResponse( QByteArray* ba, const QString& formatString ) const
void QgsSOAPRequestHandler::sendGetPrintResponse( QByteArray* ba ) const
{
//soon...
}
Expand Down
2 changes: 1 addition & 1 deletion src/mapserver/qgssoaprequesthandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class QgsSOAPRequestHandler: public QgsHttpRequestHandler
void sendGetFeatureInfoResponse( const QDomDocument& infoDoc, const QString& infoFormat ) const;
void sendServiceException( const QgsMapServiceException& ex ) const;
void sendGetStyleResponse( const QDomDocument& doc ) const;
void sendGetPrintResponse( QByteArray* ba, const QString& formatString ) const;
void sendGetPrintResponse( QByteArray* ba ) const;
private:
/**Parses the xml of a getMap request and fills the parameters into the map. Returns 0 in case of success*/
int parseGetMapElement( std::map<QString, QString>& parameterMap, const QDomElement& getMapElement ) const;
Expand Down
19 changes: 5 additions & 14 deletions src/mapserver/qgswmsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,11 @@ QDomDocument QgsWMSServer::getStyle()
return mConfigParser->getStyle( styleName, layerName );
}

QByteArray* QgsWMSServer::getPrint( QString& formatString )
QByteArray* QgsWMSServer::getPrint( const QString& formatString )
{
QStringList layersList, stylesList, layerIdList;
QImage* theImage = initializeRendering( layersList, stylesList, layerIdList, formatString );
QString dummyFormat;
QImage* theImage = initializeRendering( layersList, stylesList, layerIdList );
if ( !theImage )
{
return 0;
Expand Down Expand Up @@ -455,8 +456,7 @@ QByteArray* QgsWMSServer::getPrint( QString& formatString )
QImage* QgsWMSServer::getMap()
{
QStringList layersList, stylesList, layerIdList;
QString outputFormat;
QImage* theImage = initializeRendering( layersList, stylesList, layerIdList, outputFormat );
QImage* theImage = initializeRendering( layersList, stylesList, layerIdList );

QPainter thePainter( theImage );
thePainter.setRenderHint( QPainter::Antialiasing ); //make it look nicer
Expand Down Expand Up @@ -652,7 +652,7 @@ int QgsWMSServer::getFeatureInfo( QDomDocument& result )
return 0;
}

QImage* QgsWMSServer::initializeRendering( QStringList& layersList, QStringList& stylesList, QStringList& layerIdList, QString& outputFormat )
QImage* QgsWMSServer::initializeRendering( QStringList& layersList, QStringList& stylesList, QStringList& layerIdList )
{
if ( !mConfigParser )
{
Expand Down Expand Up @@ -695,15 +695,6 @@ QImage* QgsWMSServer::initializeRendering( QStringList& layersList, QStringList&
}
}

//get output format
std::map<QString, QString>::const_iterator outIt = mParameterMap.find( "FORMAT" );
if ( outIt == mParameterMap.end() )
{
QgsMSDebugMsg( "Error, no parameter FORMAT found" )
return 0; //output format parameter also mandatory
}
outputFormat = outIt->second;

QImage* theImage = createImage();
if ( !theImage )
{
Expand Down
5 changes: 2 additions & 3 deletions src/mapserver/qgswmsserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class QgsWMSServer
/**Returns printed page as binary
@param formatString out: format of the print output (e.g. pdf, svg, png, ...)
@return printed page as binary or 0 in case of error*/
QByteArray* getPrint( QString& formatString );
QByteArray* getPrint( const QString& formatString );

/**Creates an xml document that describes the result of the getFeatureInfo request.
@return 0 in case of success*/
Expand All @@ -80,9 +80,8 @@ class QgsWMSServer
@param layersList out: list with WMS layer names
@param stylesList out: list with WMS style names
@param layerIdList out: list with QGIS layer ids
@param outputFormat out: name of requested output format
@return image configured together with mMapRenderer (or 0 in case of error). The calling function takes ownership of the image*/
QImage* initializeRendering( QStringList& layersList, QStringList& stylesList, QStringList& layerIdList, QString& outputFormat );
QImage* initializeRendering( QStringList& layersList, QStringList& stylesList, QStringList& layerIdList );

/**Creates a QImage from the HEIGHT and WIDTH parameters
@param width image width (or -1 if width should be taken from WIDTH wms parameter)
Expand Down

0 comments on commit d172b17

Please sign in to comment.