source: subversion/utils/planet.osm/php/planet_to_mysql.phps @ 1047

Last change on this file since 1047 was 996, checked in by steve, 14 years ago

collect planet.osm tools and add php from Dan Karran

File size: 1.3 KB
Line 
1<?php
2
3/**
4 * Code to read in the monthly data dump from OpenStreetMap (planet.osm) and insert into MySQL database
5 *   by Dan Karran - dan@karran.net
6 */
7
8// database connection to go in separate file:
9include("globals.php");
10
11// read the file in and run regular expressions to put the nodes and segments into an array
12print "Opening file...\n";
13$filename = "planet.osm";
14$file = fopen($filename, "r");
15while (!feof($file)) {
16  $contents .= fread($file, 8092);
17}
18preg_match_all("<node id=\"(.*)\" lat=\"(.*)\" lon=\"(.*)\">", $contents, $nodes[]);
19preg_match_all("<segment id=\"(.*)\" from=\"(.*)\" to=\"(.*)\">", $contents, $segments[]);
20fclose($file);
21
22// write the nodes and segments to database
23print "Writing nodes... ";
24$index = 0;
25$result = mysql_query("DELETE FROM nodes");
26foreach ($nodes[0][1] as $node) {
27  $sql = "INSERT INTO nodes (id, latitude, longitude) VALUES (".$nodes[0][1][$index].','.$nodes[0][2][$index].','.$nodes[0][3][$index].")";
28  $result = mysql_query($sql);
29  $index++;
30}
31print $index . "\n";
32
33print "Writing segments... ";
34$index = 0;
35$result = mysql_query("DELETE FROM segments");
36foreach ($segments[0][1] as $segment) {
37  $sql = "INSERT INTO segments (id, node_a, node_b) VALUES (".$segments[0][1][$index].','.$segments[0][2][$index].','.$segments[0][3][$index].")";
38  $result = mysql_query($sql);
39  $index++;
40}
41print $index . "\n";
Note: See TracBrowser for help on using the repository browser.