代码之家  ›  专栏  ›  技术社区  ›  Zach

创建给定行数、列数和起点的坐标网格

  •  0
  • Zach  · 技术社区  · 6 年前

    例如,列0和行0应该是前四个坐标对,以形成第一个网格单元,即1NM*1NM。列0行1应该是接下来的四个坐标对,以使下一个网格单元位于第一个网格单元之上。在200行之后,移到下一列,以此类推。

    <?php
    header('Content-Type:text/plain');
    
    // southwest coordinate pair (starting point)
    $lat = 38.883672;
    $lon = -105.698848;
    
    for($col = 0; $col < 200; $col++) {
      $startLat = $startLat2 = 0;
      $startLon = $startLon2 = 0;
    
      if($col > 0) {
        $lat = Extrapolate($lat, $lon, 1.0, 90)[0];
        $lon = Extrapolate($lat, $lon, 1.0, 90)[1];
      }
    
      $debug = sprintf("%s,%s\r\n", $lat, $lon);
    
      for($row = 0; $row < 200; $row++) {
        if($row == 0) {
          $startLat = Extrapolate($lat, $lon, 1.0, 360)[0];
          $startLon = Extrapolate($lat, $lon, 1.0, 360)[1];
    
          $startLat2 = Extrapolate($lat, $lon, 1.0, 90)[0];
          $startLon2 = Extrapolate($lat, $lon, 1.0, 90)[1];
    
          $nextLat = $startLat;
          $nextLon = $startLon;
    
          $nextLat2 = $startLat2;
          $nextLon2 = $startLon2;
    
          $debug .= sprintf("%s,%s\r\n", $startLat, $startLon);
          $debug .= sprintf("%s,%s\r\n", $startLat2, $startLon2);
        }
        else {
          $nextLat = Extrapolate($nextLat, $nextLon, 1.0, 360)[0];
          $nextLon = Extrapolate($nextLat, $nextLon, 1.0, 360)[1];
    
          $nextLat2 = Extrapolate($nextLat2, $nextLon2, 1.0, 90)[0];
          $nextLon2 = Extrapolate($nextLat2, $nextLon2, 1.0, 90)[1];
    
          $debug .= sprintf("%s,%s\r\n", $nextLat, $nextLon);
          $debug .= sprintf("%s,%s\r\n", $nextLat2, $nextLon2);
        }
      }
    
      echo $debug;
    }
    
    function Extrapolate($lat1,$long1,$d,$angle)
    {
      # Earth Radious in KM
      $R = 6378.14;
    
      # Degree to Radian
      $latitude1 = $lat1 * (M_PI/180);
      $longitude1 = $long1 * (M_PI/180);
      $brng = $angle * (M_PI/180);
    
      # Distance to NM
      $d *= 1.85200;
    
      $latitude2 = asin(sin($latitude1)*cos($d/$R) + cos($latitude1)*sin($d/$R)*cos($brng));
      $longitude2 = $longitude1 + atan2(sin($brng)*sin($d/$R)*cos($latitude1),cos($d/$R)-sin($latitude1)*sin($latitude2));
    
      # back to degrees
      $latitude2 = $latitude2 * (180/M_PI);
      $longitude2 = $longitude2 * (180/M_PI);
    
      $lat2 = round ($latitude2,6);
      $long2 = round ($longitude2,6);
    
      // Push in array and get back
      $coords[0] = $lat2;
      $coords[1] = $long2;
      return $coords;
     }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   bcperth    6 年前

    当你移动N和E时,坐标增加。这意味着要向东移动1NM或向北移动1NM(对应于1NM网格),只需将1/60度添加到相应的坐标。

    //西南坐标对(起点)

    $lat = 38.883672;
    $lon = -105.698848;
    

    $lat[1] = 38.883672 + .016667; // 38.900339
    

    向东移动1海里(1格方格)

    $lon[1] = -105.698848 + .016667; // -105.682181
    

    因此,使用这个你可以很容易地建立一个网格200x200只需增加。

    向北移动200海里(200格方格)

    $lat[1] = 38.883672 + .016667*200; // 42.217072
    

    $lon[1] = -105.698848 + .016667*200; // -102.365448
    

    另外,如果要构建网格,最好将其存储在二维数组中,如下所示:

    <?php
    //header('Content-Type:text/plain');
    
    // southwest coordinate pair (starting point)
    $lat = 38.883672;
    $lon = -105.698848;
    
    for ($col=0; $col< 200; $col++){
        echo PHP_EOL."Grid values for col ".$col.PHP_EOL;
        for ($row=0; $row< 200; $row++){
          $newCoords = extrapolate($lat,$lon,$row,$col);
          $coords[$col][$row]["lat"] = $newCoords["lat"];
          $coords[$col][$row]["lon"] = $newCoords["lon"];
          echo "Row ".$row." = ".$coords[$col][$row]["lat"]." , ";
          echo $coords[$col][$row]["lon"].PHP_EOL;
        }
    }
    
    function extrapolate($lat,$lon,$row,$col){
      $newCoords["lat"] = round($lat + (1/60)*$row,6);
      $newCoords["lon"] = round($lon + (1/60)*$col,6);
      return ($newCoords);
    }
    

    现在,如果您喜欢使用自己的方法来计算偏移,您可以修改 extrapolate() 功能。

    一旦你有了上面的数组,你就可以选择每个单元格周围的坐标,如下所示。举例来说,我们将选择最下面的SW单元。其4个坐标对为:

    //西南角

    $coords[0][0]["lat"];
    $coords[0][0]["lon"];
    

    //西北角

    $coords[0][1]["lat"];
    $coords[0][1]["lon"];
    

    //东南角

    $coords[1][0]["lat"];
    $coords[1][0]["lon"];
    

    //东北角

    $coords[1][1]["lat"];
    $coords[1][1]["lon"];
    

    再举一个例子,我们将选择顶部的NE单元格。其4个坐标对为:

    //西南角

    $coords[198][198]["lat"];
    $coords[198][198]["lon"];
    

    //西北角

    $coords[198][199]["lat"];
    $coords[198][199]["lon"];
    

    $coords[199][198]["lat"];
    $coords[199][198]["lon"];
    

    //东北角

    $coords[199][199]["lat"];
    $coords[199][199]["lon"];
    

    推荐文章