question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create circle around coordinates

See original GitHub issue

Is there any possibility to get a circle around a point with the actual diameter? I have the latitude, longitude and the diameter of a Mesocyclone object. Currently I use a default value for the radius while drawing a circle using a Polygon. Is there any possibility, to draw the circle around that point with the actual size (e.g. 10km)? My current code:

        private static Layer CreateMesoDiameterLayer(List<Mesocyclone> mesocyclones)
        {
            var features = new Features();
            return new Layer("Diameter Layer")
            {
                DataSource = new MemoryProvider(CreateDiameterCircles(mesocyclones)),
                Style = new VectorStyle
                {
                    Fill = new Brush(new Color(255, 97, 247, 50)),
                    Outline = new Pen
                    {
                        Color = new Color(121, 97, 255, 200),
                        Width = 2,
                        PenStyle = PenStyle.LongDashDot,
                        PenStrokeCap = PenStrokeCap.Butt
                    }
                }
            };
        }

        private static List<Polygon> CreateDiameterCircles(List<Mesocyclone> mesocyclones)
        {
            var result = new List<Polygon>();

            foreach (var meso in mesocyclones)
            {
                var polygon = new Polygon();
                var middle = FromLongLat(meso.Longitude, meso.Latitude);
                double radius = 100000; // Currently default value. Here I want to have the actual diameter (property of a mesocyclone

                for (double angle = 0; angle <= 2 * Math.PI; angle += 0.2)
                {
                    polygon.ExteriorRing.Vertices.Add(
                        new Point(
                            middle.X + radius * Math.Cos(angle),
                            middle.Y + radius * Math.Sin(angle)));
                }

                result.Add(polygon);
            }

            return result;
        }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
buu-huucommented, Jun 27, 2021

I dont’t know, if theres a better way, but I managed to do the job with a little bit of maths. Maybe, it will help someone in the future, so I share my solution with you.

Basically I created a function, that creates a new Point from a given longitude, latitude, distance and bearing/rotation:

private const double RADIUS_EQUATOR = 6378.1;

private static Point GetDistancePoint(double longitude, double latitude, double rotation, double distance)
{
    double brng = ConvertDegreesToRadians(rotation); // In radians! 90° == 1.57

    double lat1 = ConvertDegreesToRadians(latitude);
    double lon1 = ConvertDegreesToRadians(longitude);

    double lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(distance / RADIUS_EQUATOR) +
        Math.Cos(lat1) * Math.Sin(distance / RADIUS_EQUATOR) * Math.Cos(brng));

    double lon2 = lon1 + Math.Atan2(Math.Sin(brng) * Math.Sin(distance / RADIUS_EQUATOR) * Math.Cos(lat1),
        Math.Cos(distance / RADIUS_EQUATOR) - Math.Sin(lat1) * Math.Sin(lat2));

    lat2 = ConvertRadiansToDegrees(lat2);
    lon2 = ConvertRadiansToDegrees(lon2);
    return FromLongLat(lon2, lat2);
}

When I create the layer for the diameter rings, I just call that function in a for loop (<= 360 degrees) and add the generated point to the polygon:

private static List<Polygon> CreateDiameterCircles(List<Mesocyclone> mesocyclones)
{
    var result = new List<Polygon>();

    foreach (var meso in mesocyclones)
    {
        var polygon = new Polygon();
        var mesoCenterLon = meso.Longitude;
        var mesoCenterLat = meso.Latitude;
        double radius = meso.Diameter/2;

        for (int step = 0; step <= 360; step += 5)
        {
            polygon.ExteriorRing.Vertices.Add(
                GetDistancePoint(mesoCenterLon, mesoCenterLat, step, radius));
        }
        result.Add(polygon);
    }
    return result;
}

/* Math Helpers */

private static Point FromLongLat(double longitude, double latitude)
{
    return SphericalMercator.FromLonLat(longitude, latitude);
}

public static double ConvertRadiansToDegrees(double radians)
{
    double degrees = (180 / Math.PI) * radians;
    return degrees;
}

public static double ConvertDegreesToRadians(double degrees)
{
    double radians = (Math.PI / 180) * degrees;
    return radians;
}
1reaction
charlennicommented, Jun 27, 2021

I assume, that you don’t use Xamarin.Forms. That’s the reason why you don’t have this class. But you could look, how the Xamarin.Forms implementation does it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Draw a circle with a radius on a map
Use this tool to draw a circle by entering its radius along with an address. You can also click a point on the...
Read more >
Create circles—ArcGIS Pro | Documentation
Create circles from known coordinates · In the Distance and Direction pane, click the Circle tab. · Enter a coordinate in the Center...
Read more >
Radius Around a Point on a Map
First type in the radius required in kilometers or miles and then click on the map at the center of where you wish...
Read more >
Producing Points in a Circle with Specific Radius from ...
I need to be able to take a set of lat/lon coordinates and draw a circular "fence" around those coordinates on a map,...
Read more >
Generate a circle of coordinates around a point, but I'm ...
I'm using the following code to attempt to generate a circle of coordinates a fixed distance (in this case 1km) around a point....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found