Class: MapWKT::Geometry::Point

Inherits:
MapWKT::Geometry show all
Defined in:
lib/mapwkt/wkt/point.rb

Instance Method Summary collapse

Methods inherited from MapWKT::Geometry

parse_linestrings, parse_points, parse_polygons, parse_wkt, parse_x_y

Constructor Details

#initialize(φ, λ) ⇒ Point

Returns a new MapWKT::Geometry::Point with latitude and longitude normalized to fit within the range [-90,90] for latitude and (-180,180] for longitude.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mapwkt/wkt/point.rb', line 21

def initialize (φ, λ)
  # Check arguments.
  if [φ, λ].all? {|arg| arg.respond_to?(:to_f) }
    φ = φ.to_f
    λ = λ.to_f
  else
    raise ArgumentError, "expected (latitude, longitude), got (#{φ.inspect}, #{λ.inspect})"
  end
  
  # Normalize latitude (-90..90) and longitude (-179..180).
  λ += 180 * (((φ / 90 + 1).floor / 2) % 2)
  @y = 90 - (((90 + φ) % 360) - 180).abs
  @x = self.pole? ? 0 : (λ - 180) % -360 + 180
end

Instance Method Details

#==(other) ⇒ Object

Returns true if this Point matches the other Point, within 7 decimal places.



115
116
117
# File 'lib/mapwkt/wkt/point.rb', line 115

def == (other)
  [self.latitude, self.longitude] == [other.latitude, other.longitude]
end

#===(other) ⇒ Object

Returns true if this Point matches the other Point exactly.



120
121
122
# File 'lib/mapwkt/wkt/point.rb', line 120

def === (other)
  [self.x, self.y] == [other.x, other.y]
end

#centerObject

Returns a new Point with the same latitude and longitude as this Point.



5
6
7
# File 'lib/mapwkt/wkt/point.rb', line 5

def center
  self.dup
end

#east?Boolean

Returns true if this Point lies in the eastern hemisphere, false otherwise.

Returns:

  • (Boolean)


10
11
12
# File 'lib/mapwkt/wkt/point.rb', line 10

def east?
  self.longitude > 0
end

#equator?Boolean

Returns true if this Point lies on the equator, false otherwise.

Returns:

  • (Boolean)


15
16
17
# File 'lib/mapwkt/wkt/point.rb', line 15

def equator?
  self.latitude == 0
end

#latitudeObject

Returns this Point’s latitude as a float, rounded to 7 decimal places.



37
38
39
# File 'lib/mapwkt/wkt/point.rb', line 37

def latitude
  self.y.round(7)
end

#latitude_fObject

Returns this Point’s latitude as a string, rounded to 7 decimal places.



42
43
44
# File 'lib/mapwkt/wkt/point.rb', line 42

def latitude_f
  sprintf("%.7f", self.latitude).gsub(/\.?0*$/,'')
end

#latitude_°Object

Returns this Point’s latitude in degrees N/S, rounded to 7 decimal places.



47
48
49
50
51
52
# File 'lib/mapwkt/wkt/point.rb', line 47

def latitude_°
  φ = self.latitude
  direction = (φ == 0) ? "" : (φ > 0) ? "N" : "S"
  
  sprintf("%.7f°%s", φ.abs, direction).gsub(/\.?0*(?=°)/,'')
end

#longitudeObject

Returns this Point’s longitude as a float, rounded to 7 decimal places.



55
56
57
# File 'lib/mapwkt/wkt/point.rb', line 55

def longitude
  self.x.round(7)
end

#longitude_fObject

Returns this Point’s longitude as a string, rounded to 7 decimal places.



60
61
62
# File 'lib/mapwkt/wkt/point.rb', line 60

def longitude_f
  sprintf("%.7f", self.longitude).gsub(/\.?0*$/,'')
end

#longitude_°Object

Returns this Point’s longitude in degrees E/W, rounded to 7 decimal places.



65
66
67
68
69
70
# File 'lib/mapwkt/wkt/point.rb', line 65

def longitude_°
  λ = self.longitude
  direction = [0, 180].include?(λ) ? "" : (λ > 0) ? "E" : "W"
  
  sprintf("%.7f°%s", λ.abs, direction).gsub(/\.?0*(?=°)/,'')
end

#north_pole?Boolean

Returns true if this Point is the north pole.

Returns:

  • (Boolean)


80
81
82
# File 'lib/mapwkt/wkt/point.rb', line 80

def north_pole?
  self.latitude == 90
end

#pole?Boolean

Returns 1 if this Point is the north pole, -1 if it is the south pole, or nil if it is neither.

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/mapwkt/wkt/point.rb', line 74

def pole?
  return 1 if self.north_pole?
  return -1 if self.south_pole?
end

#south_pole?Boolean

Returns true if this Point is the south pole.

Returns:

  • (Boolean)


85
86
87
# File 'lib/mapwkt/wkt/point.rb', line 85

def south_pole?
  self.latitude == -90
end

#to_sObject

Returns a string representation of this Point.



90
91
92
# File 'lib/mapwkt/wkt/point.rb', line 90

def to_s
  "#{self.latitude_°} #{self.longitude_°}"
end

#west?Boolean

Returns true if this Point lies in the western hemisphere, false otherwise.

Returns:

  • (Boolean)


95
96
97
# File 'lib/mapwkt/wkt/point.rb', line 95

def west?
  self.longitude < 0
end

#wktObject

Returns this Point’s WKT representation, rounded to 7 decimal places.



100
101
102
# File 'lib/mapwkt/wkt/point.rb', line 100

def wkt
  "POINT(#{self.longitude_f} #{self.latitude_f})"
end

#xObject

Returns this Point’s longitude as a float.



105
106
107
# File 'lib/mapwkt/wkt/point.rb', line 105

def x
  @x
end

#yObject

Returns this Point’s latitude as a float.



110
111
112
# File 'lib/mapwkt/wkt/point.rb', line 110

def y
  @y
end