]>
pere.pagekite.me Git - homepage.git/blob - gis/openstreetmap/pcode-location-update
3 # Create postgis database from the postal code
5 # Author: Petter Reinholdtsen <pere@hungry.com>
7 # Lisence: GNU Public Lisence
11 my $dbtable = "postal_codes";
16 CREATE TABLE $dbtable (
17 postal_code text NOT NULL PRIMARY KEY,
20 SELECT AddGeometryColumn('','$dbtable','center_point', '-1','POINT', 2);
21 SELECT AddGeometryColumn('','$dbtable','area', '-1','POLYGON', 2);
27 my ($postal_code, $lat, $lon, $postal_location) = @_;
29 INSERT INTO $dbtable (postal_code, center_point)
30 VALUES ('$postal_code', GeometryFromText('POINT($lon $lat)', -1));
37 SELECT DropGeometryColumn('','$dbtable','center_point');
38 SELECT DropGeometryColumn('','$dbtable','area');
46 open(FILE
, "<pcode-location.txt") or
47 die "Unable to read pcode-location.txt";
52 my ($postal_code, $lat, $lon, $postal_location) = split;
53 print insert_location
($postal_code, $lat, $lon, $postal_location);
61 # Location of postal area codes in Norway. The values are aproximate.
63 # Author: Petter Reinholdsten <pere\@hungry.com>
66 # Updates and corrections are most welcome. The accurasy is unknown,
67 # as the positions are based on a random set of addresses with the
70 # zip latitude longitude postoffice area
73 $dbhost = "localhost";
76 $dbh = DBI-
>connect("dbi:Pg:dbname=$dbname", $dbuser) # ;host=$dbhost
77 || die "Unable to connect to DB: $DBI::errstr";
78 $dbh->do("SET CLIENT_ENCODING TO 'ISO_8859_1'");
81 SELECT postal_code, postal_location, center_point, area from $dbtable
85 my $sth = $dbh->prepare($sql);
87 while ( my @row = $sth->fetchrow_array() ) {
88 my ($postal_code, $postal_location, $center_point, $area) = @row;
90 $center_point =~ m/SRID=[-+\d]+;POINT\(([\d.]+) ([\d.]+)\)/;
91 my $line = "$postal_code\t$lat\t$lon\t$postal_location\t$area\n";