初始化项目
This commit is contained in:
@@ -0,0 +1 @@
|
||||
vendor
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013 Jefferson Lyu
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
# GeoHash
|
||||
|
||||
Simple php geohash class like python-geohash.
|
||||
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Install
|
||||
|
||||
composer require lvht/geohash
|
||||
|
||||
### System Requirements
|
||||
You need **PHP >= 5.4.0**.
|
||||
|
||||
### Usage
|
||||
Encode a coordinate:
|
||||
|
||||
use Lvht\GeoHash;
|
||||
echo GeoHash::encode(117.031689,36.65396);
|
||||
|
||||
The result is wwe0x0euu12.
|
||||
|
||||
The default precision is 0.00001 which can be changed by the third parameter
|
||||
of encode method.
|
||||
|
||||
Find the neighbors for a given geohash:
|
||||
|
||||
use Lvht\GeoHash;
|
||||
var_dump(GeoHash::expand('wwe0x0'));
|
||||
|
||||
and the result is:
|
||||
|
||||
array(8) {
|
||||
[0] =>
|
||||
string(11) "wwe0wc7zzzz"
|
||||
[1] =>
|
||||
string(11) "wwe0x17zzzz"
|
||||
[2] =>
|
||||
string(11) "wwe0x37zzzz"
|
||||
[3] =>
|
||||
string(11) "wwe0wb7zzzz"
|
||||
[4] =>
|
||||
string(11) "wwe0x27zzzz"
|
||||
[5] =>
|
||||
string(11) "wwe0qz7zzzz"
|
||||
[6] =>
|
||||
string(11) "wwe0rp7zzzz"
|
||||
[7] =>
|
||||
string(11) "wwe0rr7zzzz"
|
||||
}
|
||||
|
||||
Decode a geohash string:
|
||||
|
||||
Use Lvht\GeoHash;
|
||||
var_dump(GeoHash::decode('wwe0x0'));
|
||||
|
||||
and the result is:
|
||||
|
||||
array(4) {
|
||||
[0] =>
|
||||
double(117.0263671875) # min longitude
|
||||
[1] =>
|
||||
double(117.03735351562) # max longitude
|
||||
[2] =>
|
||||
double(36.650390625) # min latitude
|
||||
[3] =>
|
||||
double(36.655883789062) # max latitude
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "lvht/geohash",
|
||||
"type": "library",
|
||||
"description": "geohash like python-geohash",
|
||||
"keywords": ["geohash"],
|
||||
"homepage": "http://github.com/lvht/geohash",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "吕海涛",
|
||||
"email": "git@lvht.net",
|
||||
"homepage": "https://github.com/lvht"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.4.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lvht\\": "src"
|
||||
}
|
||||
}
|
||||
}
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
namespace Lvht;
|
||||
|
||||
class GeoHash
|
||||
{
|
||||
private static $table = "0123456789bcdefghjkmnpqrstuvwxyz";
|
||||
private static $bits = array(
|
||||
0b10000, 0b01000, 0b00100, 0b00010, 0b00001
|
||||
);
|
||||
|
||||
public static function encode($lng, $lat, $prec = 0.00001)
|
||||
{
|
||||
$minlng = -180;
|
||||
$maxlng = 180;
|
||||
$minlat = -90;
|
||||
$maxlat = 90;
|
||||
|
||||
$hash = array();
|
||||
$error = 180;
|
||||
$isEven = true;
|
||||
$chr = 0b00000;
|
||||
$b = 0;
|
||||
|
||||
while ($error >= $prec) {
|
||||
if ($isEven) {
|
||||
$next = ($minlng + $maxlng) / 2;
|
||||
if ($lng > $next) {
|
||||
$chr |= self::$bits[$b];
|
||||
$minlng = $next;
|
||||
} else {
|
||||
$maxlng = $next;
|
||||
}
|
||||
} else {
|
||||
$next = ($minlat + $maxlat) / 2;
|
||||
if ($lat > $next) {
|
||||
$chr |= self::$bits[$b];
|
||||
$minlat = $next;
|
||||
} else {
|
||||
$maxlat = $next;
|
||||
}
|
||||
}
|
||||
$isEven = !$isEven;
|
||||
|
||||
if ($b < 4) {
|
||||
$b++;
|
||||
} else {
|
||||
$hash[] = self::$table[$chr];
|
||||
$error = max($maxlng - $minlng, $maxlat - $minlat);
|
||||
$b = 0;
|
||||
$chr = 0b00000;
|
||||
}
|
||||
}
|
||||
|
||||
return join('', $hash);
|
||||
}
|
||||
|
||||
public static function expand($hash, $prec = 0.00001)
|
||||
{
|
||||
list($minlng, $maxlng, $minlat, $maxlat) = self::decode($hash);
|
||||
$dlng = ($maxlng - $minlng) / 2;
|
||||
$dlat = ($maxlat - $minlat) / 2;
|
||||
|
||||
return array(
|
||||
self::encode($minlng - $dlng, $maxlat + $dlat, $prec),
|
||||
self::encode($minlng + $dlng, $maxlat + $dlat, $prec),
|
||||
self::encode($maxlng + $dlng, $maxlat + $dlat, $prec),
|
||||
self::encode($minlng - $dlng, $maxlat - $dlat, $prec),
|
||||
self::encode($maxlng + $dlng, $maxlat - $dlat, $prec),
|
||||
self::encode($minlng - $dlng, $minlat - $dlat, $prec),
|
||||
self::encode($minlng + $dlng, $minlat - $dlat, $prec),
|
||||
self::encode($maxlng + $dlng, $minlat - $dlat, $prec),
|
||||
);
|
||||
}
|
||||
|
||||
public static function getRect($hash)
|
||||
{
|
||||
list($minlng, $maxlng, $minlat, $maxlat) = self::decode($hash);
|
||||
|
||||
return array(
|
||||
array($minlng, $minlat),
|
||||
array($minlng, $maxlat),
|
||||
array($maxlng, $maxlat),
|
||||
array($maxlng, $minlat),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* decode a geohash string to a geographical area
|
||||
*
|
||||
* @var $hash string geohash
|
||||
* @return array array($minlng, $maxlng, $minlat, $maxlat);
|
||||
*/
|
||||
public static function decode($hash)
|
||||
{
|
||||
$minlng = -180;
|
||||
$maxlng = 180;
|
||||
$minlat = -90;
|
||||
$maxlat = 90;
|
||||
|
||||
for ($i=0,$c=strlen($hash); $i<$c; $i++) {
|
||||
$v = strpos(self::$table, $hash[$i]);
|
||||
if (1&$i) {
|
||||
if (16&$v) {
|
||||
$minlat = ($minlat + $maxlat) / 2;
|
||||
} else {
|
||||
$maxlat = ($minlat + $maxlat) / 2;
|
||||
}
|
||||
if (8&$v) {
|
||||
$minlng = ($minlng + $maxlng) / 2;
|
||||
} else {
|
||||
$maxlng = ($minlng + $maxlng) / 2;
|
||||
}
|
||||
if (4&$v) {
|
||||
$minlat = ($minlat + $maxlat) / 2;
|
||||
} else {
|
||||
$maxlat = ($minlat + $maxlat) / 2;
|
||||
}
|
||||
if (2&$v) {
|
||||
$minlng = ($minlng + $maxlng) / 2;
|
||||
} else {
|
||||
$maxlng = ($minlng + $maxlng) / 2;
|
||||
}
|
||||
if (1&$v) {
|
||||
$minlat = ($minlat + $maxlat) / 2;
|
||||
} else {
|
||||
$maxlat = ($minlat + $maxlat) / 2;
|
||||
}
|
||||
} else {
|
||||
if (16&$v) {
|
||||
$minlng = ($minlng + $maxlng) / 2;
|
||||
} else {
|
||||
$maxlng = ($minlng + $maxlng) / 2;
|
||||
}
|
||||
if (8&$v) {
|
||||
$minlat = ($minlat + $maxlat) / 2;
|
||||
} else {
|
||||
$maxlat = ($minlat + $maxlat) / 2;
|
||||
}
|
||||
if (4&$v) {
|
||||
$minlng = ($minlng + $maxlng) / 2;
|
||||
} else {
|
||||
$maxlng = ($minlng + $maxlng) / 2;
|
||||
}
|
||||
if (2&$v) {
|
||||
$minlat = ($minlat + $maxlat) / 2;
|
||||
} else {
|
||||
$maxlat = ($minlat + $maxlat) / 2;
|
||||
}
|
||||
if (1&$v) {
|
||||
$minlng = ($minlng + $maxlng) / 2;
|
||||
} else {
|
||||
$maxlng = ($minlng + $maxlng) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array($minlng, $maxlng, $minlat, $maxlat);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user