Example #1
0
def find_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    # Case 1: State is only a single polygon
    if len(polygons) == 1:
        return make_position(latitude(find_centroid(polygons[0])), longitude(find_centroid(polygons[0])))
    # Case 2: States are multiple polygons
    x_weight = 0
    y_weight = 0
    area = 0
    for poly in polygons: # Using the formula from Wikipedia, we calculate the sum of the X and Y coordinates per polygon weighting it by the area.
        x_weight += latitude(find_centroid(poly)) * find_centroid(poly)[2]
        y_weight += longitude(find_centroid(poly)) * find_centroid(poly)[2]
        area += find_centroid(poly)[2]
    return make_position(x_weight/area, y_weight/area)
Example #2
0
def retrieve_tweets(path):
    """
    Reads in and processes all JSON data in folder given by file path.
    Filters out Tweets without location data and converts remaining Tweets
    into the preferred format.
    """
    all_data = []
    for filename in os.listdir(path):
        with open(path + filename) as read_file:
            data = json.load(read_file)
            all_data = all_data + data
    tweets = []
    #map the ones with location data to Tweets
    for i in range(0, len(all_data)):
        tweet = all_data[i]
        #if time would like to accomodate tweet['place']['place_type'] == 'admin', but that means making a dictionary of full state name to state abbreviation
        if (tweet['place'] != None and tweet['place']['country_code'] == 'US'
                and tweet['place']['place_type'] == 'city'):
            user = tweet['user']['screen_name']
            non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1),
                                        0xfffd)
            text = (tweet['text']).translate(non_bmp_map)  #to deal with emojis
            state = tweet['place']['full_name'][-2:]
            polygon = [
            ]  #want to construct a polygon that is a list of positions, with first = last
            for coord in tweet['place']['bounding_box']['coordinates'][0]:
                polygon.append(make_position(coord[1], coord[0]))
            first_coord = tweet['place']['bounding_box']['coordinates'][0][0]
            polygon.append(make_position(first_coord[1], first_coord[0]))
            position = find_centroid(
                polygon)  #find the centroid of the bounding box
            tweets.append(make_tweet(user, text, state, position))
            #display_tweet(make_tweet(user, text, state, position))
    print(len(all_data))
    return tweets
Example #3
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """

    distancias = []
    for x in state_centers:
        dist = geo_distance(
            make_position(tweet["latitude"], tweet["longitude"]),
            make_position(state_centers[x][0], state_centers[x][1]))
        distancias.append(dist)
    for x in state_centers:
        if geo_distance(
                make_position(tweet["latitude"], tweet["longitude"]),
                make_position(state_centers[x][0],
                              state_centers[x][1])) == min(distancias):
            estado = x

    return estado
Example #4
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_state_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("welcome to san Francisco", None, 38, -122)
    >>> ny = make_tweet("welcome to new York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    if "AA" in us_states:
        del us_states["AA"]
    inverse_state_centers = {state: coord for coord, state in state_centers.items()}
    location = tweet_location(tweet)
    closest = make_position(200, 200)
    closest_state = "AK"
    for state in state_centers:
        center = make_position(state_centers[state][0], state_centers[state][1])
        if abs(geo_distance(center, location)) < abs(geo_distance(closest, location)):
            closest = center
            closest_state = inverse_state_centers[center]
    return closest_state
Example #5
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    cent_x, cent_y, area = 0, 0, 0
    i = 0
    while i < len(polygons):
        cent_x = cent_x + find_centroid(polygons[i])[0] * find_centroid(polygons[i])[2]
        cent_y = cent_y + find_centroid(polygons[i])[1] * find_centroid(polygons[i])[2]
        area = area + find_centroid(polygons[i])[2]
        i = i + 1
    if area == 0:
        return make_position(cent_x, cent_y)
    return make_position(cent_x / area, cent_y / area)
Example #6
0
def find_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    n = len(polygons)  #Number of islands
    if n == 1:
        centroid = find_centroid(polygons[0])
        return make_position(get_cx(centroid), get_cy(centroid))
    cx, cy, area = [], [], []
    for p in polygons:
        centroid = find_centroid(p)
        cx.append(get_cx(centroid))
        cy.append(get_cy(centroid))
        area.append(get_area(centroid))

    def numer_term(coor):
        def numer(i):
            return coor[i] * area[i]

        return numer

    def denom_term(coor):
        def denom(i):
            return area[i]

        return denom

    x = summation(n, numer_term(cx), successor) / summation(
        n, denom_term(cx), successor)
    y = summation(n, numer_term(cy), successor) / summation(
        n, denom_term(cy), successor)
    return make_position(x, y)
Example #7
0
def find_center(shapes):
    """Compute the geographic center of a state, averaged over its shapes.

    The center is the average position of centroids of the polygons in shapes,
    weighted by the area of those polygons.
    
    Arguments:
    shapes -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    x_numerator, y_numerator, denominator = 0, 0, 0
    for polygon in shapes:
        x, y, area = find_centroid(polygon)
        if area != 0:
            x_numerator += x * area
            y_numerator += y * area
            denominator += area
    return make_position(x_numerator / denominator, y_numerator / denominator)
Example #8
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    #The initial values of the area of all polygons and the average position of centroid of polygons
    Area_overall = 0
    X_overall = 0
    Y_overall = 0

    for i in range(0, len(polygons)):
        Area_overall = Area_overall + find_centroid(polygons[i])[2]  #The formala for calculating the are of all polygons combined
        X_overall = X_overall + find_centroid(polygons[i])[0] * find_centroid(polygons[i])[2]  #The X-axis of average position of centroid of polygons
        Y_overall = Y_overall + find_centroid(polygons[i])[1] * find_centroid(polygons[i])[2]  #The Y-axis of average position of centroid of polygons
    X_overall, Y_overall = X_overall / Area_overall, Y_overall/Area_overall
    return make_position(X_overall, Y_overall)  #The final values of the X and Y coordinates.
Example #9
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in
    polygons, weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    weighted_cx = 0
    weighted_cy = 0
    sum_areas = 0
    i = 0
    while i < len(polygons):
        weighted_cx += find_centroid(polygons[i])[0]*find_centroid(polygons[i])[2]
        weighted_cy += find_centroid(polygons[i])[1]*find_centroid(polygons[i])[2]
        sum_areas += find_centroid(polygons[i])[2]
        i += 1
    return make_position(weighted_cx/sum_areas, weighted_cy/sum_areas)
Example #10
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    weighted_lat = 0
    weighted_lon = 0
    total_area = 0
    for polygon in polygons:
        centroid = find_centroid(polygon)
        weighted_lat += centroid[0]*centroid[2]
        weighted_lon += centroid[1]*centroid[2]
        total_area += find_centroid(polygon)[2]
    return make_position(weighted_lat/total_area, weighted_lon/total_area)
Example #11
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"

    sum_area = 0
    sum_x = 0
    sum_y = 0

    for polygon in polygons:
        (lat, lon, area) = find_centroid(polygon)
        sum_x += lat * area
        sum_y += lon * area
        sum_area += area

    return make_position(sum_x / sum_area, sum_y / sum_area)
Example #12
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    #implements area of polygon formula with 2 steps: summation and then division
    center_x, center_y, area_total = 0, 0, 0 #initialize
    #summation portion for component polygons
    for polygon in polygons:
      (c_x, c_y, area) = find_centroid(polygon)
      center_x += c_x*area
      center_y += c_y*area
      area_total += area
    #division portion
    center_x /= area_total
    center_y /= area_total
    return make_position(center_x, center_y)
Example #13
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    count = 0
    numeratorx = 0
    numeratory = 0
    denominator = 0
    while count < len(polygons):
        numeratorx = numeratorx + (find_centroid(polygons[count])[0] * find_centroid(polygons[count])[2])
        numeratory = numeratory + (find_centroid(polygons[count])[1] * find_centroid(polygons[count])[2])
        denominator = denominator + find_centroid(polygons[count])[2]
        count += 1
    numeratorx = numeratorx / denominator
    numeratory = numeratory / denominator
    return make_position(numeratorx, numeratory)
Example #14
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    origin = tweet_location(tweet)
    dist = make_position(1000000000000000, 100000000000000000)
    for x in state_centers.values():
        if geo_distance(origin, dist) > geo_distance(origin, x):
            dist = x
    dict_keys = state_centers.keys()
    for key in dict_keys:
        if state_centers[key] == dist:
            return key
Example #15
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"

    x1, y1, ar = 0, 0, 0
    for i in range(len(polygons)):
        la, lo, a = find_centroid(polygons[i])
        x1 += (la * a)
        y1 += (lo * a)
        ar += a
    return make_position((x1 / ar), (y1 / ar))
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    cenArea = []
    for index in range(len(polygons)):
        cenArea.append(find_centroid(polygons[index]))
    cenx = 0
    ceny = 0
    totalArea = 0
    for i in range (0, len(cenArea)):
        cenx += (cenArea[i])[0] * (cenArea[i])[2]
        ceny += (cenArea[i])[1] * (cenArea[i])[2]
        totalArea += (cenArea[i])[2]
    return make_position(cenx/totalArea, ceny/totalArea)
Example #17
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    total_area, avg_x, avg_y = 0, 0, 0
    for k in polygons:
        avg_x += find_centroid(k)[0] * find_centroid(k)[2]
        avg_y += find_centroid(k)[1] * find_centroid(k)[2]
        total_area += find_centroid(k)[2]
    avg_x = avg_x / total_area
    avg_y = avg_y / total_area
    return make_position(avg_x, avg_y)
Example #18
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    i, sum_of_centroid_x, sum_of_centroid_y, sum_area = 0, 0, 0, 0
    while i < len(polygons):
        sum_of_centroid_x = sum_of_centroid_x + (find_centroid(
            polygons[i])[0]) * (find_centroid(polygons[i])[2])
        sum_of_centroid_y = sum_of_centroid_y + (find_centroid(
            polygons[i])[1]) * (find_centroid(polygons[i])[2])
        sum_area = sum_area + find_centroid(polygons[i])[2]
        i += 1
    return make_position(sum_of_centroid_x / sum_area,
                         sum_of_centroid_y / sum_area)
Example #19
0
def find_state_center(polygons):
	"""Compute the geographic center of a state, averaged over its polygons.

	The center is the average position of centroids of the polygons in polygons,
	weighted by the area of those polygons.

	Arguments:
	polygons -- a list of polygons

	>>> ca = find_state_center(us_states['CA'])  # California
	>>> round(latitude(ca), 5)
	37.25389
	>>> round(longitude(ca), 5)
	-119.61439

	>>> hi = find_state_center(us_states['HI'])  # Hawaii
	>>> round(latitude(hi), 5)
	20.1489
	>>> round(longitude(hi), 5)
	-156.21763
	"""
	cent_lat_num, cent_lon_num, sum_areas = 0, 0, 0
	for i in range(len(polygons)):
		lat, lon, area = find_centroid(polygons[i])
		cent_lat_num += (lat * area)
		cent_lon_num += (lon * area)
		sum_areas += area
	return make_position(cent_lat_num / sum_areas, cent_lon_num / sum_areas)
Example #20
0
def find_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    total_lat = 0.0
    total_lon = 0.0
    count = 0
    for i in range(len(polygons)):
        for j in range(len(polygons[i])):
            #print(polygons[i][j])
            total_lat += polygons[i][j][0]
            total_lon += polygons[i][j][1]
            count += 1

    avg_lat = total_lat / count
    avg_lon = total_lon / count
    return make_position(round(avg_lat, 5), round(avg_lon, 5))
Example #21
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    centers = [find_centroid(p) for p in polygons]

    weighted_lat = weighted_lon = total_areas = 0
    for lat, lon, area in centers:
        weighted_lat += lat * area
        weighted_lon += lon * area
        total_areas += area

    return make_position(weighted_lat / total_areas,
                         weighted_lon / total_areas)
Example #22
0
def find_closest_state(tweet, state_centers):  #problema8
    """Return the name of the state closest to the given tweet's location.

    Use the geo_distance function (already provided) to calculate distance
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions.

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    tweet_location = make_position(tweet['latitude'], tweet['longitude'])
    estados = []
    for key in us_states:
        estados.append(
            (key, geo_distance(tweet_location, find_center(us_states[key]))))
    for x in range(0, len(estados) - 1):
        if estados[x][1] < estados[x + 1][1]:
            estados[x], estados[x + 1] = estados[x + 1], estados[x]
    return estados[len(estados) - 1][0]
def find_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    centroid_e_area = []
    for i in range(len(polygons)):
        centroid_e_area.append(find_centroid(polygons[i]))
    J = 0
    K = 0
    X = 0
    for i in range(len(centroid_e_area)):
        J += ((centroid_e_area[i])[0]) * ((centroid_e_area[i])[2])
        K += (centroid_e_area[i])[2]
        X += ((centroid_e_area[i])[1]) * ((centroid_e_area[i])[2])

    return make_position(J / K, X / K)
Example #24
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    origin=tweet_location(tweet)
    dist=make_position(1000000000000000,100000000000000000)
    for x in state_centers.values():
        if geo_distance(origin,dist)>geo_distance(origin,x):
            dist=x
    dict_keys=state_centers.keys()
    for key in dict_keys:
        if state_centers[key]==dist:
            return key
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(get_value_from_key(us_states,'CA'))  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(get_value_from_key(us_states, 'HI'))  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    tot_cx, tot_cy, i, tot_area = 0, 0, 0, 0
    while i < len(polygons):
        cx, cy, area = find_centroid(polygons[i])
        tot_cx += cx * area
        tot_cy += cy * area
        tot_area += area
        i += 1
    return make_position(tot_cx / tot_area, tot_cy / tot_area)
Example #26
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in
    polygons, weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    state_central_lat, state_central_lon, state_area = 0, 0, 0
    for polygon in polygons:  #using geometric decompostion through Wikipedia
        state_centroid = find_centroid(polygon)
        state_central_lat += state_centroid[0] * state_centroid[2]
        state_central_lon += state_centroid[1] * state_centroid[2]
        state_area += state_centroid[2]
    state_central_lat = state_central_lat / state_area
    state_central_lon = state_central_lon / state_area
    return make_position(state_central_lat, state_central_lon)
Example #27
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    total_lat, total_lon, total_area = 0, 0, 0
    for polygon in polygons:
        poly_lat, poly_lon, poly_area = find_centroid(polygon)
        total_lat += poly_lat * poly_area
        total_lon += poly_lon * poly_area
        total_area += poly_area
    return make_position(total_lat / total_area, total_lon / total_area)
Example #28
0
def find_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """

    centroid_x, centroid_y, centroid_area, index = 0, 0, 0, 0
    while index < len(polygons):
        x, y, area = find_centroid(polygons[index])
        centroid_x += x * area
        centroid_y += y * area
        centroid_area += area
        index += 1
    centroid_x = centroid_x / centroid_area
    centroid_y = centroid_y / centroid_area
    return make_position(centroid_x, centroid_y)
Example #29
0
def find_center(shapes):
    """Compute the geographic center of a state, averaged over its shapes.

	The center is the average position of centroids of the polygons in shapes,
	weighted by the area of those polygons.
	
	Arguments:
	shapes -- a list of polygons

	>>> ca = find_center(us_states['CA'])  # California
	>>> round(latitude(ca), 5)
	37.25389
	>>> round(longitude(ca), 5)
	-119.61439

	>>> hi = find_center(us_states['HI'])  # Hawaii
	>>> round(latitude(hi), 5)
	20.1489
	>>> round(longitude(hi), 5)
	-156.21763
	"""
    state_latitude, state_longitude, sum_area = 0, 0, 0

    for s in shapes:
        if find_centroid(s)[2] != 0:
            state_latitude += (find_centroid(s)[0] * find_centroid(s)[2])
            state_longitude += (find_centroid(s)[1] * find_centroid(s)[2])
            sum_area += find_centroid(s)[2]
    if sum_area != 0:
        state_latitude = state_latitude / sum_area
        state_longitude = state_longitude / sum_area
    return make_position(state_latitude, state_longitude)
Example #30
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    def total_area(polygons):
        sum = 0
        for pos in polygons:
            sum += find_centroid(pos)[2]
        return sum

    centroid_x, centroid_y = 0, 0
    area = total_area(polygons)
    for pos in polygons:
        centroid_x += find_centroid(pos)[0] * find_centroid(pos)[2]
        centroid_y += find_centroid(pos)[1] * find_centroid(pos)[2]
    return make_position(centroid_x / area, centroid_y / area)
Example #31
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(get_value_from_key(us_states,'CA'))  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(get_value_from_key(us_states, 'HI'))  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    cx_num = 0
    cy_num = 0
    cx_cy_denom = 0
    for i in range(len(polygons)):
        cx_num += list(find_centroid(polygons[i]))[0] * list(find_centroid(polygons[i]))[2]
        cy_num += list(find_centroid(polygons[i]))[1] * list(find_centroid(polygons[i]))[2]
        cx_cy_denom += list(find_centroid(polygons[i]))[2]
    cx = cx_num/cx_cy_denom
    cy = cy_num/cx_cy_denom
    return make_position(cx, cy)
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    x_total=0
    y_total=0
    total_area=0
    for n in range(len(polygons)):
        x,y,area=find_centroid(polygons[n])
        x_total+=x*area
        y_total+=y*area
        total_area+=area
    return make_position(x_total/total_area, y_total/total_area)
Example #33
0
def find_center(shapes):
    """Compute the geographic center of a state, averaged over its shapes.

    The center is the average position of centroids of the polygons in shapes,
    weighted by the area of those polygons.
    
    Arguments:
    shapes -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    centroid_x_list = 0
    centroid_y_list = 0
    area = 0
    for polygon in shapes:
        current_area = find_centroid(polygon)[2]
        centroid_x_list += (find_centroid(polygon)[0]*current_area)
        centroid_y_list += (find_centroid(polygon)[1]*current_area)
        area += current_area
    avg_x_centroid = ((centroid_x_list/(area)))
    avg_y_centroid = ((centroid_y_list/(area)))
    return make_position(avg_x_centroid, avg_y_centroid)
Example #34
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in
    polygons, weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    Denom_Area = 0
    Numerator_Cx=0
    Numerator_Cy=0
    for polygon in polygons:
        polygon = find_centroid(polygon)
        Cx= polygon[0]
        Cy= polygon[1]
        Area= polygon[2]
        Denom_Area += polygon[2]
        Numerator_Cx+=Cx*Area
        Numerator_Cy+=Cy*Area
    return make_position(Numerator_Cx/Denom_Area, Numerator_Cy/Denom_Area)
Example #35
0
File: trends.py Project: drixta/61a
def find_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    k = 0
    numx, numy, denom= 0,0,0
    centroid = find_centroid(polygons[k])
    while k < len(polygons):
        numx += centroid[0]*centroid[2]
        denom += centroid[2]
        numy += centroid[1]*centroid[2]
        k += 1
    return make_position(numx/denom,numy/denom)
Example #36
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    x, y = 0, 0
    area = 0.0
    for shape in polygons:
        centroid = find_centroid(shape)  
        area_i = centroid[2]
        c_ix = centroid[0]
        c_iy = centroid[1]
        x = x + (c_ix *area_i)
        y = y + (c_iy *area_i) # keep updating area
        area = area + area_i
    return make_position(x / area, y / area)
Example #37
0
def find_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    total_area = 0
    total_lat = 0
    total_long = 0
    for shape in polygons:
        spec_centroid = find_centroid(shape)
        area = spec_centroid[2]
        total_area += spec_centroid[2]
        total_lat += spec_centroid[0]*area
        total_long += spec_centroid[1]*area 
    center_x = total_lat/total_area 
    center_y = total_long/total_area 
    return make_position(center_x, center_y)
Example #38
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in
    polygons, weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    sum_numerator_x, sum_numerator_y, sum_denominator = 0, 0, 0
    for p in polygons:
        t = find_centroid(p)
        sum_numerator_x += t[0] * t[2]
        sum_numerator_y += t[1] * t[2]
        sum_denominator += t[2]

    x = sum_numerator_x / sum_denominator
    y = sum_numerator_y / sum_denominator

    return make_position(x, y)
Example #39
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to state shapes

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    tweet_pos = make_position(tweet['latitude'], tweet['longitude'])
    minimum = 9999999
    flag = ''
    for item in state_centers:
        if minimum > geo_distance(tweet_pos, state_centers[item]):
            minimum = geo_distance(tweet_pos, state_centers[item])
            flag = item
    return flag
Example #40
0
def tweet_location(tweet):
    """Return a position representing a tweet's location."""
    "*** YOUR CODE HERE ***"
    lat = tweet['latitude']
    lon = tweet['longitude']
    pos = make_position(lat, lon)
    return pos
Example #41
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    lat = 0
    lon = 0
    area = 0
    for polygon in polygons:
        centroid = find_centroid(polygon)
        lat += centroid[0] * centroid[2]
        lon += centroid[1] * centroid[2]
        area += centroid[2]
    lat = lat / area
    lon = lon / area
    return make_position(lat, lon)
Example #42
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    total_area, avg_x, avg_y = 0, 0, 0
    for k in polygons:
        avg_x += find_centroid(k)[0] * find_centroid(k)[2]
        avg_y += find_centroid(k)[1] * find_centroid(k)[2]
        total_area += find_centroid(k)[2]
    avg_x = avg_x / total_area
    avg_y = avg_y / total_area
    return make_position(avg_x, avg_y)
Example #43
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(get_value_from_key(us_states,'CA'))  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(get_value_from_key(us_states, 'HI'))  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    cx_num = 0
    cy_num = 0
    cx_cy_denom = 0
    for i in range(len(polygons)):
        cx_num += list(find_centroid(polygons[i]))[0] * list(
            find_centroid(polygons[i]))[2]
        cy_num += list(find_centroid(polygons[i]))[1] * list(
            find_centroid(polygons[i]))[2]
        cx_cy_denom += list(find_centroid(polygons[i]))[2]
    cx = cx_num / cx_cy_denom
    cy = cy_num / cx_cy_denom
    return make_position(cx, cy)
Example #44
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    denom, nume_x, nume_y = 0.0, 0.0, 0.0
    for polygon in polygons:
        # print(find_centroid(polygon))
        denom += find_centroid(polygon)[2]
        nume_x += find_centroid(polygon)[0]*find_centroid(polygon)[2]
        nume_y += find_centroid(polygon)[1]*find_centroid(polygon)[2]
    return make_position(nume_x/denom, nume_y/denom)
Example #45
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in
    polygons, weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    weighted_cx = 0
    weighted_cy = 0
    sum_areas = 0
    i = 0
    while i < len(polygons):
        weighted_cx += find_centroid(polygons[i])[0] * find_centroid(
            polygons[i])[2]
        weighted_cy += find_centroid(polygons[i])[1] * find_centroid(
            polygons[i])[2]
        sum_areas += find_centroid(polygons[i])[2]
        i += 1
    return make_position(weighted_cx / sum_areas, weighted_cy / sum_areas)
Example #46
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in
    polygons, weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    totalx, totaly, total = 0, 0, 0
    for p in polygons:
        cx = find_centroid(p)[0]
        cy = find_centroid(p)[1]
        a = find_centroid(p)[2]
        totalx += cx * a
        totaly += cy * a
        total += a
    x = totalx / total
    y = totaly / total
    return make_position(x, y)
Example #47
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to positions

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    
    distance = 100000000000000
    new_distance = 0
    closest_state = None
    
    for state in state_centers:
        new_distance = geo_distance(make_position(tweet['latitude'], tweet['longitude']), state_centers[state])

        if new_distance < distance:
            distance = new_distance
            closest_state = state
            
    return closest_state
Example #48
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    corx = 0
    corx1 = 0
    cory = 0
    cory1 = 0
    area = 0
    for elm in polygons:
        x, y, ar = find_centroid(elm)
        corx = corx + x * ar
        cory = cory + y * ar
        area = area + ar
    corx, cory = corx / area, cory / area
    return make_position(corx, cory)
Example #49
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    
    x1, y1, ar = 0, 0, 0
    for i in range(len(polygons)):
        la, lo, a = find_centroid(polygons[i])
        x1 += (la * a)
        y1 += (lo * a)
        ar += a
    return make_position((x1/ar),(y1/ar))
Example #50
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """

    sum_of_area = 0
    x_weighted_sum = 0
    y_weighted_sum = 0
    for i in polygons:
        xi, yi, ai = find_centroid(i)
        sum_of_area += ai
        x_weighted_sum += xi * ai
        y_weighted_sum += yi * ai

    return make_position(x_weighted_sum / sum_of_area,
                         y_weighted_sum / sum_of_area)
Example #51
0
def find_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    Aa, cx, cy = 0, 0, 0  # Aa = new area
    l_polygons = tuple(map(find_centroid, polygons))
    for elem in l_polygons:
        x, y, A = elem
        cx += x * A
        cy += y * A
        Aa += A
    x = cx / Aa
    y = cy / Aa
    return make_position(x, y)
Example #52
0
def find_closest_state(tweet, state_centers):
    """Return the name of the state closest to the given tweet's location.
    
    Use the geo_distance function (already provided) to calculate distance 
    in miles between two latitude-longitude positions.

    Arguments:
    tweet -- a tweet abstract data type
    state_centers -- a dictionary from state names to state shapes

    >>> us_centers = {n: find_center(s) for n, s in us_states.items()}
    >>> sf = make_tweet("Welcome to San Francisco", None, 38, -122)
    >>> ny = make_tweet("Welcome to New York", None, 41, -74)
    >>> find_closest_state(sf, us_centers)
    'CA'
    >>> find_closest_state(ny, us_centers)
    'NJ'
    """
    "*** YOUR CODE HERE ***"
    tweet_pos = make_position(tweet['latitude'], tweet['longitude'])
    minimum = 9999999
    flag = ''
    for item in state_centers:
        if minimum > geo_distance(tweet_pos, state_centers[item]):
            minimum = geo_distance(tweet_pos, state_centers[item])
            flag = item
    return flag
Example #53
0
def find_center(shapes):
    """Compute the geographic center of a state, averaged over its shapes.

    The center is the average position of centroids of the polygons in shapes,
    weighted by the area of those polygons.
    
    Arguments:
    # shapes -- a list of polygons
    shapes -- a tuple of polyons

    >>> ca = find_center(idict_select(us_states, 'CA'))  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_center(idict_select(us_states, 'HI'))  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"

    Xsum, Ysum, totalArea = 0, 0, 0

    for polygon in shapes:
        X,Y, area = find_centroid(polygon)
        Xsum, Ysum, totalArea = Xsum+X*area, Ysum+Y*area, totalArea+area
    Xsum = Xsum/totalArea
    Ysum = Ysum/totalArea
    return make_position(Xsum,Ysum)
Example #54
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    lat_lst = 0
    lon_lst = 0
    area = 0
    for elem in polygons:
        new_area = find_centroid(elem)[2]
        lat_lst += find_centroid(elem)[0]*new_area
        lon_lst += find_centroid(elem)[1]*new_area
        area += new_area
    avg_lat = (lat_lst/(area))
    avg_lon = (lon_lst/(area))
    abstract = make_position(avg_lat, avg_lon)
    return abstract
Example #55
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in
    polygons, weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    sum_numerator_x, sum_numerator_y, sum_denominator = 0, 0, 0
    for p in polygons:
        t = find_centroid(p)
        sum_numerator_x += t[0] * t[2]
        sum_numerator_y += t[1] * t[2]
        sum_denominator += t[2]

    x = sum_numerator_x / sum_denominator
    y = sum_numerator_y / sum_denominator

    return make_position(x, y)
Example #56
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in
    polygons, weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    weighted_latitude = sum([find_centroid(polygon)[0] * area_of_polygon(polygon) for polygon in polygons]) / (sum([area_of_polygon(polygon) for polygon in polygons]))
    weighted_longitude = sum([find_centroid(polygon)[1] * area_of_polygon(polygon) for polygon in polygons]) / (sum([area_of_polygon(polygon) for polygon in polygons]))
    return make_position(weighted_latitude, weighted_longitude)
Example #57
0
def find_state_center(polygons):
    """Compute the geographic center of a state, averaged over its polygons.

    The center is the average position of centroids of the polygons in polygons,
    weighted by the area of those polygons.

    Arguments:
    polygons -- a list of polygons

    >>> ca = find_state_center(us_states['CA'])  # California
    >>> round(latitude(ca), 5)
    37.25389
    >>> round(longitude(ca), 5)
    -119.61439

    >>> hi = find_state_center(us_states['HI'])  # Hawaii
    >>> round(latitude(hi), 5)
    20.1489
    >>> round(longitude(hi), 5)
    -156.21763
    """
    "*** YOUR CODE HERE ***"
    mypolygons=list(map(find_centroid,polygons))
    Cx=sum([ w[0]*w[2] for w in mypolygons])
    Cy=sum([ w[1]*w[2] for w in mypolygons])
    S=sum([w[2] for w in mypolygons])
    return make_position(Cx/S,Cy/S)