Click here to Skip to main content
15,851,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi. I have a lot of data of Lng and Lat in geography.
how can I find max,min Lng and max,min Lat?

I use this to find those :

is there any fast algorithm to find that?

C#
double maxLng = double.MinValue, maxLat = double.MinValue;
          double minLng = double.MaxValue, minLat = double.MaxValue;
          foreach (var item in points)
          {

          }



          foreach (var item in points)
          {
              maxLng = item.Lng > maxLng ? item.Lng : maxLng;
              maxLat = item.Lat > maxLat ? item.Lat : maxLat;
              minLng = item.Lng < minLng ? item.Lng : minLng;
              minLat = item.Lat < minLat ? item.Lat : minLat;
          }


also there is a sample of data :
point[1]:{Lat=35.7159323908571, Lng=51.6639151177448}
point[2]:{Lat=35.8288428540831, Lng=51.5313353924282}
point[3]:{Lat=35.8210003633366, Lng=51.2716503558998} and ...
thanks.
Posted

Your algorithm is the best for a 1 time problem, it is brute force, but there is no other way with what you told us.
Nota: just remove the empty loop.

Anything can be different depending on:
- number of coordinates
- number of times you need min, max
- is it a database that evolve with time
- what is the other usages of this data
- is it persistent data or new set every times.
 
Share this answer
 
Comments
Member 11829564 9-Aug-15 7:04am    
thanks..
I suggest make use of Linq. You may use the following code for your reference

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Point
    {
        public Double Lat { get; set; }
        public Double Lng { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Point> lstPoints = new List<Point>
            {
                new Point(){Lat=35.7159323908571, Lng=51.6639151177448},
                new Point(){Lat=35.8288428540831, Lng=51.5313353924282},
                new Point(){Lat=35.8210003633366, Lng=51.2716503558998}
            };

            Double MaxLat = lstPoints.OrderByDescending(x => x.Lat).First().Lat;
            Double MinLat = lstPoints.OrderByDescending(x => x.Lat).Last().Lat;

            Double MaxLng = lstPoints.OrderByDescending(x => x.Lng).First().Lng;
            Double MinLng = lstPoints.OrderByDescending(x => x.Lng).Last().Lng;

            Console.WriteLine("Max Lat:{0},Min Lat{1}", MaxLat, MinLat);
            Console.WriteLine("Max Lng:{0},Min Lng:{1}", MaxLng, MinLng);

            Console.ReadLine();

        }

    }
}
 
Share this answer
 
Comments
Member 11829564 9-Aug-15 3:28am    
well, the point are dynamic , and I want to do mathematical formula on them.
what should I do?
sreeyush sudhakaran 9-Aug-15 4:15am    
You can add Points like this dynamically:
Point objPoint = new points();
objPoint.Lat = 35.7159323908571;
objPoint.Lng=51.6639151177448;
lstPoints.Add(objPoint);

or Even like this
priavte Point GetPoints()
{
//Logic for getting points from some where

return objPoints;
}

lstPoints.Add(GetPoints());
Patrice T 9-Aug-15 3:37am    
Usually, sorting data cost more than the simple scan of data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900