Class: GeoRuby::SimpleFeatures::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/georuby-ext/georuby/point.rb

Defined Under Namespace

Classes: Endpointer

Constant Summary collapse

@@earth_radius =

Earth radius in kms

GeoRuby Point#spherical_distance uses 6370997.0 m Geokit::LatLng uses 6376.77271 km …

6370997.0
@@latitude_degree_distance =

Length of a latitude degree in meters

@@earth_radius * 2 * Math::PI / 360

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bounds(points) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/georuby-ext/georuby/point.rb', line 182

def self.bounds(points)
  return nil if points.blank?

  points.inject(points.first.envelope) do |envelope, point|
    envelope.extend!(point.envelope)
  end
end

.centroid(points) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/georuby-ext/georuby/point.rb', line 119

def self.centroid(points)
  case points.size
  when 0
    nil
  when 1
    points.first
  when 2
    from_x_y points.sum(&:x) / 2, points.sum(&:y) / 2, srid!(points)
  else
    points = [points.last, *points] # polygon must be closed for rgeo
    GeoRuby::SimpleFeatures::Polygon.from_points([points], srid!(points)).centroid
  end
end

.earth_radiusObject



9
10
11
# File 'lib/georuby-ext/georuby/point.rb', line 9

def self.earth_radius
  @@earth_radius 
end

.from_lat_lng(object, srid = 4326) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/georuby-ext/georuby/point.rb', line 133

def self.from_lat_lng(object, srid = 4326)
  ActiveSupport::Deprecation.warn "Don't use Geokit::LatLng to represent no wgs84 point" unless srid == 4326
  
  if object.respond_to?(:to_lat_lng)
    lat_lng = object.to_lat_lng
  else
    lat_lng = Geokit::LatLng.normalize object
  end 
  from_x_y lat_lng.lng, lat_lng.lat, srid
end

.from_pro4j(point, srid, ratio = nil) ⇒ Object



164
165
166
167
# File 'lib/georuby-ext/georuby/point.rb', line 164

def self.from_pro4j(point, srid, ratio = nil)
  ratio ||= (srid == 4326 ? Proj4::RAD_TO_DEG : 1.0)
  from_x_y point.x * ratio, point.y * ratio, srid
end

.latitude_degree_distanceObject



18
19
20
# File 'lib/georuby-ext/georuby/point.rb', line 18

def self.latitude_degree_distance
  @@latitude_degree_distance 
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
37
# File 'lib/georuby-ext/georuby/point.rb', line 33

def ==(other)
  other and 
    other.respond_to?(:lat) and other.respond_to?(:lng) and
    (other.respond_to?(:srid) and srid == other.srid) ? (lat == other.lat and lng == other.lng) : (spherical_distance(other) < 10e-3)
end

#bounding_boxObject

Fixes original bounding_box which creates points without srid



178
179
180
# File 'lib/georuby-ext/georuby/point.rb', line 178

def bounding_box
  Array.new(with_z ? 3 : 2) { dup }
end

#change(options) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/georuby-ext/georuby/point.rb', line 25

def change(options)
  # TODO support z
  self.class.from_x_y(options[:x] || x, 
                      options[:y] || y, 
                      options[:srid] || srid)
  # or instead of || requires parenthesis
end

#close_to?(other) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/georuby-ext/georuby/point.rb', line 107

def close_to?(other)
  spherical_distance(other) < 10e-3
end

#earth_radiusObject



12
13
14
# File 'lib/georuby-ext/georuby/point.rb', line 12

def earth_radius
  self.class.earth_radius
end

#endpoint(heading, distance, options = {}) ⇒ Object



44
45
46
# File 'lib/georuby-ext/georuby/point.rb', line 44

def endpoint(heading, distance, options={})
  Endpointer.new(self, heading, distance, options).arrival
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/georuby-ext/georuby/point.rb', line 103

def eql?(other)
   [x,y,z,srid] == [other.x, other.y, other.z, other.srid]
end

#hashObject



111
112
113
# File 'lib/georuby-ext/georuby/point.rb', line 111

def hash
  [x,y,z,srid].hash
end

#latitude_degree_distanceObject



21
22
23
# File 'lib/georuby-ext/georuby/point.rb', line 21

def latitude_degree_distance
  self.class.latitude_degree_distance
end

#metric_delta(other) ⇒ Object



190
191
192
193
194
195
# File 'lib/georuby-ext/georuby/point.rb', line 190

def metric_delta(other)
  longitude_degree_distance =
    (latitude_degree_distance * Math.cos(lat.deg2rad)).abs
  [ latitude_degree_distance * (other.lat - lat),
    longitude_degree_distance * (other.lng - lng) ]
end

#project_to(target_srid) ⇒ Object



152
153
154
155
156
# File 'lib/georuby-ext/georuby/point.rb', line 152

def project_to(target_srid)
  return self if srid == target_srid

  self.class.from_pro4j projection.transform(Proj4::Projection.for_srid(target_srid), to_proj4.x, to_proj4.y), target_srid
end

#projectionObject



148
149
150
# File 'lib/georuby-ext/georuby/point.rb', line 148

def projection
  Proj4::Projection.for_srid srid
end

#spherical_distance_with_srid_support(other) ⇒ Object



39
40
41
# File 'lib/georuby-ext/georuby/point.rb', line 39

def spherical_distance_with_srid_support(other)
  to_wgs84.spherical_distance_without_srid_support(other.to_wgs84)
end

#to_lat_lngObject



144
145
146
# File 'lib/georuby-ext/georuby/point.rb', line 144

def to_lat_lng
  Geokit::LatLng.new y, x
end

#to_openlayersObject



173
174
175
# File 'lib/georuby-ext/georuby/point.rb', line 173

def to_openlayers
  OpenLayers::LonLat.new x, y
end

#to_proj4(ratio = nil) ⇒ Object



158
159
160
161
162
# File 'lib/georuby-ext/georuby/point.rb', line 158

def to_proj4(ratio = nil)
  # Proj4 use radian instead of degres
  ratio ||= (wgs84? ? Proj4::DEG_TO_RAD : 1.0)
  Proj4::Point.new x * ratio, y * ratio
end

#to_rgeoObject



169
170
171
# File 'lib/georuby-ext/georuby/point.rb', line 169

def to_rgeo
  rgeo_factory.point x, y
end

#to_sObject



115
116
117
# File 'lib/georuby-ext/georuby/point.rb', line 115

def to_s
  "#{y},#{x}"
end