]> git.openstreetmap.org Git - rails.git/blob - app/models/trace.rb
Improve the node/way/relation already deleted error message, and get rid bug in way...
[rails.git] / app / models / trace.rb
1 class Trace < ActiveRecord::Base
2   set_table_name 'gpx_files'
3
4   validates_presence_of :user_id, :name, :timestamp
5   validates_presence_of :description, :on => :create
6   validates_length_of :name, :maximum => 255
7   validates_length_of :description, :maximum => 255
8 #  validates_numericality_of :latitude, :longitude
9   validates_inclusion_of :public, :inserted, :in => [ true, false]
10   
11   belongs_to :user
12   has_many :tags, :class_name => 'Tracetag', :foreign_key => 'gpx_id', :dependent => :delete_all
13   has_many :points, :class_name => 'Tracepoint', :foreign_key => 'gpx_id', :dependent => :delete_all
14
15   def destroy
16     super
17     FileUtils.rm_f(trace_name)
18     FileUtils.rm_f(icon_picture_name)
19     FileUtils.rm_f(large_picture_name)
20   end
21
22   def tagstring
23     return tags.collect {|tt| tt.tag}.join(" ")
24   end
25
26   def tagstring=(s)
27     self.tags = s.split().collect {|tag|
28       tt = Tracetag.new
29       tt.tag = tag
30       tt
31     }
32   end
33   
34   def large_picture= (data)
35     f = File.new(large_picture_name, "wb")
36     f.syswrite(data)
37     f.close
38   end
39   
40   def icon_picture= (data)
41     f = File.new(icon_picture_name, "wb")
42     f.syswrite(data)
43     f.close
44   end
45
46   def large_picture
47     f = File.new(large_picture_name, "rb")
48     logger.info "large picture file: '#{f.path}', bytes: #{File.size(f.path)}"
49     data = f.sysread(File.size(f.path))
50     logger.info "have read data, bytes: '#{data.length}'"
51     f.close
52     data
53   end
54   
55   def icon_picture
56     f = File.new(icon_picture_name, "rb")
57     logger.info "icon picture file: '#{f.path}'"
58     data = f.sysread(File.size(f.path))
59     f.close
60     data
61   end
62   
63   def large_picture_name
64     "#{GPX_IMAGE_DIR}/#{id}.gif"
65   end
66
67   def icon_picture_name
68     "#{GPX_IMAGE_DIR}/#{id}_icon.gif"
69   end
70
71   def trace_name
72     "#{GPX_TRACE_DIR}/#{id}.gpx"
73   end
74
75   def mime_type
76     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
77     gzipped = filetype =~ /gzip compressed/
78     bzipped = filetype =~ /bzip2 compressed/
79     zipped = filetype =~ /Zip archive/
80
81     if gzipped then
82       mimetype = "application/x-gzip"
83     elsif bzipped then
84       mimetype = "application/x-bzip2"
85     elsif zipped
86       mimetype = "application/x-zip"
87     else
88       mimetype = "text/xml"
89     end
90
91     return mimetype
92   end
93
94   def extension_name
95     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
96     gzipped = filetype =~ /gzip compressed/
97     bzipped = filetype =~ /bzip2 compressed/
98     zipped = filetype =~ /Zip archive/
99     tarred = filetype =~ /tar archive/
100
101     if tarred and gzipped then
102       extension = ".tar.gz"
103     elsif tarred and bzipped then
104       extension = ".tar.bz2"
105     elsif tarred
106       extension = ".tar"
107     elsif gzipped
108       extension = ".gpx.gz"
109     elsif bzipped
110       extension = ".gpx.bz2"
111     elsif zipped
112       extension = ".zip"
113     else
114       extension = ".gpx"
115     end
116
117     return extension
118   end
119
120   def to_xml
121     doc = OSM::API.new.get_xml_doc
122     doc.root << to_xml_node()
123     return doc
124   end
125
126   def to_xml_node
127     el1 = XML::Node.new 'gpx_file'
128     el1['id'] = self.id.to_s
129     el1['name'] = self.name.to_s
130     el1['lat'] = self.latitude.to_s
131     el1['lon'] = self.longitude.to_s
132     el1['user'] = self.user.display_name
133     el1['public'] = self.public.to_s
134     el1['pending'] = (!self.inserted).to_s
135     el1['timestamp'] = self.timestamp.xmlschema
136     return el1
137   end
138
139   def xml_file
140     # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
141     filetype = `/usr/bin/file -bz #{trace_name}`.chomp
142     gzipped = filetype =~ /gzip compressed/
143     bzipped = filetype =~ /bzip2 compressed/
144     zipped = filetype =~ /Zip archive/
145     tarred = filetype =~ /tar archive/
146
147     if gzipped or bzipped or zipped or tarred then
148       tmpfile = Tempfile.new("trace.#{id}");
149
150       if tarred and gzipped then
151         system("tar -zxOf #{trace_name} > #{tmpfile.path}")
152       elsif tarred and bzipped then
153         system("tar -jxOf #{trace_name} > #{tmpfile.path}")
154       elsif tarred
155         system("tar -xOf #{trace_name} > #{tmpfile.path}")
156       elsif gzipped
157         system("gunzip -c #{trace_name} > #{tmpfile.path}")
158       elsif bzipped
159         system("bunzip2 -c #{trace_name} > #{tmpfile.path}")
160       elsif zipped
161         system("unzip -p #{trace_name} -x '__MACOSX/*' > #{tmpfile.path}")
162       end
163
164       tmpfile.unlink
165
166       file = tmpfile.file
167     else
168       file = File.open(trace_name)
169     end
170
171     return file
172   end
173
174   def import
175     logger.info("GPX Import importing #{name} (#{id}) from #{user.email}")
176
177     gpx = GPX::File.new(self.xml_file)
178
179     f_lat = 0
180     f_lon = 0
181     first = true
182
183     # If there are any existing points for this trace then delete
184     # them - we check for existing points first to avoid locking
185     # the table in the common case where there aren't any.
186     if Tracepoint.find(:first, :conditions => ['gpx_id = ?', self.id])
187       Tracepoint.delete_all(['gpx_id = ?', self.id])
188     end
189
190     gpx.points do |point|
191       if first
192         f_lat = point.latitude
193         f_lon = point.longitude
194         first = false
195       end
196
197       tp = Tracepoint.new
198       tp.lat = point.latitude
199       tp.lon = point.longitude
200       tp.altitude = point.altitude
201       tp.timestamp = point.timestamp
202       tp.gpx_id = id
203       tp.trackid = point.segment
204       tp.save!
205     end
206
207     if gpx.actual_points > 0
208       max_lat = Tracepoint.maximum('latitude', :conditions => ['gpx_id = ?', id])
209       min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', id])
210       max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', id])
211       min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', id])
212
213       max_lat = max_lat.to_f / 10000000
214       min_lat = min_lat.to_f / 10000000
215       max_lon = max_lon.to_f / 10000000
216       min_lon = min_lon.to_f / 10000000
217
218       self.latitude = f_lat
219       self.longitude = f_lon
220       self.large_picture = gpx.picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
221       self.icon_picture = gpx.icon(min_lat, min_lon, max_lat, max_lon)
222       self.size = gpx.actual_points
223       self.inserted = true
224       self.save!
225     end
226
227     logger.info "done trace #{id}"
228
229     return gpx
230   end
231 end