|
22 | 22 |
|
23 | 23 | QString QgsMapSettingsUtils::worldFileContent( const QgsMapSettings &mapSettings ) |
24 | 24 | { |
25 | | - double xOrigin = mapSettings.visiblePolygon().at( 0 ).x() + ( mapSettings.mapUnitsPerPixel() / 2 ); |
26 | | - double yOrigin = mapSettings.visiblePolygon().at( 0 ).y() - ( mapSettings.mapUnitsPerPixel() / 2 ); |
| 25 | + QgsMapSettings ms = mapSettings; |
| 26 | + |
| 27 | + double rotation = ms.rotation(); |
| 28 | + double alpha = rotation / 180 * M_PI; |
| 29 | + |
| 30 | + // reset rotation to 0 to calculate world file parameters |
| 31 | + ms.setRotation( 0 ); |
| 32 | + |
| 33 | + double xOrigin = ms.visibleExtent().xMinimum() + ( ms.mapUnitsPerPixel() / 2 ); |
| 34 | + double yOrigin = ms.visibleExtent().yMaximum() - ( ms.mapUnitsPerPixel() / 2 ); |
| 35 | + |
| 36 | + double xCenter = ms.visibleExtent().center().x(); |
| 37 | + double yCenter = ms.visibleExtent().center().y(); |
| 38 | + |
| 39 | + // scaling matrix |
| 40 | + double s[6]; |
| 41 | + s[0] = ms.mapUnitsPerPixel(); |
| 42 | + s[1] = 0; |
| 43 | + s[2] = xOrigin; |
| 44 | + s[3] = 0; |
| 45 | + s[4] = ms.mapUnitsPerPixel(); |
| 46 | + s[5] = yOrigin; |
| 47 | + |
| 48 | + // rotation matrix |
| 49 | + double r[6]; |
| 50 | + r[0] = cos( alpha ); |
| 51 | + r[1] = -sin( alpha ); |
| 52 | + r[2] = xCenter * ( 1 - cos( alpha ) ) + yCenter * sin( alpha ); |
| 53 | + r[3] = sin( alpha ); |
| 54 | + r[4] = cos( alpha ); |
| 55 | + r[5] = - xCenter * sin( alpha ) + yCenter * ( 1 - cos( alpha ) ); |
| 56 | + |
| 57 | + // result = rotation x scaling = rotation(scaling(X)) |
| 58 | + double a = r[0] * s[0] + r[1] * s[3]; |
| 59 | + double b = r[0] * s[1] + r[1] * s[4]; |
| 60 | + double c = r[0] * s[2] + r[1] * s[5] + r[2]; |
| 61 | + double d = r[3] * s[0] + r[4] * s[3]; |
| 62 | + double e = r[3] * s[1] + r[4] * s[4]; |
| 63 | + double f = r[3] * s[2] + r[4] * s[5] + r[5]; |
27 | 64 |
|
28 | 65 | QString content; |
29 | 66 | // Pixel XDim |
30 | | - content += qgsDoubleToString( mapSettings.mapUnitsPerPixel() ) + "\r\n"; |
| 67 | + content += qgsDoubleToString( a ) + "\r\n"; |
31 | 68 | // Rotation on y axis |
32 | | - content += QString( "%1\r\n" ).arg( mapSettings.rotation() ); |
| 69 | + content += qgsDoubleToString( d ) + "\r\n"; |
33 | 70 | // Rotation on x axis |
34 | | - content += QString( "%1\r\n" ).arg( mapSettings.rotation() ); |
| 71 | + content += qgsDoubleToString( -b ) + "\r\n"; |
35 | 72 | // Pixel YDim - almost always negative |
36 | 73 | // See https://en.wikipedia.org/wiki/World_file#cite_ref-3 |
37 | | - content += '-' + qgsDoubleToString( mapSettings.mapUnitsPerPixel() ) + "\r\n"; |
| 74 | + content += "-" + qgsDoubleToString( e ) + "\r\n"; |
38 | 75 | // Origin X (center of top left cell) |
39 | | - content += qgsDoubleToString( xOrigin ) + "\r\n"; |
| 76 | + content += qgsDoubleToString( c ) + "\r\n"; |
40 | 77 | // Origin Y (center of top left cell) |
41 | | - content += qgsDoubleToString( yOrigin ) + "\r\n"; |
| 78 | + content += qgsDoubleToString( f ) + "\r\n"; |
42 | 79 |
|
43 | 80 | return content; |
44 | 81 | } |
0 commit comments