[Mapserver-users] adding shp to mapfile / projection

Paul Dymecki millardymecki at sympatico.ca
Mon Mar 3 12:58:14 PST 2003


This is a multi-part message in MIME format.

------=_NextPart_000_0166_01C2E19D.B2E21030
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hi,
   Yes i think i've covered all the obvious, things, though you never know.
Here are my two scripts that query the db and create the shp and dbf files.
 shp_ukpdate.php queries the db and eman_shp.php has supporting classes
thx,
Paul
----- Original Message -----
From: "Daniel Morissette" <morissette at dmsolutions.ca>
To: "Paul Dymecki" <millardymecki at sympatico.ca>
Sent: Monday, March 03, 2003 3:38 PM
Subject: Re: [Mapserver-users] adding shp to mapfile / projection


> Paul Dymecki wrote:
> >
> > Hi Daniel,
> >      From talking to people i think i've narrowed it down to a problem
with
> > my shp file format.  Any ways here is my current projection function,
create
> > point, and close() functions if you can see anything that's off?
> > thx,
> > Paul
> >
>
> From reading the replies on the list this is probably the next logical
> thing to check, but without seeing your complete application including
> scripts and data nobody can confirm that.
>
> Did you make sure that you pass a valid shapefile type to the call to
> ms_newShapeFileObj()?
>
> BTW, please reply via the list, this will increase your chances of
> getting help.
>
> Daniel
> --
> ------------------------------------------------------------
>  Daniel Morissette               morissette at dmsolutions.ca
>  DM Solutions Group              http://www.dmsolutions.ca/
> ------------------------------------------------------------

------=_NextPart_000_0166_01C2E19D.B2E21030
Content-Type: application/octet-stream;
	name="eman_shape.php"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="eman_shape.php"

<?php
/**
 * @descript:   Contains underlying classes called from shp_update
 *              and shp_insert.
 * @author      Paul Dymecki
 * @contact     millardymecki at sympatico.ca=20
 * @classes:    Eman_Shape,Eman_Projection,Eman_Logger
 *
 *
 *=20
 */

if ( !extension_loaded("PHPPROJ") )
  {
      dl("php_proj.dll");
  }

if ( !extension_loaded("MapScript") )
  {
      dl("php_Mapscript_36.dll");
	=20
  }
class Eman_Shape
{
	var $shpFname;
	var $dbfFname;
	var $dbfFile;
	var $shpFile;
      var $oShp;
	var $dBaseColumns;

	function eman_shape($shpFilename)
	{
	  $this-> dBaseColumns =3D
				 array(
        array("Ename",     "C" ,80),
        array("Fname",     "C",  80),
        array("latitude",      "N",   16, 5),
        array("longitude",    "N", 16,5),
        array("pk_id", "N",  5,0)
    );

		=09
  =20
	  $this-> shpFname =3D $shpFilename;
	  $this-> dbfFname =3D $this->shpFname.".dbf"; =20
	}
=09
	function createShape()
	{
	  $this->shpFile =3D ms_newShapeFileObj($this->shpFname, MS_SHP_POINT);
        $this->oShp =3D ms_newShapeObj(MS_SHAPE_POINT);
	}

    function openShape()
	{
	=20
=09
	  if(file_exists($this->shpFname.'.shp'))
	  {
		$this->shpFile =3D ms_newShapeFileObj( $this->shpFname, -2);
		return TRUE;
	  }
	  else=20
	  {
		  $message =3D "Error:The required shape file does not exist";
		  return FALSE;
		  //exit;
	  }
	 =20
	}
=09
  =20

    function createPoint( $x, $y, $def )
	{
	// Create shape
	   $oPoint =3D ms_newPointObj();
         $oPoint->setXY($x,$y);        =20
         $this->shpFile->addPoint($oPoint);
	   // Write attribute record
	   if( dbase_add_record($this->dbfFile, $def) =3D=3D FALSE )
	   {
		 $message =3D "Adding a record to dbase failed";	=20
		 $this->shpFile->free();
		 $oPoint->free();
		 return FALSE;
	   }
 =20
        $oPoint->free();
	   return TRUE;
	 }

    =20
	 function createDbaseFile()
	{
	  //IF FILE EXISTS DELETE IT
	  $file =3D $this->dbfFname;
	  if (file_exists($file))
         {=20
      =20
	    unlink($file);
          }
	=20
	 // database "definition"
	  //if (FALSE =3D=3D dbase_create($this->dbfFname, =
$this->dbaseColumns))
	  if (!dbase_create($this->dbfFname, $this->dBaseColumns))
	  {
		return FALSE;
	=09
	  }
	  else
	  {
	    return TRUE;
	  }
	}
=09
	 function openDbase()
	{
		 if (FALSE !=3D ($this->dbfFile =3D dbase_open( $this->dbfFname, 2)))
		 {
		   return TRUE;
		 }
		 else
		 {
		   return FALSE;=09
		 }
	 }
	function close()
	{
	 $this->shpFile->free();
     	 dbase_close($this->dbfFile);
       fclose($this->dbfFile);
	 } =20

}
class Eman_Projection
{

	var $lccXY;

	function projectPoint($lat,$lon)
	  {
		$dlat =3D doubleval($lat);
		$dlon =3D doubleval($lon);

		//project from geodetic coordinate degrees
		$parms1[0] =3D "proj=3Dlonglat";
		$parms1[1] =3D "ellps=3DGRS80";
		$parms1[2] =3D "datum=3DNAD83";  =20
		$parms1[3] =3D "no_defs";
		$projLonLat =3D pj_init($parms1);

		//project to lambert conical x,y
		$parms2[0] =3D "proj=3Dlcc";
		$parms2[1] =3D "ellps=3DGRS80";
		$parms2[2] =3D "datum=3DNAD83";
		$parms2[3] =3D "lat_0=3D 49.0";
		$parms2[4] =3D "lon_0=3D -95.0";
		$parms2[5] =3D "lat_1=3D 49.0";
		$parms2[6] =3D "lat_2=3D 77.0";
		$parms2[7] =3D "units=3Dm";



		$projLCC =3D pj_init($parms2);

		$this->lccXY =3D pj_transform($projLonLat, $projLCC, $dlon, $dlat,0 );
		//$this->lccXY =3D array("u"=3D> 4,"v"=3D>5);
	=09
	   pj_free($pj);
		}
		function getXYCoords()
		{=20
		if (0 !=3D array_count_values($this->lccXY) )
		{
	      return($this->lccXY);=20
		  }
	   else
	   {
	      return 0;
	    }
	 }
}

class EmanLogger
{
	var $toEmail;
	var $logFile;

	function emanLogger($email,$logFileName)
	{
	  $this -> toEmail =3D $email;
	  $this -> logFile =3D $logFileName;
	}
    function logMessage($msg,$rec)
	{
	  $d =3D date("F j, Y, g:i:s a");
	  $logMsg =3D $msg." ".$d." \n";
	  $logMsg.=3D $rec;
	  error_log($logMsg, 3, $this->logFile);
	}



	function sendErrorMail($subject,$msg,$rec,$myname)
	{
        $formattedMsg =3D "Error in script: ".$myname."<br>\n";
	 $formattedMsg.=3D "<p><b>".$msg."</b></p><br>\n";
	 $formattedMsg.=3D "Error while trying to insert the following =
record:<br>\n";
	 $formattedMsg.=3D $rec."<br>\n";
     =20

	 $myemail =3D "webmaster at eman-rese.ca";
	 $contactname =3D "error";
	 $contactemail =3D "webmaster at rese.ca";
	 $myreplyemail =3D "webmaster at rese.ca";
	  $headers .=3D "MIME-Version: 1.0\r\n";=20
	  $headers .=3D "Content-type: text/html; charset=3Diso-8859-1\r\n";=20
	  $headers .=3D "From: ".$myname." <".$myemail.">\r\n"; =20
	  $headers .=3D "To: ".$contactname." <".$contactemail.">\r\n";=20
	  $headers .=3D "Reply-To: ".$myname." <$myreplyemail>\r\n";=20
	  $headers .=3D "X-Priority: 1\r\n";=20
	  $headers .=3D "X-MSMail-Priority: High\r\n";=20
	 mail($this->toEmail, $subject, $formattedMsg,$headers);

	}
}


?>
------=_NextPart_000_0166_01C2E19D.B2E21030
Content-Type: application/octet-stream;
	name="shp_update.php"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="shp_update.php"

<html> <head> <title>Shape Update Script</title> </head>
<body>
<?php
/**
 * @author      Paul Dymecki
 * @contact     millardymecki at sympatico.ca=20
 * @inputs:     None
 * @outputs:    A new shp and dbf file with lat/lon having been =
projected to
 *              lcc.=20
 * @descript:   Database is queried for all records and new shp files =
are=20
 *              created.
 *
 *
 *=20
 */

//$cwd =3D getcwd();

include_once("eman_shape.php");
$TO_EMAIL =3D "millardymecki at sympatico.ca";
$MY_NAME =3D "shp_update.php";
$NO_RECORD =3D 0;
echo 'hello';

if (isset($_GET['shapefilepath'])) {
  $shpFname =3D $_GET['shapefilepath'];
  $SHP_FILE .=3D substr($shpFname,0, -4);//chop extension off
  echo $SHP_FILE;

} else {
  $SHP_FILE =3D 'c:\program files\apache =
group\apache2\htdocs\maplab-2.0rc3\tutorial\data\sites';
}

if (isset($_GET['logfilepath'])) {
  $LOG_FILE =3D $_GET['logfilepath'];
} else {
  $LOG_FILE =3D 'c:\shp_update.log';
}

echo($x." ".$y);

$oEmanLogger =3D new EmanLogger($TO_EMAIL,$LOG_FILE);
$oEmanProj =3D new Eman_Projection();
$oEmanShp =3D new Eman_Shape($SHP_FILE);
//----------------------------------------------------------
// -----------Begin Initialization Section -----------------
//----------------------------------------------------------

//------create new shape file
$oEmanShp->createShape();
echo "dbName ".$oEmanShp->shpFname;

//------open new shape file
if(FALSE =3D=3D ($oEmanShp->openShape()))
{
  $message =3D "Error opening SHAPE file";
  $oEmanLogger->sendErrorMail("Error in =
shp_update.php",$message,$NO_RECORD,$MY_NAME);
}

//------create new dbase file
if(FALSE =3D=3D ($oEmanShp->createDbaseFile()))
{
  $message =3D "Error creating DBASE file:".$oEmanShp->dbfFname;
  $oEmanLogger->sendErrorMail("Error in =
shp_update.php",$message,$NO_RECORD,$MY_NAME);
}

//------open dbase file

if(FALSE =3D=3D $oEmanShp->openDbase())
{
  $message =3D "Error opening DBASE file";
  $oEmanLogger->sendErrorMail("Error in =
shp_update.php",$message,$NO_RECORD,$MY_NAME);
  $oEmanShp->close();
 =20
}

//----------------------------------------------------------
// -----------End Initialization Section -----------------
//----------------------------------------------------------

//----------------------------------------------------------
// -----------Loop through the site table -----------------
//----------------------------------------------------------


$connect =3D odbc_connect('mon3', 'po8', 'po8', SQL_CUR_USE_ODBC);
$query =3D "SELECT pk_site, english_fullname, french_fullname, latitude, =
longitude FROM site, coordinates WHERE fk_coordinates =3D =
pk_coordinates";
$result =3D odbc_exec($connect, $query);
//Loop through and add records

while (odbc_fetch_row($result)) {
  $recordToAdd =3D "";
  $count =3D $count + 1;
  echo "num recs".$count;
  $frenchname =3D odbc_result($result, 3);
  $englishname =3D odbc_result($result, 2);
  $pk_site =3D odbc_result($result, 1);
  $latitude =3D odbc_result($result, 4);
  $longitude =3D odbc_result($result, 5);
  $recordToAdd .=3D "PK_SITE_ID: ".$pk_site."\n";
  $dbaseRecord =3D =
array($englishname,$frenchname,$latitude,$longitude,$pk_site);
 =20
  //currently not projecting just using lat/long vals
  //here are some sample values
  // 50.57  -60.02
  // 46.01  -81.24
  // 69.21  -108.06
  /*
  $oEmanProj->projectPoint($latitude,$longitude);
  $lccXY =3D $oEmanProj->getXYCoords();
 =20
  if($lccXY !=3D 0)
  {
   $x =3D ($lccXY["u"]);
   $y =3D ($lccXY["v"]);
  }
  else
  {
    $message =3D "Error trying to project points";
    $oEmanLogger->sendErrorMail("Error in =
shp_update.php",$message,$recordToAdd,$MY_NAME);
  }

   echo "<br>".$x."<br>";
   echo $y."<br>";
    */=20
  //i'm currently not using the projection just lat/lon values
  if($oEmanShp->createPoint( $longitude, $latitude, $dbaseRecord))
  {
    $oEmanLogger->logMessage("Successfully added/updated Record at =
",$recordToAdd);
    echo "Shapes Created.<BR>";
  }
  else
  {
    $oEmanLogger->logMessage("Unsuccessfully tried to add/update a =
Record at ",$recordToAdd);
    $message =3D "Unknown Error in createPoint function";
    $oEmanLogger->sendErrorMail("Error in =
shp_update.php",$message,$recordToAdd,$MY_NAME);
  }
}
$oEmanShp->close();
odbc_close($connect);



?>
</body>
</html>
------=_NextPart_000_0166_01C2E19D.B2E21030--




More information about the MapServer-users mailing list