aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/chouette/access_point.rb
blob: 884460881053ed5c5274c342de8bb68fd0a4f519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require 'geokit'
require 'geo_ruby'


module Chouette
  class AccessPoint < Chouette::ActiveRecord
    has_metadata

    include Geokit::Mappable
    include ProjectionFields

    has_many :access_links, :dependent => :destroy
    belongs_to :stop_area

    attr_accessor :access_point_type
    attr_writer :coordinates

    validates_presence_of :name
    validates_presence_of :access_type

    validates_presence_of :latitude, :if => :longitude
    validates_presence_of :longitude, :if => :latitude
    validates_numericality_of :latitude, :less_than_or_equal_to => 90, :greater_than_or_equal_to => -90, :allow_nil => true
    validates_numericality_of :longitude, :less_than_or_equal_to => 180, :greater_than_or_equal_to => -180, :allow_nil => true

    validates_format_of :coordinates, :with => %r{\A *-?(0?[0-9](\.[0-9]*)?|[0-8][0-9](\.[0-9]*)?|90(\.[0]*)?) *\, *-?(0?[0-9]?[0-9](\.[0-9]*)?|1[0-7][0-9](\.[0-9]*)?|180(\.[0]*)?) *\Z}, :allow_nil => true, :allow_blank => true
    before_save :coordinates_to_lat_lng
    def self.nullable_attributes
      [:street_name, :country_code, :comment, :long_lat_type, :zip_code, :city_name]
    end

    def referential
      @referential ||= Referential.where(:slug => Apartment::Tenant.current).first!
    end

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

    def coordinates
        @coordinates || combine_lat_lng
    end

    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

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

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

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

    def position
      geometry
    end

    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

    def default_position
      stop_area.geometry or stop_area.default_position
    end


    def access_point_type
      access_type && Chouette::AccessPointType.new(access_type.underscore)
    end

    def access_point_type=(access_point_type)
      self.access_type = (access_point_type ? access_point_type.camelcase : nil)
    end

    @@access_point_types = nil
    def self.access_point_types
      @@access_point_types ||= Chouette::AccessPointType.all.select do |access_point_type|
        access_point_type.to_i >= 0
      end
    end

    def generic_access_link_matrix
      matrix = Array.new
      hash = Hash.new
      access_links.each do |link|
          hash[link.link_key] = link
      end
      key=Chouette::AccessLink.build_link_key(self,stop_area,"access_point_to_stop_area")
      if hash.has_key?(key)
        matrix << hash[key]
      else
        link = Chouette::AccessLink.new
        link.access_point = self
        link.stop_area = stop_area
        link.link_orientation_type = "access_point_to_stop_area"
        matrix << link
      end
      key=Chouette::AccessLink.build_link_key(self,stop_area,"stop_area_to_access_point")
      if hash.has_key?(key)
        matrix << hash[key]
      else
        link = Chouette::AccessLink.new
        link.access_point = self
        link.stop_area = stop_area
        link.link_orientation_type = "stop_area_to_access_point"
        matrix << link
      end
      matrix
    end

    def detail_access_link_matrix
      matrix = Array.new
      hash = Hash.new
      access_links.each do |link|
          hash[link.link_key] = link
      end
      stop_area.children_at_base.each do |child|
        key=Chouette::AccessLink.build_link_key(self,child,"access_point_to_stop_area")
        if hash.has_key?(key)
          matrix << hash[key]
        else
          link = Chouette::AccessLink.new
          link.access_point = self
          link.stop_area = child
          link.link_orientation_type = "access_point_to_stop_area"
          matrix << link
        end
        key=Chouette::AccessLink.build_link_key(self,child,"stop_area_to_access_point")
        if hash.has_key?(key)
          matrix << hash[key]
        else
          link = Chouette::AccessLink.new
          link.access_point = self
          link.stop_area = child
          link.link_orientation_type = "stop_area_to_access_point"
          matrix << link
        end
      end
      matrix
    end

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