Needed a way to get the centre of a polygon, defined by a list of osmdroid_geopoints.
Part of this algorithm is to get the area of the polygon. All this works fine and I get the
centre of the polygon (defined as making the polygon balance on a pin if put at the centre)
Having this area I would like to know how to get the real are in square km or whatever unit.
The actual obtained area value is very small, so there need to be some multiplication factor, but
not sure how to get this factor.
RBS
Part of this algorithm is to get the area of the polygon. All this works fine and I get the
centre of the polygon (defined as making the polygon balance on a pin if put at the centre)
Having this area I would like to know how to get the real are in square km or whatever unit.
The actual obtained area value is very small, so there need to be some multiplication factor, but
not sure how to get this factor.
B4X:
Sub GetPolygonArea(lstPoints As List) As Double
Dim i As Int
Dim dArea As Double
'Get the areas.
For i = 0 To lstPoints.Size - 2
Dim oGeoPoint1 As OSMDroid_GeoPoint = lstPoints.Get(i)
Dim oGeoPoint2 As OSMDroid_GeoPoint = lstPoints.Get(i + 1)
dArea = dArea + _
(oGeoPoint2.Longitude - oGeoPoint1.Longitude) * _
(oGeoPoint2.Latitude + oGeoPoint1.Latitude) / 2
Next
Return Abs(dArea)
End Sub
Sub FindCentroid(lstPoints As List) As OSMDroid_GeoPoint
Dim i As Long
Dim second_factor As Double
Dim polygon_area As Double
Dim X As Double
Dim Y As Double
Dim oGeoPointCenter As OSMDroid_GeoPoint
For i = 0 To lstPoints.Size - 2
Dim oGeoPoint1 As OSMDroid_GeoPoint = lstPoints.Get(i)
Dim oGeoPoint2 As OSMDroid_GeoPoint = lstPoints.Get(i + 1)
second_factor = _
oGeoPoint1.Longitude * oGeoPoint2.Latitude - _
oGeoPoint2.Longitude * oGeoPoint1.Latitude
X = X + (oGeoPoint1.Longitude + oGeoPoint2.Longitude) * _
second_factor
Y = Y + (oGeoPoint1.Latitude + oGeoPoint2.Latitude) * _
second_factor
Next
'Divide by 6 times the polygon's area.
polygon_area = GetPolygonArea(lstPoints)
General.RunLog("FindCentroid, polygon_area: " & polygon_area)
X = X / 6 / polygon_area
Y = Y / 6 / polygon_area
'If the values are negative, the polygon is
'oriented counterclockwise. Reverse the signs.
'(this works for me, but maybe wrong for other locations)
If Y < 0 Then
X = -X
Y = -Y
End If
oGeoPointCenter.Initialize(Y, X)
Return oGeoPointCenter
End Sub
RBS