source: subversion/applications/utils/planet.osm/planet.rb @ 4432

Last change on this file since 4432 was 4432, checked in by spaetz, 12 years ago

apply kleptog's planet dump speedup patch

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1#!/usr/bin/ruby -w
2
3#$: << Dir.pwd+"/../../www.openstreetmap.org/ruby/api"
4
5require 'mysql'
6require 'time'
7require 'osm/servinfo.rb'
8require 'cgi'
9
10$mysql = Mysql.real_connect $DBSERVER, $USERNAME, $PASSWORD, $DATABASE
11
12# create a hash of entries out of a list of semi colon seperated key=value pairs
13def read_tags tag_str
14  tags_arr = tag_str.split(';').collect {|tag| tag =~ /=/ ? [$`,$'] : [tag,""] }
15  Hash[*tags_arr.flatten]
16end
17
18# create a timestamp or nil out of a time string
19def read_timestamp time_str
20  (time_str.nil? or time_str == "" or time_str == "NULL") ? Time.at(0) : Time.parse(time_str)
21end
22
23def pageSQL(lastid)
24  if lastid == 0?
25    return ""
26  else
27    return " and id > #{lastid}"
28end
29
30# yields for every node with parameter
31# id, lat, lon, timestamp, tags
32# 'tags' are a hash in format {key1=>value1, key2=>value2...}
33def all_nodes(lastid)
34  $mysql.query "select id, latitude, longitude, timestamp, tags from current_nodes where visible = 1 #{pageSQL(lastid)} order by id limit 500000" do |rows|
35    rows.each do |row|
36      yield row[0].to_i, row[1].to_f, row[2].to_f, read_timestamp(row[3]), read_tags(row[4])
37    end
38  end
39end
40
41# yields for every segment
42# id, from_id, to_id, timestamp, tags
43def all_segments(lastid)
44  $mysql.query "select id, node_a, node_b, timestamp, tags from current_segments where visible = 1 #{pageSQL(lastid)} order by id limit 500000" do |rows|
45    rows.each do |row|
46      yield row[0].to_i, row[1].to_i, row[2].to_i, read_timestamp(row[3]), read_tags(row[4])
47    end
48  end
49end
50
51# yields for every way
52# id, [id1,id2,id3...], timestamp, tags
53def all_ways(lastid)
54  $mysql.query "select id, timestamp from current_ways where visible = 1 #{pageSQL(lastid)} order by id limit 500000" do |ways|
55    ways.each do |row|
56      id = row[0].to_i
57      segs = []
58      $mysql.query "select segment_id from current_way_segments where id = #{id} order by sequence_id;" do |segments|
59        segments.each {|s| segs << s[0].to_i}
60      end
61      tags_arr = []
62      $mysql.query "select k,v from current_way_tags where id = #{id};" do |tags|
63        tags.each {|t| tags_arr << t[0] << t[1]}
64      end
65      yield id, segs, read_timestamp(row[1]), Hash[*tags_arr]
66    end
67  end
68end
69
70# output all tags in the hash
71def out_tags tags
72  tags.each {|key, value| puts %{    <tag k="#{CGI.escapeHTML(key)}" v="#{CGI.escapeHTML(value)}" />}}
73end
74
75puts '<?xml version="1.0" encoding="UTF-8"?>'
76puts '<osm version="0.3" generator="OpenStreetMap planet.rb">'
77puts '  <bound box="-90,-180,90,180" origin="http://www.openstreetmap.org/api/0.4" />'
78
79done = false
80lastid = 0
81
82while not done
83  done = true
84  all_nodes(lastid) do |id, lat, lon, timestamp, tags|
85    done = false
86    lastid = id
87    print %{  <node id="#{id}" lat="#{sprintf('%.7f', lat)}" lon="#{sprintf('%.7f', lon)}" timestamp="#{timestamp.xmlschema}"}
88    if tags.empty?
89      puts "/>"
90    else
91      puts ">"
92      out_tags tags
93      puts "  </node>"
94    end
95  end
96end
97
98done = false
99lastid = 0
100
101while not done
102  done = true
103  all_segments(lastid) do |id, from, to, timestamp, tags|
104    done = false
105    lastid = id
106    print %{  <segment id="#{id}" from="#{from}" to="#{to}" timestamp="#{timestamp.xmlschema}"}
107    if tags.empty?
108      puts "/>"
109    else
110      puts ">"
111      out_tags tags
112      puts "  </segment>"
113    end
114  end
115end
116
117done = false
118lastid = 0
119
120while not done
121  done = true
122  all_ways(lastid) do |id, segs, timestamp, tags|
123    done = false
124    lastid = id
125    print %{  <way id="#{id}" timestamp="#{timestamp.xmlschema}"}
126    if tags.empty? and segs.empty?
127      puts "/>"
128    else
129      puts ">"
130      segs.each {|seg_id| puts %{    <seg id="#{seg_id}" />}}
131      out_tags tags
132      puts "  </way>"
133    end
134  end
135end
136puts "</osm>"
Note: See TracBrowser for help on using the repository browser.