source: subversion/utils/coast_import/coast_upload.pl @ 2254

Last change on this file since 2254 was 1545, checked in by joerg, 13 years ago

insert Man pages/Copyright Info/Copright? Stubs to some of our perl Programs

  • Property svn:executable set to *
File size: 4.6 KB
Line 
1#!/usr/bin/perl
2#-----------------------------------------------------------------------------
3# coast_upload.pl
4#
5# This program will upload PGS coastlines to openstreetmap
6#
7# Usage:
8#   perl coast_upload.pl LatS LatN LongW LongE [datafile]
9#
10# Contact OJW on the Openstreetmap wiki for help using this program
11
12use Geo::ShapeFile;
13use LWP::Simple;
14use osm;
15use strict;
16
17# Lat, Lat, Long, Long, Sector
18my $Y1 = shift();
19my $Y2 = shift();
20my $X1 = shift();
21my $X2 = shift();
22my $Sector = shift() || "20";
23
24createCoasts("Data/NGA_GlobalShoreline_cd".$Sector, $Y1, $Y2, $X1, $X2);
25print "Done\n";
26
27
28sub createCoasts(){
29  # Filename, Lat, Lat, Long, Long
30  my ($Filename, $Y1, $Y2, $X1, $X2) = @_;
31 
32  my $PW = "----"; # Password for reporting progress to the almien website
33  my $ID = int(get(sprintf("http://almien.co.uk/OSM/CoastlineUpload/Update/?pg=$PW&action=start&S=%f&N=%f&W=%f&E=%f",$Y1,$Y2,$X1,$X2)));
34   
35  my $osm = new osm;
36 
37  # Enter your OSM username for downloading
38  $osm->setup("username\@domain","password","almien_coastlines");
39 
40  # Specify some temporary filenames the program can use
41  $osm->tempfiles("temp1.txt", "temp2.txt");
42 
43  # Coastline tags
44  my $TagDefault = "<tag k=\"source\" v=\"PGS\"/>";
45  my $TagSegment = $TagDefault."<tag k=\"natural\" v=\"coastline\"/>";
46  my $TagWay = $TagDefault."<tag k=\"natural\" v=\"coastline\"/>";
47 
48  # Logfile
49  open(LOG, ">log.txt") || die("Can't create logfile");
50  printf LOG "Long %f to %f, Lat %f to %f, shapefile %s, row ID %d\n";
51    $X1, $X2, $Y1, $Y2, $Filename, $ID;
52  printf LOG "(%f, %f)\n", $X2 - $X1, $Y2 - $Y1;
53 
54  my $shapefile = new Geo::ShapeFile( $Filename );
55  printf "%d shapes\n", $shapefile->shapes();
56
57  my $Count = 0;
58  for(1 .. $shapefile->shapes()) {
59    my $shape = $shapefile->get_shp_record($_);
60    my $LastValid = 0;
61    my $NoBreaks = 1;
62    my $FirstNode = 0;
63    my $LastNode = 0;
64    my @Segments;
65   
66    foreach my $Point($shape->points()){
67      my $Long = $Point->X();
68      my $Lat = $Point->Y();
69   
70      my $InArea = (($Lat > $Y1) && ($Lat < $Y2) && ($Long > $X1) && ($Long < $X2));
71     
72      if($InArea){
73          my $Node = $osm->uploadNode($Lat, $Long, $TagDefault);
74          printf LOG "Node #%d: %f, %f\n", $Node, $Lat, $Long;
75         
76          if($LastValid){
77            my $Segment = $osm->uploadSegment($LastNode, $Node, $TagSegment);
78            push(@Segments, $Segment) if($Segment);
79            printf LOG "Segment #%d: %d, %d\n", $Segment, $LastNode, $Node;
80         
81            if(scalar(@Segments) > 80){
82              my $Way = $osm->uploadWay($TagWay, @Segments);
83              printf LOG "InterimWay %d: %s\n", $Way, join(", ", @Segments);
84              @Segments = ();
85            }
86
87          }
88          $LastNode = $Node;
89          $FirstNode = $Node if(!$FirstNode);
90         
91          # Upload status to website
92          if(++$Count % 100 == 0){
93            get(sprintf("http://almien.co.uk/OSM/CoastlineUpload/Update/?pg=$PW&action=status&ID=%d&lat=%f&long=%f&count=%d",$ID, $Lat, $Long, $Count));
94          }
95
96          $LastValid = 1;
97        }
98        else
99        {
100          $LastValid = 0;
101          $NoBreaks = 0;
102        }
103      }
104         
105      if(scalar(@Segments) > 0){
106        my $Way = $osm->uploadWay($TagWay, @Segments);
107        printf LOG "Way %d: %s\n", $Way, join(", ", @Segments);
108      }
109    }
110   
111    print LOG "Complete\n";
112    close LOG;
113    print "Upload complete\n";   
114
115  # Report this segment as finished
116  get(sprintf("http://almien.co.uk/OSM/CoastlineUpload/Update/?pg=$PW&action=finish&ID=%d",$ID));
117}
118
119__END__
120
121=head1 NAME
122
123B<coast_upload.pl>
124
125=head1 DESCRIPTION
126
127This program will upload PGS coastlines to openstreetmap
128
129=head1 SYNOPSIS
130
131Usage:
132   perl coast_upload.pl LatS LatN LongW LongE [datafile]
133
134=head1 OPTIONS
135
136Contact OJW on the Openstreetmap wiki for help using this program
137
138=head1 COPYRIGHT
139
140Copyright 2006, Jörg Ostertag
141
142This program is free software; you can redistribute it and/or
143modify it under the terms of the GNU General Public License
144as published by the Free Software Foundation; either version 2
145of the License, or (at your option) any later version.
146
147This program is distributed in the hope that it will be useful,
148but WITHOUT ANY WARRANTY; without even the implied warranty of
149MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
150GNU General Public License for more details.
151
152You should have received a copy of the GNU General Public License
153along with this program; if not, write to the Free Software
154Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
155
156=head1 AUTHOR
157
158OJW
159
160=head1 SEE ALSO
161
162http://www.openstreetmap.org/
163
164=cut
Note: See TracBrowser for help on using the repository browser.