Class: GeoRuby::SimpleFeatures::Point
- Inherits:
-
Object
- Object
- GeoRuby::SimpleFeatures::Point
- 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
- .bounds(points) ⇒ Object
- .centroid(points) ⇒ Object
- .earth_radius ⇒ Object
- .from_lat_lng(object, srid = 4326) ⇒ Object
- .from_pro4j(point, srid, ratio = nil) ⇒ Object
- .latitude_degree_distance ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#bounding_box ⇒ Object
Fixes original bounding_box which creates points without srid.
- #change(options) ⇒ Object
- #close_to?(other) ⇒ Boolean
- #earth_radius ⇒ Object
- #endpoint(heading, distance, options = {}) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #latitude_degree_distance ⇒ Object
- #metric_delta(other) ⇒ Object
- #project_to(target_srid) ⇒ Object
- #projection ⇒ Object
- #spherical_distance_with_srid_support(other) ⇒ Object
- #to_lat_lng ⇒ Object
- #to_openlayers ⇒ Object
- #to_proj4(ratio = nil) ⇒ Object
- #to_rgeo ⇒ Object
- #to_s ⇒ Object
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_radius ⇒ Object
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_distance ⇒ Object
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_box ⇒ Object
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() # TODO support z self.class.from_x_y([:x] || x, [:y] || y, [:srid] || srid) # or instead of || requires parenthesis end |
#close_to?(other) ⇒ Boolean
107 108 109 |
# File 'lib/georuby-ext/georuby/point.rb', line 107 def close_to?(other) spherical_distance(other) < 10e-3 end |
#earth_radius ⇒ Object
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, ={}) Endpointer.new(self, heading, distance, ).arrival end |
#eql?(other) ⇒ 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 |
#hash ⇒ Object
111 112 113 |
# File 'lib/georuby-ext/georuby/point.rb', line 111 def hash [x,y,z,srid].hash end |
#latitude_degree_distance ⇒ Object
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 |
#projection ⇒ Object
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_lng ⇒ Object
144 145 146 |
# File 'lib/georuby-ext/georuby/point.rb', line 144 def to_lat_lng Geokit::LatLng.new y, x end |
#to_openlayers ⇒ Object
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_rgeo ⇒ Object
169 170 171 |
# File 'lib/georuby-ext/georuby/point.rb', line 169 def to_rgeo rgeo_factory.point x, y end |
#to_s ⇒ Object
115 116 117 |
# File 'lib/georuby-ext/georuby/point.rb', line 115 def to_s "#{y},#{x}" end |