use DBI;
use strict;

BEGIN { $|++ }

my $db = DBI->connect( 'dbi:Pg:dbname=hellas' );
my $st = $db->prepare( "select * from place where name_nd ilike ?" );
my $store = $db->prepare( q{
    insert into interest (id, name_nd, feature_type, refcount, wkb_geometry)
	values (?, ?, ?, ?, GeometryFromText(?, 4326)) });

while (<>) {
    chomp;
    my ($count, $name) = split /\s/, $_, 2;
    next unless $name =~ /\S/;

    my $rows = $st->execute($name);
    if ($rows > 0) {
	while (my $row = $st->fetchrow_hashref) {
	    warn "FOUND: $row->{name_nd} ($count)\n";
	    $store->execute( $row->{id}, $row->{name_nd}, $row->{feature_type},
			     $count, "POINT($row->{lon} $row->{lat} 0)" );
	}
    } 
    elsif ($count > 1) {
	print "$count\t$name\n";
    }
}

