# main direction in 2D map of data

It may be a very simple question. We would appreciate any comments, guide or complete solution. We prefer however incomplete solutions but novel practical ideas.
- How to find the main direction of variation i.e., main diagonal in the following example figure (north-east,south-west)?
Any algorithm and idea for coding or snippet is welcome. We found that the procedure mentioned in the accepted answer is almost PCA (Principal Component Analysis) in its simplest implementation. PCA worked very well. The resulting direction is perfectly matched with what we could expect, visually.

• Do you only have the contour map raster image available, or do you have the raw elevation data that the contours are generated from? Jan 15, 2012 at 17:53
• Both kinds of data are available. The original data is a matrix. Contours are generated later based on the matrix. That is the number of levels in contouring is felxible. Jan 16, 2012 at 8:48

You could compute the covariance matrix from your elevation data. The eigenvector that belongs to the larger eigenvalue will give you the main direction of variation:

http://en.wikipedia.org/wiki/Covariance_matrix

• +1 The idea that you mentioned is almost PCA (principal components analysis) in the simplest case. We could implement it so and the result is completely satisfactory. Jan 18, 2012 at 9:00

Here are a few ideas:

1. If the contours are not already in separate data structures, use an edge-following (a.k.a. "contour tracing") algorithm to follow each one. Once you have the contour, find the two points farthest apart. Once you have the two points farthest apart (Feret's diameter), take the midpoint of those points, project out a perpendicular line, and check what you may be something like a minor axis of a rough elliptical shape. Check the pair of 2nd farthest points and their "minor axis", the 3rd farthest, etc.
2. From the center of mass of the innermost contour, scan outwards in radial directions. Keep track of the crossing points at each contour. The radial direction (or group of radial directions) with the greatest average contour-to-contour distance will have the gentlest slope and longest distance.
3. Perform a "water drop" test. This requires a little physics. Imagine you have a drop of water or ball at your peak (somewhere inside the innermost contour). Push it in some radial direction theta. Given gravity and a nominally frictionless surface, calculate the speed of your drop/ball when it reaches the bottom horizontal plane.

For any of the above techniques, consider angles from 0 to 179 even though you may use a radial technique (0 to 359 degrees). If there is more than one peak response, find the longest radius (or whatever) that has more neighbors with large responses.