[英]Getting area from GPS coordinates
So, I've got a method that returns the area of a shape defined by its points (given in CCW or CW order, it doesn't really matter). 因此,我有一个方法可以返回由其点定义的形状区域(按CCW或CW顺序,这并不重要)。 I tested it with some shapes and it seems to be working.
我用一些形状对其进行了测试,它似乎可以正常工作。
The problem is, I want to use this method with GPS coordinates, and I want to return the result in m² or km², but that's definitly not what happens. 问题是,我想将此方法与GPS坐标一起使用,并且我想以m²或km²为单位返回结果,但是绝对不会发生这种情况。 In fact, I don't even know in which unit the result is when I use this method with that kind of coordinates.
实际上,我什至不知道将这种方法与那种坐标一起使用时的结果是哪个单位。
So the question is, how to convert the result I have into m² or km² ? 所以问题是,如何将我得到的结果转换为m²或km²? I tried some things, but either it does not work or it's inaccurate.
我尝试了一些方法,但是它不起作用或不正确。
Here's my method, if you want to check : 这是我的方法,如果您要检查:
public static double getArea(List<Vector2D> points) {
double firstSum = 0, secondSum = 0;
for (int i = 0 ; i < points.size()-1 ; i++) {
firstSum += points.get(i).x * points.get(i+1).y;
secondSum += points.get(i).y * points.get(i+1).x;
}
firstSum += points.get( points.size()-1 ).x * points.get(0).y;
secondSum += points.get( points.size()-1 ).y * points.get(0).x;
return Math.abs((firstSum-secondSum)/2);
}
( Vector2D
is the class I use for points, with x
as the latitude and y
as the longitude) (
Vector2D
是我用于点的类,其中x
为纬度, y
为经度)
The problem is that you're not taking the (approximately) spherical nature of the Earth into account. 问题在于您没有考虑地球的(近似)球形性质。 For a start, you need to take into account the radius of the Earth - you could have the same list of latitude and longitude on a smaller (or bigger) planet, but the radius would be different, and consequently the area would be different too.
首先,您需要考虑地球的半径-在较小(或较大)的行星上可以具有相同的纬度和经度列表,但是半径会有所不同,因此面积也将有所不同。
You can use the approach in this question . 您可以在此问题中使用该方法。 It's trivial to convert that to Java:
将其转换为Java很简单:
public static double CalculatePolygonArea(List<Vector2D> coordinates)
{
double area = 0;
if (coordinates.size() > 2)
{
for (int i = 0; i < coordinates.size()-1; i++)
{
Vector2D p1, p2;
p1 = coordinates.get(i);
p2 = coordinates.get(i + 1);
area += Math.toRadians(p2.x - p1.x) * (2 + Math.sin(Math.toRadians(p1.y))
+ Math.sin(Math.toRadians(p2.y)));
}
area = area * R * R / 2;
}
return Math.abs(area);
}
(assuming Vector2D.x
is the longitude and Vector2D.y
is the latitude). (假设
Vector2D.x
是经度, Vector2D.y
是纬度)。
R
is the radius of the Earth. R
是地球的半径。 Use a value in the unit you want the area result to be in (eg 6_371_000 metres for square metres, 6_371 km for square km, 3_959 miles for square miles...) 在您要面积结果使用的单位中使用一个值(例如,6_371_000米为平方米,6_371 km为平方公里,3_959英里为平方英里...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.