class GeoRuby::SimpleFeatures::LineString
Represents a line string as an array of points (see Point
).
Attributes
points[R]
the list of points forming the line string
Public Class Methods
from_coordinates(points, srid = DEFAULT_SRID, z = false, m = false)
click to toggle source
Creates a new line string. Accept a sequence of points as argument:
((x,y)...(x,y))
# File lib/geo_ruby/simple_features/line_string.rb, line 217 def self.from_coordinates(points, srid = DEFAULT_SRID, z = false, m = false) line_string = new(srid, z, m) line_string.concat(points.map { |p| Point.from_coordinates(p, srid, z, m) }) line_string end
from_points(points, srid = DEFAULT_SRID, z = false, m = false)
click to toggle source
Creates a new line string. Accept an array of points as argument
# File lib/geo_ruby/simple_features/line_string.rb, line 209 def self.from_points(points, srid = DEFAULT_SRID, z = false, m = false) line_string = new(srid, z, m) line_string.concat(points) line_string end
new(srid = DEFAULT_SRID, with_z = false, with_m = false)
click to toggle source
Calls superclass method
GeoRuby::SimpleFeatures::Geometry::new
# File lib/geo_ruby/simple_features/line_string.rb, line 10 def initialize(srid = DEFAULT_SRID, with_z = false, with_m = false) super(srid, with_z, with_m) @points = [] end
Public Instance Methods
==(other)
click to toggle source
Tests the equality of line strings
# File lib/geo_ruby/simple_features/line_string.rb, line 95 def ==(other) if other.class != self.class || other.length != length false else index = 0 while index < length return false if self[index] != other[index] index += 1 end true end end
as_json(_options = {})
click to toggle source
# File lib/geo_ruby/simple_features/line_string.rb, line 204 def as_json(_options = {}) { type: 'LineString', coordinates: to_coordinates } end
bounding_box()
click to toggle source
Bounding box in 2D/3D. Returns an array of 2 points
# File lib/geo_ruby/simple_features/line_string.rb, line 36 def bounding_box max_x = max_y = -Float::MAX min_x = min_y = Float::MAX if with_z max_z, min_z = -Float::MAX, Float::MAX each do |point| max_y = point.y if point.y > max_y min_y = point.y if point.y < min_y max_x = point.x if point.x > max_x min_x = point.x if point.x < min_x max_z = point.z if point.z > max_z min_z = point.z if point.z < min_z end [Point.from_x_y_z(min_x, min_y, min_z), Point.from_x_y_z(max_x, max_y, max_z)] else each do |point| max_y = point.y if point.y > max_y min_y = point.y if point.y < min_y max_x = point.x if point.x > max_x min_x = point.x if point.x < min_x end [Point.from_x_y(min_x, min_y), Point.from_x_y(max_x, max_y)] end end
clockwise?()
click to toggle source
# File lib/geo_ruby/simple_features/line_string.rb, line 26 def clockwise? tuples = @points.zip( @points[1..-1] + [@points[0]], @points[2..-1] + [@points[0], @points[1]]) tuples.map! { |a, b, c| b.x * (c.y - a.y) } sum = tuples.reduce(0.0) { |a, e| a + e } sum < 0.0 end
closed?()
click to toggle source
tests if the line string is closed
# File lib/geo_ruby/simple_features/line_string.rb, line 21 def closed? # a bit naive... @points.first == @points.last end
do_simplify(list, epsilon)
click to toggle source
# File lib/geo_ruby/simple_features/line_string.rb, line 188 def do_simplify(list, epsilon) index = dmax = 0 2.upto(list.length - 1) do |i| d = list[i].orthogonal_distance(list[0], list[-1]) index, dmax = i, d if d > dmax end return [list[0], list[-1]] if dmax < epsilon res1 = do_simplify(list[0..index], epsilon) res2 = do_simplify(list[index..-1], epsilon) res1[0..-2] + res2[0..-1] end
euclidian_distance()
click to toggle source
# File lib/geo_ruby/simple_features/line_string.rb, line 86 def euclidian_distance total = 0 @points.each_with_index do |p, i| total += p.euclidian_distance(@points[i + 1]) if @points[i + 1] end total end
intersects?(_other_line_string)
click to toggle source
call to native Geo intersect, return true or false
# File lib/geo_ruby/simple_features/line_string.rb, line 75 def intersects?(_other_line_string) end
m_range()
click to toggle source
# File lib/geo_ruby/simple_features/line_string.rb, line 61 def m_range if with_m max_m, min_m = -Float::MAX, Float::MAX each do |point| max_m = point.m if point.m.to_f > max_m min_m = point.m if point.m.to_f < min_m end [min_m, max_m] else [0, 0] end end
method_missing(method_name, *args, &b)
click to toggle source
Delegate the unknown methods to the points array
# File lib/geo_ruby/simple_features/line_string.rb, line 16 def method_missing(method_name, *args, &b) @points.send(method_name, *args, &b) end
simplify(epsilon = 1)
click to toggle source
Simplify linestring (Douglas Peucker Algorithm) en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
# File lib/geo_ruby/simple_features/line_string.rb, line 184 def simplify(epsilon = 1) LineString.from_points(do_simplify(@points, epsilon)) end
spherical_distance()
click to toggle source
# File lib/geo_ruby/simple_features/line_string.rb, line 78 def spherical_distance total = 0 @points.each_with_index do |p, i| total += p.spherical_distance(@points[i + 1]) if @points[i + 1] end total end
to_coordinates()
click to toggle source
# File lib/geo_ruby/simple_features/line_string.rb, line 200 def to_coordinates points.map(&:to_coordinates) end