-
Notifications
You must be signed in to change notification settings - Fork 1
/
revertclean.pl
147 lines (119 loc) · 2.77 KB
/
revertclean.pl
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/perl -w
#
# Clean revert changeset
# Actually only removes created entities
#
# Ukraine extracts are available at
# http://download.geofabrik.de/osm/europe/
# http://downloads.cloudmade.com/europe/ukraine/
#
# Copyright (c) 2011, Eugene Sandulenko <sev.mail@gmail.com>
#
# This file is provided under GPLv2 license.
#
# Usage:
# perl -CD revert.pl ukraine.osm changeset.xml >revert.osm
use utf8;
use Geo::Parse::OSM;
BEGIN { $| = 1; }
binmode STDERR, ':utf8';
sub purgeWay($);
sub purgeNode($);
sub processRelation($);
sub processWay($);
my $ukrname = shift or die "Usage: $0: ukraine.osm changeset.xml";
my $changeset = shift or die "Usage: $0: ukraine.osm changeset.xml";
use Data::Dumper;
my %nodes = ();
my %ways = ();
my $processor = sub {
if ($_[0]->{type} eq 'node') {
if ($_[0]->{version} != 1 and exists $nodes{$_[0]->{id}}) {
purgeNode($_[0]->{id});
}
} elsif ($_[0]->{type} eq 'way') {
if (exists $ways{$_[0]->{id}}) {
if ($_[0]->{version} != 1) {
purgeWay($_[0]);
}
} else {
processWay($_[0]);
}
} elsif ($_[0]->{type} eq 'relation') {
processRelation($_[0]);
}
};
my $changesetprocessor = sub {
if ($_[0]->{type} eq 'node') {
return unless $_[0]->{version} == 1;
my %t = %{ $_[0] };
$nodes{$_[0]->{id}} = \%t;
} elsif ($_[0]->{type} eq 'way') {
if ($_[0]->{version} != 1) {
purgeWay($_[0]);
} else {
my %t = %{ $_[0] };
$ways{$_[0]->{id}} = \%t;
}
} else {
die "Unsupported type ($_[0]->{type})\n";
}
};
Geo::Parse::OSM->parse_file($changeset, $changesetprocessor);
Geo::Parse::OSM->parse_file($ukrname, $processor);
print "<osm version='0.6'>\n";
for my $n (sort keys %nodes) {
$nodes{$n}->{action} = 'delete';
print Geo::Parse::OSM::object_to_xml($nodes{$n});
}
for my $w (sort keys %ways) {
$ways{$w}->{action} = 'delete';
delete $ways{$w}->{tag};
print Geo::Parse::OSM::object_to_xml($ways{$w});
}
print "</osm>\n";
exit;
sub purgeWay($) {
my $w = shift;
# Delete all nodes from the way
for my $nd (@{$w->{chain}}) {
if (exists $nodes{$nd}) {
delete $nodes{$nd};
}
}
delete $ways{$w->{id}};
}
sub purgeNode($) {
my $id = shift;
# Check if some way refers to this node, then we cannot
# remove this way and all its nodes
for my $w (sort keys %ways) {
if (grep { $_ == $id } @{$ways{$w}->{chain}}) {
purgeWay($ways{$w});
}
}
delete $nodes{$id};
}
sub processRelation($) {
my $r = shift;
for my $m (@{$r->{members}}) {
if ($m->{type} eq 'node') {
if (exists $nodes{$m->{ref}}) {
purgeNode($m->{ref});
}
} elsif ($m->{type} eq 'way') {
if (exists $ways{$m->{ref}}) {
purgeWay($ways{$m->{ref}});
}
}
}
}
sub processWay($) {
my $w = shift;
# Delete all nodes from the way
for my $nd (@{$w->{chain}}) {
if (exists $nodes{$nd}) {
delete $nodes{$nd};
}
}
}