Overview
Comment: | Remove Karto, update Karto\Point and Karto\Set |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | 7.3 |
Files: | files | file ages | folders |
SHA1: |
d5252aa9315ab2fa992e1b86e0240512 |
User & Date: | bohwaz on 2019-12-18 11:56:37 |
Other Links: | branch diff | manifest | tags |
Context
2019-12-18
| ||
12:01 | Rename graphics libraries check-in: 70c87a9b18 user: bohwaz tags: 7.3 | |
11:56 | Remove Karto, update Karto\Point and Karto\Set check-in: d5252aa931 user: bohwaz tags: 7.3 | |
11:42 | Move Karto libraries check-in: ac4c400e8b user: bohwaz tags: 7.3 | |
Changes
Deleted src/lib/KD2/Karto/Karto.php version [d616b5b332].
|
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < |
Modified src/lib/KD2/Karto/Point.php from [79ddb58824] to [77f4e12bb5].
︙ | ︙ | |||
22 23 24 25 26 27 28 | /** * Karto: an independent PHP library providing basic mapping tools */ namespace KD2\Karto; use ArrayAccess; | | > > > > > | | | | | 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 | /** * Karto: an independent PHP library providing basic mapping tools */ namespace KD2\Karto; use ArrayAccess; use KD2\Karto\Set; class Point implements ArrayAccess { /** * Half of the earth circumference in pixels at zoom level 21 * * Full map size is 536870912 × 536870912 pixels. * Center of the map in pixel coordinates is 268435456,268435456 * which in latitude and longitude would be 0,0. */ const PIXELS_OFFSET = 268435456; /** * PIXELS_OFFSET / pi() */ const PIXELS_RADIUS = 85445659.4471; /** * Average radius of earth (in kilometers) */ const RADIUS = 6371; /** * Latitude value * @var float */ protected $lat = null; /** * Longitude value * @var float */ protected $lon = null; /** * "Magic" constructor will accept either float coordinates, an array, a Point object, or a string * @param float|Point|string|array|null $lat Latitude * @param float|null $lon Longitude */ public function __construct($lat = null, $lon = null) { $self = __CLASS__; if (is_object($lat) && $lat instanceof $self) { |
︙ | ︙ | |||
186 187 188 189 190 191 192 | */ public function offsetGet($offset) { return $this->__get($offset); } /** * Gets distance between this point and another | | | | | | | | 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | */ public function offsetGet($offset) { return $this->__get($offset); } /** * Gets distance between this point and another * @param Point $point Distant point * @return float Distance in KM */ public function distanceTo(Point $point): float { $lat1 = $this->lat; $lon1 = $this->lon; $lat2 = $point->lat; $lon2 = $point->lon; // convert lat1 and lat2 into radians now, to avoid doing it twice below $lat1rad = deg2rad($lat1); $lat2rad = deg2rad($lat2); // apply the spherical law of cosines to our latitudes and longitudes, and set the result appropriately return (acos(sin($lat1rad) * sin($lat2rad) + cos($lat1rad) * cos($lat2rad) * cos(deg2rad($lon2) - deg2rad($lon1))) * self::RADIUS); } /** * Distance in pixels between two points at a specific zoom level * @param Point $point Distant point * @param integer $zoom Zoom level * @param mixed $restrict Restrict direction (x or y, or null to have the diagonal) * @return int Distance in pixels */ public function pixelDistanceTo(Point $point, int $zoom, ?string $restrict = null): int { list($x1, $y1) = $this->XY(); list($x2, $y2) = $point->XY(); // Restrict direction if ($restrict == 'x') { |
︙ | ︙ | |||
233 234 235 236 237 238 239 | return sqrt(pow(($x1-$x2),2) + pow(($y1-$y2),2)) >> (21 - $zoom); } /** * Get position in pixels for map * @return array [int X, int Y] position on map in pixels at zoom 21 */ | | | | 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | return sqrt(pow(($x1-$x2),2) + pow(($y1-$y2),2)) >> (21 - $zoom); } /** * Get position in pixels for map * @return array [int X, int Y] position on map in pixels at zoom 21 */ public function XY(): array { $x = round(self::PIXELS_OFFSET + self::PIXELS_RADIUS * $this->lon * pi() / 180); $y = round(self::PIXELS_OFFSET - self::PIXELS_RADIUS * log((1 + sin($this->lat * pi() / 180)) / (1 - sin($this->lat * pi() / 180))) / 2); return [$x, $y]; } /** * Converts a latitude and longitude from decimal to DMS notation * @return array Latitude / Longitude in DMS notation, eg. [45 5 56 S, 174 11 37 E] */ public function toDMS(): array { $convert = function ($dec) { $dec = explode('.', $dec); if (count($dec) != 2) return [0, 0, 0]; $m = $dec[1]; $h = $dec[0]; |
︙ | ︙ | |||
271 272 273 274 275 276 277 | $lon = abs($lon[0]) . ' ' . abs($lon[1]) . ' ' . abs($lon[2]) . ' ' . ($lon[0] > 0 ? 'E' : 'W'); return [$lat, $lon]; } /** * Is this latitude/longitude contained inside the given set bounds? | | | | 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | $lon = abs($lon[0]) . ' ' . abs($lon[1]) . ' ' . abs($lon[2]) . ' ' . ($lon[0] > 0 ? 'E' : 'W'); return [$lat, $lon]; } /** * Is this latitude/longitude contained inside the given set bounds? * @param Set $set Set of points * @return boolean true if point is inside the boundaries, false if it is outside */ public function isContainedIn(Set $set): bool { $bbox = $set->getBBox(); if ($this->lon >= $bbox->minLon && $this->lon <= $bbox->maxLon && $this->lat >= $bbox->minLat && $this->lat <= $bbox->maxLat) |
︙ | ︙ |
Modified src/lib/KD2/Karto/Set.php from [e31647690f] to [fff9bc50c1].
︙ | ︙ | |||
24 25 26 27 28 29 30 31 | * * @author bohwaz http://bohwaz.net/ */ namespace KD2; use KD2\Karto\Point; | > | | 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | * * @author bohwaz http://bohwaz.net/ */ namespace KD2; use KD2\Karto\Point; use stdClass; class Set { /** * Internal storage of set of points * @var array */ protected $points = []; |
︙ | ︙ | |||
47 48 49 50 51 52 53 | { $this->add($point); } } /** * Add a new point to the set | | > > > > | | | | | | | | | | | | | | 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 | { $this->add($point); } } /** * Add a new point to the set * @param mixed $point Could be a Point object, or an array like [85, 180] or [lat => 85, lon => 180] */ public function add($point) { if (!is_object($point) || !($point instanceof Point)) { $point = new Point($point); } $this->points[] = $point; } /** * Returns all the points of the set * @return array list of Point points */ public function getPoints(): array { return $this->points; } /** * Returns the number of points in the current set * @return integer Number of points */ public function count(): int { return count($this->points); } /** * Returns the bounding box of the current set of coordinates * @return \stdClass {float minLat, float maxLat, float minLon, float maxLon, Point northEast, Point southWest} */ public function getBBox(): stdClass { if (count($this->points) < 1) throw new \OutOfRangeException('Empty point set'); $bbox = new stdClass; $point = $this->points[0]; $bbox->minLat = $bbox->maxLat = $point->lat; $bbox->minLon = $bbox->maxLon = $point->lon; foreach ($this->points as $point) { $bbox->minLat = min($bbox->minLat, $point->lat); $bbox->maxLat = max($bbox->maxLat, $point->lat); $bbox->minLon = min($bbox->minLon, $point->lon); $bbox->maxLon = max($bbox->maxLon, $point->lon); } $bbox->northEast = new Point($bbox->maxLat, $bbox->minLon); $bbox->southWest = new Point($bbox->minLat, $bbox->maxLon); return $bbox; } /** * Calculate average latitude and longitude of supplied array of points * @param array $points Each array item MUST have at least two keys named 'lat' and 'lon' * @return object (obj)->lat = x.xxxx, ->lon => y.yyyy */ public function getCenter(): Point { if (count($this->points) < 1) throw new \OutOfRangeException('Empty point set'); /* Calculate average lat and lon of points. */ $lat_sum = $lon_sum = 0; foreach ($this->points as $point) { $lat_sum += $point->lat; $lon_sum += $point->lon; } $lat_avg = $lat_sum / count($this->points); $lon_avg = $lon_sum / count($this->points); return new Point($lat_avg, $lon_avg); } /** * Cluster points to avoid having too much points * @param integer $distance Maximum distance between two points to cluster them * @param integer $zoom Map zoom level * @return array Each row will contain a key named 'points' containing all the points in the cluster and * a key named 'center' containing the center coordinates of the cluster. */ public function cluster(int $distance, int $zoom): array { $clustered = []; $points = $this->points; /* Loop until all points have been compared. */ while (count($points)) { $point = array_pop($points); $cluster = new Set; /* Compare against all points which are left. */ foreach ($points as $key => $target) { $pixels = $point->pixelDistanceTo($target, $zoom); /* If two points are closer than given distance remove */ |
︙ | ︙ | |||
179 180 181 182 183 184 185 | return $clustered; } /** * Decode a polyline into a set of coordinates * @link https://developers.google.com/maps/documentation/utilities/polylinealgorithm Polyline algorithm * @param string $line Polyline encoded string | | | | 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | return $clustered; } /** * Decode a polyline into a set of coordinates * @link https://developers.google.com/maps/documentation/utilities/polylinealgorithm Polyline algorithm * @param string $line Polyline encoded string * @return Set A set of lat/lon tuples */ static public function fromPolyline(string $line): Set { $precision = 5; $index = $i = 0; $points = []; $previous = [0,0]; while ($i < strlen($line)) |
︙ | ︙ | |||
206 207 208 209 210 211 212 | $number = $previous[$index % 2] + $diff; $previous[$index % 2] = $number; $index++; $points[] = $number * 1 / pow(10, $precision); } | | | | 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | $number = $previous[$index % 2] + $diff; $previous[$index % 2] = $number; $index++; $points[] = $number * 1 / pow(10, $precision); } return new Set(array_chunk($points, 2)); } /** * Encode the current set of coordinates as a polyline * @link https://developers.google.com/maps/documentation/utilities/polylinealgorithm Polyline algorithm * @return string A polyline encoded string */ public function toPolyline(): string { // Flatten array $points = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->points)); $precision = 5; $encodedString = ''; $index = 0; |
︙ | ︙ | |||
256 257 258 259 260 261 262 | /** * Returns mercator suitable zoom level according to the set of points * @param integer $mapWidth Target map width in pixels * @param integer $mapHeight Target map height in pixels * @return integer Zoom level between 0 and 21 * @link http://stackoverflow.com/a/15397775 */ | | | 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | /** * Returns mercator suitable zoom level according to the set of points * @param integer $mapWidth Target map width in pixels * @param integer $mapHeight Target map height in pixels * @return integer Zoom level between 0 and 21 * @link http://stackoverflow.com/a/15397775 */ public function getZoomLevel(int $mapWidth, int $mapHeight): int { $bbox = $this->getBBox(); for ($zoom = 21; $zoom >= 0; --$zoom) { $distance_w = $bbox->northEast->pixelDistanceTo($bbox->southWest, $zoom, 'x'); $distance_h = $bbox->northEast->pixelDistanceTo($bbox->southWest, $zoom, 'y'); |
︙ | ︙ |