【问题标题】:why is this formula for a circle giving me an ellipsoid in Javascript but a circle in Python?为什么这个圆的公式在 Javascript 中给了我一个椭圆体,而在 Python 中却给了我一个圆?
【发布时间】:2016-08-04 21:40:15
【问题描述】:

我为 page 上的 python 修改了以下代码: 对于 Javascript 等效项。

import math

# inputs
radius = 1000.0 # m - the following code is an approximation that stays reasonably accurate for distances < 100km
centerLat = 30.0 # latitude of circle center, decimal degrees
centerLon = -100.0 # Longitude of circle center, decimal degrees

# parameters
N = 10 # number of discrete sample points to be generated along the circle

# generate points
circlePoints = []
for k in xrange(N):
    # compute
    angle = math.pi*2*k/N
    dx = radius*math.cos(angle)
    dy = radius*math.sin(angle)
    point = {}
    point['lat']=centerLat + (180/math.pi)*(dy/6378137)
    point['lon']=centerLon + (180/math.pi)*(dx/6378137)/math.cos(centerLat*math.pi/180)
    # add to list
    circlePoints.append(point)

print circlePoints

这些点之间的距离应该是恒定的。

据我所知,我的 JS 版本是等效的:

  var nodesCount = 8;
  var coords = [];

  for (var i = 0; i <= nodesCount; i++) {
    var radius = 1000;
    var angle = Math.PI*2*i/nodesCount;
    var dx = radius*Math.cos(angle);
    var dy = radius*Math.sin(angle);

    coords.push([(rootLongitude + (180 / Math.PI) * (dx / EARTH_RADIUS) / Math.cos(rootLatitude * Math.PI / 180)),(rootLatitude + (180 / Math.PI) * (dy / EARTH_RADIUS))]);
  }

但是当我输出这个时,坐标离中心不是等距的。

这非常令人沮丧——我已经尝试调试了一天。谁能看到是什么导致 JS 代码失败?

【问题讨论】:

    标签: javascript python gps geometry trigonometry


    【解决方案1】:

    你不知何故把纬度/经度颠倒了。

    var linkDistance = 10; //$('#linkDistance').val();
    var nodesCount = 8;
    var bandwidth = "10 GB/s";
    var centerLat = 35.088878;
    var centerLon = -106.65262;
    var EARTH_RADIUS = 6378137;
    
    var mymap = L.map('mapid').setView([centerLat, centerLon], 11);
    
    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw', {
      maxZoom: 18,
      attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
        '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
        'Imagery © <a href="http://mapbox.com">Mapbox</a>',
      id: 'mapbox.streets'
    }).addTo(mymap);
    
    
    function drawNext(centerLat, centerLon) {
      var coords = [];
    
      for (var i = 0; i < nodesCount; i++) {
        var radius = linkDistance * 1000;
        var angle = Math.PI * 2 * i / nodesCount;
        var dx = radius * Math.cos(angle);
        var dy = radius * Math.sin(angle);
    
        var lat = centerLon + (180 / Math.PI) * (dy / 6378137);
        var lon = centerLat + (180 / Math.PI) * (dx / 6378137) / Math.cos(centerLon * Math.PI / 180);
    
        coords.push([lat, lon]);
      }
    
      for (var i = 0; i < coords.length; i++) {
        new L.Circle(coords[i], 500, {
          color: 'black',
          fillColor: '#f03',
          fillOpacity: 0.1
        }).addTo(mymap);
        console.log("added circle to: " + coords[i]);
      }
    
    }
    
    drawNext(centerLon, centerLat);
    
    
    var popup = L.popup();
    
    function onMapClick(e) {
      popup
        .setLatLng(e.latlng)
        .setContent("You clicked the map at " + e.latlng.toString())
        .openOn(mymap);
    }
    
    mymap.on('click', onMapClick);
    #mapid {
      height: 500px;
    }
    <script src="https://npmcdn.com/leaflet@1.0.0-rc.2/dist/leaflet-src.js"></script>
    <link href="https://npmcdn.com/leaflet@1.0.0-rc.2/dist/leaflet.css" rel="stylesheet"/>
    <div id="mapid"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多