#/usr/bin/perl

#===========================================================================
#  kiban2mp.pl : converts JSGI Kiban Contour Data to polish format.
#
#    usage: perl kiban2mp.pl  xmlfile  MapID 
#===========================================================================

sub  output_header {

	my $mapID = $_[0];

	print "[IMG ID]\n";
	
	print "ID=$mapID\n";
	print "Name=$fileName\n";
	print "TreSize=1536\n";
	print "RgnLimit=768\n";

	print "TreMargin=0.00000\n";
	print "LBLcoding=9\n";

	print "Elevation=M\n";
	print "Transparent=N\n";

	print "Levels=4\n";
	print "Level0=23\n";
	print "Level1=21\n";
	print "Level2=19\n";
	print "Level3=17\n";

	print "[END-IMG ID]\n";

} # End of "sub  header_out"

sub gen_latlon_dataset {

	$i = 0;
	$numOfItems = @latitude;

	printf( "Data0=(%.6f,%.6f),", $latitude[0], $longitude[0] );
	for ( $i = 0; $i < $numOfItems-1; $i++ ) {
		printf( "(%.6f,%.6f),", $latitude[$i], $longitude[$i] );
	}; 
	printf( "(%.6f,%.6f)\n", $latitude[$len-1], $longitude[$len-1] );

	
}; # End of "sub  gen_latlon_dataset" 

sub  gen_contour_object {

	my $altitude = $_[0];

	if ( $altitude > 0 ) {
	
		# 
		if ( $altitude % 500 == 0 ) {
			# multiples of 500m (500, 1000, 1500, ... )
			print("[POLYLINE]\n");
			print("Label=$altitude\n");
			print("TYPE=0x0022\n"); # thin gray line
			print("EndLevel=3\n");
			gen_latlon_dataset( @latitude, @longitude );
			print("[END]\n");

		} elsif ( $altitude % 100 == 0 ) {
			#100m ( 100, 200, 300, 400, 600, ... )
			print("[POLYLINE]\n");
			print("Label=$altitude\n");
			print("TYPE=0x0022\n"); # bold contour line
			print("EndLevel=2\n");
			gen_latlon_dataset( @latitude, @longitude );
			print("[END]\n");

		} elsif ( $altitude % 50 == 0 ) {
			#50 m ( 50, 150, 250, 350, ... )
			print("[POLYLINE]\n");
			print("Label=$altitude\n");
			print("TYPE=0x0021\n"); # thin contour line
			print("EndLevel=1\n");
			gen_latlon_dataset( @latitude, @longitude );
			print("[END]\n");

		} elsif ( $altitude % 20 == 0 ) {
			# 20m ( 20, 40, 60, 80, 120, 140, 160, 280, 220, ... )
			print("[POLYLINE]\n");
			print("; Label=$altitude\n");
			print("TYPE=0x0020\n"); # thin dashed contour line
			print("EndLevel=0\n");
			gen_latlon_dataset( @latitude, @longitude );
			print("[END]\n");
		}

	}

} # End of "sub  gen_contour_object"

#=================  main =================
	$fileName = $ARGV[0];
	$mapID = $ARGV[1];

	# open file stream
	open( IN,"$fileName" ) or die( "File Open Error" );

	output_header( $mapID );

	while ( <IN> ) {

		$inline = $_;

		if ( $inline =~ /<jps:coordinate>/ ) {

			# get a coordinate
			@latlon = split( " ", $inline );
			$lat = substr( @latlon[0], 16, 12 );
			$lon = substr( @latlon[1], 0, 13 );

			# push to the stuck array
			push( @latitude, $lat );
			push( @longitude, $lon );
		
		} elsif ( $inline =~ /<alti>\d+<\/alti>/ ) {
		
			# get an altitude level
			$inline =~ /\d+/;
			$alt = $&;
			
		} elsif ( $inline =~ /<\/Cntr>/ ) {

			# reached the last tag
			gen_contour_object( $alt );

			$alt = -1;
			@latitude = ();
			@longitude = ();

		}

	}; # end while
	
	close( IN );

#=================  main =================
