def get_country_details(self, country):
     """Returns country code(alpha_3) and continent"""
     try:
         country_obj = pycountry.countries.get(name=country)
         if country_obj is None:
             c = pycountry.countries.search_fuzzy(country)
             country_obj = c[0]
         continent_code = pc.country_alpha2_to_continent_code(
             country_obj.alpha_2)
         continent = pc.convert_continent_code_to_continent_name(
             continent_code)
         return country_obj.alpha_3, continent
     except:
         if 'Congo' in country:
             country = 'Congo'
         if country == 'Mainland China':
             country = 'China'
         elif country == 'Diamond Princess' or country == 'Laos' or country == 'MS Zaandam' or country == 'Holy See' or country == 'Timor-Leste':
             return country, country
         elif country == 'Korea, South' or country == 'South Korea':
             country = 'Korea, Republic of'
         elif country == 'Taiwan*':
             country = 'Taiwan'
         elif country == 'Burma':
             country = 'Myanmar'
         elif country == 'West Bank and Gaza':
             country = 'Gaza'
         else:
             return country, country
         country_obj = pycountry.countries.search_fuzzy(country)
         continent_code = pc.country_alpha2_to_continent_code(
             country_obj[0].alpha_2)
         continent = pc.convert_continent_code_to_continent_name(
             continent_code)
         return country_obj[0].alpha_3, continent
Beispiel #2
0
    def all_countries(self):
        resp = requests.get(self.all)
        json = resp.json()
        first = json[0]['CountryCode']
        last10daysAll = []
        last10daysSpecific = []
        for i in json:
            if i['CountryCode'] == first:
                try:

                    code = pc.country_alpha2_to_continent_code(i['CountryCode'])
                    last10daysSpecific.append((code,i['Confirmed']))
                except:
                    pass
            else:
                last10daysSpecific = last10daysSpecific[-10:]
                if (len(last10daysSpecific) != 0):
                    last10daysAll.append(last10daysSpecific)
                last10daysSpecific.clear()
                first = i['CountryCode']
                try:
                    code = pc.country_alpha2_to_continent_code(i['CountryCode'])
                    last10daysSpecific.append((code,i['Confirmed']))
                except:
                    pass
        print(json)
def get_continent(country):
    """
    DESCRIPTION: return continent code given country name.
        
    INPUT: country - (str) country name
        
    OUTPUT: continent_code - (str) continent code for country
    """

    try:
        #get country code
        country_code = pc.country_name_to_country_alpha2(
            country, cn_name_format="default")
        # get continent code
        continent_code = pc.country_alpha2_to_continent_code(country_code)

    except:

        # given that there are only two countries that cause an exception
        # it's easy to deal with them manually

        if country == 'The Bahamas':
            return 'NA'

        elif country == 'Myanmar (Burma)':
            return 'AS'

        # Redundant case
        else:
            print('Something Went Wrong')
            return -1

    return continent_code
def execute_dtw(df_period_group, compare_group):
    dtw_results = []
    for country in df_period_group:
        try:
            country_code = pc.country_name_to_country_alpha2(
                country[0], cn_name_format="default")
        except Exception as e:
            print(country[0], "has error", e)
            continue
        try:
            continent_name = pc.country_alpha2_to_continent_code(country_code)
        except Exception as e:
            print(country[0], "has error", e)
            continue

        try:
            result = dtw.dtw(compare_group["StringencyIndex"],
                             country[1]["StringencyIndex"])
        except Exception as e:
            print(country[0], "has error", e)
            continue

        dtw_results.append([
            country[0], continent_name, result.distance,
            result.normalizedDistance
        ])
    dtw_df = pd.DataFrame(
        data=dtw_results,
        columns=["Country", "Continent", "Distance", "Normalized_distance"],
    )
    return dtw_df
def get_continent_name(code):
    # Input:
    # -- code: iso alpha 3 country code
    # Output:
    # -- continent: name of the continent

    # Create continent variable
    continent = np.nan

    # Convert iso alpha 3 code to iso alpha 2 code
    country_code = alpha3_to_alpha2(code)
    if country_code == 'VA':
        country_code = 'IT'
    elif country_code == 'SX':
        country_code = 'NL'

    # Convert is alpha 2 code to continent code
    continent_code = pc.country_alpha2_to_continent_code(country_code)

    # Convert continent code to continent name
    if continent_code == 'NA':
        continent = 'North America'
    elif continent_code == 'SA':
        continent = 'South America'
    elif continent_code == 'AF':
        continent = 'Africa'
    elif continent_code == 'AS':
        continent = 'Asia'
    elif continent_code == 'EU':
        continent = 'Europe'

    # Return continent name
    return continent
def classify_Ark_probes_per_continent(List_probes):
    ## Classifies Ark probes per continent and stores them into corresponding files
    Dict_cont_probe = {}
    continents = {
    'NA': 'North_America',
    'SA': 'South_America',
    'AS': 'Asia',
    'OC': 'Oceania_Australia',
    'AF': 'Africa',
    'EU': 'Europe'
    }
    i = 0
    for probe in List_probes:
        tab = probe.split('-')
        cont = ''
        try:
            cont = pc.country_alpha2_to_continent_code(str(tab[1]).upper())
        #i+=1
        except:
            if str(tab[1]).upper() == 'UK':
                cont = 'EU'
            #i+=1
            pass
        print ("The probe " + probe + " is in " + cont)
        
        ## store in a dictionary
        if continents[cont] in Dict_cont_probe:
            Dict_cont_probe[continents[cont]].append(probe)
 def get_continents(self, country_name):
     continents_table = {
         "AS": "Asia",
         "EU": "Europe",
         "NA": "North-America",
         "SA": "South-America",
         "AF": "Africa",
         "OC": "Oceania"
     }
     countries_not_found_table = {
         "England": "Europe",
         "Wales": "Europe",
         "Scotland": "Europe",
         "Northern Ireland": "Europe",
         "Kosovo": "Europe",
         "Chinese Taipei": "Asia",
         "Palestinian territories": "Asia",
         "East Timor": "Asia",
         "Netherlands Antilles": "South-America",
         "Saint Helena": "Africa"
     }
     try:
         iso2_cname = pycc.country_name_to_country_alpha2(country_name)
         continent_code = pycc.country_alpha2_to_continent_code(iso2_cname)
         return continents_table.get(continent_code)
     except KeyError:
         return countries_not_found_table.get(country_name)
Beispiel #8
0
def get_continent(row):
    """ retrieve the continent data for a given row """

    if row['Country Name'] in [
            'Bahamas, The', 'Curacao', 'Sint Maarten (Dutch part)',
            'St. Kitts and Nevis', 'St. Lucia', 'St. Martin (French part)',
            'St. Vincent and the Grenadines', 'Virgin Islands (U.S.)'
    ]:
        return 'NA'
    elif row['Country Name'] in [
            'Channel Islands', 'Czech Republic', 'Faeroe Islands', 'Kosovo',
            'Macedonia, FYR', 'Moldova', 'Slovak Republic'
    ]:
        return 'EU'
    elif row['Country Name'] in [
            'Hong Kong SAR, China', 'Iran, Islamic Rep.', 'Korea, Dem. Rep.',
            'Korea, Rep.', 'Kyrgyz Republic', 'Lao PDR', 'Macao SAR, China',
            'Micronesia, Fed. Sts.', 'Timor-Leste', 'Vietnam',
            'West Bank and Gaza', 'Yemen, Rep.'
    ]:
        return 'AS'
    elif row['Country Name'] in [
            'Congo, Dem. Rep.', 'Congo, Rep.', "Cote d'Ivoire",
            'Egypt, Arab Rep.', 'Tanzania', 'Gambia, The'
    ]:
        return 'AF'
    elif row['Country Name'] in ['Bolivia', 'Venezuela, RB']:
        return 'SA'
    else:
        return pycountry_convert.country_alpha2_to_continent_code(
            pycountry.countries.get(name=row['Country Name']).alpha_2)
Beispiel #9
0
 def addContinent(self, df):
     df['Continent'] = df.apply(
         lambda row: pc.convert_continent_code_to_continent_name(
             pc.country_alpha2_to_continent_code(
                 pc.country_name_to_country_alpha2(
                     row.country, cn_name_format="default"))),
         axis=1)
Beispiel #10
0
    def add_continent_name(self) -> None:
        """
		Adds a continent name to this tweet.
		"""
        if self.has_continent_name():
            # already has a continent name
            return

        if self.country_code is None or len(self.country_code) != 2:
            # incorrect country code
            self.continent: None = None
            return

        if self.country_code == 'AQ':
            # special case
            self.continent: str = 'Antarctica'
            return

        try:
            # return continent name from country code
            self.continent: str = convert_continent_code_to_continent_name(
                country_alpha2_to_continent_code(self.country_code))
            return
        except:
            # return None if this fails
            self.continent: None = None
            return
Beispiel #11
0
def continents(elem):
    if elem == 'Mainland China':
        return 'China'
    if elem == 'US':
        return 'Nordamerika'
    if elem == 'UK':
        return 'Europa'
    if elem == 'Others':
        return 'Andere'
    try:
        country_code = pc.country_name_to_country_alpha2(
            elem, cn_name_format="default")
        continent_name = pc.country_alpha2_to_continent_code(country_code)
        if continent_name == "NA":
            return 'Nordamerika'
        if continent_name == 'EU':
            return "Europa"
        if continent_name == 'AS':
            return "Asien"
        if continent_name == 'AF':
            return "Afrika"
        if continent_name == 'OC':
            return "Ozeanien"
        return continent_name
    except:
        return elem
Beispiel #12
0
def main():
  data = read_file('internet.csv')
  incomeperperson = pd.Series(get_variable(data, "incomeperperson"))
  internetuserate = pd.Series(get_variable(data, "internetuserate"))
  urbanrate = pd.Series(get_variable(data, "urbanrate"))

  incomeperperson_vs_internetuserate = internetuserate.corr(incomeperperson)
  urbanrate_vs_internetuserate = internetuserate.corr(urbanrate)
  urbanrate_vs_incomeperperson = incomeperperson.corr(urbanrate)

  print(incomeperperson_vs_internetuserate)
  print(urbanrate_vs_internetuserate)
  print(urbanrate_vs_incomeperperson)

  continents = {
    'NA': 'North America',
    'SA': 'South America', 
    'AS': 'Asia',
    'OC': 'Australia',
    'AF': 'Africa',
    'EU': 'Europe'
  }
  for point in data:
    country_code = pc.country_name_to_country_alpha2(point['country'], cn_name_format="default")
    continent_code = pc.country_alpha2_to_continent_code(country_code)
    point['continent'] = continents[continent_code]
  write_file(data)
Beispiel #13
0
def get_continent(country):
    try:
        country_code = pc.country_name_to_country_alpha2(
            country, cn_name_format='default')
        return pc.country_alpha2_to_continent_code(country_code)
    except (KeyError, TypeError):
        return country
    def geolocator_process(loc_string):
        """
    user location will be determent by the geo-locator library. it receives the users location as written in the web
    converts it to  latitude and longitude then it will be called again to convert the latitude and longitude to
    a global unique name of country and continent.
        """
        country, continent = None, None  # initiate the returned variables
        loc = geolocator.geocode(loc_string)
        if loc:
            lat, lon = loc.latitude, loc.longitude
            time.sleep(config.SLEEP_TIME_FOR_LOCATIONS_API)
            try:
                new_loc = geolocator.reverse([lat, lon], language='en')
                country = new_loc.raw["address"]["country"]
                continent = config.continents_dict[
                    country_alpha2_to_continent_code(
                        country_name_to_country_alpha2(country))]

            except TypeError:
                logger.warning(
                    config.USER_PROBLEMATIC_COUNTRY.format(loc_string))

            except KeyError:
                if country in config.KNOWN_COUNTRIES:
                    country, continent = config.KNOWN_COUNTRIES[country]
            finally:
                time.sleep(config.SLEEP_TIME_FOR_LOCATIONS_API)
        return country, continent
Beispiel #15
0
    def test_country_alpha2_to_continent(self):
        cn_continent = country_alpha2_to_continent_code('US')
        assert (cn_continent)
        assert (cn_continent == 'NA')

        cn_continent = country_alpha2_to_continent_code('AU')
        assert (cn_continent)
        assert (cn_continent == 'OC')

        cn_continent = country_alpha2_to_continent_code('NZ')
        assert (cn_continent)
        assert (cn_continent == 'OC')

        cn_continent = country_alpha2_to_continent_code('JP')
        assert (cn_continent)
        assert (cn_continent == 'AS')
Beispiel #16
0
def get_country_continent_from_address(address_dict):
    """
	
	Takes address dictionary (from 'get_address_by_location'), returns country

	"""

    # dictionary of continent names:
    continent_name_dict = {
        "AF": "Africa",
        "AS": "Asia",
        "NA": "North America",
        "SA": "South America",
        "OC": "Oceania",
        "EU": "Europe"
    }

    # get info from address dict
    address = address_dict["address"]
    country = address["country"]

    # get continent
    country_code = pc.country_name_to_country_alpha2(country,
                                                     cn_name_format="default")

    continent_abbreviation = pc.country_alpha2_to_continent_code(country_code)
    continent_name = continent_name_dict[continent_abbreviation]

    return [country, continent_name]
def reverse_geocode(lat, lng):
    reverse_data = geocoder.osm_reverse.OsmReverse((lat, lng)).geojson
    country = None
    city = None
    continent = None

    try:
        feature_properties = reverse_data['features'][0]['properties']
        cc = feature_properties['country_code'].upper()
        city = None
        if "city" in feature_properties:
            city = feature_properties['city']
        elif "town" in feature_properties:
            city = feature_properties["town"]
        elif "state" in feature_properties:
            city = feature_properties['state']
        elif "county" in feature_properties:
            city = feature_properties['county']

        country = pycountry.countries.get(alpha_2=cc).name
        continent_code = country_alpha2_to_continent_code(cc)
        continent = convert_continent_code_to_continent_name(continent_code)
    except:
        return None, None, None
    else:
        return continent, country, city
Beispiel #18
0
def country_to_continent(country_name):
    country_alpha2 = pc.country_name_to_country_alpha2(country_name)
    country_continent_code = pc.country_alpha2_to_continent_code(
        country_alpha2)
    country_continent_name = pc.convert_continent_code_to_continent_name(
        country_continent_code)
    return country_continent_name
    def gather(self, data_name):
        """Importing and cleaning OECD.
                returns istat as pandas.dataframe"""

        # Import, Rename, adjust values in million
        oecd = pd.read_csv(f'data/OECD_{data_name}.csv')
        oecd.rename(columns={
            'LOCATION': 'Country',
            'TIME': 'Year',
            'Value': data_name
        },
                    inplace=True)
        oecd['Country'] = oecd['Country'].apply(lambda x: x.split(' ')[
            0] if re.match(r'[France|Germany].*', x) else x)

        oecd[data_name] = oecd[data_name].apply(
            lambda value: float(value * 1000000))

        # Kick out every country with the wrong code len (they're not in europe anyways) & not in europe
        oecd = oecd[oecd['Country'].apply(lambda x: len(x) == 3)]
        oecd = oecd[oecd['Country'].apply(
            lambda x: pc.country_alpha2_to_continent_code(
                country_2_code=pc.country_alpha3_to_country_alpha2(x
                                                                   )) == 'EU')]

        # Turn country codes to full country names
        oecd['Country'] = oecd['Country'].apply(
            lambda x: pc.map_country_alpha3_to_country_name()[x])
        oecd.set_index(['Country', 'Year'], inplace=True)
        oecd.sort_index(inplace=True)
        oecd = pd.DataFrame(oecd[data_name])

        self.data.append(oecd)
Beispiel #20
0
def country_to_continent():
    country_name = input("country name = ")
    country_alpha2 = pc.country_name_to_country_alpha2(country_name)
    country_continent_code = pc.country_alpha2_to_continent_code(
        country_alpha2)
    country_continent_name = pc.convert_continent_code_to_continent_name(
        country_continent_code)
    print(country_continent_name)
Beispiel #21
0
    def country_code_to_continent(self, country_code_iso2: str) -> str:
        """ Convert country code iso2 to country code iso2 continent """
        try:
            continent = pc.country_alpha2_to_continent_code(country_code_iso2)
        except:
            continent = 'Other'

        return continent
Beispiel #22
0
def country_to_continent(country_name):
    "Convert country name string to continent"
    country_alpha2 = pcc.country_name_to_country_alpha2(country_name)
    country_continent_code = pcc.country_alpha2_to_continent_code(
        country_alpha2)
    country_continent_name = pcc.convert_continent_code_to_continent_name(
        country_continent_code)
    return country_continent_name
Beispiel #23
0
def get_regions_by_markets(markets: list):
    regions: list = []
    try:
        regions = list(set([
            pc.country_alpha2_to_continent_code(country_code)
            for country_code in markets]))
    except Exception as e:
        logger.error(e)
    return regions
Beispiel #24
0
def get_continent(country_code):
  if country_code =='VA':
    return 'Europe'
  if country_code =='TL':
    return 'Asia'
  else:
    continent_name = pc.country_alpha2_to_continent_code(country_code)
    country_continent_name = pc.convert_continent_code_to_continent_name(continent_name)
    return country_continent_name
def load_master_data():
    """
    Continent details for each city from weatherbit metadata is pulled using pycountry_convert API and stored in
    city_country_mdata table.

    :return void:
    """
    db_name = "postgres"
    db_password = "******"
    db_user = "******"
    db_host = "localhost"

    root_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    filepath = os.path.join(root_dir, "data")

    data = pd.read_csv(os.path.join(filepath, "cities_all.csv"))
    data["city_country"] = data["city_name"].str.cat(data["country_full"],
                                                     sep=", ")
    data = data.dropna()

    try:
        conn = psycopg2.connect(dbname=db_name,
                                password=db_password,
                                user=db_user,
                                host=db_host)
        cur = conn.cursor()

        for item in data.itertuples():
            continent_flag = 0
            continent_name = ""

            if re.sub("[^a-zA-Z0-9, ]+", "", item[8]):
                try:
                    country_code = pc.country_name_to_country_alpha2(
                        item[5], cn_name_format="default")
                    continent_name = pc.country_alpha2_to_continent_code(
                        country_code)
                except:
                    continent_flag = 1

                if continent_flag == 0:
                    # query = """ INSERT INTO city_country_mdata (lat, lon, citycountry, continent) VALUES (%s,%s,%s,%s) """
                    record = (item[6], item[7],
                              re.sub("[^a-zA-Z0-9, ]+", "",
                                     item[8]), continent_name)
                    cur.execute(query, record)

        conn.commit()
        conn.close()

    except Exception as e:
        print("Exiting load_master_data due to exception: ", e.__class__)

    finally:
        if conn:
            cur.close()
            conn.close()
    def country_alpha2_to_continent(country_alpha2):
        """Map country name to continent"""

        try:
            continent_code = country_alpha2_to_continent_code(country_alpha2)
        except:
            continent_code = "N/A"

        return (continent_code)
def readLocation():
    memoizedDict = {}
    # index = 0
    for fileName in alignScores:
        tempdf = pd.read_csv(fileName)
        outfileName = "relativeDistAll" + fileName
        # initialize
        geoLocation = []
        relativeDistance = []
        ascensionNumL = []
        countryCodeL = []
        continentCodeL = []
        # get relative distance
        # for index in range(1000):
        for index in range(len(tempdf["Location"])):
            print(index)

            location = tempdf["Location"][index]
            ascensionNumL.append(tempdf["AscensionNum"][index])
            # check memo dictionary to see if we've already done this loc
            # note that if geolocation is none we just ignore it
            if not location in memoizedDict.keys():
                currentGeo = findGeocode(location)
                # save relative distance
                if currentGeo != None:
                    latitudeLongitude = (currentGeo.latitude,
                                         currentGeo.longitude)
                    geolocator = Nominatim(user_agent="your_app_name")
                    longlat = geolocator.reverse(
                        [currentGeo.latitude, currentGeo.longitude])
                    countryCode = longlat.raw['address']['country_code']
                    continentCode = country_alpha2_to_continent_code(
                        countryCode.upper())
                    if continentCode == "NA":
                        continentCode = "NorthA"
                    if type(currentGeo) != float:
                        memoizedDict[location] = (distance(
                            originPoint, latitudeLongitude), currentGeo,
                                                  countryCode, continentCode)
                else:
                    memoizedDict[location] = (0, currentGeo, countryCode,
                                              continentCode)
            relativeDistance.append(memoizedDict[location][0])
            geoLocation.append(memoizedDict[location][1])
            countryCodeL.append(memoizedDict[location][2])
            continentCodeL.append(memoizedDict[location][3])
        # Now add the column to dataframe
        currentOutputDF = pd.DataFrame({
            "geoLocation": geoLocation,
            "relativeDistance": relativeDistance,
            "AscensionNum": ascensionNumL,
            "countryCode": countryCodeL,
            "continentCode": continentCodeL
        })
        # And output the df to a new csv file.
        currentOutputDF.to_csv(outfileName)
Beispiel #28
0
def country_alpha2_to_continent_code(a2):
    if a2 == 'AQ': return 'AN'
    elif a2 == 'TF': return 'AF'
    elif a2 == 'EH': return 'AF'
    elif a2 == 'PN': return 'OC'
    elif a2 == 'SX': return 'NA'
    elif a2 == 'TL': return 'AS'
    elif a2 == 'UM': return 'NA'
    elif a2 == 'VA': return 'EU'
    else: return pc.country_alpha2_to_continent_code(a2)
                    def country_to_continent(country_name):
                        country_continent_name = ""
                        try:
                            country_alpha2 = pc.country_name_to_country_alpha2(country_name)
                            country_continent_code = pc.country_alpha2_to_continent_code(country_alpha2)
                            country_continent_name = pc.convert_continent_code_to_continent_name(country_continent_code)
                        except:
                            pass

                        return country_continent_name
Beispiel #30
0
def get_continent(col):
    try:
        cn_a2_code = country_name_to_country_alpha2(col)
    except:
        cn_a2_code = 'Unknown'
    try:
        cn_continent = country_alpha2_to_continent_code(cn_a2_code)
    except:
        cn_continent = 'Unknown'
    return (cn_a2_code, cn_continent)