aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/referential.rb
blob: 608b0c8fcd011f1dc0140e31a9405d19c9b5d216 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# -*- coding: utf-8 -*-
class Referential < ActiveRecord::Base
  validates_presence_of :name 
  validates_presence_of :slug
  validates_presence_of :prefix
  validates_presence_of :time_zone
  validates_presence_of :upper_corner
  validates_presence_of :lower_corner
  validates_uniqueness_of :slug
  validates_uniqueness_of :name
  validates_format_of :slug, :with => %r{\A[a-z][0-9a-z_]+\Z}
  validates_format_of :prefix, :with => %r{\A[0-9a-zA-Z_]+\Z}
  validates_format_of :upper_corner, :with => %r{\A-?[0-9]+\.?[0-9]*\,-?[0-9]+\.?[0-9]*\Z}
  validates_format_of :lower_corner, :with => %r{\A-?[0-9]+\.?[0-9]*\,-?[0-9]+\.?[0-9]*\Z}
  validate :slug_excluded_values
  
  attr_accessor :resources
  attr_accessor :upper_corner
  attr_accessor :lower_corner

  has_many :imports, :dependent => :destroy
  has_many :exports, :dependent => :destroy
  has_many :api_keys, :class_name => 'Api::V1::ApiKey', :dependent => :destroy
  
  belongs_to :organisation
  validates_presence_of :organisation

  def slug_excluded_values
    if ! slug.nil?
      if slug.start_with? "pg_" 
        errors.add(:slug,I18n.t("referentials.errors.pg_excluded"))
      end
      if slug == 'public'
        errors.add(:slug,I18n.t("referentials.errors.public_excluded"))
      end
    end
  end

  def human_attribute_name(*args)
    self.class.human_attribute_name(*args)
  end

  def lines
    Chouette::Line.scoped
  end

  def networks
    Chouette::Network.scoped
  end

  def group_of_lines
    Chouette::GroupOfLine.scoped
  end

  def companies
    Chouette::Company.scoped
  end

  def stop_areas
    Chouette::StopArea.scoped
  end

  def access_points
    Chouette::AccessPoint.scoped
  end

  def access_links
    Chouette::AccessLink.scoped
  end

  def time_tables
    Chouette::TimeTable.scoped
  end
  
  def connection_links
    Chouette::ConnectionLink.scoped
  end

  def vehicle_journeys
    Chouette::VehicleJourney.scoped
  end

  after_initialize :define_default_attributes

  def define_default_attributes
    self.time_zone ||= Time.zone.name
  end

  def switch
    raise "Referential not created" if new_record?
    Apartment::Database.switch(slug)
    self
  end

  def self.available_srids
    [
     [ "NTF Lambert Zone 1 (27561)", 27561 ],
     [ "NTF Lambert Zone 2 (27562)", 27562 ],
     [ "NTF Lambert Zone 3 (27563)", 27563 ],
     [ "NTF Lambert Zone 4 (27564)", 27564 ],
     [ "NTF Lambert 1 (27571)", 27571 ],
     [ "NTF Lambert 2 étendu (27572)", 27572 ],
     [ "NTF Lambert 3 (27573)", 27573 ],
     [ "NTF Lambert 4 (27574)", 27574 ],
     [ "RGF 93 Lambert 93 (2154)", 2154 ]
    ]
  end

  def projection_type_label 
    self.class.available_srids.each do |a|
      if a.last.to_s == projection_type
        return a.first.split('(').first.rstrip
      end
    end
    projection_type || ""
  end

  before_create :create_schema
  def create_schema
    Apartment::Database.create slug
  end

  before_destroy :destroy_schema
  def destroy_schema
    Apartment::Database.drop slug
  end

  after_create :import_resources  
  def import_resources
    imports.create(:resources => resources) if resources
  end

  def upper_corner
    envelope.upper_corner
  end

  def upper_corner=(upper_corner)
    if String === upper_corner
      upper_corner = (upper_corner.blank? ? nil : GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.normalize(upper_corner), 4326))
    end

    envelope.tap do |envelope|
      envelope.upper_corner = upper_corner
      self.bounds = envelope.to_polygon.as_ewkt
    end
  end

  def lower_corner    
    envelope.lower_corner
  end

  def lower_corner=(lower_corner)
    if String === lower_corner
      lower_corner = (lower_corner.blank? ? nil : GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.normalize(lower_corner), 4326))
    end

    envelope.tap do |envelope|
      envelope.lower_corner = lower_corner
      self.bounds = envelope.to_polygon.as_ewkt
    end
  end

  def default_bounds
    GeoRuby::SimpleFeatures::Envelope.from_coordinates( [ [-5.2, 42.25], [8.23, 51.1] ] ).to_polygon.as_ewkt
  end

  def envelope
    bounds = read_attribute(:bounds)
    GeoRuby::SimpleFeatures::Geometry.from_ewkt(bounds.present? ? bounds : default_bounds ).envelope
  end

end

Rails.application.config.after_initialize do
  
  Chouette::TridentActiveRecord

  class Chouette::TridentActiveRecord

    # add referential relationship for objectid and localization functions
    def referential
      @referential ||= Referential.where(:slug => Apartment::Database.current_database).first!
    end

    # override prefix for good prefix in objectid generation
    def prefix
      self.referential.prefix
    end

  end
  
  Chouette::StopArea
  
  class Chouette::StopArea
    
    attr_accessible :projection_x,:projection_y
    
    # override default_position method to add referential envelope when no stoparea is positioned
    def default_position 
      # for first StopArea ... the bounds is nil , set to referential center 
      Chouette::StopArea.bounds ? Chouette::StopArea.bounds.center : self.referential.envelope.center
    end
    
    # add projection_type set on pre-insert and pre_update action
    before_validation :set_projections
    def set_projections
      if ! self.latitude.nil? && ! self.longitude.nil?
        self.long_lat_type = 'WGS84'
      else
        self.long_lat_type = nil
      end
#      if ! self.referential.projection_type.nil? && !self.referential.projection_type.empty?
#        if ! self.x.nil?  && ! self.y.nil?
#          self.projection_type = referential.projection_type_label
#        else
#          self.projection_type = nil
#        end
#      else
#          self.projection_type = nil
#          self.x = nil
#          self.y = nil
#      end
    end
    
    def projection
      if self.referential.projection_type.nil? || self.referential.projection_type.empty? 
        nil
      else
        self.referential.projection_type
      end
    end
    @point = nil

    def projection_x
      if self.long_lat_type.nil? || self.projection.nil?
        nil
      else
        @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i)
        
        @point.x
      end
    end
    def projection_y
      if self.long_lat_type.nil? || self.projection.nil?
        nil
      else
        @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i)
        @point.y
      end
    end
    def projection_x=(dummy)
      # dummy method
    end
    def projection_y=(dummy)
      # dummy method
    end
 
  end

  Chouette::AccessPoint
  
  class Chouette::AccessPoint
    attr_accessible :projection_x,:projection_y

    # add projection_type set on pre-insert and pre_update action
    before_validation :set_projections
    def set_projections
      if ! self.latitude.nil? && ! self.longitude.nil?
        self.long_lat_type = 'WGS84'
      else
        self.long_lat_type = nil
      end
#      if ! self.referential.projection_type.nil? && !self.referential.projection_type.empty?
#        if ! self.x.nil?  && ! self.y.nil?
#          self.projection_type = referential.projection_type_label
#        else
#          self.projection_type = nil
#        end
#      else
#          self.projection_type = nil
#          self.x = nil
#          self.y = nil
#      end
    end
    
    def projection
      if self.referential.projection_type.nil? || self.referential.projection_type.empty? 
        nil
      else
        self.referential.projection_type
      end
    end
    @point = nil

    def projection_x
      if self.long_lat_type.nil? || self.projection.nil?
        nil
      else
        @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i)
        
        @point.x
      end
    end
    def projection_y
      if self.long_lat_type.nil? || self.projection.nil?
        nil
      else
        @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i)
        @point.y
      end
    end
    def projection_x=(dummy)
      # dummy method
    end
    def projection_y=(dummy)
      # dummy method
    end
  end
 
end