''' Purpose: determine a rough estimate of the geocenter of the continental USA Strategy1: Average the latitude and the longitude Strategy2 (Median Strategy): Put them in order and find the one in the middle (sort them and find the "median" value) Strategy3 (Average of the Min/Max Strategy): Average the Min and Max latitudes and longitudes ''' # get access to web-based data support import url # specify dataset web location CSV_WEB_FOLDER = 'http://www.cs.virginia.edu/~cs1112/datasets/csv/' USA_DATASET = CSV_WEB_FOLDER + 'continental-usa.csv' # get the dataset of interest dataset = url.get_dataset( USA_DATASET ) # determine webfile header and location data header = dataset[ 0 ] # ['Zipcode', 'Town','State', 'Latitude','Longitude'] locations = dataset[ 1 : ] # determine geocenter # Accumulators list_latitudes = [] # Add all the latitudes from each entry into this list list_longitudes = [] # Add all the longitudes from each entry into this list # a loop that might be helpful for solving the problem for entry in locations : # We are looking at each row (list) after the header zipcode, town, state, latitude, longitude = entry # Get the values in each entry and store them in variables # Ex. one entry is [99362, 'Walla Walla', 'OR', 45.986775, -118.07919] # So the variables are assigned the values from the list so that # zipcode is 99362, town is 'Walla Walla', state is 'OR', latitude is 45.986775, longitude is -118.07919 list_latitudes.append( latitude ) # For the entry we're on, put it's latitude into our list_latitudes list_longitudes.append( longitude ) # For each entry we're on, put it's longitude into our list_longitudes print( list_latitudes ) print( list_longitudes) # Get Average of Min and Max Latitude and Longitude min_latitude = min( list_latitudes ) max_latitude = max( list_latitudes ) min_longitude = min( list_longitudes ) max_longitude = max( list_longitudes ) average_latitude_1 = (min_latitude + max_latitude ) / 2 average_longitude_1 = (min_longitude + max_longitude) / 2 print('Guess 1: ', average_latitude_1, average_longitude_1 ) # 36.954868499999996 -95.81564449999999 # Get Average of All Latitudes and All Longitudes number_locations = len( list_latitudes ) # How many latitudes and longitudes do we have (how many locations) # Average = sum / total # of things average_latitude_2 = sum( list_latitudes ) / number_locations average_longitude_2 = sum( list_longitudes ) / number_locations print('Guess 2: ', average_latitude_2, average_longitude_2 ) # 38.52071282671683 -90.43203752861102 # Get the median of the latitudes and longitudes (middle value) # The sorted() function takes in a list and hands back a sorted version of that list sorted_latitudes = sorted( list_latitudes ) sorted_longitudes = sorted( list_longitudes ) middle_index = number_locations // 2 # index of the middle location # Use index to get the latitude and longitude at that index position middle_index middle_latitude = sorted_latitudes[middle_index] middle_longitude = sorted_longitudes[middle_index] print( 'Guess 3:', middle_latitude, middle_longitude ) # 39.143979 -87.76815