source: subversion/applications/utils/import/and_import/protected2polygon.pl @ 16144

Last change on this file since 16144 was 4297, checked in by martinvoosterhout, 12 years ago

Add licence information and improve documentation.

  • Property svn:executable set to *
File size: 1.6 KB
Line 
1#!/usr/bin/perl -w
2
3# Simple script to take list of way IDs representing protected areas and
4# producing polygon files. The actually hard work is done by osm2polygon.pl.
5#
6# By Martijn van Oosterhout <kleptog@svana.org>  August 2007
7# Licence: BSD
8
9
10use strict;
11use warnings;
12use LWP::Simple;
13
14use constant PROTECTED_FILE => "protected_areas";
15
16open my $fh, "<", PROTECTED_FILE or die "Couldn't open ".PROTECTED_FILE." ($!)\n";
17
18while(<$fh>)
19{
20  next if /^#/;
21  if( /^(\d+)$/ )
22  {
23    my $way = $1;
24    my $data = get("http://www.openstreetmap.org/api/0.4/way/$way/full");
25    if( $data !~ /nl:protected_and_import/ )
26    {
27      print STDERR "WARNING: Way $way not correctly tagged, skipping\n";
28      next;
29    }
30    my $polygon = filter($data);
31    if( $polygon !~ /END/ )
32    {
33      print STDERR "WARNING: Conversion of way $way failed, skipping\n";
34      next;
35    }
36    print $polygon;
37  }
38}
39
40# This function takes OSM XML data and filters it through osm2polygon.pl.
41# Due to the size of the data you can't simply use open2 because the buffers
42# are not big enough, hence the double fork()
43sub filter
44{
45  my $data = shift;
46 
47  my $pid = open( my $fh, "-|" );
48  if( $pid == 0 )
49  {
50    my $pid2 = open( my $fh2, "-|" );
51    if( $pid2 == 0 )
52    {
53      # In second level child, we write to STDOUT
54      print $data;
55      exit;
56    }
57    # In first level child, we read $fh2 and write STDOUT
58    open( STDIN, "<&", $fh2 ) or die "Couldn't dup STDIN ($!)\n";
59    exec( "./osm2polygon.pl", "-" );
60    die "Failed to exec osm2polygon.pl ($!)\n";
61    exit;
62  }
63  my $result = "!";  # Invert polygon
64  while(<$fh>)
65  {
66    $result .= $_;
67  }
68  return $result;
69}
Note: See TracBrowser for help on using the repository browser.