<jupyter_start><jupyter_text>"Geospatial Analysis exercise - proximity analysis"> "Geospatial Analysis exercise - proximity analysis"- toc:true- branch: master- badges: true- comments: true- author: jaeeon- categories: [jupyter, python] **Introduction**You are part of a crisis response team, and you want to identify how hospitals have been responding to crash collisions in New York City.Before you get started, run the code cell below to set everything up.<jupyter_code>import math
import geopandas as gpd
import pandas as pd
from shapely.geometry import MultiPolygon

import folium
from folium import Choropleth, Marker
from folium.plugins import HeatMap, MarkerCluster

from learntools.core import binder
binder.bind(globals())
from learntools.geospatial.ex5 import *<jupyter_output><empty_output><jupyter_text>You'll use the `embed_map()` function to visualize your maps.지도를 시각화하기 위해 `embed_map()` 함수를 사용할 것입니다.<jupyter_code>def embed_map(m, file_name):
    from IPython.display import IFrame
    m.save(file_name)
    return IFrame(file_name, width='100%', height='500px')<jupyter_output><empty_output><jupyter_text>Exercises 1) Visualize the collision data.Run the code cell below to load a GeoDataFrame `collisions` tracking major motor vehicle collisions in 2013-2018.아래 코드 셀을 실행하여 2013-2018년의 주요 자동차 충돌을 추적하는 GeoDataFrame`collisions`을 로드합니다.<jupyter_code>collisions = gpd.read_file("../input/geospatial-learn-course-data/NYPD_Motor_Vehicle_Collisions/NYPD_Motor_Vehicle_Collisions/NYPD_Motor_Vehicle_Collisions.shp")
collisions.head()<jupyter_output><empty_output><jupyter_text>Use the "LATITUDE" and "LONGITUDE" columns to create an interactive map to visualize the collision data.  What type of map do you think is most effective?"LATITUDE" 및 "LONGITUDE" 열을 사용하여 충돌 데이터를 시각화하는 대화형 맵을 만듭니다. 어떤 유형의 지도가 가장 효과적이라고 생각하십니까?<jupyter_code>m_1 = folium.Map(location=[40.7, -74], zoom_start=11) 

# Your code here: Visualize the collision data
HeatMap(data=collisions[['LATITUDE', 'LONGITUDE']], radius=10).add_to(m_1)

# Uncomment to see a hint
q_1.hint()

# Show the map
embed_map(m_1, "q_1.html")
# Get credit for your work after you have created a map
q_1.check()

# Uncomment to see our solution (your code may look different!)
#q_1.solution()<jupyter_output><empty_output><jupyter_text>2) Understand hospital coverage.Run the next code cell to load the hospital data.다음 코드 셀을 실행하여 병원 데이터를 로드합니다.<jupyter_code>hospitals = gpd.read_file("../input/geospatial-learn-course-data/nyu_2451_34494/nyu_2451_34494/nyu_2451_34494.shp")
hospitals.head()<jupyter_output><empty_output><jupyter_text>Use the "latitude" and "longitude" columns to visualize the hospital locations."위도" 및 "경도" 열을 사용하여 병원 위치를 시각화합니다.<jupyter_code>m_2 = folium.Map(location=[40.7, -74], zoom_start=11) 

# Your code here: Visualize the hospital locations
for idx, row in hospitals.iterrows():
    Marker([row['latitude'], row['longitude']], popup=row['name']).add_to(m_2)

# Uncomment to see a hint
q_2.hint()
        
# Show the map
embed_map(m_2, "q_2.html")
# Get credit for your work after you have created a map
q_2.check()

# Uncomment to see our solution (your code may look different!)
#q_2.solution()<jupyter_output><empty_output><jupyter_text>3) When was the closest hospital more than 10 kilometers away?Create a DataFrame `outside_range` containing all rows from `collisions` with crashes that occurred more than 10 kilometers from the closest hospital.Note that both `hospitals` and `collisions` have EPSG 2263 as the coordinate reference system, and EPSG 2263 has units of meters.------------------------------**3) 10km 이상 떨어진 가장 가까운 병원은 언제였습니까?**가장 가까운 병원에서 10km 이상 떨어진 곳에서 발생한 충돌이 있는 `collisions` 의 모든 행을 포함하는 DataFrame `outside_range`를 만듭니다.`hospitals`과 `collisions` 모두 좌표 참조 시스템으로 EPSG 2263을 사용하고 EPSG 2263은 미터 단위를 사용합니다.<jupyter_code># Your code here
hos_df_buffer = gpd.GeoDataFrame(geometry=hospitals.geometry).buffer(10000)
print(hos_df_buffer)

hos_union = hos_df_buffer.geometry.unary_union
hos_union
# 가장 가까운 병원에서 10km 이상 떨어진 곳에서 발생한 충돌이 있는 collisions 의 모든 행을 포함
outside_range = collisions.loc[~collisions["geometry"].apply(lambda x: hos_union.contains(x))]
outside_range
# Check your answer
q_3.check()
# Lines below will give you a hint or solution code
q_3.hint()
#q_3.solution()<jupyter_output><empty_output><jupyter_text>The next code cell calculates the percentage of collisions that occurred more than 10 kilometers away from the closest hospital.다음 코드 셀은 가장 가까운 병원에서 10km 이상 떨어진 곳에서 발생한 충돌의 비율을 계산합니다.<jupyter_code>percentage = round(100*len(outside_range)/len(collisions), 2)
print("Percentage of collisions more than 10 km away from the closest hospital: {}%".format(percentage))<jupyter_output><empty_output><jupyter_text>4) Make a recommender.When collisions occur in distant locations, it becomes even more vital that injured persons are transported to the nearest available hospital.With this in mind, you decide to create a recommender that:- takes the location of the crash (in EPSG 2263) as input,- finds the closest hospital (where distance calculations are done in EPSG 2263), and - returns the name of the closest hospital. --------------------------멀리 떨어진 곳에서 충돌이 발생하면 부상자를 가까운 병원으로 이송하는 것이 더욱 중요해집니다.이를 염두에 두고 다음과 같은 추천자를 만들기로 결정합니다.- 충돌 위치(EPSG 2263에서)를 입력으로 사용합니다.- 가장 가까운 병원을 찾습니다(거리 계산은 EPSG 2263에서 수행됨).- 가장 가까운 병원의 이름을 반환합니다.<jupyter_code>def best_hospital(collision_location):
    # Your code here
    idx_min = hospitals.geometry.distance(collision_location).idxmin()
    my_hospital = hospitals.iloc[idx_min]
    name = my_hospital["name"]
    return name

# Test your function: this should suggest CALVARY HOSPITAL INC
print(best_hospital(outside_range.geometry.iloc[0]))
# Check your answer
q_4.check()
# Lines below will give you a hint or solution code
q_4.hint()
#q_4.solution()<jupyter_output><empty_output><jupyter_text>5) Which hospital is under the highest demand?Considering only collisions in the `outside_range` DataFrame, which hospital is most recommended?  Your answer should be a Python string that exactly matches the name of the hospital returned by the function you created in **4)**.-------------------------**5) 수요가 가장 많은 병원은?**`outside_range` DataFrame에서 충돌만 고려한다면 어느 병원을 가장 추천하는가?**4)**에서 생성한 함수가 반환한 병원 이름과 정확히 일치하는 Python 문자열이어야 합니다.<jupyter_code>print(best_hospital(outside_range.geometry.iloc[0]))
#> CALVARY HOSPITAL INC
# Your code here
highest_demand = outside_range.geometry.apply(best_hospital).value_counts().idxmax
highest_demand 
# Check your answer
q_5.check()
# Lines below will give you a hint or solution code
q_5.hint()
#q_5.solution()<jupyter_output><empty_output><jupyter_text>6) Where should the city construct new hospitals?Run the next code cell (without changes) to visualize hospital locations, in addition to collisions that occurred more than 10 kilometers away from the closest hospital. 다음 코드 셀(변경 없이)을 실행하여 가장 가까운 병원에서 10km 이상 떨어진 곳에서 발생한 충돌 외에도 병원 위치를 시각화합니다.<jupyter_code>m_6 = folium.Map(location=[40.7, -74], zoom_start=11) 

coverage = gpd.GeoDataFrame(geometry=hospitals.geometry).buffer(10000)
folium.GeoJson(coverage.geometry.to_crs(epsg=4326)).add_to(m_6)
HeatMap(data=outside_range[['LATITUDE', 'LONGITUDE']], radius=9).add_to(m_6)
folium.LatLngPopup().add_to(m_6)

embed_map(m_6, 'm_6.html')<jupyter_output><empty_output><jupyter_text>Click anywhere on the map to see a pop-up with the corresponding location in latitude and longitude.The city of New York reaches out to you for help with deciding locations for two brand new hospitals.  They specifically want your help with identifying locations to bring the calculated percentage from step **3)** to less than ten percent.  Using the map (and without worrying about zoning laws or what potential buildings would have to be removed in order to build the hospitals), can you identify two locations that would help the city accomplish this goal?  Put the proposed latitude and longitude for hospital 1 in `lat_1` and `long_1`, respectively.  (Likewise for hospital 2.)Then, run the rest of the cell as-is to see the effect of the new hospitals.  Your answer will be marked correct, if the two new hospitals bring the percentage to less than ten percent.-----------------------------지도의 아무 곳이나 클릭하면 해당 위치의 위도 및 경도 팝업이 표시됩니다.New York 시는 두 곳의 새로운 병원을 지을 위치를 결정하는 데 도움을 드리기 위해 연락을 드립니다. 그들은 특히 **3)** 단계에서 계산된 백분율을 10% 미만으로 만들기 위해 위치 식별에 대한 귀하의 도움을 원합니다. 지도를 사용하여(지역 설정법이나 병원을 건설하기 위해 제거해야 할 잠재적 건물에 대해 걱정하지 않고) 도시가 이 목표를 달성하는 데 도움이 될 두 위치를 식별할 수 있습니까?병원 1에 대해 제안된 위도와 경도를 각각 `lat_1`과 `long_1`에 넣습니다. (병원2도 마찬가지)그런 다음 나머지 셀을 그대로 실행하여 새 병원의 효과를 확인하십시오. 두 개의 새로운 병원에서 백분율을 10% 미만으로 낮추면 답이 정답으로 표시됩니다.<jupyter_code># Your answer here: proposed location of hospital 1
lat_1 = 40.6714
long_1 = -73.8492

# Your answer here: proposed location of hospital 2
lat_2 = 40.6702
long_2 = -73.7612


# Do not modify the code below this line
try:
    new_df = pd.DataFrame(
        {'Latitude': [lat_1, lat_2],
         'Longitude': [long_1, long_2]})
    new_gdf = gpd.GeoDataFrame(new_df, geometry=gpd.points_from_xy(new_df.Longitude, new_df.Latitude))
    new_gdf.crs = {'init' :'epsg:4326'}
    new_gdf = new_gdf.to_crs(epsg=2263)
    # get new percentage
    new_coverage = gpd.GeoDataFrame(geometry=new_gdf.geometry).buffer(10000)
    new_my_union = new_coverage.geometry.unary_union
    new_outside_range = outside_range.loc[~outside_range["geometry"].apply(lambda x: new_my_union.contains(x))]
    new_percentage = round(100*len(new_outside_range)/len(collisions), 2)
    print("(NEW) Percentage of collisions more than 10 km away from the closest hospital: {}%".format(new_percentage))
    # Did you help the city to meet its goal?
    q_6.check()
    # make the map
    m = folium.Map(location=[40.7, -74], zoom_start=11) 
    folium.GeoJson(coverage.geometry.to_crs(epsg=4326)).add_to(m)
    folium.GeoJson(new_coverage.geometry.to_crs(epsg=4326)).add_to(m)
    for idx, row in new_gdf.iterrows():
        Marker([row['Latitude'], row['Longitude']]).add_to(m)
    HeatMap(data=new_outside_range[['LATITUDE', 'LONGITUDE']], radius=9).add_to(m)
    folium.LatLngPopup().add_to(m)
    display(embed_map(m, 'q_6.html'))
except:
    q_6.hint()
# Uncomment to see one potential answer 
q_6.solution()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10077972709551657, 'fraction_numerical': 0.026510721247563353, 'mean_word_length': 5.011130638547159, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '122221', 'license': 'Apache-2.0', 'n_tokens_mistral': 4356, 'n_tokens_neox': 4500, 'n_words': 1155, 'path': '_notebooks/2022-05-24-Ex5_proximity_analysis.ipynb', 'repo_name': 'WooJaaEon/jaong', 'size': '10260'}