source: subversion/applications/utils/osm-extract/polygons/poly2wkt.pl

Last change on this file was 26497, checked in by frederik, 8 years ago

new utilities: rel2poly.pl reads OSM relation and makes polygon file; poly2wkt.pl creates WKT string for use with spatial databases

File size: 980 bytes
Line 
1#!/usr/bin/perl
2
3# script to convert a polygon file to a WKT file for loading it into a
4# database etc.
5#
6# written by Frederik Ramm <frederik@remote.org>, public domain.
7
8use strict;
9use warnings;
10
11# first line
12# (employ workaround for polygon files without initial text line)
13my $poly_file = <>; chomp($poly_file);
14my $workaround = 0;
15if ($poly_file =~ /^\d+$/)
16{
17    $workaround=$poly_file;
18    $poly_file="none";
19}
20
21my $polygons;
22
23while(1)
24{
25    my $poly_id;
26    if ($workaround==0) 
27    {
28        $poly_id=<>;
29    } 
30    else 
31    {
32        $poly_id=$workaround;
33    }
34    chomp($poly_id);
35    last if ($poly_id =~ /^END/); # end of file
36        my $coords;
37
38    while(my $line = <>)
39    {
40        last if ($line =~ /^END/); # end of poly
41            my ($dummy, $x, $y) = split(/\s+/, $line);
42        push(@$coords, sprintf("%f %f", $x, $y));
43    }
44    push(@$polygons, "((".join(",", @$coords)."))");
45    $workaround=0;
46}
47
48print "MULTIPOLYGON(".join(",",@$polygons).")\n";
Note: See TracBrowser for help on using the repository browser.