source: subversion/applications/utils/planet.osm/planet05.rb @ 29793

Last change on this file since 29793 was 4845, checked in by gabriel, 12 years ago

Add planet05.rb.

This is a modified version of the former planet.rb, it doesn't have the
efficiency changes made to planet.rb in the meantime.

  • Property svn:executable set to *
File size: 3.9 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 way
39# id, [id1,id2,id3...], timestamp, tags
40def all_ways(page)
41  $mysql.query "select id, timestamp from current_ways where visible = 1 order by id #{pageSQL(page)}" do |ways|
42    ways.each do |row|
43      id = row[0].to_i
44      nds = []
45      $mysql.query "select node_id from current_way_nodes where id = #{id} order by sequence_id;" do |nodes|
46        nodes.each {|s| nds << s[0].to_i}
47      end
48      tags_arr = []
49      $mysql.query "select k,v from current_way_tags where id = #{id};" do |tags|
50        tags.each {|t| tags_arr << t[0] << t[1]}
51      end
52      yield id, nds, read_timestamp(row[1]), Hash[*tags_arr]
53    end
54  end
55end
56
57# yields for every relation
58# id, [[type,ref,role],...], timestamp, tags
59def all_relations(page)
60  $mysql.query "select id, timestamp from current_relations where visible = 1 order by id #{pageSQL(page)}" do |rels|
61    rels.each do |row|
62      id = row[0].to_i
63      rms = []
64      $mysql.query "select member_type, member_id, member_role from current_relation_members where id = #{id};" do |members|
65        members.each { |rm| rms << rm }
66      end
67      tags_arr = []
68      $mysql.query "select k,v from current_relation_tags where id = #{id};" do |tags|
69        tags.each {|t| tags_arr << t[0] << t[1]}
70      end
71      yield id, rms, read_timestamp(row[1]), Hash[*tags_arr]
72    end
73  end
74end
75
76# output all tags in the hash
77def out_tags tags
78  tags.each {|key, value| puts %{    <tag k="#{CGI.escapeHTML(key)}" v="#{CGI.escapeHTML(value)}" />}}
79end
80
81puts '<?xml version="1.0" encoding="UTF-8"?>'
82puts '<osm version="0.5" generator="OpenStreetMap planet.rb">'
83
84done = false
85page = 0
86
87while not done
88  done = true
89  all_nodes(page) do |id, lat, lon, timestamp, tags|
90    done = false
91    print %{  <node id="#{id}" lat="#{sprintf('%.7f', lat/10000000.0).lstrip}" lon="#{sprintf('%.7f', lon/10000000.0).lstrip}" timestamp="#{timestamp.xmlschema}"}
92    if tags.empty?
93      puts "/>"
94    else
95      puts ">"
96      out_tags tags
97      puts "  </node>"
98    end
99  end
100  page += 1
101end
102
103done = false
104page = 0
105
106while not done
107  done = true
108  all_ways(page) do |id, nds, timestamp, tags|
109    done = false
110    print %{  <way id="#{id}" timestamp="#{timestamp.xmlschema}"}
111    if tags.empty? and nds.empty?
112      puts "/>"
113    else
114      puts ">"
115      nds.each {|nd_id| puts %{    <nd ref="#{nd_id}" />}}
116      out_tags tags
117      puts "  </way>"
118    end
119  end
120  page += 1
121end
122
123done = false
124page = 0
125
126while not done
127  done = true
128  all_relations(page) do |id, rms, timestamp, tags|
129    done = false
130    print %{  <relation id="#{id}" timestamp="#{timestamp.xmlschema}"}
131    if tags.empty? and rms.empty?
132      puts "/>"
133    else
134      puts ">"
135      rms.each { |type, ref, role|
136        puts %{    <member type="#{type}" ref="#{ref}" role="#{CGI.escapeHTML(role)}" />}
137      }
138      out_tags tags
139      puts "  </relation>"
140    end
141  end
142  page += 1
143end
144puts "</osm>"
Note: See TracBrowser for help on using the repository browser.