Parsing coordinates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Umar99
Forum Newbie
Posts: 2
Joined: Mon Jan 05, 2009 7:09 pm

Parsing coordinates

Post by Umar99 »

Hi,

I'm a complete PHP newbie trying to get above Filemaker Pro scripts.

I have a map87.txt file which contains point coordinates (POI) and coordinates which forms a line (POLYLINE):
...
...
[POI]
Type=0x6401
CountryName=MALAYSIA~[0x1d]MYS
RegionName=PAHANG~[0x1d]PHG
CityName=KUANTAN
Data0=(3.83569,103.33313)
[END]

[POLYLINE]
Type=0x2
Label=~[0x06]12
EndLevel=4
RoadID=771
RouteParam=5,3,0,0,0,0,0,0,0,0,0,0
Data0=(3.70593,103.12008),(3.70227,103.12075),(3.70145,103.12069),(3.69467,103.11905),(3.69265,103.11890),(3.68750,103.11922),(3.68540,103.11905),(3.68402,103.11875),(3.68175,103.11787),(3.67527,103.11581),(3.67407,103.11551),(3.67300,103.11532),(3.67064,103.11499),(3.66909,103.11480),(3.66667,103.11492)
Nod1=0,281773,0
Nod2=11,281765,0
Nod3=14,286218,1
[END]
...
..
Coordinates - in (latitude, longitude) format - which are bolded above is the only text that I want to parse for out-of-bounds values. That is just a snip. The largest file size is 3MB and contains hundreds and thousands of coordinate data.

The reference bound coordinates are in a separate bounds.txt file which contains the North (latitude), South (latitude), East (longitude) and West (longitude):
...
...
map86.txt N4.00000 S3.66667 E102.50000 W103.00000
map87.txt N4.00000 S3.66667 E103.00000 W103.50000
...
...
How can I write a PHP code to evaluate whether all the coordinates in the map87.txt file is within the bounds of the bounds.txt file?

Should I:

1. Use regular expressions - research tells me that this will be slow
2. Put the coordinates in an array - is this faster? How do I tell PHP to put the coordinates into an array and evaluate whether:

a) 3.66667 < latitude < 4.00000
b) 103.00000 < longitude < 103.50000

and then list all coordinates which are out-of-bounds?

Thanks heaps in advance.

Warm regards,
Umar
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Parsing coordinates

Post by requinix »

Regular expressions aren't slow, they're relatively slow. A lot of the time people use them when they should be using plain-old string functions, but in this case...

For the bounds, read lines from the file until you find one that starts with the file name you want. Then take the line and run it through

Code: Select all

preg_match('/N([\d.]+) S([\d.]+) E([\d.]+) W([\d.]+)/i', $line, $bounds);
$bounds[1] [2] [3] [4] are the N, S, E, and W boundaries respectively.

For the coordinates, it looks like it would be safe to assume that any "(number,number)" in the file is a pair you want so I'd use a regular expression to grab them all.

Code: Select all

preg_match_all('/\(([\d.]+),([\d.]+)\)/', $file, $coords, PREG_SET_ORDER);
$coords is an multidimensional array with "array(coordinates, latitude, longitude)" elements.

Then loop through $coords looking for something that's out of bounds.
Umar99
Forum Newbie
Posts: 2
Joined: Mon Jan 05, 2009 7:09 pm

Re: Parsing coordinates

Post by Umar99 »

Thanks Tasairis. Yours looks simpler than mine. Please don't laugh:

Code: Select all

<?php
 
$ignore = array('.', '..', '.svn', '_Legend.mp');
$path = 'D:\GPS\masmap\sources\trunk';
 
$files = scandir($path);
$files = array_diff($files, $ignore);
 
$content = file_get_contents($path . DIRECTORY_SEPARATOR . 'map87.txt');
 
$find = '/Data0=\(.*/';
 
preg_match_all($find, $content, $out);
 
foreach ($out[0] as $o) 
    {
        $points[] = explode(',', substr($o, 6));
    }
 
foreach ($points as $i => $point) 
    {
        foreach ($point as $key => $coord) 
            {
                $coord = str_replace(array('(', ')'), '', $coord);
 
                if ($coord > 103.5 or $coord < 3.66667) 
                    {
                        echo "Out of bound error: $coord\n";
                    }
                        else 
                    {
                        if ($coord > 4 && $coord < 103) 
                            {
                                echo "Out of bound error: $coord\n";
                            }
                    }
            }
    }
?>
Post Reply