Class: GeoRuby::SimpleFeatures::LineString

Inherits:
Geometry
  • Object
show all
Defined in:
lib/geo_ruby/simple_features/line_string.rb

Overview

Represents a line string as an array of points (see Point).

Direct Known Subclasses

LinearRing

Instance Attribute Summary collapse

Attributes inherited from Geometry

#srid, #with_m, #with_z

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Geometry

#as_ewkb, #as_ewkt, #as_georss, #as_hex_ewkb, #as_hex_wkb, #as_kml, #as_wkb, #as_wkt, #envelope, from_ewkb, from_ewkt, from_georss, from_georss_with_tags, from_hex_ewkb, from_kml, kml_to_wkt

Constructor Details

#initialize(srid = DEFAULT_SRID, with_z = false, with_m = false) ⇒ LineString

Returns a new instance of LineString.



10
11
12
13
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &b) ⇒ Object

Delegate the unknown methods to the points array



16
17
18
# 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

Instance Attribute Details

#pointsObject (readonly)

the list of points forming the line string



8
9
10
# File 'lib/geo_ruby/simple_features/line_string.rb', line 8

def points
  @points
end

Class Method Details

.from_coordinates(points, srid = DEFAULT_SRID, with_z = false, with_m = false) ⇒ Object

Creates a new line string. Accept a sequence of points as argument : ((x,y)…(x,y))



158
159
160
161
162
# File 'lib/geo_ruby/simple_features/line_string.rb', line 158

def self.from_coordinates(points,srid=DEFAULT_SRID,with_z=false,with_m=false)
  line_string = new(srid,with_z,with_m)
  line_string.concat( points.collect{|point_coords| Point.from_coordinates(point_coords,srid,with_z,with_m)  } )
  line_string
end

.from_points(points, srid = DEFAULT_SRID, with_z = false, with_m = false) ⇒ Object

Creates a new line string. Accept an array of points as argument



151
152
153
154
155
# File 'lib/geo_ruby/simple_features/line_string.rb', line 151

def self.from_points(points,srid=DEFAULT_SRID,with_z=false,with_m=false)
  line_string = new(srid,with_z,with_m)
  line_string.concat(points)
  line_string
end

Instance Method Details

#==(other_line_string) ⇒ Object

Tests the equality of line strings



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/geo_ruby/simple_features/line_string.rb', line 65

def ==(other_line_string)
  if(other_line_string.class != self.class or 
       other_line_string.length != self.length)
    false
  else
    index=0
    while index<length
      return false if self[index] != other_line_string[index]
      index+=1
    end
    true
  end
end

#binary_geometry_typeObject

WKB geometry type



87
88
89
# File 'lib/geo_ruby/simple_features/line_string.rb', line 87

def binary_geometry_type #:nodoc: 

  2
end

#binary_representation(allow_z = true, allow_m = true) ⇒ Object

Binary representation of a line string



80
81
82
83
84
# File 'lib/geo_ruby/simple_features/line_string.rb', line 80

def binary_representation(allow_z=true,allow_m=true) #:nodoc: 

  rep = [length].pack("V")
  each {|point| rep << point.binary_representation(allow_z,allow_m) }
  rep
end

#bounding_boxObject

Bounding box in 2D/3D. Returns an array of 2 points



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/geo_ruby/simple_features/line_string.rb', line 27

def bounding_box
  max_x, min_x, max_y, min_y = -Float::MAX, Float::MAX, -Float::MAX, 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

#georss_gml_representation(options) ⇒ Object

georss gml representation



112
113
114
115
116
117
118
119
# File 'lib/geo_ruby/simple_features/line_string.rb', line 112

def georss_gml_representation(options) #:nodoc: 

  georss_ns = options[:georss_ns] || "georss"
  gml_ns = options[:gml_ns] || "gml"
  
  result = "<#{georss_ns}:where>\n<#{gml_ns}:LineString>\n<#{gml_ns}:posList>\n"
  result += georss_poslist
  result += "\n</#{gml_ns}:posList>\n</#{gml_ns}:LineString>\n</#{georss_ns}:where>\n"
end

#georss_poslistObject

:nodoc:



121
122
123
# File 'lib/geo_ruby/simple_features/line_string.rb', line 121

def georss_poslist #:nodoc: 

  map {|point| "#{point.y} #{point.x}"}.join(" ")
end

#georss_simple_representation(options) ⇒ Object

georss simple representation



101
102
103
104
105
# File 'lib/geo_ruby/simple_features/line_string.rb', line 101

def georss_simple_representation(options) #:nodoc: 

  georss_ns = options[:georss_ns] || "georss"
  geom_attr = options[:geom_attr]
  "<#{georss_ns}:line#{geom_attr}>" + georss_poslist + "</#{georss_ns}:line>\n"
end

#georss_w3cgeo_representation(options) ⇒ Object

georss w3c representation : outputs the first point of the line



107
108
109
110
# File 'lib/geo_ruby/simple_features/line_string.rb', line 107

def georss_w3cgeo_representation(options) #:nodoc: 

  w3cgeo_ns = options[:w3cgeo_ns] || "geo"
  "<#{w3cgeo_ns}:lat>#{self[0].y}</#{w3cgeo_ns}:lat>\n<#{w3cgeo_ns}:long>#{self[0].x}</#{w3cgeo_ns}:long>\n"
end

#is_closedObject

tests if the line string is closed



21
22
23
24
# File 'lib/geo_ruby/simple_features/line_string.rb', line 21

def is_closed
  #a bit naive...

  @points.first == @points.last
end

#kml_poslist(options) ⇒ Object

:nodoc:



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/geo_ruby/simple_features/line_string.rb', line 137

def kml_poslist(options) #:nodoc: 

   pos_list = if options[:allow_z]
     map {|point| "#{point.x},#{point.y},#{options[:fixed_z] || point.z || 0}" }
  else
    map {|point| "#{point.x},#{point.y}" }
  end
  if(options[:reverse])
     pos_list.reverse.join(" ")
  else
     pos_list.join(" ")
  end
end

#kml_representation(options = {}) ⇒ Object

outputs the geometry in kml format : options are :id, :tesselate, :extrude, :altitude_mode. If the altitude_mode option is not present, the Z (if present) will not be output (since it won’t be used by GE anyway: clampToGround is the default)



128
129
130
131
132
133
134
135
# File 'lib/geo_ruby/simple_features/line_string.rb', line 128

def kml_representation(options = {}) #:nodoc: 

  result = "<LineString#{options[:id_attr]}>\n"
  result += options[:geom_data] if options[:geom_data]
  result += "<coordinates>"
  result += kml_poslist(options)
  result += "</coordinates>\n"
  result += "</LineString>\n"
end

#m_rangeObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/geo_ruby/simple_features/line_string.rb', line 51

def m_range
  if with_m
    max_m, min_m = -Float::MAX, Float::MAX
    each do |point|
      max_m = point.m if point.m > max_m
      min_m = point.m if point.m < min_m
    end
    [min_m,max_m]
  else
    [0,0]
  end
end

#text_geometry_typeObject

WKT geometry type



96
97
98
# File 'lib/geo_ruby/simple_features/line_string.rb', line 96

def text_geometry_type #:nodoc: 

  "LINESTRING"
end

#text_representation(allow_z = true, allow_m = true) ⇒ Object

Text representation of a line string



92
93
94
# File 'lib/geo_ruby/simple_features/line_string.rb', line 92

def text_representation(allow_z=true,allow_m=true) #:nodoc: 

  @points.collect{|point| point.text_representation(allow_z,allow_m) }.join(",") 
end