]> git.openstreetmap.org Git - rails.git/commitdiff
updates from hacking day
authorSteve Coast <steve@asklater.com>
Sun, 6 May 2007 10:08:00 +0000 (10:08 +0000)
committerSteve Coast <steve@asklater.com>
Sun, 6 May 2007 10:08:00 +0000 (10:08 +0000)
12 files changed:
app/controllers/api_controller.rb
app/models/node.rb
app/models/segment.rb
app/models/tracepoint.rb
app/models/user.rb
app/models/way.rb
app/views/site/index.rhtml
app/views/user/view.rhtml
config/database.yml
config/environment.rb
lib/daemons/gpx_import.rb
lib/osm.rb

index 7451a91b9b347bc113731f553eb437fcac380196..068e31d2ab63b1d6d2a57de8016074cc467e6440 100644 (file)
@@ -55,7 +55,9 @@ class ApiController < ApplicationController
 
     node_ids = nodes.collect {|node| node.id }
 
-    # (in the future, we may wish to abort here if we found too many nodes)
+    if node_ids.length > 50_000
+       report_error("You requested too many nodes (limit is 50,000). Either request a smaller area, or use planet.osm")
+    end
 
     # grab the segments
     segments = Array.new
@@ -85,18 +87,54 @@ class ApiController < ApplicationController
       way_segments = WaySegment.find_all_by_segment_id(segment_ids)
       way_ids = way_segments.collect {|way_segment| way_segment.id }
       ways = Way.find(way_ids) # NB: doesn't pick up segments, tags from db until accessed via way.way_segments etc.
+
+     # seg_ids = way_segments.collect {|way_segment| way_segment.segment_id }
+      
+      list_of_way_segs = ways.collect {|way| way.way_segments}
+      list_of_way_segs.flatten!
+
+      list_of_way_segments = list_of_way_segs.collect { |way_seg| way_seg.segment_id }
+
     end
 
+    segments_to_fetch = list_of_way_segments.uniq - segment_ids
+
+    if segments_to_fetch.length > 0
+      segments += Segment.find(segments_to_fetch)
+    end
+    
+    # get more nodes
+    #
+
+    segments_nodes = segments.collect {|segment| segment.node_a }
+    segments_nodes += segments.collect {|segment| segment.node_b }
+
+    node_ids_a = nodes.collect {|node| node.id }
+
+    nodes_to_get = segments_nodes - node_ids_a
+    nodes += Node.find(nodes_to_get) if nodes_to_get.length > 0
+
+    visible_nodes = {}
+    user_display_name_cache = {}
+
     nodes.each do |node|
-      doc.root << node.to_xml_node()
+      if node.visible?
+        doc.root << node.to_xml_node(user_display_name_cache)
+        visible_nodes[node.id] = node
+      end
     end
 
+    visible_segments = {}
+
     segments.each do |segment|
-      doc.root << segment.to_xml_node()
-    end 
+      if visible_nodes[segment.node_a] and visible_nodes[segment.node_b] and segment.visible?
+        doc.root << segment.to_xml_node(user_display_name_cache) 
+        visible_segments[segment.id] = segment
+      end
+    end
 
     ways.each do |way|
-      doc.root << way.to_xml_node()
+      doc.root << way.to_xml_node(visible_segments, user_display_name_cache) if way.visible?
     end 
 
     render :text => doc.to_s
index 1288503b92b3d34ca617b67984c755e03369ee71..6f01ccee04a17af1613288cb4fb4a107ec1efcf6 100644 (file)
@@ -78,12 +78,22 @@ class Node < ActiveRecord::Base
     return doc
   end
 
-  def to_xml_node
+  def to_xml_node(user_display_name_cache = nil)
     el1 = XML::Node.new 'node'
     el1['id'] = self.id.to_s
     el1['lat'] = self.latitude.to_s
     el1['lon'] = self.longitude.to_s
-    el1['user'] = self.user.display_name if self.user.data_public?
+
+    # el['user'] = self.user.display_name if self.user.data_public?
+
+    if user_display_name_cache and user_display_name_cache[self.user_id]
+      # use the cache if available
+    else
+      user_display_name_cache[self.user_id] = self.user.display_name
+    end
+
+    el1['user'] = user_display_name_cache[self.user_id]
+
     Node.split_tags(el1, self.tags)
     el1['visible'] = self.visible.to_s
     el1['timestamp'] = self.timestamp.xmlschema
index 6a07e8cb1dad8d0f96a25d0331b4f77948c7584c..b6f60ec3f9dd9d2c181d229ff85c958fb2e15f31 100644 (file)
@@ -73,12 +73,20 @@ class Segment < ActiveRecord::Base
     return doc
   end
 
-  def to_xml_node
+  def to_xml_node(user_display_name_cache  = nil)
     el1 = XML::Node.new 'segment'
     el1['id'] = self.id.to_s
     el1['from'] = self.node_a.to_s
     el1['to'] = self.node_b.to_s
-    el1['user'] = self.user.display_name if self.user.data_public?
+    if user_display_name_cache and user_display_name_cache[self.user_id]
+      # use the cache if available
+    else
+      user_display_name_cache[self.user_id] = self.user.display_name
+    end
+    
+    #el1['user'] = self.user.display_name if self.user.data_public?
+    el1['user'] = user_display_name_cache[self.user_id]
+
     Segment.split_tags(el1, self.tags)
     el1['visible'] = self.visible.to_s
     el1['timestamp'] = self.timestamp.xmlschema
index dd75ce0e95f104f96d831ca5880e55cf66d96640..90de368b551a27210973059f2893e4b9aaeb7aa3 100644 (file)
@@ -1,9 +1,25 @@
 class Tracepoint < ActiveRecord::Base
-  set_table_name 'gps_points'
+set_table_name 'gps_points'
 
 #  validates_numericality_of :latitude
 #  validates_numericality_of :longitude
 
   belongs_to :user
   belongs_to :trace, :foreign_key => 'gpx_id'
+
+  def lat=(l)
+    self.latitude = l * 1000000
+  end
+
+  def lng=(l)
+    self.longitude = l * 1000000
+  end
+
+  def lat
+    return self.latitude.to_f / 1000000
+  end
+
+  def lon
+    return self.longitude.to_f / 1000000
+  end
 end
index f72fd5bfadd59a1aa025ba0e368b97dc0044e5f9..c64fcf21b93b66a2b1f915c1f2c77bad607482a4 100644 (file)
@@ -28,8 +28,8 @@ class User < ActiveRecord::Base
     write_attribute("pass_crypt_confirm", Digest::MD5.hexdigest(str)) 
   end
 
-  def self.authenticate(email, passwd) \r
-    find(:first, :conditions => [ "email = ? AND pass_crypt = ?", email, Digest::MD5.hexdigest(passwd)])\r
+  def self.authenticate(email, passwd)
+    find(:first, :conditions => [ "email = ? AND pass_crypt = ? AND active = true", email, Digest::MD5.hexdigest(passwd)])
   end 
 
   def self.authenticate_token(token) 
index b7d0ec118f92bf3c4e5e239173e7179d842527d3..cd06b121035cf5e89a67e8531152fc0b51ffa39b 100644 (file)
@@ -50,22 +50,44 @@ class Way < ActiveRecord::Base
     return doc
   end
 
-  def to_xml_node
+  def to_xml_node(visible_segments = nil, user_display_name_cache = nil)
     el1 = XML::Node.new 'way'
     el1['id'] = self.id.to_s
     el1['visible'] = self.visible.to_s
     el1['timestamp'] = self.timestamp.xmlschema
-    el1['user'] = self.user.display_name if self.user.data_public?
-    # make sure segments are output in sequence_id order\r
-    ordered_segments = []\r
-    self.way_segments.each do |seg| \r
-      ordered_segments[seg.sequence_id] = seg.segment_id.to_s
-    end\r
-    ordered_segments.each do |seg_id|\r
-      e = XML::Node.new 'seg'\r
-      e['id'] = seg_id\r
-      el1 << e\r
-    end\r
+
+    if user_display_name_cache and user_display_name_cache[self.user_id]
+      # use the cache if available
+    else
+      user_display_name_cache[self.user_id] = self.user.display_name
+    end
+    
+    #el1['user'] = self.user.display_name if self.user.data_public?
+    el1['user'] = user_display_name_cache[self.user_id]
+
+    # make sure segments are output in sequence_id order
+    ordered_segments = []
+    self.way_segments.each do |seg|
+      if visible_segments
+        # if there is a list of visible segments then use that to weed out deleted segments
+        if visible_segments[seg.segment_id]
+          ordered_segments[seg.sequence_id] = seg.segment_id.to_s
+        end
+      else
+        # otherwise, manually go to the db to check things
+        if seg.segment.visible? and seg.segment.from_node.visible? and seg.segment.to_node.visible?
+          ordered_segments[seg.sequence_id] = seg.segment_id.to_s
+        end
+      end
+    end
+
+    ordered_segments.each do |seg_id|
+      if seg_id
+        e = XML::Node.new 'seg'
+        e['id'] = seg_id
+        el1 << e
+      end
+    end
  
     self.way_tags.each do |tag|
       e = XML::Node.new 'tag'
@@ -76,7 +98,6 @@ class Way < ActiveRecord::Base
     return el1
   end 
 
-
   def segs
     unless @segs
         @segs = Array.new
index bed35e36f1386167604840b28b7abfc9ba8be878..403f552d1eeb1aaf7eb007b6898f988bbd58d049 100644 (file)
@@ -75,7 +75,7 @@
   function init(){
 
     OpenLayers.Util.onImageLoadError = function() {
-      this.src = "http://www.openstreetmap.org/javascript/img/404.png";
+      this.src = "http://www.openstreetmap.org/javascripts/img/404.png";
     }
     map = new OpenLayers.Map( "map", 
         {maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), maxZoomLevel:18, maxResolution:156543, units:'meters', projection: "EPSG:41001"} );
index 78016e639edba0294780eef2ac2cf2cc793643e6..2328fb39a1eb1cc129e438b2b572d84ed6bc103b 100644 (file)
@@ -62,8 +62,6 @@ Go to <%= link_to 'your account page', :controller => 'user', :action => 'accoun
 <% end %>
 <br />
 <br />
-<%= link_to 'diary', :controller => 'user', :action => 'diary', :display_name => @this_user.display_name %><br /><br />
-
 
 <p><%= params[:display_name] %> says:  </p>
 <%= simple_format(@this_user.description) %>
index 1fe4d9880030befcca52870dee65ee62f69eb3ed..a972b44b376dc1123daa720d669a0f9b35bc53b6 100644 (file)
@@ -15,7 +15,7 @@ development:
   database: openstreetmap
   username: openstreetmap
   password: openstreetmap
-  host: localhost
+  host: 128.40.58.203
 
 # Warning: The database defined as 'test' will be erased and
 # re-generated from your development database when you run 'rake'.
@@ -32,4 +32,4 @@ production:
   database: openstreetmap
   username: openstreetmap
   password: openstreetmap
-  host: db.openstreetmap.org
+  host: 128.40.58.203
index ac50d21f734c71b86d1ab5644e03fcd2b95f4d52..711d2ff8d1975001acbcc82e3e5e92677ca3152f 100644 (file)
@@ -2,7 +2,7 @@
 
 # Uncomment below to force Rails into production mode when 
 # you don't control web/app server and can't set it the proper way
-ENV['RAILS_ENV'] ||= 'production'
+ENV['RAILS_ENV'] ||= 'production'
 
 # Specifies gem version of Rails to use when vendor/rails is not present
 RAILS_GEM_VERSION = '1.2.3'
index 42e642b8bc30831c87bde9b3765a31eb6e27b3f9..9e7e906c846ca6ff1abafc0ba68ea39301a8c8a1 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env ruby
 
 #You might want to change this
-ENV["RAILS_ENV"] ||= "development"
+#ENV["RAILS_ENV"] ||= "development"
 
 require File.dirname(__FILE__) + "/../../config/environment"
 
@@ -16,13 +16,13 @@ while($running) do
   
   ActiveRecord::Base.logger.info("GPX Import daemon wake @ #{Time.now}.")
 
-  traces = Trace.find(:all, :conditions => ['inserted = ?', false])
+  traces = Trace.find(:all) #, :conditions => ['inserted = ?', false])
 
   if traces and traces.length > 0
     traces.each do |trace|
       begin
 
-        logger.info("GPX Import importing #{trace.name} from #{trace.user.email}")
+        logger.info("GPX Import importing #{trace.name} (#{trace.id}) from #{trace.user.email}")
 \r
         # TODO *nix specific, could do to work on windows... would be functionally inferior though - check for '.gz'
         gzipped = `file -b /tmp/#{trace.id}.gpx`.chomp =~/^gzip/\r
@@ -32,10 +32,11 @@ while($running) do
         else
           logger.info("not gzipped")
         end
-        gpx = OSM::GPXImporter.new("/tmp/#{trace.id}.gpx")
+
+        gpx = OSM::GPXImporter.new("/home/osm/gpx/#{trace.id}.gpx")
 
         f_lat = 0
-        l_lon = 0
+        f_lon = 0
         first = true
 
         gpx.points do |point|
@@ -45,12 +46,12 @@ while($running) do
           end
 
           tp = Tracepoint.new
-          tp.latitude = point['latitude']
-          tp.longitude = point['longitude']
-          tp.altitude = point['altitude']
+          tp.lat = point['latitude'].to_f
+          tp.lng = point['longitude'].to_f
+          tp.altitude = point['altitude'].to_f
           tp.user_id = trace.user.id
           tp.gpx_id = trace.id
-          tp.trackid = point['segment']
+          tp.trackid = point['segment'].to_i
           tp.save!
         end
 
@@ -59,7 +60,12 @@ while($running) do
           min_lat = Tracepoint.minimum('latitude', :conditions => ['gpx_id = ?', trace.id])
           max_lon = Tracepoint.maximum('longitude', :conditions => ['gpx_id = ?', trace.id])
           min_lon = Tracepoint.minimum('longitude', :conditions => ['gpx_id = ?', trace.id])
-
+          
+          max_lat = max_lat.to_f / 1000000
+          min_lat = min_lat.to_f / 1000000
+          max_lon = max_lon.to_f / 1000000
+          min_lon = min_lon.to_f / 1000000
           trace.latitude = f_lat
           trace.longitude = f_lon
           trace.large_picture = gpx.get_picture(min_lat, min_lon, max_lat, max_lon, gpx.actual_points)
@@ -67,15 +73,19 @@ while($running) do
           trace.size = gpx.actual_points
           trace.inserted = true
           trace.save
-          Notifier::deliver_gpx_success(trace, gpx.possible_points)
+
+          logger.info "done trace #{trace.id} -------------------------------------------------------------------------------"
+    #      Notifier::deliver_gpx_success(trace, gpx.possible_points)
         else
           #trace.destroy
-          Notifier::deliver_gpx_failure(trace, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
+#          Notifier::deliver_gpx_failure(trace, '0 points parsed ok. Do they all have lat,lng,alt,timestamp?')
         end
 
       rescue Exception => ex
+        logger.info ex
+        ex.backtrace.each {|l| logger.info l }
         #trace.destroy
-        Notifier::deliver_gpx_failure(trace, ex.to_s + ex.backtrace.join("\n") )
#       Notifier::deliver_gpx_failure(trace, ex.to_s + ex.backtrace.join("\n") )
       end
     end
   end
index adf34684f09a6d08e43bf8bac3c54df85319c9fc..9e2ab04ada1cc752c39f739d5763a40a5d7b271a 100644 (file)
@@ -125,6 +125,7 @@ module OSM
     end
 
     def get_picture(min_lat, min_lon, max_lat, max_lon, num_points)
+      #puts "getting picfor bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
       frames = 10
       width = 250
       height = 250
@@ -157,13 +158,14 @@ module OSM
           images[n].stroke_width(1)
           images[n].stroke('#BBBBBB')
           images[n].fill('#BBBBBB')
+        #  puts "A #{px},#{py} - #{oldpx},#{oldpy}"
           images[n].line(px, py, oldpx, oldpy ) unless first
         end
         images[mm].stroke_width(3)
         images[mm].stroke('#000000')
         images[mm].fill('#000000')
         images[mm].line(px, py, oldpx, oldpy ) unless first
-
+      #  puts "B #{px},#{py} - #{oldpx},#{oldpy}"
         m +=1
         if m > num_points.to_f / frames.to_f * (mm+1)
           mm += 1
@@ -193,6 +195,7 @@ module OSM
     end
 
     def get_icon(min_lat, min_lon, max_lat, max_lon)
+      puts "getting icon for bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
       width = 50
       height = 50
       rat= Math.cos( ((max_lat + min_lat)/2.0) /  180.0 * 3.141592)
@@ -216,6 +219,7 @@ module OSM
         px = proj.x(p['longitude'])
         py = proj.y(p['latitude'])
         gc.line(px, py, oldpx, oldpy ) unless first
+       # puts "C #{px},#{py} - #{oldpx},#{oldpy}"
         first = false
         oldpy = py
         oldpx = px