-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathModify.pm
107 lines (94 loc) · 2.26 KB
/
Modify.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/perl
# Modify.pm
# ---------
#
# Modifies the tags of an object.
#
# Part of the "osmtools" suite of programs
# Originally written by Frederik Ramm <[email protected]>; public domain
#
package Modify;
use strict;
use warnings;
use OsmApi;
use Redaction;
# modifies one object
#
# parameters:
# $what: 'node', 'way', or 'relation'
# $id: object id
# $tags: hash of key=>value combinations (empty value for deleting a tag)
# $changeset: id of changeset to use for modify operation
# return:
# success=1 failure=undef
sub modify
{
my ($what, $id, $tags, $changeset) = @_;
my $copy=0;
my $out = "";
my $resp = OsmApi::get("$what/$id");
if (!$resp->is_success)
{
print STDERR "$what $id cannot be retrieved: ".$resp->status_line."\n";
return undef;
}
foreach (split(/\n/, $resp->content()))
{
if (/<$what/)
{
/\sid="([^"]+)"/ or die;
die unless $id eq $1;
$copy = 1;
$out = $_;
}
elsif ($copy)
{
if (/<\/$what/)
{
foreach my $k(keys %$tags)
{
$out .= "<tag k=\"$k\" v=\"".$tags->{$k}."\" />\n";
}
$copy=0;
}
if (/<tag k=\"([^"]*)" v="([^"]*)" *\/>/)
{
if (defined($tags->{$1}))
{
if ($tags->{$1} ne "")
{
$out .= "<tag k=\"$1\" v=\"".$tags->{$1}."\" />\n";
}
delete $tags->{$1};
}
else
{
$out .= $_;
}
}
else
{
$out .= $_;
}
}
};
return 1 if ($out eq $resp->content());
$out =~ s/changeset="\d+"/changeset="$changeset"/;
my $osc = <<EOF;
<osmChange version='0.6'>
<modify>
$out
</modify>
</osmChange>
EOF
$resp = OsmApi::post("changeset/$changeset/upload", $osc);
if (!$resp->is_success)
{
my $c = $resp->content();
print "$c\n";
print STDERR "$what $id cannot be modified: ".$resp->status_line."\n";
return undef;
}
return 1;
}
1;