bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) { QgsDebugMsg( "mPath = " + mPath ); OGRRegisterAll(); OGRSFDriverH hDriver; OGRDataSourceH hDataSource = OGROpen( TO8F( mPath ), true, &hDriver ); if ( !hDataSource ) return false; QString driverName = OGR_Dr_GetName( hDriver ); OGR_DS_Destroy( hDataSource ); // we are able to assign CRS only to shapefiles :-( if ( driverName == "ESRI Shapefile" ) { QString layerName = mPath.left( mPath.indexOf( ".shp", Qt::CaseInsensitive ) ); QString wkt = crs.toWkt(); // save ordinary .prj file OGRSpatialReferenceH hSRS = OSRNewSpatialReference( wkt.toLocal8Bit().data() ); OSRMorphToESRI( hSRS ); // this is the important stuff for shapefile .prj char* pszOutWkt = NULL; OSRExportToWkt( hSRS, &pszOutWkt ); QFile prjFile( layerName + ".prj" ); if ( prjFile.open( QIODevice::WriteOnly ) ) { QTextStream prjStream( &prjFile ); prjStream << pszOutWkt << endl; prjFile.close(); } else { QgsMessageLog::logMessage( tr( "Couldn't open file %1.prj" ).arg( layerName ), tr( "OGR" ) ); return false; } OSRDestroySpatialReference( hSRS ); CPLFree( pszOutWkt ); // save qgis-specific .qpj file (maybe because of better wkt compatibility?) QFile qpjFile( layerName + ".qpj" ); if ( qpjFile.open( QIODevice::WriteOnly ) ) { QTextStream qpjStream( &qpjFile ); qpjStream << wkt.toLocal8Bit().data() << endl; qpjFile.close(); } else { QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( layerName ), tr( "OGR" ) ); return false; } return true; } // It it is impossible to assign a crs to an existing layer // No OGR_L_SetSpatialRef : http://trac.osgeo.org/gdal/ticket/4032 return false; }
Q_NOWARN_DEPRECATED_POP bool QgsOgrLayerItem::setCrs( QgsCoordinateReferenceSystem crs ) { if ( !( mCapabilities & SetCrs ) ) return false; QString layerName = mPath.left( mPath.indexOf( ".shp", Qt::CaseInsensitive ) ); QString wkt = crs.toWkt(); // save ordinary .prj file OGRSpatialReferenceH hSRS = OSRNewSpatialReference( wkt.toLocal8Bit().data() ); OSRMorphToESRI( hSRS ); // this is the important stuff for shapefile .prj char* pszOutWkt = nullptr; OSRExportToWkt( hSRS, &pszOutWkt ); QFile prjFile( layerName + ".prj" ); if ( prjFile.open( QIODevice::WriteOnly ) ) { QTextStream prjStream( &prjFile ); prjStream << pszOutWkt << endl; prjFile.close(); } else { QgsMessageLog::logMessage( tr( "Couldn't open file %1.prj" ).arg( layerName ), tr( "OGR" ) ); return false; } OSRDestroySpatialReference( hSRS ); CPLFree( pszOutWkt ); // save qgis-specific .qpj file (maybe because of better wkt compatibility?) QFile qpjFile( layerName + ".qpj" ); if ( qpjFile.open( QIODevice::WriteOnly ) ) { QTextStream qpjStream( &qpjFile ); qpjStream << wkt.toLocal8Bit().data() << endl; qpjFile.close(); } else { QgsMessageLog::logMessage( tr( "Couldn't open file %1.qpj" ).arg( layerName ), tr( "OGR" ) ); return false; } return true; }
int QgsGridFileWriter::writeFile( bool showProgressDialog ) { QFile outputFile( mOutputFilePath ); if ( !outputFile.open( QFile::WriteOnly ) ) { return 1; } if ( !mInterpolator ) { outputFile.remove(); return 2; } QTextStream outStream( &outputFile ); outStream.setRealNumberPrecision( 8 ); writeHeader( outStream ); double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell double currentXValue; double interpolatedValue; QProgressDialog* progressDialog = 0; if ( showProgressDialog ) { progressDialog = new QProgressDialog( QObject::tr( "Interpolating..." ), QObject::tr( "Abort" ), 0, mNumRows, 0 ); progressDialog->setWindowModality( Qt::WindowModal ); } for ( int i = 0; i < mNumRows; ++i ) { currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell for ( int j = 0; j < mNumColumns; ++j ) { if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 ) { outStream << interpolatedValue << ' '; } else { outStream << "-9999 "; } currentXValue += mCellSizeX; } outStream << endl; currentYValue -= mCellSizeY; if ( showProgressDialog ) { if ( progressDialog->wasCanceled() ) { outputFile.remove(); return 3; } progressDialog->setValue( i ); } } // create prj file QgsInterpolator::LayerData ld; ld = mInterpolator->layerData().first(); QgsVectorLayer* vl = ld.vectorLayer; QString crs = vl->crs().toWkt(); QFileInfo fi( mOutputFilePath ); QString fileName = fi.absolutePath() + '/' + fi.completeBaseName() + ".prj"; QFile prjFile( fileName ); if ( !prjFile.open( QFile::WriteOnly ) ) { return 1; } QTextStream prjStream( &prjFile ); prjStream << crs; prjStream << endl; prjFile.close(); delete progressDialog; return 0; }
int QgsGridFileWriter::writeFile( QgsFeedback *feedback ) { QFile outputFile( mOutputFilePath ); if ( !outputFile.open( QFile::WriteOnly | QIODevice::Truncate ) ) { return 1; } if ( !mInterpolator ) { outputFile.remove(); return 2; } QTextStream outStream( &outputFile ); outStream.setRealNumberPrecision( 8 ); writeHeader( outStream ); double currentYValue = mInterpolationExtent.yMaximum() - mCellSizeY / 2.0; //calculate value in the center of the cell double currentXValue; double interpolatedValue; for ( int i = 0; i < mNumRows; ++i ) { currentXValue = mInterpolationExtent.xMinimum() + mCellSizeX / 2.0; //calculate value in the center of the cell for ( int j = 0; j < mNumColumns; ++j ) { if ( mInterpolator->interpolatePoint( currentXValue, currentYValue, interpolatedValue ) == 0 ) { outStream << interpolatedValue << ' '; } else { outStream << "-9999 "; } currentXValue += mCellSizeX; } outStream << endl; currentYValue -= mCellSizeY; if ( feedback ) { if ( feedback->isCanceled() ) { outputFile.remove(); return 3; } feedback->setProgress( 100.0 * i / static_cast< double >( mNumRows ) ); } } // create prj file QgsInterpolator::LayerData ld; ld = mInterpolator->layerData().at( 0 ); QgsVectorLayer *vl = ld.vectorLayer; QString crs = vl->crs().toWkt(); QFileInfo fi( mOutputFilePath ); QString fileName = fi.absolutePath() + '/' + fi.completeBaseName() + ".prj"; QFile prjFile( fileName ); if ( !prjFile.open( QFile::WriteOnly | QIODevice::Truncate ) ) { return 1; } QTextStream prjStream( &prjFile ); prjStream << crs; prjStream << endl; prjFile.close(); return 0; }