@@ -7,7 +7,7 @@ import {makeDevicePt, makeImagePt, makeImageWorkSpacePt, makeWorldPt} from './Po
77import {
88 contains , containsEllipse , containsRec , findIntersectionPt , getAngleInDeg , getBoundingBox , getPositionAngle
99} from './VisUtil' ;
10- import { getPixScaleDeg } from './WebPlot' ;
10+ import { getPixScaleDeg , isImage } from './WebPlot' ;
1111
1212
1313export function isPlotRotatedNorth ( plot , csys = CoordinateSys . EQ_J2000 ) {
@@ -46,7 +46,7 @@ export function isPlotNorth(plot, csys = CoordinateSys.EQ_J2000) {
4646
4747/**
4848 * get the world point at the center of the plot
49- * @param {WebPlot } plot
49+ * @param {WebPlot|undefined } plot
5050 * @return {WorldPt }
5151 */
5252export function getCenterPtOfPlot ( plot ) {
@@ -55,18 +55,38 @@ export function getCenterPtOfPlot(plot) {
5555 return CCUtil . getWorldCoords ( plot , ip ) ;
5656}
5757
58- export const getRotationAngle = ( plot ) => {
59- const iWidth = plot . dataWidth ;
60- const iHeight = plot . dataHeight ;
61- const ix = iWidth / 2 ;
62- const iy = iHeight / 2 ;
58+ /**
59+ * find the rotation angle of an image from north. The calculation will be based in the center of the image or some other
60+ * world point using the image's projection.
61+ * @param {WebPlot|undefined } plot
62+ * @param {WorldPt } [centerWp] The world point to base rotation angle on. If undefined or out of the projection conversion range then the center of the image is used
63+ * @return {number } rotation angle.
64+ */
65+ export const getRotationAngle = ( plot , centerWp ) => {
66+ if ( ! plot ) return 0 ;
6367 const cc = CysConverter . make ( plot ) ;
64- const wptC = cc . getWorldCoords ( makeImageWorkSpacePt ( ix , iy ) ) ;
65- const wpt2 = cc . getWorldCoords ( makeImageWorkSpacePt ( ix , iy + iHeight / 4 ) ) ;
66- if ( wptC && wpt2 ) return getPositionAngle ( wptC . getLon ( ) , wptC . getLat ( ) , wpt2 . getLon ( ) , wpt2 . getLat ( ) ) ;
67- return 0 ;
68+ const iHeight = plot . dataHeight ;
69+
70+ if ( centerWp ) {
71+ const ip1 = cc . getImageWorkSpaceCoords ( centerWp ) ;
72+ if ( ip1 ) {
73+ const wpt2 = cc . getWorldCoords ( makeImageWorkSpacePt ( ip1 . x , ip1 . y + iHeight / 4 ) ) ;
74+ if ( wpt2 ) return getPAngle ( centerWp , wpt2 ) ;
75+ }
76+ }
77+
78+ // use fallback if no centerWp or centerWp could not project
79+ const centerPlotImagePt = makeImageWorkSpacePt ( plot . dataWidth / 2 , iHeight / 2 ) ;
80+ const wptC = cc . getWorldCoords ( centerPlotImagePt ) ;
81+ const wpt2 = cc . getWorldCoords ( makeImageWorkSpacePt ( centerPlotImagePt . x , centerPlotImagePt . y + iHeight / 4 ) ) ;
82+ return getPAngle ( wptC , wpt2 ) ;
6883} ;
6984
85+
86+ const getPAngle = ( wp1 , wp2 ) => ( wp1 && wp2 )
87+ ? getPositionAngle ( wp1 . getLon ( ) , wp1 . getLat ( ) , wp2 . getLon ( ) , wp2 . getLat ( ) ) : 0 ;
88+
89+
7090/**
7191 * Return true if east if left of north. If east is right of north return false. This works regardless of the rotation
7292 * of the image.
@@ -111,13 +131,14 @@ export const isCsysDirMatching = (p1, p2) => isEastLeftOfNorth(p1) === isEastLef
111131
112132export function getMatchingPlotRotationAngle ( masterPlot , plot , masterRotationAngle , isMasterFlipY ) {
113133 if ( ! plot || ! masterPlot ) return 0 ;
134+ const centerMasterPlot = isImage ( plot ) ? getCenterPtOfPlot ( plot ) : undefined ; // if image use center point method
114135 const masterRot = masterRotationAngle * ( isMasterFlipY ? - 1 : 1 ) ;
115- const rot = getRotationAngle ( plot ) ;
136+ const rot = getRotationAngle ( plot , centerMasterPlot ) ;
116137 let targetRotation ;
117138 if ( isEastLeftOfNorth ( masterPlot ) ) {
118- targetRotation = ( ( getRotationAngle ( masterPlot ) + masterRot ) - rot ) * ( isMasterFlipY ? 1 : - 1 ) ;
139+ targetRotation = ( ( getRotationAngle ( masterPlot , centerMasterPlot ) + masterRot ) - rot ) * ( isMasterFlipY ? 1 : - 1 ) ;
119140 } else {
120- targetRotation = ( ( getRotationAngle ( masterPlot ) + ( 360 - masterRot ) ) - rot ) * ( isMasterFlipY ? 1 : - 1 ) ;
141+ targetRotation = ( ( getRotationAngle ( masterPlot , centerMasterPlot ) + ( 360 - masterRot ) ) - rot ) * ( isMasterFlipY ? 1 : - 1 ) ;
121142
122143 }
123144 if ( ! isCsysDirMatching ( plot , masterPlot ) ) targetRotation = 360 - targetRotation ;
0 commit comments