User:⠠⠁⠝⠞⠓⠕⠝⠽/Perl

From OpenStreetMap Wiki
Jump to navigation Jump to search
#!/usr/bin/perl

use DBI;
use strict;

my $dbname = '';
my $user = '';
my $pass = '';
my $changeset=0;
my $generator='';

my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", $user, $pass) or die DBI::errstr;

my $res = $dbh->selectall_arrayref("select node_id, lon, lat from all_nodes");

print "<osmChange version=\"0.6\" generator=\"$generator\">\n";
print " <create>\n";
for my $row (@$res) {
  my $id=@$row[0];
  my $lon=@$row[1];
  my $lat=@$row[2];
  print "  <node id=\"-$id\" lat=\"$lat\" lon=\"$lon\" changeset=\"$changeset\"/>\n";
  my $lat=@$row[2];
  print "  <node id=\"-$id\" lat=\"$lat\" lon=\"$lon\" changeset=\"$changeset\"/>\n";
}

$res = $dbh->selectall_arrayref("select way_id, c, node_id from way_nodes order by way_id, c");
for my $row (@$res) {
  my $wayid=@$row[0];
  my $c=@$row[1];
  my $node_id=@$row[2];

  if ($c==1) {
    print "  </way>\n" if ($wayid!=1);
    print "  <way id=\"-$wayid\" changeset=\"$changeset\">\n";
  }
  print "    <nd ref=\"-$node_id\"/>\n";
}
print "  </way>\n";

my $lastrelid=0;
$res = $dbh->selectall_arrayref("select relation_id, site_addr, way_id from relation_members order by relation_id");
for my $row (@$res) {
  my $relid=@$row[0];
  my $site_addr=@$row[1];
  my $way_id=@$row[2];

  if ($relid!=$lastrelid) {
    print "  </relation>\n" if ($lastrelid!=0);
    print "  <relation id=\"-$relid\" changeset=\"$changeset\" version=\"2\">\n";
    print "    <tag k=\"type\" v=\"multipolygon\"/>\n";
    print "    <tag k=\"site_addr\" v=\"$site_addr\"/>\n";
    $lastrelid=$relid;
  }
  print "    <member type=\"way\" role=\"outer\" ref=\"-$way_id\"/>\n";
}
print "  </relation>\n";

print " </create>\n";
print "</osmChange>\n";

$dbh->disconnect();