We are using external Transform library with Openlayers for the polygon scale,Translate and rotate actions. After rotating the rectangle/square/polygon, when we scale it, the shape is getting stretched and it loses it's shape. Can anyone share the general formula for calculating the Longitude and Latitude which I can use inside a function using JavaScript?

  • It is as if you wanted to calculate, in longitude and latitude, the coordinates that result from rotating it in the projection. In which projection would you like to see it rotate without deforming? – Gabriel De Luca Feb 1 '19 at 5:37
  • @GabrielDeLuca My projection is EPSG:27700 – CodeBuggy Feb 1 '19 at 7:02
  • Well, there are three possible approaches. One: the spherical trigonometry. It is not difficult once it is understood. In a few days I could write the development, although the site is not friendly for the formulas. The result is rotation on the sphere. Two: transform with proj4js to equidistant azimuthal centered on the rotation point, rotate planimetrically and then transform again to geographical. The result is the exact rotation on the ellipsoid, but the formulas are run by proj4js. With both methods you will observe a stretch when projecting the result in mercator. – Gabriel De Luca Feb 1 '19 at 7:14
  • Three: Project to your system, rotate planimetrically there and then transform into geographic. Again you will not be seeing the formulas (the planimetric rotation yes, but it is simple) of projection. The result is that you will see the polygon without deformations in the projection. But it will be deforming on Earth. – Gabriel De Luca Feb 1 '19 at 7:16
  • Are you familiar with the use of proj4js? Because I do not, but I could guide you in the definition of the projection strings. I still see it quite simple to implement. – Gabriel De Luca Feb 1 '19 at 7:18

The most terrible mistake you can make is to consider the geographical coordinates as if they were flat.

To apply transformations on geographic coordinates, spherical trigonometry can be used. I do not know transformation matrices for three dimensions in spherical coordinates. Still, I could develop the formulas to establish a rotation of one point on another a certain angle. But I'm sure that's not what you need.

I recommend, on the other hand, to transform the geographical coordinates into flat ones through a projection. Perform a planimetric rotation on said projection, and then anti-project the results towards geographic coordinates again.

What is said, x is not equal to longitude; y is not equal to latitude.

Planimetric rotation

Consider x_1 and y_1 the planimetric coordinates of a point. You can calculate the rotation of an angle theta around the origin of coordinates. Consider x_~1 and y_~1 the coordinates resulting from that transformation. Therefore:

x_~1 = x_1 * cos(theta) - y_1 * sin(theta) 
y_~1 = x_1 * sin(theta) + y_1 * cos(theta) 

If you want to rotate around a point whose coordinates x_0 and y_0 do not correspond to the origin of coordinates, then you must compose three transformations: translate the center of rotation to the origin of coordinates, rotate on it, translate back to the initial position.

x_~1 = ( x_1 - x_0) * cos(theta) - (y_1 - y_0) * sin(theta) + x_0 
y_~1 = ( x_1 - x_0) * sin(theta) + (y_1 - y_0) * cos(theta) + y_0 

Finally, if you want to rotate around the centroid of the polygon, but you do not know its coordinates, you can calculate them as the algebraic sum of the coordinates of each vertex, divided by the number of vertices.

x_0 = ( x_1 + x_2 + ... + x_n ) / n 
y_0 = ( y_1 + y_2 + ... + y_n ) / n 
  • Thank you, I am learning this and trying out – CodeBuggy Feb 5 '19 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.