Skip to content

Commit

Permalink
Merge pull request FreifunkFranken#10 from adrianschmutzler/polyhood
Browse files Browse the repository at this point in the history
Improve polygon check
  • Loading branch information
ChristianDresel committed Nov 9, 2018
2 parents 3aa64d9 + 560b8ba commit c790f04
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 43 deletions.
68 changes: 46 additions & 22 deletions database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ INSERT INTO `gateways` (`ID`, `name`, `publickey`, `ip`, `port`, `hood_ID`, `tim
(9, 'vm3fffgwcd1', '373cf6dca701a8b1516b816a13c91dc9df29ac5a822d12331b503982d655399b', '144.76.70.186', 10005, 30, '2017-09-27 07:15:09'),
(46, 'fff-neptun', '3834e45fa33c048f975e81042c1e93bb11dac82d9f03a0b24071bb72205247a8', '84.23.95.3', 10011, 31, '2018-09-02 09:00:59'),
(47, 'fff-neptun', '3834e45fa33c048f975e81042c1e93bb11dac82d9f03a0b24071bb72205247a8', '84.23.95.3', 10012, 0, '2018-09-02 14:36:14'),
(49, 'fff-neptun', '3834e45fa33c048f975e81042c1e93bb11dac82d9f03a0b24071bb72205247a8', '84.23.95.3', 10013, 1, '2018-09-02 14:36:14'),
(49, 'fff-neptun', '3834e45fa33c048f975e81042c1e93bb11dac82d9f03a0b24071bb72205247a8', '84.23.95.3', 10013, 1, '2018-09-02 14:36:14');


-- --------------------------------------------------------
Expand Down Expand Up @@ -98,32 +98,50 @@ INSERT INTO `hoods` (`ID`, `name`, `net`, `lat`, `lon`, `prefix`, `ntp_ip`, `ESS
-- Table structure for table `polyhood`
--

CREATE TABLE `polyhood` (
CREATE TABLE `polyhoods` (
`polyid` int(10) unsigned NOT NULL,
`hoodid` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `polyhoods`
--

INSERT INTO `polyhoods` (`polyid`, `hoodid`) VALUES
(1, 31),
(2, 32);

-- --------------------------------------------------------

--
-- Table structure for table `polygons`
--

CREATE TABLE `polygons` (
`ID` int(10) unsigned NOT NULL,
`polyid` int(10) unsigned NOT NULL,
`lat` double NOT NULL,
`lon` double NOT NULL,
`hoodid` int(10) unsigned NOT NULL
`lon` double NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `polyhood`
--

INSERT INTO `polyhood` (`ID`, `polyid`, `lat`, `lon`, `hoodid`) VALUES
(18, 1, 49.58982152, 10.99503994, 31),
(19, 1, 49.58940422, 11.01199150, 31),
(20, 1, 49.59685950, 11.01787090, 31),
(21, 1, 49.60270052, 11.01722717, 31),
(22, 1, 49.60712255, 10.99988937, 31),
(23, 1, 49.58982152, 10.99503994, 31),
(24, 2, 49.46979740, 11.01302147, 32),
(25, 2, 49.47983623, 10.99259377, 32),
(26, 2, 49.48569126, 10.98083496, 32),
(27, 2, 49.45546063, 10.97740173, 32),
(28, 2, 49.44798376, 10.99851608, 32),
(29, 2, 49.45395418, 11.00915909, 32),
(30, 2, 49.46979740, 11.01302147, 32);
INSERT INTO `polygons` (`ID`, `polyid`, `lat`, `lon`) VALUES
(18, 1, 49.58982152, 10.99503994),
(19, 1, 49.58940422, 11.01199150),
(20, 1, 49.59685950, 11.01787090),
(21, 1, 49.60270052, 11.01722717),
(22, 1, 49.60712255, 10.99988937),
(23, 1, 49.58982152, 10.99503994),
(24, 2, 49.46979740, 11.01302147),
(25, 2, 49.47983623, 10.99259377),
(26, 2, 49.48569126, 10.98083496),
(27, 2, 49.45546063, 10.97740173),
(28, 2, 49.44798376, 10.99851608),
(29, 2, 49.45395418, 11.00915909),
(30, 2, 49.46979740, 11.01302147);

--
-- Indexes for dumped tables
Expand All @@ -143,9 +161,15 @@ ALTER TABLE `hoods`
ADD PRIMARY KEY (`ID`);

--
-- Indexes for table `polyhood`
-- Indexes for table `polyhoods`
--
ALTER TABLE `polyhood`
ALTER TABLE `polyhoods`
ADD PRIMARY KEY (`polyid`);

--
-- Indexes for table `polygons`
--
ALTER TABLE `polygons`
ADD PRIMARY KEY (`ID`),
ADD KEY `polyid` (`polyid`);

Expand All @@ -161,8 +185,8 @@ ALTER TABLE `gateways`
--
-- AUTO_INCREMENT for table `polyhood`
--
ALTER TABLE `polyhood`
MODIFY `id` int(10) unsigned NOT NULL AUTO_INCREMENT;
ALTER TABLE `polygons`
MODIFY `ID` int(10) unsigned NOT NULL AUTO_INCREMENT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
Expand Down
8 changes: 8 additions & 0 deletions function.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class pointLocation {
// Original version: https://gist.github.com/jeremejazz/5219848
// Modified by Adrian Schmutzler, 2018.

function excludePolygon($point, $minlon, $maxlon, $minlat, $maxlat) {
// exclude polygon if LAT/LNG of point is smaller than minimum lat/lng of all vertices
// or bigger than maximum ...

// returning TRUE means exclusion, so polygon should NOT be used
return ($point[0] < $minlon or $point[0] > $maxlon or $point[1] < $minlat or $point[1] > $maxlat);
}

function pointInPolygon($point, $polygon, $pointOnVertex = true) {

// Support both string version "lng lat" and array(lng,lat)
Expand Down
62 changes: 41 additions & 21 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,64 @@
if (isset($_GET['lat']) && $_GET['lat'] !== "" && isset($_GET['long']) && $_GET['long'] !== "" && is_numeric($_GET['lat']) && is_numeric($_GET['long'])) {
$lat = $_GET['lat'];
$lon = $_GET['long'];
$point = array($lon,$lat); // coordinates of router

// Zuerst nach geojson hood pruefen
$pointLocation = new pointLocation();

// First only retrieve list of polyids
try {
$rc = db::getInstance()->prepare("SELECT DISTINCT polyid, hoodid FROM polyhood");
$rc = db::getInstance()->prepare("
SELECT polyhoods.polyid, hoodid, MIN(lat) AS minlat, MIN(lon) AS minlon, MAX(lat) AS maxlat, MAX(lon) AS maxlon
FROM polyhoods INNER JOIN polygons ON polyhoods.polyid = polygons.polyid
GROUP BY polyid, hoodid
"); // This query will automatically exclude polyhoods being present in polyhoods table, but without vertices in polygons table
$rc->execute();
} catch (PDOException $e) {
exit(showError(500, $e));
}
$allpoly = $rc->fetchAll(); // list of polyids

// Abfrage der Polygone ob eins passt
foreach($allpoly as $row) {
try {
$rs = db::getInstance()->prepare("SELECT lat, lon FROM polyhood WHERE polyid=:polyid");
$rs->bindParam(':polyid', $row['polyid']);
$rs->execute();
} catch (PDOException $e) {
exit(showError(500, $e));
}

// create array of polygons
$polygons = array(); // list of polygons (array(lng,lat)) for the current polyid
while ($polygeo = $rs->fetch(PDO::FETCH_ASSOC)) {
debug('lon: '.$polygeo["lon"].' lat: '.$polygeo["lat"]);
array_push($polygons, array($polygeo["lon"],$polygeo["lat"]));
// Set up all polygons, but do it without vertex coordinates
$polystore = array();
while($row = $rc->fetch(PDO::FETCH_ASSOC)) {
$polystore[$row['polyid']] = $row;
$polystore[$row['polyid']]['data'] = array(); // prepare array for vertex coordinates
}

// Now query the coordinates, all in one query
try {
$rc = db::getInstance()->prepare("SELECT polyid, lat, lon FROM polygons");
$rc->execute();
} catch (PDOException $e) {
exit(showError(500, $e));
}

// Write polygon coordinates into array
while($row = $rc->fetch(PDO::FETCH_ASSOC)) {
if(!isset($polystore[$row['polyid']])) {
debug('Database inconsistent: No polyhood defined for ID '.$row['polyid']);
continue; // Skip those orphaned vertex entries
}
$polystore[$row['polyid']]['data'][] = array($row["lon"],$row["lat"]);
debug('lon: '.$row["lon"].' lat: '.$row["lat"]);
}

$point = array($lon,$lat); // coordinates of router
$inside = $pointLocation->pointInPolygon($point, $polygons);
debug("point $lon $lat: " . $inside . "<br>");
// Interpret polygon data
foreach($polystore as $polygon) {
// First check whether point coordinates are outside the most extreme values for lat/lng
$exclude = $pointLocation->excludePolygon($point, $polygon['minlon'], $polygon['maxlon'], $polygon['minlat'], $polygon['maxlat']);
if ($exclude) {
debug("polygon #" . $polygon['polyid'] . " excluded<br>");
continue;
}
// Now really check whether point is inside polygon
$inside = $pointLocation->pointInPolygon($point, $polygon['data']);
debug("point in polygon #" . $polygon['polyid'] . ": " . $inside . "<br>");
if ($inside) {
debug("PolyHood gefunden...");
try {
$rs = db::getInstance()->prepare("SELECT ".hood_mysql_fields." FROM hoods WHERE id=:hoodid;");
$rs->bindParam(':hoodid', $row['hoodid'], PDO::PARAM_INT);
$rs->bindParam(':hoodid', $polygon['hoodid'], PDO::PARAM_INT);
$rs->execute();
} catch (PDOException $e) {
exit(showError(500, $e));
Expand Down

0 comments on commit c790f04

Please sign in to comment.