package GeoDB;

# This is the future home of code which can turn the kind of location
# names you get off of a shipper's website into human readable names,
# latitudes and longitudes. This is a difficult problem, and would likely
# always need some hand tweaking.

# It appears that the dataset used by:
#   http://tomcat-dmaweb1.jrc.it/fuzzyg/query/
# would work, and maybe even screen scraping it would work. It is the
# source for the hand converted locations below. The other problem is
# that APL (and problably other shippers) are pretty lazy about how
# they refer to places; note the Salt Lake City has no state or country
# and that San Pedro has the state, but no country. Also, note that
# the country for Djibouti (EP = Ethiopia) is wrong; Djibouti is now
# an independent state.

# APL's website does use codes for each location, and there's an HTML
# page for each location, but they are not well filled in:
#   http://homeport.apl.com/eschedules/cityinfo.do?cityCode=SAH

# Anyway, geocoding (the industry term for turning unstructured text
# into a lat/long) is huge problem with lots of proposed solutions,
# none of which work 100%, especially in an international context.

sub new {
  my $class = shift;
  my $self = bless {}, $class;
  return $self;
}

# this is just a hacky, by hand thing for now.
sub load {
  my $self = shift;
  $self->add('SALT LAKE CITY', [ 'Salt Lake City, Utah, USA', 40.77, -111.93 ]);
  $self->add('SAN PEDRO, CA', [ 'San Pedro, California, USA', 33.74, -118.29 ]);
  $self->add('CHIWAN, GD,PRC', [ 'Chiwan, Guangdong, China', 22.47, 113.88 ]);
  $self->add('SALALAH, OMAN', [ 'Salalah, Oman', 17.02, 54.08 ]);
  $self->add('DJIBOUTI, EP', [ 'Djibouti', 11.6, 43.15 ]);
  $self->add('ADDIS ABABA, EP', [ 'Addis Ababa, Ethiopia', 8.95, 38.77 ]);
	$self->add('Yokohama, JPN', [ 'Yokohama, Japan', 35.45, 139.633333 ]);
	$self->add('Hong Kong, HKG', [ 'Hong Kong, China', 22.3, 114.2 ]);
	$self->add('Santos, BRA', [ 'Santos, Brazil', -23.966667, -46.333333 ]);
	$self->add('Pelabuhan Tanjung Pelepas Terminal, Tanjung Pelepas, Malaysia',
		[ 'Tanjung Pelepas, Malaysia', 1.366347, 103.548367 ]);
	$self->add('Port Qasim Terminal, Port Qasim, Pakistan',
		[ 'Port Qasim, Pakistan', 24.766667, 67.333333 ]);
	$self->add('CHICAGO - LANDERS TERMINAL, Chicago, United States',
		[ 'Chicago, IL, USA', 41.888988, -87.622833 ]);
	$self->add('Chicago, United States',
		[ 'Chicago, IL, USA', 41.888988, -87.622833 ]);
	$self->add('Cincinnati, United States',
		[ 'Cincinnati, OH, USA', 39.136111, -84.503056 ]);
	$self->add('Knoxville, United States',
		[ 'Knoxville, TN, USA', 35.972778, -83.942222 ]);
	$self->add('Norfolk (Portsmouth) - APMT, Norfolk, United States',
		[ 'Norfolk, VA, USA', 36.91, -76.201867 ]),
	$self->add('Gioia Tauro Term., Gioia Tauro, Italy',
		[ 'Gioia Tauro, Italy', 38.433333, 15.9 ]);
	$self->add('Ashdod Terminal, Ashdod, Israel',
		[ 'Ashdod, Israel', 31.8, 34.65 ]);
	$self->add('Danville, United States', [ 'Danville, IL, USA', 40.139273, -87.621507 ]);
	$self->add('Lynchburg, United States', [ 'Lynchburg, VA, USA', 37.403611, -79.17 ]);
	$self->add('Georgetown, United States', [ 'Georgetown, OH, USA', 38.866944, -83.9025 ]);
}

sub add {
  my ($self, $k, $v) = @_;
  $self->{$k} = $v; 
}

sub find {
  my($self, $q) = @_;
  if (defined $self->{$q}) {
    return @{$self->{$q}};
  } else {
    return ( undef, 0, 0 );
  }
}

1;

