#native_company# #native_desc#
#native_cta#

GIS Mapping with PHP; Part Two Page 4

By Simon Moss
on February 9, 2004

The import script is made up of two functions, LoadMIF and GetPolyString.

<?php

  

function LoadMIF($file)



    
//Loads MIF file into a set of arrays.

    //Open the file

    
$hfile fopen("$file""r");

      
$polygons = array();

      
$in_data false;

    
// Read through the file until we hit the end

    
while (!feof($hfile)) 

       { 

         
$line strtoupper(fgets($hfile1024));

        
// You could do this with reg. expressions. I hate them. So there.

        // The DATA tag tells us we have got past the header info 

        // and are into the vector data proper.

        
if(substr($line,0,4)=="DATA"

           { 

               
$in_data=true;

          } 

           else if(
$in_data

           { 

            
// Are we a LINE? NB we don't plot these in this article.

             
if(substr($line,0,4)=="LINE"

               { 

                   
$array explode(" ",$line);

                
$poly_info = array();

                
$poly_info["min_long"] = $long 

                
trim($array[1]);

                
$poly_info["min_lat"] = $lat trim($array[2]);

                
$poly_info["max_long"] = $long_to 

                
trim($array[3]);

                
$poly_info["max_lat"] = $lat_to 

                
trim($array[4]);

                
$poly_info["vector_type"] = 1;

                
$poly_info["poly_count"] = 2;

                
$poly_info["poly_string"] = "$long 

                $lat $long_to $lat_to"
;

            } 

            
// Are we a PLINE ( poly-line: A hollow polygon )

             
else if(substr($line,0,5)=="PLINE")

               { 

                   
$array explode(" ",$line);

                
// Get all the points in this polygon

                // The first word on the line always stores 

                // the number of points in the polygon

                
$poly_info GetPolyString($hfile,$array[1]);

                   
$poly_info["vector_type"] = 2;

               } 

            
// Are we a region ( a filled polygon )

             
else if(substr($line,0,6)=="REGION"

               { 

                  
$line fgets($hfile

                  
1024);

                
// Again, get all the points in this polygon

                // The first line always stores the number of points in the polygon

                 
$poly_info 

                  
GetPolyString($hfile,$line);

                  
$poly_info["vector_type"] = 3;

               } 

            if(isset(
$poly_info))

            {     

                   
$polygons[] = $poly_info;

                   unset(
$poly_info);

              }

          } 

       }  

       
fclose($hfile);

    return 
$polygons;

function GetPolyString($hfile,$poly_count



   
$ret_vector = array();

   
$ret_vector["min_long"] = 9999999;

   
$ret_vector["min_lat"] = 9999999;

   
$ret_vector["max_long"] = -9999999;

   
$ret_vector["max_lat"] = -9999999;

   
$ret_vector["poly_string"] = "";

   
$ret_vector["poly_count"] = $poly_count;

    
// Loop though the coordinates

      // Each line contains the long. and lats. coordinates

      // delimited by a space.

  

       
for($i=0;$i<$ret_vector->poly_count;$i++) 

    { 

           
$line fgets($hfile1024);

           
$array explode(" ",$line);

          
$long $array[0];

        
$lat $array[1];

        
$ret_vector["min_long"] = min($long,$ret_vector["min_long"]);

        
$ret_vector["min_lat"] = min($lat ,$ret_vector["min_lat"]);

        
$ret_vector["max_long"] = max($long,$ret_vector["max_long"]);

        
$ret_vector["max_lat"] = max($lat ,$ret_vector["max_lat"]);

           if(!empty(
$ret_vector["poly_line"]))$ret_vector["poly_line"] .=    " ";

           
$ret_vector["poly_line"] .= "$long $lat";

      } 

      return 
$ret_vector;

}

?>



1
|
2
|
3
|
4
|
5
|
6
|
7