diff --git a/esupy/ISO_3166.csv b/esupy/ISO_3166.csv new file mode 100644 index 0000000..a4c4165 --- /dev/null +++ b/esupy/ISO_3166.csv @@ -0,0 +1,250 @@ +English short name,Alpha-2 code,Alpha-3 code +Afghanistan,AF,AFG +Albania,AL,ALB +Algeria,DZ,DZA +American Samoa,AS,ASM +Andorra,AD,AND +Angola,AO,AGO +Anguilla,AI,AIA +Antarctica,AQ,ATA +Antigua and Barbuda,AG,ATG +Argentina,AR,ARG +Armenia,AM,ARM +Aruba,AW,ABW +Australia,AU,AUS +Austria,AT,AUT +Azerbaijan,AZ,AZE +Bahamas (the),BS,BHS +Bahrain,BH,BHR +Bangladesh,BD,BGD +Barbados,BB,BRB +Belarus,BY,BLR +Belgium,BE,BEL +Belize,BZ,BLZ +Benin,BJ,BEN +Bermuda,BM,BMU +Bhutan,BT,BTN +Bolivia (Plurinational State of),BO,BOL +"Bonaire, Sint Eustatius and Saba",BQ,BES +Bosnia and Herzegovina,BA,BIH +Botswana,BW,BWA +Bouvet Island,BV,BVT +Brazil,BR,BRA +British Indian Ocean Territory (the),IO,IOT +Brunei Darussalam,BN,BRN +Bulgaria,BG,BGR +Burkina Faso,BF,BFA +Burundi,BI,BDI +Cabo Verde,CV,CPV +Cambodia,KH,KHM +Cameroon,CM,CMR +Canada,CA,CAN +Cayman Islands (the),KY,CYM +Central African Republic (the),CF,CAF +Chad,TD,TCD +Chile,CL,CHL +China,CN,CHN +Christmas Island,CX,CXR +Cocos (Keeling) Islands (the),CC,CCK +Colombia,CO,COL +Comoros (the),KM,COM +Congo (the Democratic Republic of the),CD,COD +Congo (the),CG,COG +Cook Islands (the),CK,COK +Costa Rica,CR,CRI +Croatia,HR,HRV +Cuba,CU,CUB +Curaçao,CW,CUW +Cyprus,CY,CYP +Czechia,CZ,CZE +Côte d'Ivoire,CI,CIV +Denmark,DK,DNK +Djibouti,DJ,DJI +Dominica,DM,DMA +Dominican Republic (the),DO,DOM +Ecuador,EC,ECU +Egypt,EG,EGY +El Salvador,SV,SLV +Equatorial Guinea,GQ,GNQ +Eritrea,ER,ERI +Estonia,EE,EST +Eswatini,SZ,SWZ +Ethiopia,ET,ETH +Falkland Islands (the) [Malvinas],FK,FLK +Faroe Islands (the),FO,FRO +Fiji,FJ,FJI +Finland,FI,FIN +France,FR,FRA +French Guiana,GF,GUF +French Polynesia,PF,PYF +French Southern Territories (the),TF,ATF +Gabon,GA,GAB +Gambia (the),GM,GMB +Georgia,GE,GEO +Germany,DE,DEU +Ghana,GH,GHA +Gibraltar,GI,GIB +Greece,GR,GRC +Greenland,GL,GRL +Grenada,GD,GRD +Guadeloupe,GP,GLP +Guam,GU,GUM +Guatemala,GT,GTM +Guernsey,GG,GGY +Guinea,GN,GIN +Guinea-Bissau,GW,GNB +Guyana,GY,GUY +Haiti,HT,HTI +Heard Island and McDonald Islands,HM,HMD +Holy See (the),VA,VAT +Honduras,HN,HND +Hong Kong,HK,HKG +Hungary,HU,HUN +Iceland,IS,ISL +India,IN,IND +Indonesia,ID,IDN +Iran (Islamic Republic of),IR,IRN +Iraq,IQ,IRQ +Ireland,IE,IRL +Isle of Man,IM,IMN +Israel,IL,ISR +Italy,IT,ITA +Jamaica,JM,JAM +Japan,JP,JPN +Jersey,JE,JEY +Jordan,JO,JOR +Kazakhstan,KZ,KAZ +Kenya,KE,KEN +Kiribati,KI,KIR +Korea (the Democratic People's Republic of),KP,PRK +Korea (the Republic of),KR,KOR +Kuwait,KW,KWT +Kyrgyzstan,KG,KGZ +Lao People's Democratic Republic (the),LA,LAO +Latvia,LV,LVA +Lebanon,LB,LBN +Lesotho,LS,LSO +Liberia,LR,LBR +Libya,LY,LBY +Liechtenstein,LI,LIE +Lithuania,LT,LTU +Luxembourg,LU,LUX +Macao,MO,MAC +Madagascar,MG,MDG +Malawi,MW,MWI +Malaysia,MY,MYS +Maldives,MV,MDV +Mali,ML,MLI +Malta,MT,MLT +Marshall Islands (the),MH,MHL +Martinique,MQ,MTQ +Mauritania,MR,MRT +Mauritius,MU,MUS +Mayotte,YT,MYT +Mexico,MX,MEX +Micronesia (Federated States of),FM,FSM +Moldova (the Republic of),MD,MDA +Monaco,MC,MCO +Mongolia,MN,MNG +Montenegro,ME,MNE +Montserrat,MS,MSR +Morocco,MA,MAR +Mozambique,MZ,MOZ +Myanmar,MM,MMR +Namibia,NA,NAM +Nauru,NR,NRU +Nepal,NP,NPL +Netherlands (Kingdom of the),NL,NLD +New Caledonia,NC,NCL +New Zealand,NZ,NZL +Nicaragua,NI,NIC +Niger (the),NE,NER +Nigeria,NG,NGA +Niue,NU,NIU +Norfolk Island,NF,NFK +North Macedonia,MK,MKD +Northern Mariana Islands (the),MP,MNP +Norway,NO,NOR +Oman,OM,OMN +Pakistan,PK,PAK +Palau,PW,PLW +"Palestine, State of",PS,PSE +Panama,PA,PAN +Papua New Guinea,PG,PNG +Paraguay,PY,PRY +Peru,PE,PER +Philippines (the),PH,PHL +Pitcairn,PN,PCN +Poland,PL,POL +Portugal,PT,PRT +Puerto Rico,PR,PRI +Qatar,QA,QAT +Romania,RO,ROU +Russian Federation (the),RU,RUS +Rwanda,RW,RWA +Réunion,RE,REU +Saint Barthélemy,BL,BLM +"Saint Helena, Ascension and Tristan da Cunha",SH,SHN +Saint Kitts and Nevis,KN,KNA +Saint Lucia,LC,LCA +Saint Martin (French part),MF,MAF +Saint Pierre and Miquelon,PM,SPM +Saint Vincent and the Grenadines,VC,VCT +Samoa,WS,WSM +San Marino,SM,SMR +Sao Tome and Principe,ST,STP +Saudi Arabia,SA,SAU +Senegal,SN,SEN +Serbia,RS,SRB +Seychelles,SC,SYC +Sierra Leone,SL,SLE +Singapore,SG,SGP +Sint Maarten (Dutch part),SX,SXM +Slovakia,SK,SVK +Slovenia,SI,SVN +Solomon Islands,SB,SLB +Somalia,SO,SOM +South Africa,ZA,ZAF +South Georgia and the South Sandwich Islands,GS,SGS +South Sudan,SS,SSD +Spain,ES,ESP +Sri Lanka,LK,LKA +Sudan (the),SD,SDN +Suriname,SR,SUR +Svalbard and Jan Mayen,SJ,SJM +Sweden,SE,SWE +Switzerland,CH,CHE +Syrian Arab Republic (the),SY,SYR +Taiwan (Province of China),TW,TWN +Tajikistan,TJ,TJK +"Tanzania, the United Republic of",TZ,TZA +Thailand,TH,THA +Timor-Leste,TL,TLS +Togo,TG,TGO +Tokelau,TK,TKL +Tonga,TO,TON +Trinidad and Tobago,TT,TTO +Tunisia,TN,TUN +Turkmenistan,TM,TKM +Turks and Caicos Islands (the),TC,TCA +Tuvalu,TV,TUV +Türkiye,TR,TUR +Uganda,UG,UGA +Ukraine,UA,UKR +United Arab Emirates (the),AE,ARE +United Kingdom of Great Britain and Northern Ireland (the),GB,GBR +United States Minor Outlying Islands (the),UM,UMI +United States of America (the),US,USA +Uruguay,UY,URY +Uzbekistan,UZ,UZB +Vanuatu,VU,VUT +Venezuela (Bolivarian Republic of),VE,VEN +Viet Nam,VN,VNM +Virgin Islands (British),VG,VGB +Virgin Islands (U.S.),VI,VIR +Wallis and Futuna,WF,WLF +Western Sahara*,EH,ESH +Yemen,YE,YEM +Zambia,ZM,ZMB +Zimbabwe,ZW,ZWE +Åland Islands,AX,ALA diff --git a/esupy/location.py b/esupy/location.py new file mode 100644 index 0000000..b3d2257 --- /dev/null +++ b/esupy/location.py @@ -0,0 +1,70 @@ +# location.py (esupy) +# !/usr/bin/env python3 +# coding=utf-8 +""" +Functions to facilitate creation of location objects +""" + +import bz2 +import json +import pandas as pd +from pathlib import Path + +from esupy.remote import make_url_request + +location_meta = ('https://raw.githubusercontent.com/GreenDelta/data/' + 'master/refdata/locations.csv') +path = Path(__file__).parent + +# %% get GeoJSON +url = 'https://geography.ecoinvent.org/files' +# https://geography.ecoinvent.org/#id12 + +object_dict = {'states': 'states.geojson.bz2', + 'countries': 'countries.geojson.bz2'} + +def extract_coordinates(group) -> dict: + file = object_dict.get(group) + if file is None: + print('error') + return + response = make_url_request(f'{url}/{file}') + content = bz2.decompress(response.content) + data = json.loads(content) + + # %% extract GeoJSON objects from the FeatureCollection + features = data['features'] + + ## need to also grab the UUID? + if group == 'states': + d = {f['properties']['shortname']: f['geometry'] + for f in features + if f['properties']['shortname'].startswith('US')} + elif group == "countries": + d = {f['properties']['shortname']: f['geometry'] for f in features} + return d + + +def assign_state_names(df): + import flowsa + f = flowsa.location.get_state_FIPS(abbrev=True).drop(columns='County') + f['State'] = f['State'].apply(lambda x: f"US-{x}") + fd = f.set_index('FIPS').to_dict()['State'] + fd['00000'] = 'US' + df['Location'] = df['Location'].replace(fd) + return df.dropna(subset='Location') + + +def read_iso_3166(): + # accessed from the ISO online browing platform + # https://www.iso.org/obp/ui/#search + df = pd.read_csv(path / 'ISO_3166.csv') + df = df.rename(columns={'English short name': 'Name', + 'Alpha-2 code': 'ISO-2d', + 'Alpha-3 code': 'ISO-3d'}) + return df + + +if __name__ == "__main__": + # d = extract_coordinates(group='states') + d = extract_coordinates(group='countries')