Code:
<?php
/* * * * * * * * * * * * * * * * * * * * * * * * *
Klasse Umkreissuche
(c) 2008 Philipp Mamat
http://www.mamat-online.de/
http://www.mamat-online.de/umkreissuche/opengeodb.php
* * * * * * * * * * * * * * * * * * * * * * * * */
class Umkreissuche {
// Erdradius in Kilometern
private $Erdradius = 6371;
// mysql link identifier
private $db;
// Datentabelle
private $table = false;
// Fehler zeigen?
public $zeigeFehler = true;
public function __construct($db, $table = 'vereine') {
if (!is_resource($db) || get_resource_type($db) != 'mysql link') {
trigger_error('Keine MySQL-Ressource übergeben', E_USER_ERROR);
}
$this->db = $db;
$this->table = $table;
// leere Koordinaten in Tabelle füllen
$sql = 'SELECT `ID`, `PLZ`
FROM `' . $this->table . '`
WHERE
`KoordX` = "0"
AND `KoordY` = "0"
AND `KoordZ` = "0"
';
$re = mysql_query($sql, $this->db);
while ($rd = mysql_fetch_object($re)) {
if (!$this->Plz2Koord($rd->PLZ, $lon, $lat)) {
if ($this->zeigeFehler) {
trigger_error('Postleitzahl ' . $rd->PLZ . ' konnte nicht zugeordnet werden', E_USER_NOTICE);
}
continue;
}
$this->Kugel2Kartesisch($lon, $lat, $x, $y, $z);
$sql = 'UPDATE `' . $this->table . '`
SET
`Longitude` = "' . $lon . '",
`Latitude` = "' . $lat . '",
`KoordX` = "' . $x . '",
`KoordY` = "' . $y . '",
`KoordZ` = "' . $z . '"
WHERE
`ID` = "' . (int)$rd->ID . '"
LIMIT 1
';
mysql_query($sql, $this->db);
while ($rd = mysql_fetch_object($re)) {
$rd->Entfernung = 2* $this->Erdradius *
asin(
sqrt(
pow($UrsprungX - $rd->KoordX, 2)
+ pow($UrsprungY - $rd->KoordY, 2)
+ pow($UrsprungZ - $rd->KoordZ, 2)
) / (2 * $this->Erdradius)
);
$result[] = $rd;
}
}
}
public function Kugel2Kartesisch($lon, $lat, &$x, &$y, &$z) {
$lambda = $lon * pi() / 180;
$phi = $lat * pi() / 180;
$x = $this->Erdradius * cos($phi) * cos($lambda);
$y = $this->Erdradius * cos($phi) * sin($lambda);
$z = $this->Erdradius * sin($phi);
return true;
}
public function Plz2Koord($PLZ, &$lon, &$lat) {
$sql = 'SELECT
coo.lon,
coo.lat
FROM geodb_coordinates AS coo
INNER JOIN geodb_textdata AS textdata
ON textdata.loc_id = coo.loc_id
WHERE
textdata.text_val = "' . mysql_real_escape_string($PLZ, $this->db) . '"
AND textdata.text_type = "500300000"
LIMIT 1';
$re = mysql_query($sql, $this->db);
if (mysql_num_rows($re) != 1) {
return false;
}
list($lon, $lat) = mysql_fetch_row($re);
return true;
}
public function Suche($PLZ, $Radius, $Spalten = array(), $Reihenfolge = false, $Richtung = 'ASC') {
if (!is_array($Spalten) || count($Spalten) == 0) {
$Spalten = '*';
} else {
$Spalten = '`' . implode('`, `', $Spalten) . '`';
}
if (!$this->Plz2Koord($PLZ, $lon, $lat)) {
if ($this->zeigeFehler) {
trigger_error('Postleitzahl ' . $PLZ . ' konnte nicht zugeordnet werden', E_USER_NOTICE);
}
return false;
}
$this->Kugel2Kartesisch($lon, $lat, $UrsprungX, $UrsprungY, $UrsprungZ);//KoordX, KoordY, KoordZ
$sql = 'SELECT ' . $Spalten . ',
' . (2 * $this->Erdradius) . ' *
ASIN(
SQRT(
POWER(' . $UrsprungX .' - KoordX, 2)
+ POWER(' . $UrsprungY .' - KoordY, 2)
+ POWER(' . $UrsprungZ .' - KoordZ, 2)
) / ' . (2 * $this->Erdradius) . ' ) AS Entfernung
FROM `' . $this->table . '`
WHERE
KoordX >= ' . ($UrsprungX - $Radius) . '
AND KoordX <= ' . ($UrsprungX + $Radius) . '
AND KoordY >= ' . ($UrsprungY - $Radius) . '
AND KoordY <= ' . ($UrsprungY + $Radius) . '
AND KoordZ >= ' . ($UrsprungZ - $Radius) . '
AND KoordZ <= ' . ($UrsprungZ + $Radius) . '
AND POWER(' . $UrsprungX .' - KoordX, 2)
+ POWER(' . $UrsprungY .' - KoordY, 2)
+ POWER(' . $UrsprungZ .' - KoordZ, 2)
<= "' . pow(2 * $this->Erdradius * sin($Radius / (2 * $this->Erdradius)), 2) . '"';
/*if ($Reihenfolge && strpos($Spalten, $Reihenfolge) !== false) {
$Richtung = (strtoupper($Richtung) == 'DESC') ? 'DESC' : 'ASC';
$sql .= "\n" . 'ORDER BY `' . $Reihenfolge . '` ' . $Richtung;
}*/
if($Reihenfolge !== false)
{
$sql .= "\n" . 'ORDER BY `' . $Reihenfolge . '` ' . $Richtung;
}
$re = mysql_query($sql, $this->db);
$result = array();
while ($rd = mysql_fetch_object($re)) {
/*$rd->Entfernung = 2* $this->Erdradius *
asin(
sqrt(
pow($UrsprungX - $rd->KoordX, 2)
+ pow($UrsprungY - $rd->KoordY, 2)
+ pow($UrsprungZ - $rd->KoordZ, 2)
) / (2 * $this->Erdradius)
);*/
$result[] = $rd;
}
return $result;
}
}
?>
Muss ich vielleicht die Domain zu der anderen Domain, und nicht nur den Namen angeben ( in Umkreissuche zu class.php )? Wie gesagt, ohne Joomla funktioniert das ganze fehlerfrei...
Lesezeichen