Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Addded some RDoc comments

  • Loading branch information...
commit 7e59c60895f9d27646020ac20bc054b1bf995eeb 1 parent 29a7389
Tallak Tveide authored
1  .gitignore
View
@@ -1,3 +1,4 @@
*.swp
pkg
geoutm*.gem
+.yardoc
74 README.rdoc
View
@@ -6,51 +6,22 @@ GeoUtm
require 'geoutm'
- coordinate = LatLon.new 100.0 100.0
- utm_coord = coordinate.to_utm
- reverse = utm_coord.to_lat_lon
+ coordinate = GeoUtm::LatLon.new 100.0, 100.0
+ to_utm = coordinate.to_utm
+ and_back = utm_coord.to_lat_lon
=INSTALL
- sudo gem sources -a http://gems.github.com
- sudo gem install tallakt-geoutm
+ gem install geoutm
=DESCRIPTION
-This module will translate latitude longitude coordinates to Universal Transverse Mercator(UTM) coordinates and vice versa.
+This gem translates between latitude and longitude coordinates to Universal Transverse Mercator(UTM) coordinates.
+The first kind is generally reported by GPS units, while the second is useful as they are represented in meters,
+thus making distance calculations easier.
-This is a shameless port of the Perl library Geo::Coordinates::UTM written by Graham Crookham. Most text is copied directly from this library, adapted to ruby syntax.
-
-
-== Mercator Projection
-
-The Mercator projection was first invented to help mariners. They needed to be able to take a course and know the distance traveled, and draw a line on the map which showed the day's journey. In order to do this, Mercator invented a projection which preserved length, by projecting the earth's surface onto a cylinder, sharing the same axis as the earth itself.
-This caused all Latitude and Longitude lines to intersect at a 90 degree angle, thereby negating the problem that longitude lines get closer together at the poles.
-
-== Transverse Mercator Projection
-
-A Transverse Mercator projection takes the cylinder and turns it on its side. Now the cylinder's axis passes through the equator, and it can be rotated to line up with the area of interest. Many countries use Transverse Mercator for their grid systems.
-
-== Universal Transverse Mercator
-
-The Universal Transverse Mercator(UTM) system sets up a universal world wide system for mapping. The Transverse Mercator projection is used, with the cylinder in 60 positions. This creates 60 zones around the world.
-Positions are measured using Eastings and Northings, measured in meters, instead of Latitude and Longitude. Eastings start at 500,000 on the centre line of each zone.
-In the Northern Hemisphere, Northings are zero at the equator and increase northward. In the Southern Hemisphere, Northings start at 10 million at the equator, and decrease southward. You must know which hemisphere and zone you are in to interpret your location globally.
-Distortion of scale, distance, direction and area increase away from the central meridian.
-
-UTM projection is used to define horizontal positions world-wide by dividing the surface of the Earth into 6 degree zones, each mapped by the Transverse Mercator projection with a central meridian in the center of the zone.
-UTM zone numbers designate 6 degree longitudinal strips extending from 80 degrees South latitude to 84 degrees North latitude. UTM zone characters designate 8 degree zones extending north and south from the equator. Eastings are measured from the central meridian (with a 500 km false easting to insure positive coordinates). Northings are measured from the equator (with a 10,000 km false northing for positions south of the equator).
-
-UTM is applied separately to the Northern and Southern Hemisphere, thus within a single UTM zone, a single X / Y pair of values will occur in both the Northern and Southern Hemisphere.
-To eliminate this confusion, and to speed location of points, a UTM zone is sometimes subdivided into 20 zones of Latitude. These grids can be further subdivided into 100,000 meter grid squares with double-letter designations. This subdivision by Latitude and further division into grid squares is generally referred to as the Military Grid Reference System (MGRS).
-The unit of measurement of UTM is always meters and the zones are numbered from 1 to 60 eastward, beginning at the 180th meridian.
-The scale distortion in a north-south direction parallel to the central meridian (CM) is constant However, the scale distortion increases either direction away from the CM. To equalize the distortion of the map across the UTM zone, a scale factor of 0.9996 is applied to all distance measurements within the zone. The distortion at the zone boundary, 3 degrees away from the CM is approximately 1%.
-
-== Datums and Ellipsoids
-
-Unlike local surveys, which treat the Earth as a plane, the precise determination of the latitude and longitude of points over a broad area must take into account the actual shape of the Earth. To achieve the precision necessary for accurate location, the Earth cannot be assumed to be a sphere. Rather, the Earth's shape more closely approximates an ellipsoid (oblate spheroid): flattened at the poles and bulging at the Equator. Thus the Earth's shape, when cut through its polar axis, approximates an ellipse.
-A "Datum" is a standard representation of shape and offset for coordinates, which includes an ellipsoid and an origin. You must consider the Datum when working with geospatial data, since data with two different Datum will not line up. The difference can be as much as a kilometer!
+For more information please refer to http://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system
= EXAMPLES
@@ -58,8 +29,7 @@ A description of the available ellipsoids and sample usage of the conversion rou
== Ellipsoids
-The Ellipsoids available are as follows:
-
+Ellipsoids describe the shape of the earth used to calculate the UTM grid. As a default, WGS-84 is selected, but any of the following ellipses may be used:
1. Airy
2. Australian National
@@ -91,23 +61,8 @@ The Ellipsoids available are as follows:
28. WGS-72
29. WGS-84
-
-== ellipsoid_names
-
-The ellipsoids can be accessed using
-
- GeoUtm::Ellipsoid::list_names
-
-Alternatively
-
- GeouUtm::Ellipsoid::each {|ellipsoid| ... }
-
-When converting to and from UTM coordinates, the ellipsoid
-is specified as a parameter which defaults to WGS-84. To use another
-ellipsoid, use
-
- coord.to_utm Ellipsoid::lookup :wgs60
-
+When specifying the ellipsoid as a parameter, you may use an instance of the GeoUtm::Ellipsoid class,
+a string containing the name above, or alternatively just a symbol (eg. :wgs84).
The name is not case sensitive and symbols are allowed. Spaces and -
are ignored.
@@ -131,13 +86,14 @@ returns
utm.n = 6409932.13416127
-== AUTHOR
+= AUTHOR
+This gem is heavily inspired and to a large extent also copied directly from by the Perl library Geo::Coordinates::UTM written by Graham Crookham.
Graham Crookham, grahamc@cpan.org
-Ruby port by Tallak Tveide 2009
+Ruby port by Tallak Tveide 2009/2011
-== COPYRIGHT
+= COPYRIGHT
Copyright (c) 2000,2002,2004 by Graham Crookham. All rights reserved.
1  lib/README.rd
View
@@ -0,0 +1 @@
+Testing 123
2  lib/geoutm.rb
View
@@ -5,5 +5,5 @@
require 'geoutm/latlon'
module GeoUtm
- VERSION = '0.0.4'
+ VERSION = '1.0.0'
end
79 lib/geoutm/ellipsoid.rb
View
@@ -1,5 +1,38 @@
module GeoUtm
+ # This class represents the ellipsoid used to convert from latitude/longitude into UTM coordinates. All
+ # operations default to using WGS-84.
class Ellipsoid
+ ELLIPSOID_DATA = [
+ [ "Airy", 6377563, 0.00667054],
+ [ "Australian National", 6378160, 0.006694542],
+ [ "Bessel 1841", 6377397, 0.006674372],
+ [ "Bessel 1841 Nambia", 6377484, 0.006674372],
+ [ "Clarke 1866", 6378206, 0.006768658],
+ [ "Clarke 1880", 6378249, 0.006803511],
+ [ "Everest 1830 India", 6377276, 0.006637847],
+ [ "Fischer 1960 Mercury", 6378166, 0.006693422],
+ [ "Fischer 1968", 6378150, 0.006693422],
+ [ "GRS 1967", 6378160, 0.006694605],
+ [ "GRS 1980", 6378137, 0.00669438],
+ [ "Helmert 1906", 6378200, 0.006693422],
+ [ "Hough", 6378270, 0.00672267],
+ [ "International", 6378388, 0.00672267],
+ [ "Krassovsky", 6378245, 0.006693422],
+ [ "Modified Airy", 6377340, 0.00667054],
+ [ "Modified Everest", 6377304, 0.006637847],
+ [ "Modified Fischer 1960", 6378155, 0.006693422],
+ [ "South American 1969", 6378160, 0.006694542],
+ [ "WGS 60", 6378165, 0.006693422],
+ [ "WGS 66", 6378145, 0.006694542],
+ [ "WGS-72", 6378135, 0.006694318],
+ [ "WGS-84", 6378137, 0.00669438 ],
+ [ "Everest 1830 Malaysia", 6377299, 0.006637847],
+ [ "Everest 1956 India", 6377301, 0.006637847],
+ [ "Everest 1964 Malaysia and Singapore", 6377304, 0.006637847],
+ [ "Everest 1969 Malaysia", 6377296, 0.006637847],
+ [ "Everest Pakistan", 6377296, 0.006637534],
+ [ "Indonesian 1974", 6378160, 0.006694609],
+ ]
attr_reader :name, :radius, :eccentricity
@@ -7,16 +40,24 @@ def initialize(name, radius, eccentricity)
@name, @radius, @eccentricity = name, radius, eccentricity
end
+ # Find a preconfigured ellipsoid
+ # @param [String] the name of the ellipsoid. Spaces, case and `-` are ignored
+ # @return [Ellipsoid]
def Ellipsoid.lookup(name)
result = List[normalize_name(name.to_s)]
raise GeoUtmException, 'Ellipsoid not found: ' + name.to_s unless result
result
end
+
+ # @see #lookup
def Ellipsoid.[](name)
lookup name
end
+ # Use this method when you get an ellipsoid-like as a parameter to convert to an ellipsoid
+ # @param [Ellipsoid, String, Symbol]
+ # @return [Ellipsoid]
def Ellipsoid.clean_parameter(ellipsoid_or_name)
case ellipsoid_or_name
when Ellipsoid
@@ -26,58 +67,30 @@ def Ellipsoid.clean_parameter(ellipsoid_or_name)
end
end
+ # @return [Array<String>] A list of all the available ellipsoid names
def Ellipsoid.list_names
List.keys.sort.map do |k|
List[k].name
end
end
+ # Iterate over the ellipsoid names
def Ellipsoid.each
List.keys.sort do |k|
yield List[k]
end
end
- private
+ # :nodoc:
def Ellipsoid.normalize_name(name)
name.gsub(/[\s\-\(\)]/, '').upcase
end
+ # :nodoc:
def Ellipsoid.generate_list
result = {}
- data = [
- [ "Airy", 6377563, 0.00667054],
- [ "Australian National", 6378160, 0.006694542],
- [ "Bessel 1841", 6377397, 0.006674372],
- [ "Bessel 1841 Nambia", 6377484, 0.006674372],
- [ "Clarke 1866", 6378206, 0.006768658],
- [ "Clarke 1880", 6378249, 0.006803511],
- [ "Everest 1830 India", 6377276, 0.006637847],
- [ "Fischer 1960 Mercury", 6378166, 0.006693422],
- [ "Fischer 1968", 6378150, 0.006693422],
- [ "GRS 1967", 6378160, 0.006694605],
- [ "GRS 1980", 6378137, 0.00669438],
- [ "Helmert 1906", 6378200, 0.006693422],
- [ "Hough", 6378270, 0.00672267],
- [ "International", 6378388, 0.00672267],
- [ "Krassovsky", 6378245, 0.006693422],
- [ "Modified Airy", 6377340, 0.00667054],
- [ "Modified Everest", 6377304, 0.006637847],
- [ "Modified Fischer 1960", 6378155, 0.006693422],
- [ "South American 1969", 6378160, 0.006694542],
- [ "WGS 60", 6378165, 0.006693422],
- [ "WGS 66", 6378145, 0.006694542],
- [ "WGS-72", 6378135, 0.006694318],
- [ "WGS-84", 6378137, 0.00669438 ],
- [ "Everest 1830 Malaysia", 6377299, 0.006637847],
- [ "Everest 1956 India", 6377301, 0.006637847],
- [ "Everest 1964 Malaysia and Singapore", 6377304, 0.006637847],
- [ "Everest 1969 Malaysia", 6377296, 0.006637847],
- [ "Everest Pakistan", 6377296, 0.006637534],
- [ "Indonesian 1974", 6378160, 0.006694609],
- ]
- data.each do |item|
+ ELLIPSOID_DATA.each do |item|
el = Ellipsoid.new *item
result[normalize_name(el.name)] = el
end
10 lib/geoutm/latlon.rb
View
@@ -7,17 +7,27 @@ class LatLon
include Math
attr_reader :lat, :lon
+ # Create a new coordinate instance based on latitude and longitude
+ #
+ # @param [Float] the coordinate latitude
+ # @param [Float] the coordinate longitude
def initialize(lat, lon)
raise GeoUtmException, "Invalid longitude #{lon}" unless (-180.0...180.0).member? lon
@lat, @lon = lat, lon
end
+ # Textual representation of the coordinate
def to_s
north_south = if @lat >= 0.0 then 'N' else 'S' end
east_west = if @lon >= 0.0 then 'E' else 'W' end
'%0.6f%s %0.6f%s' % [@lat.abs, north_south, @lon.abs, east_west]
end
+ # Convert the coordinate in latutude/longitude into the UTM coordinate system
+ #
+ # @option options [String,Symbol,Ellipsoid] :ellipsoid The ellipsoid to use
+ # @option options [String] :zone Force the coordiante into another UTM zone than it belongs. Use this to compare two coordinates in different zones
+ # @return [UTM] The converted UTM representation
def to_utm(options = {})
UTM::latlon_to_utm self, options
end
22 lib/geoutm/utm.rb
View
@@ -6,12 +6,24 @@
module GeoUtm
class UTM
- attr_reader :n, :e, :zone, :ellipsoid
+ # Northing
+ attr_reader :n
+ # Easting
+ attr_reader :e
+ attr_reader :zone
+ attr_reader :ellipsoid
+ # Creates a new UTM coorrdinate
+ #
+ # @param [String] The UTM zone to use, eg. `32H`
+ # @param [Float] The easting component of the coordinate
+ # @param [Float] The northing component of the coordinate
+ # @param [String,Symbol,Ellipsoid] The ellipsoid to use
def initialize(zone, e, n, ellipsoid = Ellipsoid::WGS84)
@n, @e, @zone, @ellipsoid = n, e, zone, Ellipsoid::clean_parameter(ellipsoid)
end
+ #:nodoc:
def UTM.latlon_to_utm(latlon, options = {})
ellipsoid = (options[:ellipsoid] && Ellipsoid::clean_parameter(options[:ellipsoid])) || Ellipsoid::WGS84
@@ -42,12 +54,14 @@ def UTM.latlon_to_utm(latlon, options = {})
UTM.new zone, utm_easting, utm_northing, ellipsoid
end
+ # Returns the band letter portion of the zone, eg `H` for `32H`
def zone_letter
UTMZones::split_zone(@zone).last
end
alias :zone_band :zone_letter
+ # Returns the number portion of the zone, eg `32` for `32H`
def zone_number
UTMZones::split_zone(@zone).first.to_i
end
@@ -57,6 +71,8 @@ def to_s
'%s %.2f %.2f' % [zone, e, n]
end
+ # Converts this position into the latitude/longitude coordinate system
+ # @return [LatLon]
def to_lat_lon
k0 = 0.9996
x = @e - 500000 # Remove Longitude offset
@@ -92,6 +108,9 @@ def to_lat_lon
LatLon.new latitude_deg, longitude_deg
end
+ # The euclidian distance between two points (ie. `sqrt(dx * dx + dy * dy)`)
+ # @param [LatLon,UTM] The other coordinate. Should be in close proximity to avoid large errors
+ # @return [Float]
def distance_to(other)
if other.class == LatLon
other = UTM::latlon_to_utm other, :ellipsoid => @ellipsoid, :zone => @zone
@@ -101,6 +120,5 @@ def distance_to(other)
end
Math::sqrt((@n - other.n) ** 2.0 + (@e - other.e) ** 2.0)
end
-
end
end
4 lib/geoutm/utm_zones.rb
View
@@ -1,7 +1,9 @@
require 'geoutm/geo_utm_exception'
module GeoUtm
- module UTMZones # :nodoc:
+ # This module is used by the UTM class and is generally never used directly
+ module UTMZones
+ # :nodoc:
SPECIAL_ZONES = {
'31V' => {:lat => (56.0..64.0), :lon => (0.0..3.0), :lon_origin => 3.0},
'32V' => {:lat => (56.0..64.0), :lon => (3.0..12.0), :lon_origin => 15.0},
Please sign in to comment.
Something went wrong with that request. Please try again.