Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
organizing by distance
#1
This is my code. Depending on the season and climate chosen I need to print the 5 closest cities from to coordinates 37°22'N and 107°25'W. I don't know where I would go from here. How do I find the closest cities. I already have the distance formula in my code but I can't implement it correctly

P.S. I know my for loops at the end are wrong. I have them there so I could get the correct syntax.

with open('cities.txt','r', encoding = 'cp1252') as fin: # opens file with read permission

    import math

    season = input('Will you take your vacation during summer or winter? ')
    climate = input('Do you want to go to a warm or a cold place? ')

    coldsummer = [] # these are empty lists that will be filled in later
    warmsummer = []
    coldwinter = []
    warmwinter = []
        
    data = fin.read().splitlines(True)
    
    for line in data[1:-1]: 
        line = line.split('\t') # splits elements in line with a tab

        lat = line[0] # these assign variables to certain parts of the file
        long = line[1]
        city = line[2]
          
        lat_deg = int(lat[0:2].strip('°').strip('N').strip('S')) # strips non integer parts
        lat_min = int(lat[3:5].strip('°').strip('N').strip('S'))
        long_deg = int(long[0:2].strip('°').strip('E').strip('W'))
        long_min = int(long[3:5].strip('°').strip('E').strip('W'))

        lat_DMS = lat_deg + (lat_min/60) # converts to degree-minute-second
        lat_rad = lat_DMS*(math.pi/180) # converts to radian
        long_DMS = long_deg + (long_min/60)
        long_rad = long_DMS*(math.pi/180)

        R = 6360 # assumed radius of Earth
        Mercedlat_DMS = 37 + 22/60 # coordinate for Merced, CA in degree-minute-second
        Mercedlat_rad = Mercedlat_DMS*(math.pi/180) # in radians
        Mercedlong_DMS = 107 + 25/60
        Mercedlong_rad = Mercedlong_DMS*(math.pi/180)

        distance = 2*R*math.sin(math.sqrt(math.sin((lat_rad - Mercedlat_rad)/2)**2 + # distance formula
                                          math.cos(lat_rad)*math.cos(Mercedlat_rad) *
                                          math.sin((long_rad - Mercedlong_rad)/2)**2))**-1

        if lat_deg > 66: # these if statements will fill in lists with appropriate values
            coldsummer.append(line)
            coldwinter.append(line)
        elif lat_deg > 35 and lat_deg < 66:
            warmsummer.append(line)
            coldwinter.append(line)
        else:
            warmsummer.append(line)
            warmwinter.append(line)
     
        for x in warmsummer: # the for loops will take desired list and print values
            if season == 'summer':
                if climate == 'warm':
                    print(x[2])
        for x in coldsummer:
            if season == 'summer':
                if climate == 'cold':
                    print(x[2])
        for x in warmwinter:
            if season == 'winter':
                if climate == 'warm':
                    print(x[2])
        for x in coldwinter:
            if season == 'winter':
                if climate == 'cold':
                    print(x[2])
Reply


Messages In This Thread
organizing by distance - by gonzo620 - Oct-10-2018, 11:27 PM
RE: organizing by distance - by stullis - Oct-11-2018, 01:55 AM
RE: organizing by distance - by gonzo620 - Oct-11-2018, 03:55 AM
RE: organizing by distance - by stullis - Oct-11-2018, 11:27 AM
RE: organizing by distance - by gonzo620 - Oct-15-2018, 04:30 PM
RE: organizing by distance - by stullis - Oct-16-2018, 12:28 AM
RE: organizing by distance - by gonzo620 - Oct-16-2018, 01:22 AM
RE: organizing by distance - by stullis - Oct-16-2018, 01:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Organizing several similar classes with overlapping variables 6hearts 7 1,435 May-07-2023, 02:00 PM
Last Post: 6hearts
  Organizing list of objects by attribute scarney1988 3 2,246 Mar-11-2020, 03:55 PM
Last Post: scarney1988
  Visualize Geo Map/Calculate distance zarize 1 1,912 Dec-05-2019, 08:36 PM
Last Post: Larz60+
  Organizing Data in Dictionary Ranjirock 3 2,658 Aug-27-2019, 02:48 PM
Last Post: Ranjirock
  euclidean distance jenya56 3 2,848 Mar-29-2019, 02:56 AM
Last Post: scidam
  Organizing tips Demini 1 2,238 Feb-10-2018, 05:32 AM
Last Post: metulburr
  Python Ble Distance Problem aleynasarcanli 10 12,580 Feb-09-2018, 10:48 PM
Last Post: aleynasarcanli
  Organizing results acmichelman 2 3,299 Sep-05-2017, 06:39 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020