[Mapserver-users] get proj.4 parameters from a raster image.

pond bear pond_bear at hotmail.com
Fri Jun 27 13:16:28 EDT 2003


thanks a lot.
I just write a PHP script to get the projection type.

This script requires gdalinfo.

<?php
//change it to your image's path
define('working_path','/image');

@ chdir (working_path) or die("Cannot set the current working 
directory<br>.");

//get the projection information from the raster file
//and put it into an array.
function proj_init($file) {

	//using gdalinfo to get information of the image and put the information 
into $command_output
	exec("gdalinfo $file", $command_output, $return_value);
	if ($return_value){
		die ("The file $file doesn't exist, please check and input it 
again.<br>");
	}else{

		//put projection information in array proj
		$proj_arr=array();
		for ($i = 0; $i < count($command_output); $i++ ) {
			//search proj information in lines containing '['
			//echo $command_output[$i].'<br>';
			if(strpos($command_output[$i],'[')){
				/*remove ']' and ',' at the end of the line
				remove ' ' at the beginning of the line
				remove double quotes*/
				$output_line=ereg_replace('^ *|\"|\]*,?$','',$command_output[$i]);

				$output_line_arr=explode('[',$output_line);
				$proj_arr[$output_line_arr[0]][]=$output_line_arr[1];
			}//end if strpos($command_output[$i],'[')
		}
	}

	for ($i =0;$i< count($proj_arr['PARAMETER']);$i++)
		$proj_arr['PARAMETER'][$i]=end(explode(',',$proj_arr['PARAMETER'][$i]));
	//print_r($proj_arr);
	//echo '<br>';
	return $proj_arr;
} //end of function proj_init

function get_type($type) {

	if ($type=='Lambert_Azimuthal_Equal_Area')
		$proj_type='proj=laea';
	elseif ($type=='Albers_Conic_Equal_Area')
		$proj_type='proj=aea';
	elseif ($type=='Transverse_Mercator')
		$proj_type='proj=utm';
	else die("The projection of the image isn't supported yet");

	return $proj_type;
}

function get_datum($datum) {
	if ($datum=='North_American_Datum_1983')
		$datum_string='datum=NAD83';
	elseif ($datum == 'WGS_1984')
		$datum_string='datum=WGS84';
	else die('unknow datum');
	return $datum_string;
}

function get_ellips($ellips) {
	if ($ellips[0]=='Unknown') {
		$a=$SPHEROID[1];
    $b=$SPHEROID[1]-$SPHEROID[0];
    $ellips_string='a='.$a.'+b='.$b;
	} elseif ($ellips[0]=='WGS 84') {
		$ellips_string='ellips=WGS84';
	} elseif ($ellips[0]=='GRS 1980'){
		$ellips_string='ellips=GRS80';
	}	else die('unknown spheroid');
	return $ellips_string;
}
/*
proj=laea  +lat_0=Latitude at projection center
              +lon_0=Longitude at projection center
              +x_0=False Easting
              +y_0=False Northing

+proj=aea   +lat_1=Latitude of first standard parallel
            +lat_2=Latitude of second standard parallel
             +lat_0=Latitude of false origin
              +lon_0=Longitude of false origin
              +x_0=Easting of false origin
              +y_0=Northing of false origin
*/

function get_parameter($parameter,$type){
	$para=array();
	if ($type=='proj=laea'){
		$para[]='lat_0='.$parameter[0];
		$para[]='lon_0='.$parameter[1];
		$para[]='x_0='.$parameter[2];
		$para[]='y_0='.$parameter[3];
	} elseif ($type=='proj=aea'){
		$para[]='lat_1='.$parameter[0];
		$para[]='lat_2='.$parameter[1];
		$para[]='lat_0='.$parameter[2];
		$para[]='lon_0='.$parameter[3];
		$para[]='x_0='.$parameter[4];
		$para[]='y_0='.$parameter[5];
	} elseif ($type=='proj=utm'){
		$zone_number=(int)((183+$parameter[1])/6);
		$para[]='zone='.$zone_number;
	} else die('unknow projection');
	return $para;
}

function get_proj($img_name) {

	$proj=proj_init($img_name);

	$projection=array();
	$projection[]=get_type($proj['PROJECTION'][0]);
	$projection[]=get_datum($proj['DATUM'][0]);

	$spheroid=explode(',',$proj['SPHEROID'][0]);
	$projection[]=get_ellips($spheroid);

	$parameter=get_parameter($proj['PARAMETER'],$projection[0]);

	$projection=array_merge($projection,$parameter);
	$proj_string=$proj_type.'+'.$proj_datum.'+'.$proj_ellips;
	return $projection;
} //end of function get_proj

if (!$_POST['file_name']){
?>
<html>
  <head>
    <title>Please  input  the  file name </title>
  </head>
  <body>
  <form method=POST action="<?= $PHP_SELF; ?>" >
  File name:
  <input TYPE=file NAME="file_name"  SIZE=30 > <br>
  <input TYPE=submit VALUE="Submit">
  </body>
</html>
<?php
} else {
	$projection=get_proj($_POST['file_name']);
	print_r($projection);
}
?>

University of North Dakota




>From: "Juanse" <georef at tie.cl>
>To: "pond bear" <pond_bear at hotmail.com>
>Subject: RE: [Mapserver-users] get proj.4 parameters from a raster image.
>Date: Thu, 26 Jun 2003 10:39:19 -0400
>
>Do not know if Chips can do it because i am starting to use it, but Chips 
>is
>an excelent piece of software for handling georeferenced rasters that i 
>have
>just found. And it is crackeable and unexpensive if you can buy it, and 
>with
>decent 15 days on trial (being able to export/save changes), and then keeps
>on working without saving capabilities.
>
>check here http://www.geogr.ku.dk/chips/
>
>for the crack ... ask.
>
>juanse
>temuko-Chile
>
>
>----- Original Message -----
>From: pond bear <pond_bear at hotmail.com>
>To: <mapserver-users at lists.gis.umn.edu>
>Sent: Thursday, June 26, 2003 3:58 PM
>Subject: [Mapserver-users] get proj.4 parameters from a raster image.
>
>
> > Hi all
> >
> > Is there any tools which can read the raster tif file and return the
>proj.4
> > string of the image.
> >
> > Jing
> > University of North Dakota
> >
> > _________________________________________________________________
> > The new MSN 8: smart spam protection and 2 months FREE*
> > http://join.msn.com/?page=features/junkmail
> >
> > _______________________________________________
> > Mapserver-users mailing list
> > Mapserver-users at lists.gis.umn.edu
> > http://lists.gis.umn.edu/mailman/listinfo/mapserver-users
>
>
>---
>Outgoing mail is certified Virus Free.
>Checked by AVG anti-virus system (http://www.grisoft.com).
>Version: 6.0.491 / Virus Database: 290 - Release Date: 18/06/03
>

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online  
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963




More information about the mapserver-users mailing list