I'm writing a program which expects a number of lat/long points, and I convert them internally to UTM in order to do some calculations in metres.
The range of the lat/long points themselves is quite small — about 200m x 200m. They can be relied on almost always to be within a single UTM zone (unless you get unlucky and are across the border of a zone).
However, the zone that the lat/longs are in is unrestricted. One day the program might be run for people in Australia (and oh, how many zones does even a single state lie across, and how much pain has that caused me already…), and another day for people in Mexico.
My question is — is there a way to determine which zone a particular long/lat is in so that it may be fed into a conversion library (I currently use proj4 and also the R package rgdal
).
My language is R, but the answer doesn't have to be — maybe it's just a simple calculation, or maybe I can embed a system call to the proj
exectuable.
cheers.
Best Answer
Edit: For (non-R) code that works for all non-polar areas on earth, see here or here.
Unless you are dealing with data from a couple of exceptional areas (Svalbard and parts of Norway), this is a simple enough calculation that you might as well just do it yourself in R. Here is Wikipedia's description of how longitude relates to UTM Zone number:
So, assuming that in your data longitudes to the west of the Prime Meridian are encoded as running from -180 to 0 degrees, here's an R-code version of the above:
That expression could obviously be simplified a bit, but I think in this form the logic underlying its construction is most clear. The
%% 60
bit is in there just in case some of your longitudes are greater than 180 or less than -180.