import urllib.request
import webbrowser
import json
import os

shortState = "WV"
district = "3"
year = "2016"

stateDict =  { 
    "AK" : "Alaska",
    "AL" : "Alabama",
    "AR" : "Arkansas",
    "AZ" : "Arizona",
    "CA" : "California",
    "CO" : "Colorado",
    "CT" : "Connecticut",
    "DE" : "Delaware",
    "FL" : "Florida",
    "GA" : "Georgia",
    "HI" : "Hawaii",
    "IA" : "Iowa",
    "ID" : "Idaho",
    "IL" : "Illinois",
    "IN" : "Indiana",
    "KS" : "Kansas",
    "KY" : "Kentucky",
    "LA" : "Louisiana",
    "MA" : "Massachusetts",
    "MD" : "Maryland",
    "ME" : "Maine",
    "MI" : "Michigan",
    "MN" : "Minnesota",
    "MO" : "Missouri",
    "MS" : "Mississippi",
    "MT" : "Montana",
    "NC" : "North Carolina",
    "ND" : "North Dakota",
    "NE" : "Nebraska",
    "NH" : "New Hampshire",
    "NJ" : "New Jersey",
    "NM" : "New Mexico",
    "NV" : "Nevada",
    "NY" : "New York",
    "OH" : "Ohio",
    "OK" : "Oklahoma",
    "OR" : "Oregon",
    "PA" : "Pennsylvania",
    "RI" : "Rhode Island",
    "SC" : "South Carolina",
    "SD" : "South Dakota",
    "TN" : "Tennessee",
    "TX" : "Texas",
    "UT" : "Utah",
    "VA" : "Virginia",
    "VT" : "Vermont",
    "WA" : "Washington",
    "WI" : "Wisconsin",
    "WV" : "West Virginia",
    "WY" : "Wyoming" }
    
state = stateDict[shortState]
    
def getOrdinal(number):
    if number in ["1", "21", "31", "41", "51"]:
        ordinal = "st"
    elif number in ["2", "22", "32", "42", "52"]:
        ordinal = "nd"
    elif number in ["3", "23", "33", "43", "53"]:
        ordinal = "rd"
    else:
        ordinal = "th"
    return ordinal
        

def getCentroid(mapText):
    data = json.loads(mapText)
    minlat = 90
    minlon = 180
    maxlat = -90
    maxlon = -180
    if data["geometry"]["type"] == "MultiPolygon":
        coordsList = data["geometry"]['coordinates'][0][0]
    else:
        coordsList = data["geometry"]['coordinates'][0]
        
    for coordinates in coordsList:
            lat = coordinates[1]
            lon = coordinates[0]
            if lat < minlat:
                minlat = lat
            if lat > maxlat:
                maxlat = lat
            if lon < minlon:
                minlon = lon
            if lon > maxlon:
                maxlon = lon
            
    centrelat = (minlat + maxlat)/2            
    centrelon = (minlon + maxlon)/2
    area = (maxlat-minlat)*(maxlon-minlon)
    zoom = 11
    if area > 0.25:
        zoom = 10
    if area > 1:
        zoom = 9
    if area > 2.5:
        zoom = 8
    if area > 5:
        zoom = 7
    return str(centrelat), str(centrelon), zoom

mapText = ""

for x in range(int(district)):
    
    file = "https://theunitedstates.io/districts/cds/%s/%s-%s/shape.geojson" % (year, shortState, str(x+1))
    print(file)
    ordinal = getOrdinal(str(x+1))
    response = urllib.request.urlopen(file)
    response = response.read()
    geojson = response.decode('utf-8')    
    centrelat, centrelon, zoom = getCentroid(geojson)

    header = '''{
    "license": "CC0-1.0",
    "description": {
        "en": "%s's %s%s Congressional District (%s)"
    },
    "sources": "https://github.com/unitedstates/districts, https://theunitedstates.io/districts/cds/%s/%s-%s/shape.geojson",
    "zoom": %s,
    "latitude": %s,
    "longitude": %s,
    "data": {
        "type":"FeatureCollection",
        "features":[
    ''' % (state, str(x+1), ordinal, year, year, shortState, str(x+1), zoom, centrelat, centrelon)

    footer = '''        ]
    }
}'''

    mapText += "%s        %s%s" % (header, str(geojson), footer)
    url = "https://commons.wikimedia.org/w/index.php?title=Data:%s's %s%s Congressional District (%s).map&action=edit" % (state, str(x+1), ordinal, year)
    url = url.replace(" ", "%20")
    mapText += "\n\n" + url + "\n\n"
    
f = open("output.txt", "w")
f.write(mapText)

print("Done!")
https://theunitedstates.io/districts/cds/2016/WV-1/shape.geojson
https://theunitedstates.io/districts/cds/2016/WV-2/shape.geojson
https://theunitedstates.io/districts/cds/2016/WV-3/shape.geojson
Done!