bool QgsMapLayer::readLayerXML( const QDomElement& layerElement ) { QgsCoordinateReferenceSystem savedCRS; CUSTOM_CRS_VALIDATION savedValidation; bool layerError; QDomNode mnl; QDomElement mne; // read provider QString provider; mnl = layerElement.namedItem( "provider" ); mne = mnl.toElement(); provider = mne.text(); // set data source mnl = layerElement.namedItem( "datasource" ); mne = mnl.toElement(); mDataSource = mne.text(); // TODO: this should go to providers if ( provider == "spatialite" ) { QgsDataSourceURI uri( mDataSource ); uri.setDatabase( QgsProject::instance()->readPath( uri.database() ) ); mDataSource = uri.uri(); } else if ( provider == "ogr" ) { QStringList theURIParts = mDataSource.split( "|" ); theURIParts[0] = QgsProject::instance()->readPath( theURIParts[0] ); mDataSource = theURIParts.join( "|" ); } else if ( provider == "delimitedtext" ) { QUrl urlSource = QUrl::fromEncoded( mDataSource.toAscii() ); if ( !mDataSource.startsWith( "file:" ) ) { QUrl file = QUrl::fromLocalFile( mDataSource.left( mDataSource.indexOf( "?" ) ) ); urlSource.setScheme( "file" ); urlSource.setPath( file.path() ); } QUrl urlDest = QUrl::fromLocalFile( QgsProject::instance()->readPath( urlSource.toLocalFile() ) ); urlDest.setQueryItems( urlSource.queryItems() ); mDataSource = QString::fromAscii( urlDest.toEncoded() ); } else if ( provider == "wms" ) { // >>> BACKWARD COMPATIBILITY < 1.9 // For project file backward compatibility we must support old format: // 1. mode: <url> // example: http://example.org/wms? // 2. mode: tiled=<width>;<height>;<resolution>;<resolution>...,ignoreUrl=GetMap;GetFeatureInfo,featureCount=<count>,username=<name>,password=<password>,url=<url> // example: tiled=256;256;0.703;0.351,url=http://example.org/tilecache? // example: featureCount=10,http://example.org/wms? // example: ignoreUrl=GetMap;GetFeatureInfo,username=cimrman,password=jara,url=http://example.org/wms? // This is modified version of old QgsWmsProvider::parseUri // The new format has always params crs,format,layers,styles and that params // should not appear in old format url -> use them to identify version if ( !mDataSource.contains( "crs=" ) && !mDataSource.contains( "format=" ) ) { QgsDebugMsg( "Old WMS URI format detected -> converting to new format" ); QgsDataSourceURI uri; if ( !mDataSource.startsWith( "http:" ) ) { QStringList parts = mDataSource.split( "," ); QStringListIterator iter( parts ); while ( iter.hasNext() ) { QString item = iter.next(); if ( item.startsWith( "username="******"username", item.mid( 9 ) ); } else if ( item.startsWith( "password="******"password", item.mid( 9 ) ); } else if ( item.startsWith( "tiled=" ) ) { // in < 1.9 tiled= may apper in to variants: // tiled=width;height - non tiled mode, specifies max width and max height // tiled=width;height;resolutions-1;resolution2;... - tile mode QStringList params = item.mid( 6 ).split( ";" ); if ( params.size() == 2 ) // non tiled mode { uri.setParam( "maxWidth", params.takeFirst() ); uri.setParam( "maxHeight", params.takeFirst() ); } else if ( params.size() > 2 ) // tiled mode { // resolutions are no more needed and size limit is not used for tiles // we have to tell to the provider however that it is tiled uri.setParam( "tileMatrixSet", "" ); } } else if ( item.startsWith( "featureCount=" ) ) { uri.setParam( "featureCount", item.mid( 13 ) ); } else if ( item.startsWith( "url=" ) ) { uri.setParam( "url", item.mid( 4 ) ); } else if ( item.startsWith( "ignoreUrl=" ) ) { uri.setParam( "ignoreUrl", item.mid( 10 ).split( ";" ) ); } } } else { uri.setParam( "url", mDataSource ); } mDataSource = uri.encodedUri(); // At this point, the URI is obviously incomplete, we add additional params // in QgsRasterLayer::readXml } // <<< BACKWARD COMPATIBILITY < 1.9 } else { mDataSource = QgsProject::instance()->readPath( mDataSource ); } // Set the CRS from project file, asking the user if necessary. // Make it the saved CRS to have WMS layer projected correctly. // We will still overwrite whatever GDAL etc picks up anyway // further down this function. mnl = layerElement.namedItem( "layername" ); mne = mnl.toElement(); QDomNode srsNode = layerElement.namedItem( "srs" ); mCRS->readXML( srsNode ); mCRS->setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) ); mCRS->validate(); savedCRS = *mCRS; // Do not validate any projections in children, they will be overwritten anyway. // No need to ask the user for a projections when it is overwritten, is there? savedValidation = QgsCoordinateReferenceSystem::customSrsValidation(); QgsCoordinateReferenceSystem::setCustomSrsValidation( NULL ); // now let the children grab what they need from the Dom node. layerError = !readXml( layerElement ); // overwrite CRS with what we read from project file before the raster/vector // file readnig functions changed it. They will if projections is specfied in the file. // FIXME: is this necessary? QgsCoordinateReferenceSystem::setCustomSrsValidation( savedValidation ); *mCRS = savedCRS; // Abort if any error in layer, such as not found. if ( layerError ) { return false; } // the internal name is just the data source basename //QFileInfo dataSourceFileInfo( mDataSource ); //internalName = dataSourceFileInfo.baseName(); // set ID mnl = layerElement.namedItem( "id" ); if ( ! mnl.isNull() ) { mne = mnl.toElement(); if ( ! mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz) { mID = mne.text(); } } // use scale dependent visibility flag toggleScaleBasedVisibility( layerElement.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 ); setMinimumScale( layerElement.attribute( "minimumScale" ).toFloat() ); setMaximumScale( layerElement.attribute( "maximumScale" ).toFloat() ); // set name mnl = layerElement.namedItem( "layername" ); mne = mnl.toElement(); setLayerName( mne.text() ); //title QDomElement titleElem = layerElement.firstChildElement( "title" ); if ( !titleElem.isNull() ) { mTitle = titleElem.text(); } //abstract QDomElement abstractElem = layerElement.firstChildElement( "abstract" ); if ( !abstractElem.isNull() ) { mAbstract = abstractElem.text(); } //keywordList QDomElement keywordListElem = layerElement.firstChildElement( "keywordList" ); if ( !keywordListElem.isNull() ) { QStringList kwdList; for ( QDomNode n = keywordListElem.firstChild(); !n.isNull(); n = n.nextSibling() ) { kwdList << n.toElement().text(); } mKeywordList = kwdList.join( ", " ); } //metadataUrl QDomElement dataUrlElem = layerElement.firstChildElement( "dataUrl" ); if ( !dataUrlElem.isNull() ) { mDataUrl = dataUrlElem.text(); mDataUrlFormat = dataUrlElem.attribute( "format", "" ); } //attribution QDomElement attribElem = layerElement.firstChildElement( "attribution" ); if ( !attribElem.isNull() ) { mAttribution = attribElem.text(); mAttributionUrl = attribElem.attribute( "href", "" ); } //metadataUrl QDomElement metaUrlElem = layerElement.firstChildElement( "metadataUrl" ); if ( !metaUrlElem.isNull() ) { mMetadataUrl = metaUrlElem.text(); mMetadataUrlType = metaUrlElem.attribute( "type", "" ); mMetadataUrlFormat = metaUrlElem.attribute( "format", "" ); } #if 0 //read transparency level QDomNode transparencyNode = layer_node.namedItem( "transparencyLevelInt" ); if ( ! transparencyNode.isNull() ) { // set transparency level only if it's in project // (otherwise it sets the layer transparent) QDomElement myElement = transparencyNode.toElement(); setTransparency( myElement.text().toInt() ); } #endif readCustomProperties( layerElement ); return true; } // bool QgsMapLayer::readLayerXML
QString QgsMapLayer::loadNamedStyle( const QString &theURI, bool &theResultFlag ) { QgsDebugMsg( QString( "uri = %1 myURI = %2" ).arg( theURI ).arg( publicSource() ) ); theResultFlag = false; QDomDocument myDocument( "qgis" ); // location of problem associated with errorMsg int line, column; QString myErrorMessage; QFile myFile( theURI ); if ( myFile.open( QFile::ReadOnly ) ) { // read file theResultFlag = myDocument.setContent( &myFile, &myErrorMessage, &line, &column ); if ( !theResultFlag ) myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column ); myFile.close(); } else { QFileInfo project( QgsProject::instance()->fileName() ); QgsDebugMsg( QString( "project fileName: %1" ).arg( project.absoluteFilePath() ) ); QString qml; if ( loadNamedStyleFromDb( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( "qgis.qmldb" ), theURI, qml ) || ( project.exists() && loadNamedStyleFromDb( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), theURI, qml ) ) || loadNamedStyleFromDb( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( "resources/qgis.qmldb" ), theURI, qml ) ) { theResultFlag = myDocument.setContent( qml, &myErrorMessage, &line, &column ); if ( !theResultFlag ) { myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column ); } } else { myErrorMessage = tr( "style not found in database" ); } } if ( !theResultFlag ) { return myErrorMessage; } // get style file version string, if any QgsProjectVersion fileVersion( myDocument.firstChildElement( "qgis" ).attribute( "version" ) ); QgsProjectVersion thisVersion( QGis::QGIS_VERSION ); if ( thisVersion > fileVersion ) { QgsLogger::warning( "Loading a style file that was saved with an older " "version of qgis (saved in " + fileVersion.text() + ", loaded in " + QGis::QGIS_VERSION + "). Problems may occur." ); QgsProjectFileTransform styleFile( myDocument, fileVersion ); // styleFile.dump(); styleFile.updateRevision( thisVersion ); // styleFile.dump(); } // now get the layer node out and pass it over to the layer // to deserialise... QDomElement myRoot = myDocument.firstChildElement( "qgis" ); if ( myRoot.isNull() ) { myErrorMessage = tr( "Error: qgis element could not be found in %1" ).arg( theURI ); theResultFlag = false; return myErrorMessage; } // use scale dependent visibility flag toggleScaleBasedVisibility( myRoot.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 ); setMinimumScale( myRoot.attribute( "minimumScale" ).toFloat() ); setMaximumScale( myRoot.attribute( "maximumScale" ).toFloat() ); #if 0 //read transparency level QDomNode transparencyNode = myRoot.namedItem( "transparencyLevelInt" ); if ( ! transparencyNode.isNull() ) { // set transparency level only if it's in project // (otherwise it sets the layer transparent) QDomElement myElement = transparencyNode.toElement(); setTransparency( myElement.text().toInt() ); } #endif QString errorMsg; theResultFlag = readSymbology( myRoot, errorMsg ); if ( !theResultFlag ) { myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( theURI ).arg( errorMsg ); return myErrorMessage; } return ""; }
bool QgsMapLayer::readXML( const QDomNode& layer_node ) { QgsCoordinateReferenceSystem savedCRS; CUSTOM_CRS_VALIDATION savedValidation; bool layerError; QDomElement element = layer_node.toElement(); QDomNode mnl; QDomElement mne; // read provider QString provider; mnl = layer_node.namedItem( "provider" ); mne = mnl.toElement(); provider = mne.text(); // set data source mnl = layer_node.namedItem( "datasource" ); mne = mnl.toElement(); mDataSource = mne.text(); if ( provider == "spatialite" ) { QgsDataSourceURI uri( mDataSource ); uri.setDatabase( QgsProject::instance()->readPath( uri.database() ) ); mDataSource = uri.uri(); } else if ( provider == "ogr" ) { QStringList theURIParts = mDataSource.split( "|" ); theURIParts[0] = QgsProject::instance()->readPath( theURIParts[0] ); mDataSource = theURIParts.join( "|" ); } else if ( provider == "delimitedtext" ) { QUrl urlSource = QUrl::fromEncoded( mDataSource.toAscii() ); if ( !mDataSource.startsWith( "file:" ) ) { QUrl file = QUrl::fromLocalFile( mDataSource.left( mDataSource.indexOf( "?" ) ) ); urlSource.setScheme( "file" ); urlSource.setPath( file.path() ); } QUrl urlDest = QUrl::fromLocalFile( QgsProject::instance()->readPath( urlSource.toLocalFile() ) ); urlDest.setQueryItems( urlSource.queryItems() ); mDataSource = QString::fromAscii( urlDest.toEncoded() ); } else { mDataSource = QgsProject::instance()->readPath( mDataSource ); } // Set the CRS from project file, asking the user if necessary. // Make it the saved CRS to have WMS layer projected correctly. // We will still overwrite whatever GDAL etc picks up anyway // further down this function. mnl = layer_node.namedItem( "layername" ); mne = mnl.toElement(); QDomNode srsNode = layer_node.namedItem( "srs" ); mCRS->readXML( srsNode ); mCRS->setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) ); mCRS->validate(); savedCRS = *mCRS; // Do not validate any projections in children, they will be overwritten anyway. // No need to ask the user for a projections when it is overwritten, is there? savedValidation = QgsCoordinateReferenceSystem::customSrsValidation(); QgsCoordinateReferenceSystem::setCustomSrsValidation( NULL ); // now let the children grab what they need from the Dom node. layerError = !readXml( layer_node ); // overwrite CRS with what we read from project file before the raster/vector // file readnig functions changed it. They will if projections is specfied in the file. // FIXME: is this necessary? QgsCoordinateReferenceSystem::setCustomSrsValidation( savedValidation ); *mCRS = savedCRS; // Abort if any error in layer, such as not found. if ( layerError ) { return false; } // the internal name is just the data source basename //QFileInfo dataSourceFileInfo( mDataSource ); //internalName = dataSourceFileInfo.baseName(); // set ID mnl = layer_node.namedItem( "id" ); if ( ! mnl.isNull() ) { mne = mnl.toElement(); if ( ! mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz) { mID = mne.text(); } } // use scale dependent visibility flag toggleScaleBasedVisibility( element.attribute( "hasScaleBasedVisibilityFlag" ).toInt() == 1 ); setMinimumScale( element.attribute( "minimumScale" ).toFloat() ); setMaximumScale( element.attribute( "maximumScale" ).toFloat() ); // set name mnl = layer_node.namedItem( "layername" ); mne = mnl.toElement(); setLayerName( mne.text() ); //title QDomElement titleElem = layer_node.firstChildElement( "title" ); if ( !titleElem.isNull() ) { mTitle = titleElem.text(); } //abstract QDomElement abstractElem = layer_node.firstChildElement( "abstract" ); if ( !abstractElem.isNull() ) { mAbstract = abstractElem.text(); } //read transparency level QDomNode transparencyNode = layer_node.namedItem( "transparencyLevelInt" ); if ( ! transparencyNode.isNull() ) { // set transparency level only if it's in project // (otherwise it sets the layer transparent) QDomElement myElement = transparencyNode.toElement(); setTransparency( myElement.text().toInt() ); } readCustomProperties( layer_node ); return true; } // void QgsMapLayer::readXML