Class: Geospatial::Location

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/geospatial/location.rb

Overview

This location is specifically relating to a WGS84 coordinate on Earth.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(longitude, latitude) ⇒ Location

Returns a new instance of Location.


73
74
75
76
# File 'lib/geospatial/location.rb', line 73

def initialize(longitude, latitude)
	@longitude = longitude
	@latitude = latitude
end

Instance Attribute Details

#latitudeObject (readonly)

-90 -> 90 (equivalent to y)


107
108
109
# File 'lib/geospatial/location.rb', line 107

def latitude
  @latitude
end

#longitudeObject (readonly)

-180 -> 180 (equivalent to x)


106
107
108
# File 'lib/geospatial/location.rb', line 106

def longitude
  @longitude
end

Class Method Details

.from_ecef(x, y, z) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/geospatial/location.rb', line 50

def from_ecef(x, y, z)
	# Constants (WGS ellipsoid)
	a = WGS84_A
	e = WGS84_E
		
	b = Math::sqrt((a*a) * (1.0-(e*e)))
	ep = Math::sqrt(((a*a)-(b*b))/(b*b))
	
	p = Math::sqrt((x*x)+(y*y))
	th = Math::atan2(a*z, b*p)
	
	lon = Math::atan2(y, x)
	lat = Math::atan2((z+ep*ep*b*(Math::sin(th) ** 3)), (p-e*e*a*(Math::cos(th)**3)))
	
	# n = a / Math::sqrt(1.0-e*e*(Math::sin(lat) ** 2))
	# alt = p / Math::cos(lat)-n
	
	return self.new(lat*R2D, lon*R2D)
end

Instance Method Details

#-(other) ⇒ Object


202
203
204
# File 'lib/geospatial/location.rb', line 202

def - other
	Distance.new(self.distance_from(other))
end

#<=>(other) ⇒ Object


100
101
102
# File 'lib/geospatial/location.rb', line 100

def <=> other
	to_a <=> other.to_a
end

#bearing_from(other) ⇒ Numeric

Returns bearing in degrees.

Returns:

  • (Numeric)

    bearing in degrees.


177
178
179
180
181
182
183
184
185
186
187
# File 'lib/geospatial/location.rb', line 177

def bearing_from(other)
	lon1 = other.longitude * D2R 
	lat1 = other.latitude * D2R 
	lon2 = self.longitude * D2R 
	lat2 = self.latitude * D2R 
	
	return Math::atan2(
		Math::sin(lon2 - lon1) * Math::cos(lat2),
		Math::cos(lat1) * Math::sin(lat2) - Math::sin(lat1) * Math::cos(lat2) * Math::cos(lon2-lon1)
	) * R2D
end

#bounding_box(distance, radius = R) ⇒ Object

Raises:

  • (ArgumentError)

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/geospatial/location.rb', line 110

def bounding_box(distance, radius = R)
	raise ArgumentError.new("Invalid distance or radius") if distance < 0 or radius < 0

	# angular distance in radians on a great circle
	angular_distance = distance / radius

	min_latitude = (self.latitude * D2R) - angular_distance
	max_latitude = (self.latitude * D2R) + angular_distance

	if min_latitude > MIN_LATITUDE and max_latitude < MAX_LATITUDE
		longitude_delta = Math::asin(Math::sin(angular_distance) / Math::cos(self.latitude * D2R))
		
		min_longitude = (self.longitude * D2R) - longitude_delta
		min_longitude += 2.0 * Math::PI if (min_longitude < MIN_LONGITUDE)
		
		max_longitude = (self.longitude * D2R) + longitude_delta;
		max_longitude -= 2.0 * Math::PI if (max_longitude > MAX_LONGITUDE)
	else
		# a pole is within the distance
		min_latitude = [min_latitude, MIN_LATITUDE].max
		max_latitude = [max_latitude, MAX_LATITUDE].min
		
		min_longitude = MIN_LONGITUDE
		max_longitude = MAX_LONGITUDE
	end
	
	return {
		:latitude => Range.new(min_latitude * R2D, max_latitude * R2D),
		:longitude => Range.new(min_longitude * R2D, max_longitude * R2D),
	}
end

#distance_from(other) ⇒ Object

calculate distance in metres between us and something else ref: codingandweb.blogspot.co.nz/2012/04/calculating-distance-between-two-points.html


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/geospatial/location.rb', line 160

def distance_from(other)
	rlong1 = self.longitude * D2R
	rlat1 = self.latitude * D2R
	rlong2 = other.longitude * D2R
	rlat2 = other.latitude * D2R
	
	dlon = rlong1 - rlong2
	dlat = rlat1 - rlat2
	
	a = Math::sin(dlat/2) ** 2 + Math::cos(rlat1) * Math::cos(rlat2) * Math::sin(dlon/2) ** 2
	c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))
	d = R * c
	
	return d
end

#location_by(bearing, distance) ⇒ Object

Parameters:

  • distance (Numeric)

    distance in meters.

  • bearing (Numeric)

    bearing in degrees.


191
192
193
194
195
196
197
198
199
200
# File 'lib/geospatial/location.rb', line 191

def location_by(bearing, distance)
	lon1 = self.longitude * D2R
	lat1 = self.latitude * D2R
	
	lat2 = Math::asin(Math::sin(lat1)*Math::cos(distance/R) + Math::cos(lat1)*Math::sin(distance/R)*Math::cos(bearing * D2R))
	
	lon2 = lon1 + Math::atan2(Math::sin(bearing * D2R)*Math::sin(distance/R)*Math::cos(lat1), Math::cos(distance/R)-Math::sin(lat1)*Math::sin(lat2))
	
	return self.class.new(lon2 * R2D, lat2 * R2D)
end

#midpoints_to(other, count) ⇒ Object

Compute count midpoints between self and other.

Parameters:

  • count (Integer)

    the number of segments to generate.


208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/geospatial/location.rb', line 208

def midpoints_to(other, count)
	return to_enum(:midpoints_to, other, count) unless block_given?
	
	distance = other.distance_from(self)
	bearing = other.bearing_from(self)
	
	step = distance / count
	
	(1...count).each do |i|
		yield self.location_by(bearing, step * i)
	end
end

#to_aObject


82
83
84
# File 'lib/geospatial/location.rb', line 82

def to_a
	[@longitude, @latitude]
end

#to_aryObject


86
87
88
# File 'lib/geospatial/location.rb', line 86

def to_ary
	to_a
end

#to_ecefObject

Converts latitude, longitude to ECEF coordinate system


143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/geospatial/location.rb', line 143

def to_ecef
	clon = Math::cos(lon * D2R)
	slon = Math::sin(lon * D2R)
	clat = Math::cos(lat * D2R)
	slat = Math::sin(lat * D2R)

	n = WGS84_A / Math::sqrt(1.0 - WGS84_E * WGS84_E * slat * slat)

	x = n * clat * clon
	y = n * clat * slon
	z = n * (1.0 - WGS84_E * WGS84_E) * slat
	
	return x, y, z
end

#to_hObject


90
91
92
# File 'lib/geospatial/location.rb', line 90

def to_h
	{latitude: @latitude, longitude: @longitude}
end

#to_sObject Also known as: inspect


94
95
96
# File 'lib/geospatial/location.rb', line 94

def to_s
	"#{self.class}[#{self.longitude.to_f}, #{self.latitude.to_f}]"
end

#valid?Boolean

Returns:

  • (Boolean)

78
79
80
# File 'lib/geospatial/location.rb', line 78

def valid?
	VALID_LONGITUDE.include?(longitude) and VALID_LATITUDE.include?(latitude)
end