LoginSignup
1
1

More than 3 years have passed since last update.

PHPでメッシュコード(最大8分の1)から中心点を取得する

Posted at

PHPでメッシュコードの中心点を取るコードが以下です。
呼び出すのは coordinateFromMeshCode($mesh_code) で引数にメッシュコードを入れればOKです。
4分の1などを出したい場合は末尾の数字を削っていけばOKです。
その際中心出す距離も適宜変更が必要です。

function coordinateFromMeshCode($mesh_code) {
    $first_coordinate = $this->first($mesh_code);
    $second_coordinate = $this->second($mesh_code);
    $third_coordinate = $this->third($mesh_code);
    $forth_coordinate = $this->forth($mesh_code);
    $fifth_coordinate = $this->fifth($mesh_code);
    $sixth_coordinate = $this->sixth($mesh_code);
    $lat = ($first_coordinate[1] + $second_coordinate[1] + $third_coordinate[1] + $forth_coordinate[1] + $fifth_coordinate[1] + $sixth_coordinate[1]) / 3600;
    $lon = ($first_coordinate[0] + $second_coordinate[0] + $third_coordinate[0] + $forth_coordinate[0] + $fifth_coordinate[0] + $sixth_coordinate[0]) / 3600;

    $center_lat = $lat + 3.75 / 3600 / 2;
    $center_lon = $lon + 5.625 / 3600 / 2;
    return [$center_lat, $center_lon];
}

private function first($mesh_code) {
    $lat = substr($mesh_code, 0, 2);
    $lon = substr($mesh_code, 2, 2);
    return [(intval($lon) + 100) * 3600, $lat / 1.5 * 3600]; // 3600をかけて秒に直す
}

private function second($mesh_code) {
    $lat = substr($mesh_code, 4, 1);
    $lon = substr($mesh_code, 5, 1);
    return [$lon * 7.5 * 60, $lat * 5 * 60]; // 60をかけて秒に直す
}

private function third($mesh_code) {
    $lat = substr($mesh_code, 6, 1);
    $lon = substr($mesh_code, 7, 1);
    return [$lon * 45, $lat * 30];
}

private function forth($mesh_code) {
    $forth = substr($mesh_code, 8, 1);
    if ($this->is_south($forth)) {
        $lat = 0;
    } else {
        $lat = 15;
    }

    if ($this->is_west($forth)) {
        $lon = 0;
    } else {
        $lon = 22.5;
    }

    return [$lon, $lat];
}

private function fifth($mesh_code) {
    $fifth = substr($mesh_code, 9, 1);
    if ($this->is_south($fifth)) {
        $lat = 0;
    } else {
        $lat = 7.5;
    }

    if ($this->is_west($fifth)) {
        $lon = 0;
    } else {
        $lon = 11.25;
    }

    return [$lon, $lat];
}

private function sixth($mesh_code) {
    $sixth = substr($mesh_code, 10, 1);
    if ($this->is_south($sixth)) {
        $lat = 0;
    } else {
        $lat = 3.75;
    }

    if ($this->is_west($sixth)) {
        $lon = 0;
    } else {
        $lon = 5.625;
    }

    return [$lon, $lat];
}

private function is_south($code) {
    if ($code == 1 || $code == 2) {
        return true;
    }
    return false;
}

private function is_west($code) {
    if ($code == 1 || $code == 3) {
        return true;
    }
    return false;
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1