-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcity-mapper.php
More file actions
209 lines (148 loc) · 6.14 KB
/
city-mapper.php
File metadata and controls
209 lines (148 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* Created by PhpStorm.
* User: brayden
* Date: 2017-11-27
* Time: 5:53 PM
*/
ini_set("memory_limit", "512M");
//
//set_include_path(get_include_path() . PATH_SEPARATOR . '/LINQ/Classes');
//set_include_path(get_include_path() . PATH_SEPARATOR . '/LINQ/Classes/PhpLinq');
//set_include_path(get_include_path() . PATH_SEPARATOR . '/LINQ/Classes/PhpLinq/Adapter');
//
//
//require_once('LINQ/Classes/PHPLinq.php');
require_once 'mapping/MapTools.php';
$timeStart = floatval(microtime(true));
// to run: > php city-mapper.php <lat> <long>
// NOTE: firebase stores them in backwards format but this script accepts in correct format. Input a lat / long from
$mode = $argv[1];
// generates bounding box cache if
//generateBoundingBoxCache('-1 days');
//$cached = isset($argv[3]) && $argv[3] == '--cached';
switch(strtolower(trim($mode))) {
case 'cache' :
MapTools::getMap($argv[2]);
$time = floatval(microtime(true));
die("caching total time elapsed: ". (($time - $timeStart) * 1000) . " ms\n");
case 'sequential' :
$myLat = $argv[2];
$myLong = $argv[3];
$boundingBoxes = MapTools::getMapCached("locations.json");
//$locations = $data['locations'];
//$keys = array_keys($boundingBoxes);
//$possibleRegions = [];
$count = count($boundingBoxes);
echo "sequential search from 0 to $count\n";
$matchingBoxes = MapTools::sequentialSearch($boundingBoxes, 0);
echo "\n\n" . empty($rayCastMatches[0]['name']) ? "Unknown location" : "Location in " . $rayCastMatches[0]['name'];
$rayCastMatches = [];
foreach($matchingBoxes AS $box) {
$inRegion = MapTools::isInRegion($box['name']);
if($inRegion) $rayCastMatches[] = $box;
}
if(empty($rayCastMatches)) {
$regionBoxes = MapTools::getMapCached("regions.json");
$count = count($regionBoxes);
//foreach($regionBoxes AS $box) {var_dump($box);}
echo "region sequential search from 0 to $count\n";
$matchingBoxes = MapTools::sequentialSearch($boundingBoxes, 0);
echo "found ".count($matchingBoxes)." matching region boxes!\n";
$rayCastMatches = [];
foreach($matchingBoxes AS $box) {
$inRegion = MapTools::isInRegion($box['name'], 'regions');
if($inRegion) $rayCastMatches[] = $box;
}
}
if(empty($rayCastMatches)) {
echo "No Raycast matches... using bounding box matches\n";
$rayCastMatches = $matchingBoxes;
}
echo "\n\n" . empty($rayCastMatches[0]['name']) ? "Unknown location" : "Location in " . $rayCastMatches[0]['name'];
$time = floatval(microtime(true));
echo "\n---------------------\n";
echo "sequential search total time elapsed: " . floor(($time - $timeStart) * 1000) . " ms" . PHP_EOL;
exit();
case 'find' :
$myLat = $argv[2];
$myLong = $argv[3];
$boundingBoxes = MapTools::getMapCached("locations.json");
//$locations = $data['locations'];
//$keys = array_keys($boundingBoxes);
//$possibleRegions = [];
$startIndex = MapTools::binarySearch($boundingBoxes, count($boundingBoxes) - 1);
echo "(binary) sequential search from $startIndex to 0\n";
$matchingBoxes = MapTools::sequentialSearch($boundingBoxes, $startIndex);
echo "found ".count($matchingBoxes)." matching bounding boxes!\n";
// var_dump ($matchingBoxes);
$rayCastMatches = [];
foreach($matchingBoxes AS $box) {
$inRegion = MapTools::isInRegion($box['name']);
if($inRegion)
$rayCastMatches[] = $box;
}
if(empty($rayCastMatches)) {
$regionBoxes = MapTools::getMapCached("regions.json");
$startIndex = MapTools::binarySearch($regionBoxes, count($regionBoxes) - 1);
echo "region (binary) sequential search from $startIndex to 0\n";
$matchingBoxes = MapTools::sequentialSearch($boundingBoxes, $startIndex);
echo "found ".count($matchingBoxes)." matching region boxes!\n";
$rayCastMatches = [];
foreach($matchingBoxes AS $box) {
$inRegion = MapTools::isInRegion($box['name'], 'regions');
if($inRegion) $rayCastMatches[] = $box;
}
}
if(empty($rayCastMatches)) {
echo "No Raycast matches... using bounding box matches\n";
$rayCastMatches = $matchingBoxes;
}
echo "\n\n" . empty($rayCastMatches[0]['name']) ? "nearby" : "in " . $rayCastMatches[0]['name'];
$time = floatval(microtime(true));
echo "\n---------------------\n";
echo "binary search total time elapsed: " . floor(($time - $timeStart) * 1000) . " ms" . PHP_EOL;
exit();
default : die("usage: city-mapper.php cache|find <lat> <long>");
}
//function generateBoundingBoxCache($oldestValid) {
//
// $name = "tmp";
//
// $coordinateFolder = "locations/{$name}/geometry";
// $coordinateFile = "$coordinateFolder/coordinates.json";
// $cachedFile = "./firebase-cached/$coordinateFile";
//
// if(!file_exists($cachedFile) || filemtime($cachedFile) < strtotime('-1 days')) {
// // cache file
//
// $url = "https://preventanyl.firebaseio.com/$coordinateFile";
//
// echo "downloading $url\n";
//// $ch = curl_init($url);
//// curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
//
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $url);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//// curl_setopt($ch, CURLOPT_POST, 1);
//// curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
// $json = curl_exec($ch);
// curl_close($ch);
//
//
// $dir = "firebase-cached/$coordinateFolder";
//
// echo "caching to $dir";
//
// if (!is_dir($dir)) {
// // dir doesn't exist, make it
//
// mkdir($dir, 0777, true);
// }
//
// file_put_contents($cachedFile, $json);
//
// }
//}