Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Issue #26 Solutions (matlab, py)
Browse files Browse the repository at this point in the history
Code executes when inputting lat/long, does not pull from LADI imagery.
  • Loading branch information
zroberts45 authored Oct 28, 2020
1 parent 29c5492 commit ad5b78e
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tutorials/code/airport_distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 14 18:08:46 2020
@author: zachr
"""


## Inputs either a N X 1 imagery UUID or an image itself


import pandas as pd
import csv
import numpy as np
from math import sqrt

## Calculate the distance, in units meters, between the latitude, longitude metadata of the image and all nearby airports

# Assign value to lat/long of image
#latimage = 0
#longimage = 0

# making data frame
df = pd.read_csv("Airports.csv")
longairport = df['X'].values

This comment has been minimized.

Copy link
@aweinert-MIT

aweinert-MIT Oct 28, 2020

Member

Please add units to variable names and update variables to align with PEP 8 naming conventions (PEP 8 reference). So longairport could be airport_lon_deg and latairport can be airport_lat_deg, etc.

latairport = df['Y'].values
nameairport = df['NAME'].values
idairport = df['GLOBAL_ID'].values

#print(type(longairport[0]))
#airportloc = np.array([longairport, latairport])

# make new column for distance to airports
def findclosestairport(latimage,longimage):

closest_index = 0
closest_distance = np.inf

#for latimage, longimage in somelist:
#latimage_list = []
for idx in range(len(longairport)):
curr_long = float(longairport[idx])
curr_lat = float(latairport[idx])

curr_distance = get_distance_degrees(curr_long, curr_lat, latimage, longimage)

if curr_distance < closest_distance:
closest_distance = curr_distance
closest_index = idx

distance_meters = closest_distance * 111194.926644559

return [distance_meters, longairport[closest_index], latairport[closest_index], nameairport[closest_index], idairport[closest_index]]




def get_distance_degrees(long, lat, latimage, longimage):
return sqrt((abs(latimage - lat))**2 + (abs(longimage - long))**2)

# input long,lat, output closest airport info
print(findclosestairport(42.373615, -71.109734))



58 changes: 58 additions & 0 deletions tutorials/code/closest_airport.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
% location of cambridge,ma
latimage = 42.373615;
longimage = -71.109734;

% make into array
longairport = table2array(Airports(:,1));

This comment has been minimized.

Copy link
@aweinert-MIT

aweinert-MIT Oct 28, 2020

Member

Where is Airports defined?

This comment has been minimized.

Copy link
@zroberts45

This comment has been minimized.

Copy link
@aweinert-MIT

aweinert-MIT Oct 28, 2020

Member

This CSV is downloaded as part of setup.sh, please note this as a comment in your matlab code. Also please include the command to load the csv into matlab memory.

latairport = table2array(Airports(:,2));
uuidairport = table2array(Airports(:,4));
nameairport = table2array(Airports(:,6));

% zero variables
distance_degrees = zeros(height(Airports),1);
distance_meters = zeros(height(Airports),1);

% iterate over all airports
for i = 1: height(Airports)
distance_degrees(i) = sqrt((abs(latimage - latairport(i)))^2 + (abs(longimage - longairport(i)))^2);

This comment has been minimized.

Copy link
@aweinert-MIT

aweinert-MIT Oct 28, 2020

Member

Can also use hypot

distance_meters(i) = distance_degrees(i) * 111194.926644559;
end

airport_id = table2array(Airports(:,3));
% airport_uuid = table2array(Airports(:,4));
% airport_name = table2array(Airports(:,6));

% organize information
airportinfo = zeros(height(Airports),2);
for i = 1:height(Airports)
airportinfo(i,1) = distance_meters(i);
airportinfo(i,2) = airport_id(i);
end

% airportinfo(i,2) = airport_uuid(i);
% airportinfo(i,3) = airport_name(i);

% sort distances
airportsort = sortrows(airportinfo,1);

% Output a N X 4 column of UUID, image latitude, image longitude,
% distance to nearest airport, UUID of nearest airport

closestairportlong = longairport(airportsort(1,2));
x1 = 'The longitude of the closest airport is ';
disp(x1)

This comment has been minimized.

Copy link
@aweinert-MIT

aweinert-MIT Oct 28, 2020

Member

Recommend using fprintf instead of disp, as it is easier to port to other languages then

This comment has been minimized.

Copy link
@zroberts45

zroberts45 Oct 28, 2020

Author

I was having difficulties outputting, but essentially what you are looking for is stored in the values of the statement. I was also trying to get the code to output all the information as one array, but it has difficulty combining text and numbers as Doubles, strings, etc.

This comment has been minimized.

Copy link
@aweinert-MIT

aweinert-MIT Oct 28, 2020

Member

Understood. I'll take a stab of updating this with fprintf to help give you an example to work off of.

closestairportlong
x2 = 'The latitude of the closest airport is ';
disp(x2)
latairport(airportsort(1,2))
x3 = 'The distance in meters from the closest airport is ';
disp(x3)
airportsort(1,1)
x4 = ['The UUID of the closest airport is ', uuidairport(airportsort(1,2))];
disp(x4)
x5 = ['The name of the closest airport is ', nameairport(airportsort(1,2))];
disp(x5)




1 comment on commit ad5b78e

@aweinert-MIT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zroberts45 , I have a few commits with the Python units / naming convention as the most important comment. Thanks

Please sign in to comment.