Skip to content
Browse files

distance calc notes

  • Loading branch information...
1 parent 4d9fdad commit ab0390629c7829ec8c4458e0c66abe87f169c9d2 @kristianmandrup committed May 11, 2011
Showing with 61 additions and 1 deletion.
  1. +32 −0 Geo calculation notes.txt
  2. +3 −0 README.textile
  3. +16 −0 lib/geo_magic/vector.rb
  4. +10 −1 lib/geo_magic/vector/direction_vector.rb
View
32 Geo calculation notes.txt
@@ -0,0 +1,32 @@
+Alex Reiner (geocoder):
+
+The angle/distance stuff is complicated until the moment when it all suddenly makes sense. It's probably much simpler than you're thinking it is. You should probably take a look at the methods in my Calculations module: https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/calculations.rb
+
+The first method calculates the distance of one degree of latitude (which is simpler than longitude). When you're translating radians (or degrees) to distance, think about it as moving along a Great Circle. This simplifies your mental picture to two dimensions, so you can use a simple circle circumference equations:
+
+C = 2(pi)r
+
+We know the radius of the Earth is around 6359 km so if we substitute that we get:
+
+C = 2(pi)(6359) = 39955
+
+which is pretty close to the Earth's circumference: 40075 km.
+
+To calculate the distance spanned by one degree of latitude you just divide by the number of degrees in the whole circumference, which is
+
+360: 39955 / 360 = 111
+
+So to calculate the distance spanned by x degrees of latitude you just multiply by 111:
+
+distance = 111x
+
+For longitude it's slightly more complicated because the degrees range from 111 km at the equator to almost 0 km at the poles.
+But really you just do the same thing you did for longitude and then multiply by COS(latitude).
+
+So, for example, the distance of one degree of longitude along the 40 degree latitude line:
+
+111 * COS(40) = 85 km
+
+When you're not moving precisely along a line of latitude or longitude it's obviously more complicated, although really you can split the "diagonal" distance up into two vectors and calculate the length of the "hypotenuse."
+
+Conceptually the key is to think of moving along a Great Circle. A certain number of degrees (or radians) around that circle will always be the same distance, but degrees of longitude are a different thing and you have to translate them into circle degrees before calculating distance.
View
3 README.textile
@@ -13,6 +13,9 @@ Some of the main features:
* Calculate nearest points from a point
* Find points within a radius (circle or square from center)
* Calculate points within a bounding box
+* Apply vectors to points
+* Generate random points within a radius (for testing purposes)
+* Distance unit conversions (feet, meters, kms, miles)
You can either include the complete library like this:
View
16 lib/geo_magic/vector.rb
@@ -9,6 +9,22 @@ def apply_to arg
when GeoMagic::PointVector
end
end
+
+ def << arg
+ apply_to arg
+ end
+
+ def + arg
+ apply_to arg
+ end
+
+ def scale scalar
+
+ end
+
+ def * scalar
+ scale scalar
+ end
end
end
end
View
11 lib/geo_magic/vector/direction_vector.rb
@@ -17,12 +17,21 @@ def self.valid_directions
def to_point_vector
GeoMagic::PointVector.from_origin calc_point
end
-
+
+ ## move should use either + or << operator
def apply_to arg
raise ArgumentError, "Argument must be a GeoMagic::Point or a GeoMagic::PointVector" if !arg.any_kind_of?(GeoMagic::Vector, GeoMagic::PointVector)
case arg
when GeoMagic::Point
+ point = arg
+ v = calc_point
+ point.move(v.latitude, v.longitude)
when GeoMagic::PointVector
+ pv = arg
+ v = calc_point
+ pv.latitude = pv.latitude + v.longitude
+ pv.longitude = pv.longitude + v.longitude
+ pv
end
end

0 comments on commit ab03906

Please sign in to comment.
Something went wrong with that request. Please try again.