Class: Chouette::StopArea

Inherits:
TridentActiveRecord show all
Includes:
Geokit::Mappable
Defined in:
app/models/chouette/stop_area.rb

Constant Summary collapse

@@stop_area_types =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TridentActiveRecord

#build_objectid, #clean_object_id, #default_values, #fix_uniq_objectid, model_name, object_id_key, #objectid, #objectid_format_compliance, #prefix, #prepare_auto_columns, #reset_auto_columns, #timestamp_attributes_for_create, #timestamp_attributes_for_update, #uniq_objectid

Methods inherited from ActiveRecord

#human_attribute_name, #nil_if_blank

Instance Attribute Details

#children_idsObject

Returns the value of attribute children_ids.



17
18
19
# File 'app/models/chouette/stop_area.rb', line 17

def children_ids
  @children_ids
end

#coordinatesObject



57
58
59
# File 'app/models/chouette/stop_area.rb', line 57

def coordinates
    @coordinates || combine_lat_lng
end

#stop_area_typeObject

Returns the value of attribute stop_area_type.



16
17
18
# File 'app/models/chouette/stop_area.rb', line 16

def stop_area_type
  @stop_area_type
end

Class Method Details

.boundsObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/models/chouette/stop_area.rb', line 172

def self.bounds
  # Give something like :
  # [["113.5292500000000000", "22.1127580000000000", "113.5819330000000000", "22.2157050000000000"]]
  min_and_max = connection.select_rows("select min(longitude) as min_lon, min(latitude) as min_lat, max(longitude) as max_lon, max(latitude) as max_lat from #{table_name} where latitude is not null and longitude is not null").first
  return nil unless min_and_max

  # Ignore [nil, nil, nil, nil]
  min_and_max.compact!
  return nil unless min_and_max.size == 4

  min_and_max.collect! { |n| n.to_f }

  # We need something like :
  # [[113.5292500000000000, 22.1127580000000000], [113.5819330000000000, 22.2157050000000000]]
  coordinates = min_and_max.each_slice(2).to_a
  GeoRuby::SimpleFeatures::Envelope.from_coordinates coordinates
end

.commercialObject



118
119
120
# File 'app/models/chouette/stop_area.rb', line 118

def self.commercial
  where :area_type => "CommercialStopPoint"
end

.default_geometry!Object



246
247
248
249
250
251
252
253
254
# File 'app/models/chouette/stop_area.rb', line 246

def self.default_geometry!
  count = 0
  where(nil).find_each do |stop_area|
    Chouette::StopArea.unscoped do
      count += 1 if stop_area.default_geometry!
    end
  end
  count
end

.itlObject



130
131
132
# File 'app/models/chouette/stop_area.rb', line 130

def self.itl
  where :area_type => "ITL"
end

.near(origin, distance = 0.3) ⇒ Object



163
164
165
166
167
168
169
170
# File 'app/models/chouette/stop_area.rb', line 163

def self.near(origin, distance = 0.3)
  origin = origin.to_lat_lng

  lat_degree_units = units_per_latitude_degree(:kms)
  lng_degree_units = units_per_longitude_degree(origin.lat, :kms)

  where "SQRT(POW(#{lat_degree_units}*(#{origin.lat}-latitude),2)+POW(#{lng_degree_units}*(#{origin.lng}-longitude),2)) <= #{distance}"
end

.nullable_attributesObject



40
41
42
43
# File 'app/models/chouette/stop_area.rb', line 40

def self.nullable_attributes
  [:registration_number, :street_name, :country_code, :fare_code,
   :nearest_topic_name, :comment, :long_lat_type, :zip_code, :city_name, :url, :time_zone]
end

.physicalObject



126
127
128
# File 'app/models/chouette/stop_area.rb', line 126

def self.physical
  where :area_type => [ "BoardingPosition", "Quay" ]
end

.stop_area_typesObject



202
203
204
205
206
# File 'app/models/chouette/stop_area.rb', line 202

def self.stop_area_types
  @@stop_area_types ||= Chouette::AreaType.all.select do |stop_area_type|
    stop_area_type.to_i >= 0
  end
end

.stop_placeObject



122
123
124
# File 'app/models/chouette/stop_area.rb', line 122

def self.stop_place
  where :area_type => "StopPlace"
end

.with_geometryObject



242
243
244
# File 'app/models/chouette/stop_area.rb', line 242

def self.with_geometry
  where("latitude is not null and longitude is not null")
end

.without_geometryObject



238
239
240
# File 'app/models/chouette/stop_area.rb', line 238

def self.without_geometry
  where("latitude is null or longitude is null")
end

Instance Method Details

#children_at_baseObject



282
283
284
285
286
287
288
289
290
# File 'app/models/chouette/stop_area.rb', line 282

def children_at_base
  list = Array.new
  children_in_depth.each do |child|
    if child.area_type == 'Quay' || child.area_type == 'BoardingPosition'
      list << child
    end
  end
  list
end

#children_in_depthObject



74
75
76
77
78
79
80
# File 'app/models/chouette/stop_area.rb', line 74

def children_in_depth
  return [] if self.children.empty?

  self.children + self.children.map do |child|
    child.children_in_depth
  end.flatten.compact
end


301
302
303
304
305
306
307
308
309
310
311
# File 'app/models/chouette/stop_area.rb', line 301

def clean_invalid_access_links
  stop_parents = parents
  access_links.each do |link|
    unless stop_parents.include? link.access_point.stop_area
      link.delete
    end
  end
  children.each do |child|
    child.clean_invalid_access_links
  end
end

#combine_lat_lngObject



49
50
51
52
53
54
55
# File 'app/models/chouette/stop_area.rb', line 49

def combine_lat_lng
  if self.latitude.nil? || self.longitude.nil?
    ""
  else
    self.latitude.to_s+","+self.longitude.to_s
  end
end

#coordinates_to_lat_lngObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/chouette/stop_area.rb', line 61

def coordinates_to_lat_lng
  if ! @coordinates.nil?
    if @coordinates.empty?
      self.latitude = nil
      self.longitude = nil
    else
      self.latitude = BigDecimal.new(@coordinates.split(",").first)
      self.longitude = BigDecimal.new(@coordinates.split(",").last)
    end
    @coordinates = nil
  end
end

#default_geometryObject



261
262
263
264
# File 'app/models/chouette/stop_area.rb', line 261

def default_geometry
  children_geometries = children.with_geometry.map(&:geometry).uniq
  GeoRuby::SimpleFeatures::Point.centroid children_geometries if children_geometries.present?
end

#default_geometry!Object



256
257
258
259
# File 'app/models/chouette/stop_area.rb', line 256

def default_geometry!
  new_geometry = default_geometry
  update_attribute :geometry, new_geometry if new_geometry
end

#default_positionObject



158
159
160
161
# File 'app/models/chouette/stop_area.rb', line 158

def default_position
  # for first StopArea ... the bounds is nil :(
  Chouette::StopArea.bounds and Chouette::StopArea.bounds.center
end


274
275
276
277
278
279
280
# File 'app/models/chouette/stop_area.rb', line 274

def detail_access_link_matrix
   matrix = Array.new
   access_points.each do |access_point|
     matrix += access_point.detail_access_link_matrix
   end
   matrix
end

#duplicateObject



313
314
315
316
317
318
# File 'app/models/chouette/stop_area.rb', line 313

def duplicate
  sa = self.deep_clone :except => [:object_version, :parent_id, :registration_number]
  sa.uniq_objectid
  sa.name = I18n.t("activerecord.copy", :name => self.name)
  sa
end


266
267
268
269
270
271
272
# File 'app/models/chouette/stop_area.rb', line 266

def generic_access_link_matrix
   matrix = Array.new
   access_points.each do |access_point|
     matrix += access_point.generic_access_link_matrix
   end
   matrix
end

#geometryObject



138
139
140
# File 'app/models/chouette/stop_area.rb', line 138

def geometry
  GeoRuby::SimpleFeatures::Point.from_lon_lat(longitude, latitude, 4326) if latitude and longitude
end

#geometry=(geometry) ⇒ Object



142
143
144
145
# File 'app/models/chouette/stop_area.rb', line 142

def geometry=(geometry)
  geometry = geometry.to_wgs84
  self.latitude, self.longitude, self.long_lat_type = geometry.lat, geometry.lng, "WGS84"
end

#geometry_presenterObject



102
103
104
# File 'app/models/chouette/stop_area.rb', line 102

def geometry_presenter
  Chouette::Geometry::StopAreaPresenter.new self
end

#linesObject



106
107
108
109
110
111
112
# File 'app/models/chouette/stop_area.rb', line 106

def lines
  if (area_type == 'CommercialStopPoint')
    self.children.collect(&:stop_points).flatten.collect(&:route).flatten.collect(&:line).flatten.uniq
  else
    self.stop_points.collect(&:route).flatten.collect(&:line).flatten.uniq
  end
end

#parentsObject



292
293
294
295
296
297
298
299
# File 'app/models/chouette/stop_area.rb', line 292

def parents
  list = Array.new
  if !parent.nil?
    list << parent
    list += parent.parents
  end
  list
end

#positionObject



147
148
149
# File 'app/models/chouette/stop_area.rb', line 147

def position
  geometry
end

#position=(position) ⇒ Object



151
152
153
154
155
156
# File 'app/models/chouette/stop_area.rb', line 151

def position=(position)
  position = nil if String === position && position == ""
  position = Geokit::LatLng.normalize(position), 4326 if String === position
  self.latitude = position.lat
  self.longitude = position.lng
end

#possible_childrenObject



82
83
84
85
86
87
88
89
90
91
# File 'app/models/chouette/stop_area.rb', line 82

def possible_children
  case area_type
    when "BoardingPosition" then []
    when "Quay" then []
    when "CommercialStopPoint" then Chouette::StopArea.where(:area_type => ['Quay', 'BoardingPosition']) - [self]
    when "StopPlace" then Chouette::StopArea.where(:area_type => ['StopPlace', 'CommercialStopPoint']) - [self]
    when "ITL" then Chouette::StopArea.where(:area_type => ['Quay', 'BoardingPosition', 'StopPlace', 'CommercialStopPoint'])
  end

end

#possible_parentsObject



93
94
95
96
97
98
99
100
# File 'app/models/chouette/stop_area.rb', line 93

def possible_parents
  case area_type
    when "BoardingPosition" then Chouette::StopArea.where(:area_type => "CommercialStopPoint")  - [self]
    when "Quay" then Chouette::StopArea.where(:area_type => "CommercialStopPoint") - [self]
    when "CommercialStopPoint" then Chouette::StopArea.where(:area_type => "StopPlace") - [self]
    when "StopPlace" then Chouette::StopArea.where(:area_type => "StopPlace") - [self]
  end
end

#routesObject



114
115
116
# File 'app/models/chouette/stop_area.rb', line 114

def routes
  self.stop_points.collect(&:route).flatten.uniq
end

#routing_line_ids=(routing_line_ids) ⇒ Object



230
231
232
233
234
235
236
# File 'app/models/chouette/stop_area.rb', line 230

def routing_line_ids=(routing_line_ids)
  lines = routing_line_ids.split(',').uniq
  self.routing_lines.clear
  Chouette::Line.find(lines).each do |line|
    self.routing_lines << line
  end
end

#routing_stop_ids=(routing_stop_ids) ⇒ Object



222
223
224
225
226
227
228
# File 'app/models/chouette/stop_area.rb', line 222

def routing_stop_ids=(routing_stop_ids)
  stops = routing_stop_ids.split(',').uniq
  self.routing_stops.clear
  Chouette::StopArea.find(stops).each do |stop|
    self.routing_stops << stop
  end
end

#to_lat_lngObject



134
135
136
# File 'app/models/chouette/stop_area.rb', line 134

def to_lat_lng
  Geokit::LatLng.new(latitude, longitude) if latitude and longitude
end