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

Last change on this file since 4434 was 4434, checked in by tomhughes, 12 years ago

Apply kleptog's patch again with syntax errors fixed.

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