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

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

Reverting the sprintf's. It seems to slow planet generation down a lot. Disadvantage is that small longitudes are of format 5E-3 again

  • Property svn:executable set to *
File size: 3.4 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(page)
24  return " limit #{page * 500000}, 500000"
25end
26
27# yields for every node with parameter
28# id, lat, lon, timestamp, tags
29# 'tags' are a hash in format {key1=>value1, key2=>value2...}
30def all_nodes(page)
31  $mysql.query "select id, latitude, longitude, timestamp, tags from current_nodes where visible = 1 order by id #{pageSQL(page)}" do |rows|
32    rows.each do |row|
33      yield row[0].to_i, row[1].to_f, row[2].to_f, read_timestamp(row[3]), read_tags(row[4])
34    end
35  end
36end
37
38# yields for every segment
39# id, from_id, to_id, timestamp, tags
40def all_segments(page)
41  $mysql.query "select id, node_a, node_b, timestamp, tags from current_segments where visible = 1 order by id #{pageSQL(page)}" do |rows|
42    rows.each do |row|
43      yield row[0].to_i, row[1].to_i, row[2].to_i, read_timestamp(row[3]), read_tags(row[4])
44    end
45  end
46end
47
48# yields for every way
49# id, [id1,id2,id3...], timestamp, tags
50def all_ways(page)
51  $mysql.query "select id, timestamp from current_ways where visible = 1 order by id #{pageSQL(page)}" do |ways|
52    ways.each do |row|
53      id = row[0].to_i
54      segs = []
55      $mysql.query "select segment_id from current_way_segments where id = #{id} order by sequence_id;" do |segments|
56        segments.each {|s| segs << s[0].to_i}
57      end
58      tags_arr = []
59      $mysql.query "select k,v from current_way_tags where id = #{id};" do |tags|
60        tags.each {|t| tags_arr << t[0] << t[1]}
61      end
62      yield id, segs, read_timestamp(row[1]), Hash[*tags_arr]
63    end
64  end
65end
66
67# output all tags in the hash
68def out_tags tags
69  tags.each {|key, value| puts %{    <tag k="#{CGI.escapeHTML(key)}" v="#{CGI.escapeHTML(value)}" />}}
70end
71
72puts '<?xml version="1.0" encoding="UTF-8"?>'
73puts '<osm version="0.3" generator="OpenStreetMap planet.rb">'
74
75done = false
76page = 0
77
78while not done
79  done = true
80  all_nodes(page) do |id, lat, lon, timestamp, tags|
81    done = false
82    print %{  <node id="#{id}" lat="#{lat}" lon="#{lon}" timestamp="#{timestamp.xmlschema}"}
83    if tags.empty?
84      puts "/>"
85    else
86      puts ">"
87      out_tags tags
88      puts "  </node>"
89    end
90  end
91  page += 1
92end
93
94done = false
95page = 0
96
97while not done
98  done = true
99  all_segments(page) do |id, from, to, timestamp, tags|
100    done = false
101    print %{  <segment id="#{id}" from="#{from}" to="#{to}" timestamp="#{timestamp.xmlschema}"}
102    if tags.empty?
103      puts "/>"
104    else
105      puts ">"
106      out_tags tags
107      puts "  </segment>"
108    end
109  end
110  page += 1
111end
112
113done = false
114page = 0
115
116while not done
117  done = true
118  all_ways(page) do |id, segs, timestamp, tags|
119    done = false
120    print %{  <way id="#{id}" timestamp="#{timestamp.xmlschema}"}
121    if tags.empty? and segs.empty?
122      puts "/>"
123    else
124      puts ">"
125      segs.each {|seg_id| puts %{    <seg id="#{seg_id}" />}}
126      out_tags tags
127      puts "  </way>"
128    end
129  end
130  page += 1
131end
132puts "</osm>"
Note: See TracBrowser for help on using the repository browser.