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

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

back out kleptog's patch. It exits with error.

  • Property svn:executable set to *
File size: 3.6 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">'
74puts '  <bound box="-90,-180,90,180" origin="http://www.openstreetmap.org/api/0.4" />'
75
76done = false
77page = 0
78
79while not done
80  done = true
81  all_nodes(page) do |id, lat, lon, timestamp, tags|
82    done = false
83    print %{  <node id="#{id}" lat="#{sprintf('%.7f', lat)}" lon="#{sprintf('%.7f', lon)}" timestamp="#{timestamp.xmlschema}"}
84    if tags.empty?
85      puts "/>"
86    else
87      puts ">"
88      out_tags tags
89      puts "  </node>"
90    end
91  end
92  page += 1
93end
94
95done = false
96page = 0
97
98while not done
99  done = true
100  all_segments(page) do |id, from, to, timestamp, tags|
101    done = false
102    print %{  <segment id="#{id}" from="#{from}" to="#{to}" timestamp="#{timestamp.xmlschema}"}
103    if tags.empty?
104      puts "/>"
105    else
106      puts ">"
107      out_tags tags
108      puts "  </segment>"
109    end
110  end
111  page += 1
112end
113
114done = false
115page = 0
116
117while not done
118  done = true
119  all_ways(page) do |id, segs, timestamp, tags|
120    done = false
121    print %{  <way id="#{id}" timestamp="#{timestamp.xmlschema}"}
122    if tags.empty? and segs.empty?
123      puts "/>"
124    else
125      puts ">"
126      segs.each {|seg_id| puts %{    <seg id="#{seg_id}" />}}
127      out_tags tags
128      puts "  </way>"
129    end
130  end
131  page += 1
132end
133puts "</osm>"
Note: See TracBrowser for help on using the repository browser.