Jump to content

find the parse errors #2


a2bardeals

Recommended Posts

i am posting a geocoding script that i found online that gives me parse errors like crazy. I need this to work. I can't figure out the problem.

 

The first error i get is:

Parse error: parse error in /Library/WebServer/Documents/bar/ver2/geocode/geo.php on line 3

 

I am also using PHP 4.1.2 if that makes a difference.

 

When I fix the obvious one which i belive is the address input value:

Address: <input type="text" size="40" name="address" value="<?=$_REQUEST[" />">

to

Address: <input type=text" size="40" name="address" value="<? $_REQUEST["address] ?>">

 

I get a parse error on line 10

 

Here it is:

 

<?
define("DEBUG", false);
// ===== Database parameters
$db["user"] = "username";
$db["pass"] = "password";
$db["host"] = "localhost";
$db["database"] = "dbname";
?>
<h2>Geocode</h2>
<form name="mainform" action="geocode.php" method="get">
Address: <input type="text" size="40" name="address" value="<?=$_REQUEST[" />">
<input type="submit" name="geo" value="Geocode" />
</form> <hr />
<?
if ( array_key_exists("geo", $_REQUEST) ) {
showresults();
}
?><?php// ====================================================================
// ==================================================================== PHP Functions
// ====================================================================
// ==================================================================== show results
//
function showresults() {
$address = $_REQUEST["address"];
// === Parse the address into it's various parts.
$paddr = parse_address($address);
// === Query the database for matching streets.
$rsary = find_address($paddr);
if ( !$rsary || count($rsary)> 1 ) {
echo "No records found or more than one street segment matched. Be more specific.
";
return;
}
// === Geocode the address.
$latlon = geocode_address($rsary[0], $paddr);
// === Display the lat/lon and a google maps link
echo $latlon["lat"] . ", " . $latlon["lon"];
echo "
<a href="http://blog.tooleshed.com/%5C%22http://maps.google.com/maps?f=q&hl=en&q=%22">Google Map</a>";
}
// ==================================================================== geocode
//
function geocode_address($data, $addr) {
$house_number = $addr["number"];
$from_number = $data["from_addr"];
$to_number = $data["to_addr"];
// === Convert the linedata string into an array of lat/lon pairs
$chain = parse_linedata($data["linedata"]);
// === Find the proportion between our house number and the fr/to numbers of the street segement
$prop = ($house_number - $from_number) / ($to_number - $from_number);
debug("house: $house_number fr: $from_number to: $to_number prop: $prop");
debug($chain);
if ( count($chain) <2 ) {
die("Error, linedata field bad: " . $data["linedata"] . "
");
}
if ( count($chain) == 2 ) {
// === Using linear interpolation, we can calculate the lat/lon of our house on the street segment
$house = lerp($chain[0], $chain[1], $prop);
} else {
// === The chain has more than two points, so a more complex calculation is required.
$house = calc_position($chain, $prop);
}
return $house;
}
// ==================================================================== calc_position
//
function calc_position($chain, $prop) {
$total_dist = 0;
// First calculate the distance between each set of points
// and the total distance of our entire street segment
for ($i=0; $i
$dist = calc_dist($chain[$i], $chain[$i+1]);
$p2p_dist[$i] = $dist;
$total_dist += $dist;
debug("Distance from Point $i to " . ($i+1) . " = $dist  ($total_dist)");
}
// Now that we have the total distance we can figure out where
// on the segment our address will be using the proportion.
$house_dist = $total_dist * $prop;
// Next we need to determine between which two set of points
// our house_dist lies.
$i=0;
$part_dist = 0;
while ($part_dist <$house_dist) {
$part_dist += $p2p_dist[$i++];
}
$i--;
// Now we need to find the proportion at the partial distance level
$part_from = $part_dist - $p2p_dist[$i];
$part_prop = ($house_dist - $part_from) / ($part_dist - $part_from);
debug("i: $i house_dist: $house_dist part_dist: $part_dist, part_from: $part_from, part_prop: $part_prop");
// And with that we can now do the interpolation betwen the points at
// that sub-part of the street segment.
$house = lerp($chain[$i], $chain[$i+1], $part_prop);
debug($house);
return $house;
}
// ==================================================================== calc_dist
// Calculates the distance between two lat/lon points
// See: http://www.meridianworlddata.com/Distance-Calculation.asp
function calc_dist($fr, $to) {
$lat1 = deg2rad($fr["lat"]);
$lon1 = deg2rad($fr["lon"]);
$lat2 = deg2rad($to["lat"]);
$lon2 = deg2rad($to["lon"]);
$d = 3963 * acos( sin($lat1)*sin($lat2) + cos($lat1)*cos($lat2)*cos($lon2 - $lon1) );
return $d;
}
// ==================================================================== lerp
// Performs linear interpolation on the lat and lon
function lerp($fr, $to, $prop) {
$frlat = $fr["lat"];
$frlon = $fr["lon"];
$tolat = $to["lat"];
$tolon = $to["lon"];
$point["lat"] = $frlat + ($prop * ($tolat - $frlat));
$point["lon"] = $frlon + ($prop * ($tolon - $frlon));
return $point;
}
// ==================================================================== parse_linedata
//
function parse_linedata($linedata) {
// === Explode the linedata string of lat/lon's into an array:
/* Example returned array:
Array
(
[0] => Array
(
[lon] => -122.628906
[lat] => 45.53354
)
[1] => Array
(
[lon] => -122.627906
[lat] => 45.53354
)
)
*/
foreach ( explode(",", $linedata) as $latlon) {
list($lon, $lat) = explode(" ", $latlon);
$ll[] = array("lon" => ($lon/1000000), "lat" => ($lat/1000000));
}
return $ll;
}
// ==================================================================== find_address
// Build a query to find the street in the database
//
function find_address($addr) {
global $db;
require_once("adodb/adodb.inc.php");
$number = $addr["number"];
$dsn = "mysql://" . $db["user"] . ":" . $db["pass"] . "@" . $db["host"] . "/" . $db["database"];
$dbConn = ADONewConnection($dsn);
$dbConn->debug = DEBUG;
if (!$dbConn) { echo("Connection failed");  exit; }
$q = "  select cc_id, linedata, fedirp, fename, fetype, fedirs,
if(fraddl % 2 = $number % 2, fraddl, fraddr) as from_addr,
if(fraddl % 2 = $number % 2, toaddl, toaddr) as to_addr
from completechain ";
$q .= "where fename = " . $dbConn->qstr($addr["name"]) . " ";
// === Build the Where clause
$wh = "";
if ( array_key_exists("prefix", $addr) ) {
$wh .= "and fedirp = " . $dbConn->qstr($addr["prefix"]) . " ";
}
if ( array_key_exists("type", $addr) ) {
$wh .= "and fetype = " . $dbConn->qstr($addr["type"]) . " ";
}
if ( array_key_exists("suffix", $addr) ) {
$wh .= "and fedirs = " . $dbConn->qstr($addr["suffix"]) . " ";
}
$wh .= "and (
(fraddr <= $number and toaddr>= $number) OR
(fraddl <= $number and toaddl>= $number)
)";
// === Add the where to the query
$q .= $wh;
debug($q);
$rs = $dbConn->GetAll($q);
$dbConn->Close();
return $rs;
}
// ==================================================================== parse_address
//
function parse_address($address="") {
$dir=array(
"N"=>"N","S"=>"S","E"=>"E","W"=>"W","NW"=>"NW","SW"=>"SW","NE"=>"NE","SE"=>"SE",
"North"=>"N","South"=>"S","East"=>"E","West"=>"W","Northwest"=>"NW",
"Southwest"=>"SW","Northeast"=>"NE","Southeast"=>"SE"
);
$type=array(
"ave"=>"Ave","blvd"=>"Blvd","st"=>"St","wy"=>"Wy","cir"=>"Cir","dr"=>"Dr",
"ln"=>"Ln","Pl"=>"Pl","Rd"=>"Rd","Bvd"=>"Blvd","Avenue"=>"Ave","Boulevard"=>"Blvd",
"Street"=>"St","Way"=>"Wy","Circle"=>"Cir","Drive"=>"Dr","Lane"=>"Ln","Place"=>"Pl",
"Road"=>"Rd"
);
$apts = "Apt|Apartment|Suite|Ste|Unit|Bldg|Building|Room|Rm|#";
$out["address_type"] = "Presumed Standard";
$address=trim($address);
$out["raw_address"] = $address;
$original = $address;
//remove any unit or apt # from the end
//a number alone at the end is not enough, we need at least # or one of the descriptors in ()
if( preg_match('/(\s+(' . $apts . ')\s*)+#?[-a-z0-9]+$/i', $address, $matches)){
$out["raw_unit"] = $matches[0];
$out["unit"] = preg_replace('/(\s+(' . $apts . ')\s*)+#?/i','',$matches[0]);
//break raw unit down
$address = substr($address, 0, strlen($address)-strlen($matches[0]));
}
//parse suffix direction (SW)
$dirs = implode("|", array_keys($dir));
if(preg_match('/\s+(' . $dirs . ')$/i', $address, $matches)){
$out["raw_suffix"] = trim($matches[0]);
$out["suffix"] = $dir[$out["raw_suffix"]];
$address=substr($address, 0, strlen($address)-strlen($matches[0]));
}
//remove type of street
$types = implode("|", array_keys($type));
if(preg_match('/\s+(' . $types . ')$/i', $address, $matches)){
$out["raw_type"] = trim($matches[0]);
strlen($out["raw_type"])> 3 || strtolower($out["raw_type"]) == 'way'
|| strtolower($out["raw_type"]) == 'bvd' ? $typeDefinite = false : $typeDefinite = true;
$out[type] = $type[strtolower($out["raw_type"])];
$address = substr($address, 0, strlen($address)-strlen($matches[0]));
}
//remove number and fraction
if(preg_match('/^[0-9]+(\s+[0-9]+\/[0-9]+)*/', $address, $matches)){
$address = substr($address, strlen($matches[0]), strlen($address)-strlen($matches[0]));
if(preg_match('/\s+[0-9]+\/[0-9]+$/',$matches[0],$mmatch)){
$out["fraction"] = $mmatch[0];
$matches[0] = substr($matches[0], 0, strlen($matches[0])-strlen($mmatch[0]));
}
$out[number] = trim($matches[0]);
$numberFormat = 'standard';
} else {
$numberFormat = 'irregular';
//account for possible P.O. Boxes and Rural Routes
if(preg_match('/^(POB\s+|P\s*O\s*Box|Post Office Box|Postal Box|Box|Boite Postal)\s*[0-9a-z]+(-[0-9a-z]+)*/i', $address, $matches)) {
$out["raw_po_box"] = $matches[0];
preg_match('/[0-9a-z]+(-[0-9a-z]+)*$/i', $matches[0], $mmatch);
$out["po_box"] = strtoupper($mmatch[0]);
$out["address_type"] = "Post Office Box";
}
if(preg_match('/(Rrte|RR|Rural Route|Rt|Rte|Route)\s+[0-9]+\s+(Box|Bx)\s+[0-9]+/i', $address, $matches)){
$out["raw_route"] = $matches[0];
$matches = explode('b',strtolower($matches[0]));
$out["route_number"] = preg_replace('/[^0-9]+/', '', $matches[0]);
$out["route_box_number"] = preg_replace('/[^0-9]+/', '', $matches[1]);
$out["address_type"] = "Rural Route";
}
//Account for HC nomenclature -- for drawmack
if(preg_match('/(HC|Highway County|Hwy Cty|Hwy County)\s+[0-9]+\s+(Box|Bx)\s+[0-9]+/i',$address,$matches)){
$out["raw_hc"]=$matches[0];
$matches=explode('b', strtolower($matches[0]));
$out["hc_number"] = preg_replace('/[^0-9]+/', '', $matches[0]);
$out["hc_box_number"] = preg_replace('/[^0-9]+/', '',$matches[1]);
$out["address_type"] = "Highway County Route";
}
//Account for * | Star Route
if(preg_match('/(\*\s+Rte|\*\s+Route|Star\s+Route|Star\s+Rte)\s+[0-9]+\s+(Box|Bx)\s+[0-9]+/i',$address,$matches)){
$out["raw_starrt"]=$matches[0];
$matches=explode('b',strtolower($matches[0]));
$out["starrt_number"]=preg_replace('/[^0-9]+/','',$matches[0]);
$out["starrt_box_number"]=preg_replace('/[^0-9]+/','',$matches[1]);
$out["address_type"]="Star Route";
}
/***
Note on the above 4 nodes: we don't check that an address only partially
conforms, such as Rte 1 (no box number), and perhaps we should.  Perhaps
"Route 1" is even OK in some areas
***/
}
//what remains is the prefix direction, and street, several analyses to make here
/***
note that if there is still an address left over yet we pulled a PO Box above
or a Rural Route, then either something is wrong or our code missed something,
this should be flagged.
***/
$address = trim($address);
if(preg_match('/^(' . $dirs . ')\s+/i', $address, $matches)){
$out["prefix"] = $dir[trim($matches[0])];
strlen($matches[0])> 2 ? $out["raw_prefix"] = $matches[0] : '';
$address = substr($address, strlen($matches[0]), strlen($address)-strlen($matches[0]));
}
//presume all else is the name
$out["name"] = trim($address);
//present the array visibly in a logical order -- not required for operation but nice
$order=array(
"type_definite",
"address_type",
"route_number",
"route_box_number",
"hc_number",
"hc_box_number",
"starrt_number",
"starrt_box_number",
"po_box",
"number",
"fraction",
"prefix",
"name",
"type",
"suffix",
"unit",
"raw_po_box",
"raw_route",
"raw_hc",
"raw_starrt",
"raw_prefix",
"raw_suffix",
"raw_type",
"raw_unit",
"raw_address"
);
foreach($order as $v){
isset($out[$v]) ? $ret[$v] = $out[$v] : '';
}
return $ret;
}
// =========================== Utility functions
function debug($s) {
if ( DEBUG ) {
if ( is_array($s) || is_object($s) ) {
view($s);
} else {
echo "
$s
";
}
}
}
function view($obj) {
echo "
<pre>"; print_r($obj); echo "</pre>
";
}
?>


Link to comment
Share on other sites

// === Display the lat/lon and a google maps link
echo $latlon["lat"] . ", " . $latlon["lon"];
echo "
<a href="http://blog.tooleshed.com/%5C%22http://maps.google.com/maps?f=q&hl=en&q=%22">Google Map</a>";

 

should be

 

// === Display the lat/lon and a google maps link
echo $latlon["lat"] . ", " . $latlon["lon"];
echo "
<a href=\"http://blog.tooleshed.com/%5C%22http://maps.google.com/maps?f=q&hl=en&q=%22\">Google Map</a>";

 

Those double quotes need to be escaped.

 

--FrosT

Link to comment
Share on other sites

is this correct please

function calc_position($chain, $prop) {
$total_dist = 0;
// First calculate the distance between each set of points
// and the total distance of our entire street segment
for ($i=0; $i $dist=calc_dist( $chain[$i] , $chain[$i+1] ){
$p2p_dist[$i] = $dist;
$total_dist += $dist;
debug("Distance from Point $i to " . ($i+1) . " = $dist  ($total_dist)");
}

 

line 43-82 function calc_position

Link to comment
Share on other sites

i have read the article yes. that is where i got the code from and how i got to step 2. the first file they give you had tons of errors but i hacked it  a bit until i loaded the data in. the second script i cannot get to work which is the reason for my post in the first place

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.