#!/usr/bin/perl

use strict;
use warnings;

use lib qw(/usr/local/lib/mpmasql/modules);
use SimpleObject;
use EnhancedGrid;

my $mapfile;

while (@ARGV) {
	my $el = shift @ARGV;
	if (not $el =~ /^-/) {
		$mapfile = $el;
	}
	elsif ($el =~ /^-v/ or $el =~ /^--version/ ) {
		print <<WC2;
maptoxy 1.0
(c)	Christian Troost, Universität Hohenheim, 2016
WC2
	}	
			
	elsif ($el =~ /^-h/ or $el=~ /^-\?/ ) {
		print <<HELP;

Lists cells of a raster map in ESRI ASCII format as X Y Value table.

Usage: maptoxy <filename1>  <options>

Options:
	-h or -? 	prints this help and exits
	-v, --version	version information

HELP
	exit 0;
	}
	
	
}



print STDERR "Please specify an ASCII map file!\n"and exit(1) unless (defined $mapfile) ;

my $map = EnhancedGrid->create_from_ascii($mapfile);

my $tab = $map->list_cells();


print "plotX\tplotY\tValue\n";
foreach (@$tab) {
	print join("\t", $_->{x}, $_->{y}, $_->{value} ), "\n";
}
