# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
from shapely.geometry import box, Polygon

import folium
import requests
import json
import plotly.express as px
import pandas as pd

m = folium.Map(location=[40.28, -89.39])

# +
# playing around with folium, this is probably going to replace the google maps call in place on the app currently

url = (
    'https://raw.githubusercontent.com/deldersveld/topojson/master/countries/us-states/IL-17-illinois-counties.json'
)



m = folium.Map(width = 500,
               height = 500,
    location=[40.28, -89.39],
    tiles="cartodbpositron",
    zoom_start=5,
)

folium.Marker(
    location=[36.9815, -91.51135],
    popup='reference',
).add_to(m)

folium.Circle(
    radius=1115,
    location=[40.28, -89.39],
    popup="Chicago",
    color="red",
    fill=True,
    fill_color='red'
).add_to(m)

# folium.GeoJson(url, name="geojson").add_to(m)

folium.TopoJson(
    json.loads(requests.get(url).text),
    "objects.cb_2015_illinois_county_20m",
    name="topojson",
    
).add_to(m)

folium.LayerControl().add_to(m)

m
# -

m.save('m.html')

# +
# summoning and inspecting the topojson data

alpha = json.loads(requests.get(url).text)

# +
# coordinates for reference in understanding above data


# 41.148339, -89.638487
# 36.981528, -91.511353

# +
# inspecting the scale key under the transform key inside of the alpha dictionary

lat_ratio = ((41.148339 - 36.981528)/3344)
long_ratio = ((-89.638487 + 91.511353)/1061)
print(lat_ratio, 'lat ratio')
print(long_ratio, 'long ratio')

# +
# One degree of latitude equals approximately 364,000 feet (69 miles), one minute equals 6,068 feet (1.15 miles), 
# and one-second equals 101 feet. One-degree of 
# longitude equals 288,200 feet (54.6 miles), one minute equals 4,800 feet (0.91 mile), 
# and one second equals 80 feet.

# +
# fitting polygons to shapely Polygon objects

alpha = json.loads(requests.get(url).text)
beta = alpha['arcs']
polys = []
county_names = []

for i, x in enumerate(alpha['objects']['cb_2015_illinois_county_20m']['geometries']):
    poly_shape = []
    for y in x['arcs'][0]:        
        if y<0:
            temp = []
            y = abs(y+1)
            arc_ind = beta[y].copy()
            ref = np.array(arc_ind[0]) 
            temp.append(ref)
            for j in range(1, len(arc_ind)):            
                ref = ref + np.array(arc_ind[j])
                temp.append(ref)
            for zed in reversed(temp):
                poly_shape.append(zed)
        else:
            arc_ind = beta[y].copy()
            ref = np.array(arc_ind[0])
            poly_shape.append(ref)
            for j in range(1, len(arc_ind)):            
                ref = ref + np.array(arc_ind[j])
                poly_shape.append(ref)
    shape_final = Polygon(poly_shape)
    polys.append(shape_final)
    county_names.append(x['properties']['NAME'])

# +
# take a look at marshall county!

polys[0]
# perfect
# -

for x in polys:
    display(x)

# +
# testing to see that these distinct and seperate polygons are not intersecting:

marshall = polys[0]
cumberland = polys[1]
marshall.intersects(cumberland)

#False -- excellent!

# +
import math

# functions to determine points of circle

# This function gets just one pair of coordinates based on the angle theta
def get_circle_coord(theta, x_center, y_center, radius):
    x = radius * math.cos(theta) + x_center
    y = radius * math.sin(theta) + y_center
    return [int(x),int(y)]

# This function gets all the pairs of coordinates
def get_all_circle_coords(x_center, y_center, radius, n_points):
    thetas = [i/n_points * math.tau for i in range(n_points+1)]
    circle_coords = [get_circle_coord(theta, x_center, y_center, radius) for theta in thetas]
    return circle_coords

# Using the second function to generate all the pairs of coordinates.
# circle_coords = get_all_circle_coords(x_center = 5, 
#                                       y_center = 15,
#                                       radius = 2.5,
#                                       n_points = 5000)


# +
# testing above functions

get_all_circle_coords(0, 0, 50, 24)
# yep, thats a circle!

# +
# developing function to return intersections of circle radius
# enter radius in miles

coords = [41.015051, -89.307540]
def find_intersecting_counties(lat, long, radius):
    """Function to return intersecting counties"""
    
    refs = [36.981528, -91.511353]
    lat_ratio = ((41.148339 - 36.981528)/3344)
    long_ratio = ((-89.638487 + 91.511353)/1061)
    radius = radius/69
    radius = int(radius/lat_ratio)
    lat_pre = abs(refs[0] - lat)
    long_pre = abs(refs[1] - long)
    y_coord = int(long_pre/long_ratio)
    x_coord = int(lat_pre/lat_ratio)
    center = [y_coord, x_coord]
    coords = get_all_circle_coords(center[0], center[1], radius, 60)
    circle = Polygon(coords)
    intersectors = []
    for i, shape in enumerate(polys):
        if shape.intersects(circle):            
            intersectors.append(i)            
    return intersectors, circle


# -

delta, beta = find_intersecting_counties(coords[0], coords[1], 10)

for x in delta:
    print(county_names[x])

beta

for x in polys:
    display(x)

df = pd.read_csv('<csv_here>')

df.head()



StarcoderdataJupyterScriptsDedupFiltered