source: subversion/applications/utils/planet.osm/planet2mysql.rb @ 34621

Last change on this file since 34621 was 4956, checked in by jonb, 12 years ago

undo accidental changes to planet2mysql.rb and planet.c

File size: 1.6 KB
Line 
1#!/usr/bin/ruby -w
2
3$: << File.dirname(__FILE__)+"/../osm-data/lib"
4$: << File.dirname(__FILE__)+"/../../www.openstreetmap.org/ruby/api"
5
6require 'mysql'
7require 'time'
8require 'osm/servinfo.rb'
9require 'cgi'
10
11require 'osm/data'
12require 'osm/rexml'
13
14$mysql = Mysql.real_connect $DBSERVER, $USERNAME, $PASSWORD, $DATABASE
15
16class Hash
17  def to_tags_str
18    if self.empty?
19      nil
20    else
21      self.collect{|k,v| "#{k}=#{v}"}.join(";").gsub('"', '\"')
22    end
23  end
24end
25
26class OSM::OsmPrimitive
27  def xmldate
28    if @timestamp
29      '"' + @timestamp.strftime("%Y-%m-%d %H:%M:%S") + '"'
30    else
31      "NULL"
32    end
33  end
34end
35
36class OSM::Node
37  def save_mysql
38    $mysql.query "insert into current_nodes values (#@id, #@lat, #@lon, 1, 1, \"#{@tags.to_tags_str}\", #{self.xmldate});"
39  end
40end
41
42class OSM::Segment
43  def save_mysql
44    $mysql.query "insert into current_segments values (#@id, #{@from.to_i}, #{@to.to_i}, 1, 1, \"#{@tags.to_tags_str}\", #{self.xmldate});"
45  end
46end
47
48class OSM::Way
49  def save_mysql
50    $mysql.query "insert into current_ways values (#@id, 1, #{self.xmldate}, 1);"
51    @tags.each do |key, value|
52      $mysql.query "insert into current_way_tags values (#@id, \"#{key}\", \"#{value}\");"
53    end
54    @segments.each_with_index do |s,i|
55      $mysql.query "insert into current_way_segments values (#@id, #{s.to_i}, #{i+1});"
56    end
57  end
58end
59
60$mysql.query "set autocommit = 0;"
61current = ""
62open(ARGV[0]).each do |line|
63  next if line =~ /\<osm|\<\/osm\>|\<\?xml/
64  current << line
65  case line
66  when /\<(\/(node|segment|way)|(node|segment|way).*\/)\>$/
67    OSM::OsmPrimitive.from_xml(current).save_mysql
68    current = ""
69  end
70end
71
72$mysql.query "commit;"
Note: See TracBrowser for help on using the repository browser.