# Find most NW, NE, SW, & SE coordinates from a group of long/lats

I have a list of 900-1000 longitude/latitude coordinates. I know and can depend that these coordinates form a rough rectangle. Is there a formula to determine which coordinates are at the corners (NW, NE, SE, SW)?

Is it correct to say that:

1. the coordinate with the largest lat and smallest long is the most NW?

and thus

1. the smallest lat and largest long is the SE corner?
2. the smallest lat and smallest long is the SW corner?
3. the largest lat and largest long is the NE corner?

The points I am dealing with are gathered by a drone flying a rectangular area approximately 100 sq meters. It crosses the entire included area of the rectangle in rows columns type fashion (snaking back and forth) and could start in the middle of the rectangle or at a corner or anywhere within the area.

Here is what I ended up with, if all corners are right angles you can look at lat/long in same if/then statement but if it is trapezoidal (non rect or square) you have to check the lat/long in separate if/thens

``````let NW = {
name:"NW",
lat:-90,
long:180
};

let NE = {
name:"NE",
lat:-90,
long:-180
};

let SW = {
name:"SW",
lat:90,
long:180
};

let SE = {
name:"SE",
lat:90,
long:-180
};

imggps.forEach((img)=>{

// the coordinate with the largest
// lat and smallest long is the most NW?
if(img.lat > NW.lat){
NW.lat = img.lat;
}
if(img.long < NW.long){
NW.long = img.long;
}

// the largest lat and largest
// long is the NE corner?
if(img.lat > NE.lat){
NE.lat = img.lat;
}
if(img.long > NE.long){
NE.long = img.long;
}

// the smallest lat and smallest
// long is the SW corner?
if(img.lat < SW.lat){
SW.lat = img.lat;
}
if(img.long < SW.long){
SW.long = img.long;
}

// the smallest lat and largest
// long is the SE corner?
if(img.long > SE.long){
SE.long = img.long;
}
if(img.lat < SE.lat){
SE.lat = img.lat;
}

});
``````

• Depends on whether you are working with data from the eastern or western hemisphere.
– Erik
Commented Nov 1, 2019 at 13:44
• Thanks Erik, western, US only - if United States only, above is accurate? Commented Nov 1, 2019 at 14:17
• this depends on the defined coordinate frame (extent) of your geographic reference system, e.g. EPSG:4326 is defined on a reference frame spanning `-180.0 - 180.0` (W-E) Lon and `-90.0 - 90.0` (S-N) Lat, so your assumptions are correct no matter where. Commented Nov 1, 2019 at 14:48
• This is longitude and latitude that you would get from googlemap, gps enabled camera, etc. `27.79668 lat` `-82.33275 long` is in the middle or Florida, United States - so I was thinking if I go North the lat gets higher and if I go West long gets smaller (more negative). So using that frame of reference, NW would be the largest lat and smallest long right? Commented Nov 1, 2019 at 15:35
– PolyGeo
Commented Nov 2, 2019 at 19:56

<1000 points it's not worth trying to optimize (i.e. guess the answer) just look at all of the points and keep track of min and max of lat and lon.

Something like:

``````import math
min_lat = math.inf
min_lon = math.inf
max_lat = -math.inf
max_lon = -math.inf
for p in points:
min_lat = min(p.lat, min_lat)
min_lon = min(p.lon, min_lon)
max_lat = max(p.lat, max_lat)
max_lon = max(p.lon, max_lon)

print(min_lat, min_lon, max_lat, max_lon)
``````
• Ian, thanks for taking the time to respond. Just curious what language are you typing above? I work with node, JS, C#, & PHP, so I am unsure what `.inf` does above. More importantly - you are confirming my statements in the question that for example, the largest lat and smallest long coordinate is the most NW coordinate?? Commented Nov 1, 2019 at 15:35
• that's python, inf is infinity - that way it is easy to see if the bbox is invalid. Also I may have misunderstood what you meant, for me coordinate means one of the input points Commented Nov 1, 2019 at 15:53