#!/usr/bin/perl # # Create postgis database from the postal code # # Author: Petter Reinholdtsen # Date: 2005-05-14 # Lisence: GNU Public Lisence use DBI; my $dbtable = "postal_codes"; sub create_table { $sql = <) { chomp; s/#.*//; next if /^\s*$/; my ($postal_code, $lat, $lon, $postal_location) = split; print insert_location($postal_code, $lat, $lon, $postal_location); } close(FILE); } sub save_sql_table { print < # Date: 2005-02-13 # # Updates and corrections are most welcome. The accurasy is unknown, # as the positions are based on a random set of addresses with the # given zip code. # # zip latitude longitude postoffice area EOF $dbname = "pere"; $dbhost = "localhost"; $dbuser = "pere"; $dbh = DBI->connect("dbi:Pg:dbname=$dbname", $dbuser) # ;host=$dbhost || die "Unable to connect to DB: $DBI::errstr"; $dbh->do("SET CLIENT_ENCODING TO 'ISO_8859_1'"); my $sql = <prepare($sql); $sth->execute(); while ( my @row = $sth->fetchrow_array() ) { my ($postal_code, $postal_location, $center_point, $area) = @row; my ($lon, $lat) = $center_point =~ m/SRID=[-+\d]+;POINT\(([\d.]+) ([\d.]+)\)/; my $line = "$postal_code\t$lat\t$lon\t$postal_location\t$area\n"; $line =~ s/\t+/\t/; $line =~ s/\t*$//; print $line; } $sth->finish(); $dbh->disconnect(); } if (1) { save_sql_table(); } else { print drop_table(); load_sql_table(); }