2010 Colombia floods/ImportMuni/Perl for Relation shell extraction

From OpenStreetMap Wiki
Jump to: navigation, search

avoid confusing this with the tool mentioned at http://lists.openstreetmap.org/pipermail/talk-co/2010-December/001619.html


As more municipalities are uploaded, less data needs to be uploaded to finish representation of a new municipality. This is due to the ability to re-use previously uploaded borders as shared borders between the new and previously loaded municipality. In order to facilitate this reuse, it is useful to be able to upload Relations without associated ways, 'shells' which can be filled with ways. The Perl script below prepares a Relation Shell file in .osm format; a sample section of one output file is shown below:

<?xml version='1.0'?>
<osm version='0.5' generator='shp2osm.pl'>
<relation id="-1860601" visible="true">
<tag k="source" v="OCHA - SIGOT"/>
<tag k="clcfile" v="MunicipiosDepartamentos/ADM_Municipios.shp"/>
<tag k="name" v="puerto parra"/>
<tag k="divipola" v="68573"/>
<tag k="DANE:area" v="745.00"/>
<tag k="entidad_te" v="Cabecera Municipal"/>
<tag k="admin_level" v="6"/>
<tag k="boundary" v="administrative"/>
<tag k="is_in:country" v="Colombia" />
<tag k="objectid" v="4460"/>
<tag k="is_in:state" v="santander"/>
<tag k="DANE:departamento" v="68"/>
<tag k='type' v='multipolygon'/>
</relation>  
<relation id="-1861238" visible="true">
<tag k="source" v="OCHA - SIGOT"/>
<tag k="clcfile" v="MunicipiosDepartamentos/ADM_Municipios.shp"/>
<tag k="name" v="chipatá"/>
<tag k="divipola" v="68179"/>
<tag k="DANE:area" v="85.00"/>
<tag k="entidad_te" v="Cabecera Municipal"/>
<tag k="admin_level" v="6"/>
<tag k="boundary" v="administrative"/>
<tag k="is_in:country" v="Colombia" />
<tag k="objectid" v="4461"/>
<tag k="is_in:state" v="santander"/>
<tag k="DANE:departamento" v="68"/>
<tag k='type' v='multipolygon'/>
</relation>  

The input file is a .osm prepared by the processing script described at 2010 Colombia floods/ImportMuni/Perl for OSMfile revisions. This script was written for ActivePerl running on a desktop PC.

Prepared by — Ceyockey

use strict;
# composed by ceyockey 26 December 2010
# ceyockey is an OSM Wiki username

print "This script is designed to extract relations from .osm files prepared for use in uploading municipality borders for Colombia as part of the 2010 Flooding Disaster Response.\n";
print "\nPROBLEM STATEMENT\nAs more municipality relations are loaded into OSM, fewer actual nodes need to be added due to the ability to add a segment of one border to a new relation.  Thus, being able to upload relation 'shells' to which ways can be added could speed loading and reduce the incidence of redundant node loads.\n";

my $infile='';
my $outfile = '';
my $inlinecount='';
my $curline = '';
my @inlines=();
my @outlines=();
my $scriptoffset = "../scripts/";
my $baseinputfilename = "ADM_Municipios_ways_";
my $inway = '';
my $inrel = '';
my $waynumber = '0';
my $relnumber = '0';
my $i = ''; # an iteration counter
my $w = ''; # an iteration counter sum
my @relcnt = ();
my %relations = ();
my @allrel = (); # set of all relations extracted from raw .osm file
my @tagsonline = (); # array of data tags on a particular line of input

print "\nI assume the file name starts with \"ADM_Municipios_ways_\" with a number following.  What is that number?  ";
chomp($infile = <STDIN>);
$outfile = $scriptoffset.$baseinputfilename.$infile."ONLY_RELATIONS.osm";
$infile = $scriptoffset.$baseinputfilename.$infile.".osm";
print "\n\nBeginning to process $infile\n";

open (OSMIN, "<", $infile) || die "Can't create $infile: $!";
@inlines = <OSMIN>;

open (RELS, ">", $outfile) || die "Can't create $outfile: $!";

chomp @inlines;

@relcnt = grep (/relation id/,@inlines);
$w = $#relcnt + 1;
print "\nNumber of relation shells to be extracted: $w\n";

foreach $curline (@inlines) {
	unless ($curline =~ /</) {next} # this is working to filter out blank lines
	if ($curline =~ /<relation id/) {
		$inrel = 'yes';
		push @allrel, $curline;
		next;
	}
	if ($curline =~ /<\/relation>/) {
		$inrel = 'no';
		push @allrel, $curline;
		next;
	}
	if ($inrel =~ 'yes') {
		push @allrel, $curline;
		next;
	}
}

@relcnt = ();
$w = '';
@relcnt = grep (/relation id/,@allrel);
$w = $#relcnt +1;
print "\nNumber of relation sets extracted to processing array: $w\n";
print "\nBeginning to process relation set . . . \n";

foreach $curline (@allrel) {
	@tagsonline = split /</, $curline;
	foreach $i (0 .. $#tagsonline) {
		unless ($tagsonline[$i] =~ />/) {next}
		if ($tagsonline[$i] =~ /^member|node/) {next}
		push @outlines, '<'.$tagsonline[$i]."\n";
		next;
	}
}

print RELS '<?xml version=\'1.0\'?>'."\n";
print RELS '<osm version=\'0.5\' generator=\'shp2osm.pl\'>'."\n";
foreach (@outlines) {
	print RELS $_;
}
print RELS '</osm>'."\n";

print "\nOutput completed.  Ending Run.\n";