mapserver export class

git-svn-id: http://svn.osgeo.org/qgis/trunk@298 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
gsherman 2003-11-12 04:43:05 +00:00
parent 316a9e7ae7
commit 089d65d962
2 changed files with 96 additions and 73 deletions

View File

@ -4,7 +4,7 @@
Date : 8-Nov-2003
Copyright : (C) 2003 by Gary E.Sherman
email : sherman at mrcc.com
/***************************************************************************
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
@ -14,44 +14,54 @@
***************************************************************************/
/* $Id$ */
#include <iostream>
#include <fstream>
#include <qfiledialog.h>
#include <qfileinfo.h>
#include <qmessagebox.h>
#include <qcolor.h>
#include <qregexp.h>
#include "qgsmaplayer.h"
#ifdef POSTGRESQL
#include "qgsdatabaselayer.h"
#endif
#include "qgsshapefilelayer.h"
#include <iostream>
#include <fstream>
#include <qfiledialog.h>
#include <qfileinfo.h>
#include <qlineedit.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <qmessagebox.h>
#include <qcolor.h>
#include <qregexp.h>
#include <qstring.h>
#include "qgsmaplayer.h"
#ifdef POSTGRESQL
#include "qgsdatabaselayer.h"
#endif
#include "qgsshapefilelayer.h"
#include "qgsmapcanvas.h"
#include "qgsrect.h"
#include "qgsmapserverexport.h"
#include "qgis.h"
QgsMapServerExport::QgsMapServerExport(QgsMapCanvas *_map, int _action) : map(_map), action(_action)
// constructor
QgsMapserverExport::QgsMapserverExport(QgsMapCanvas *_map,
QWidget* parent, const char* name, bool modal, WFlags fl )
: QgsMapserverExportBase(parent, name, modal, fl),
map(_map)
{
}
QgsMapServerExport::~QgsMapServerExport()
// Default destructor
QgsMapserverExport::~QgsMapserverExport()
{
}
QString QgsMapServerExport::baseName(){
QFileInfo fi(fullPath);
// Get the base name for the map file
QString QgsMapserverExport::baseName(){
QFileInfo fi(txtMapFilePath->text());
return fi.baseName(true);
}
bool QgsMapServerExport::write(){
if(fullPath.isEmpty()){
selectFileName();
}
// Write the map file
bool QgsMapserverExport::write(){
//QMessageBox::information(0,"Full Path",fullPath);
int okToSave = 0;
if(QFile::exists(fullPath) && (action == SAVEAS)){
okToSave = QMessageBox::warning(0,"Overwrite File?",fullPath + " exists. \nDo you want to overwrite it?", "Yes", "No");
// Check for file and prompt for overwrite if it exists
if(QFile::exists(txtMapFilePath->text())){
okToSave = QMessageBox::warning(0,"Overwrite File?",txtMapFilePath->text() +
" exists. \nDo you want to overwrite it?", "Yes", "No");
}
if(okToSave == 0){
// write the project information to the selected file
@ -59,62 +69,62 @@ bool QgsMapServerExport::write(){
return true;
}else{
return false;
}
}
}
//Read map server file to create a QGIS view
//TODO - implement this function!
bool QgsMapServerExport::read(){
return true;
}
QString QgsMapServerExport::selectFileName(){
if(action == SAVE && fullPath.isEmpty()){
action = SAVEAS;
}
switch(action){
case OPEN:
fullPath = QFileDialog::getOpenFileName("./", "MapServer files (*.map)", 0, 0, "Choose a file to open" );
break;
case SAVEAS:
fullPath = QFileDialog::getSaveFileName("./", "MapServer files (*.map)", 0, 0, "Choose a filename to save" );
break;
}
return fullPath;
}
void QgsMapServerExport::setFileName(QString fn){
void QgsMapserverExport::setFileName(QString fn){
fullPath = fn;
}
QString QgsMapServerExport::fullPathName(){
QString QgsMapserverExport::fullPathName(){
return fullPath;
}
void QgsMapServerExport::writeMapFile(){
void QgsMapserverExport::writeMapFile(){
// write the map file, making massive assumptions about default values
std::ofstream mapFile(fullPath);
std::cout << "Opening map file " << txtMapFilePath->text() << std::endl;
std::ofstream mapFile(txtMapFilePath->text());
if(!mapFile.fail()){
// header
mapFile << "#\n# Map file generated by QGIS\n#" << std::endl;
mapFile << "# Map file generated by QGIS version " << QGis::qgisVersion << std::endl;
mapFile << "# Edit this file to customize for your interface" << std::endl;
mapFile << "# Not all sections are complete. See comments for details." << std::endl;
mapFile << "NAME QGIS_Map" << std::endl; //TODO - use the project name is set
if(!chkExpLayersOnly->isChecked()){
// header
mapFile << "NAME " << txtMapName->text() << std::endl;
mapFile << "STATUS ON" << std::endl;
mapFile << "\n# Change size as desired" << std::endl;
mapFile << "SIZE 600 600" << std::endl;
mapFile << "\n# Map image size. Change size as desired" << std::endl;
mapFile << "SIZE " << txtMapWidth->text() << " " << txtMapHeight->text() << std::endl;
// web interface definition - this is minimal!
mapFile << "#" << std::endl;
mapFile << "# Start of web interface definition. Only the TEMPLATE parameter" << std::endl;
mapFile << "# must be specified to display a map. See Mapserver documentation" << std::endl;
mapFile << "#" << std::endl;
mapFile << "WEB" << std::endl;
mapFile << " #HEADER" << std::endl;
mapFile << " TEMPLATE <html template file name>" << std::endl;
mapFile << " #FOOTER" << std::endl;
mapFile << " #MINSCALE" << std::endl;
mapFile << " #MAXSCALE" << std::endl;
// if no header is supplied, write the header line but comment it out
if(txtWebHeader->text().isEmpty()){
mapFile << " # HEADER" << std::endl;
}else{
// header provided - write it
mapFile << " HEADER " << txtWebHeader->text() << std::endl;
}
// if no template provided, write the template line but comment it out
if(txtWebTemplate->text().isEmpty()){
mapFile << " # TEMPLATE" << std::endl;
}else{
// template provided - write it
mapFile << " TEMPLATE " << txtWebTemplate->text() << std::endl;
}
// if no footer provided, write the footer line but comment it out
if(txtWebFooter->text().isEmpty()){
mapFile << " # FOOTER" << std::endl;
}else{
mapFile << " FOOTER " << txtWebFooter->text() << std::endl;
}
// write min and maxscale
mapFile << " MINSCALE " << txtMinScale->text() << std::endl;
mapFile << " MAXSCALE " << txtMaxScale->text() << std::endl;
// end of web section
mapFile << "END" << std::endl;
// extent
mapFile << "\n# Extent based on full extent of QGIS view" << std::endl;
mapFile << "EXTENT ";
@ -122,15 +132,22 @@ void QgsMapServerExport::writeMapFile(){
mapFile << extent.xMin() << " " << extent.yMin() << " ";
mapFile << extent.xMax() << " " << extent.yMax() << std::endl;
// units
mapFile << "UNITS METERS" << std::endl;
mapFile << "UNITS " << cmbMapUnits->currentText() << std::endl;
// image info
mapFile << "IMAGECOLOR 255 255 255" << std::endl;
mapFile << "IMAGETYPE PNG" << std::endl;
mapFile << "IMAGETYPE " << cmbMapImageType->currentText() << std::endl;
// projection information TODO: support projections :)
mapFile << "# Projection definition" << std::endl;
// TODO - write WEB section
mapFile << "# Projections are not currenlty supported. If desired, add your own" << std::endl;
mapFile << "# projection information based on Mapserver documentation." << std::endl;
mapFile << "#" << std::endl;
// layer definitions
}else{
mapFile << " # This file contains layer definitions only and is not a complete" << std::endl;
mapFile << " # Mapserver map file." << std::endl;
}
// write layer definitions
for(int i = 0; i < map->layerCount(); i++){
bool isPolygon =false;
bool isLine = false;
@ -180,10 +197,12 @@ void QgsMapServerExport::writeMapFile(){
break;
case QgsMapLayer::DATABASE:
QgsDatabaseLayer *dblyr = (QgsDatabaseLayer *)lyr;
mapFile << " CONNECTION \"" << lyr->source() << "\"" << std::endl;
mapFile << " CONNECTION \"" << lyr->source() << "\""
<< std::endl;
mapFile << " CONNECTIONTYPE postgis" << std::endl;
mapFile << " DATA \"" << dblyr->geometryColumnName() <<
" from " << dblyr->geometryTableName() << "\"" << std::endl;
" from " << dblyr->geometryTableName() << "\""
<< std::endl;
break;
}
// create a simple class entry based on layer color
@ -208,7 +227,9 @@ void QgsMapServerExport::writeMapFile(){
mapFile << " END" << std::endl;
mapFile << "END" << std::endl;
}
mapFile << "END # Map File";
if(!chkExpLayersOnly->isChecked()){
mapFile << "END # Map File";
}
mapFile.close();
}else{
}

View File

@ -16,17 +16,19 @@
#ifndef QGSMAPSERVEREXPORT_H
#define QGSMAPSERVEREXPORT_H
#include "qgsmapserverexportbase.h"
class QgsMapCanvas;
/*! \class QgsMapServerExport
* \brief Class to handle reading and writing a Qgis project file
*/
class QgsMapServerExport
class QgsMapserverExport:public QgsMapserverExportBase
{
Q_OBJECT
public:
QgsMapServerExport(QgsMapCanvas *map=0, int action=SAVE);
~QgsMapServerExport();
QgsMapserverExport(QgsMapCanvas *map=0, QWidget* parent = 0,
const char* name = 0, bool modal = FALSE, WFlags fl = 0 );
~QgsMapserverExport();
//! Read the file and create the map
bool read();
//! Write the contents of the map to a file