Data_Mining_Kaggle
Exercise 2 - Coordinate Reference Systems
![]() |
This notebook is an exercise in the Geospatial Analysis course. You can reference the tutorial at this link.
1 Introduction
You are a bird conservation expert and want to understand migration patterns of purple martins. In your research, you discover that these birds typically spend the summer breeding season in the eastern United States, and then migrate to South America for the winter. But since this bird is under threat of endangerment, you’d like to take a closer look at the locations that these birds are more likely to visit.

There are several protected areas in South America, which operate under special regulations to ensure that species that migrate (or live) there have the best opportunity to thrive. You’d like to know if purple martins tend to visit these areas. To answer this question, you’ll use some recently collected data that tracks the year-round location of eleven different birds.
Before you get started, run the code cell below to set everything up.
import pandas as pd
import geopandas as gpd
from shapely.geometry import LineString
import matplotlib.pyplot as plt
2 Exercises
2.1 Load the data.
다음 코드 셀을 (변경 없이) 실행하여 GPS 데이터를 Panda DataFrame birds_df에 로드합니다.
# Load the data and print the first 5 rows
= pd.read_csv("C:/Users\seong taek/Desktop/archive/purple_martin.csv", parse_dates=['timestamp']) # 날짜/시간 객체로 읽기
birds_df print("There are {} different birds in the dataset.".format(birds_df["tag-local-identifier"].nunique()))
birds_df.head()
There are 11 different birds in the dataset.
timestamp | location-long | location-lat | tag-local-identifier | |
---|---|---|---|---|
0 | 2014-08-15 05:56:00 | -88.146014 | 17.513049 | 30448 |
1 | 2014-09-01 05:59:00 | -85.243501 | 13.095782 | 30448 |
2 | 2014-10-30 23:58:00 | -62.906089 | -7.852436 | 30448 |
3 | 2014-11-15 04:59:00 | -61.776826 | -11.723898 | 30448 |
4 | 2014-11-30 09:59:00 | -61.241538 | -11.612237 | 30448 |
데이터 세트에는 11마리의 새가 있으며, 각 새는 “태그-로컬-식별자” 열에서 고유한 값으로 식별됩니다. 각각의 새들은 일년 중 다른 시기에 수집된 여러 개의 측정치를 가지고 있습니다.
다음 코드 셀을 사용하여 GeoDataFrame 새를 만듭니다.
- birds는 birds_df의 모든 열과 (경도, 위도) 위치를 가진 Point 객체를 포함하는 “기하학” 열을 가져야 합니다 - 새의 CRS를 {init}: ‘epsg:4326’}(으)로 설정합니다..
# Your code here: Create the GeoDataFrame
= [Point(xy) for xy in zip(birds_df["location-long"], birds_df["location-lat"])]
geometry = gpd.GeoDataFrame(birds_df, crs="EPSG:4326", geometry=geometry)
birds
# Your code here: Set the CRS to {'init': 'epsg:4326'}
= {'init': 'epsg:4326'} birds.crs
C:\Users\seong taek\anaconda3\lib\site-packages\pyproj\crs\crs.py:141: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6
in_crs_string = _prepare_from_proj_string(in_crs_string)
birds.head()
timestamp | location-long | location-lat | tag-local-identifier | geometry | |
---|---|---|---|---|---|
0 | 2014-08-15 05:56:00 | -88.146014 | 17.513049 | 30448 | POINT (-88.14601 17.51305) |
1 | 2014-09-01 05:59:00 | -85.243501 | 13.095782 | 30448 | POINT (-85.24350 13.09578) |
2 | 2014-10-30 23:58:00 | -62.906089 | -7.852436 | 30448 | POINT (-62.90609 -7.85244) |
3 | 2014-11-15 04:59:00 | -61.776826 | -11.723898 | 30448 | POINT (-61.77683 -11.72390) |
4 | 2014-11-30 09:59:00 | -61.241538 | -11.612237 | 30448 | POINT (-61.24154 -11.61224) |
2.2 Plot the data.
다음으로 GeoPandas의 ‘naturalearth_lowres’ 데이터 세트를 로드하고 미주(북남미 모두)의 모든 국가 경계를 포함하는 GeoDataFrame으로 미주를 설정합니다. 변경하지 않고 다음 코드 셀을 실행합니다.
# Load a GeoDataFrame with country boundaries in North/South America, print the first 5 rows
= gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world = world.loc[world['continent'].isin(['North America', 'South America'])]
americas americas.head()
pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
---|---|---|---|---|---|---|
3 | 37589262.0 | North America | Canada | CAN | 1736425 | MULTIPOLYGON (((-122.84000 49.00000, -122.9742… |
4 | 328239523.0 | North America | United States of America | USA | 21433226 | MULTIPOLYGON (((-122.84000 49.00000, -120.0000… |
9 | 44938712.0 | South America | Argentina | ARG | 445445 | MULTIPOLYGON (((-68.63401 -52.63637, -68.25000… |
10 | 18952038.0 | South America | Chile | CHL | 282318 | MULTIPOLYGON (((-68.63401 -52.63637, -68.63335… |
16 | 11263077.0 | North America | Haiti | HTI | 14332 | POLYGON ((-71.71236 19.71446, -71.62487 19.169… |
다음 코드 셀을 사용하여 (1) Americas GeoDataFrame의 국가 경계와 (2) birds_gdf GeoDataFrame의 모든 점을 모두 표시하는 단일 플롯을 만듭니다.
여기서는 특별한 스타일에 대해 걱정하지 말고 모든 데이터가 올바르게 로드되었는지 신속하게 확인하기 위해 예비 플롯을 작성하면 됩니다. 특히 새를 구별하기 위해 포인트를 컬러 코딩하는 것에 대해 걱정할 필요가 없으며, 시작 포인트와 끝점을 구분할 필요가 없습니다. 우리는 연습의 다음 부분에서 그것을 할 것입니다.
### Your code here
# figure 객체와 axis 객체를 생성합니다.
= plt.subplots(figsize=(10, 10))
fig, ax
# americas GeoDataFrame의 경계를 표시합니다.
=ax, color='white', edgecolor='black')
americas.plot(ax
# birds GeoDataFrame의 점을 표시합니다.
=ax, markersize=10, color='red')
birds.plot(ax
# 그래프를 출력합니다.
plt.show()
2.3 Where does each bird start and end its journey? (Part 1)
이제, 우리는 각각의 새들의 경로를 더 자세히 볼 준비가 되었습니다. 다음 코드 셀을 실행하여 두 개의 GeoDataFrames를 만듭니다: - path_gdf에는 각 새의 경로를 표시하는 LineString 개체가 포함되어 있습니다. - LineString() 메서드를 사용하여 점 객체 목록에서 LineString 객체를 만듭니다. - start_gdf는 각 새의 시작점을 포함합니다.
# GeoDataFrame showing path for each bird
= birds.groupby("tag-local-identifier")['geometry'].apply(list).apply(lambda x: LineString(x)).reset_index()
path_df = gpd.GeoDataFrame(path_df, geometry=path_df.geometry)
path_gdf = {'init' :'epsg:4326'}
path_gdf.crs
# GeoDataFrame showing starting point for each bird
= birds.groupby("tag-local-identifier")['geometry'].apply(list).apply(lambda x: x[0]).reset_index()
start_df = gpd.GeoDataFrame(start_df, geometry=start_df.geometry)
start_gdf = {'init' :'epsg:4326'}
start_gdf.crs
# Show first five rows of GeoDataFrame
start_gdf.head()
C:\Users\seong taek\anaconda3\lib\site-packages\pyproj\crs\crs.py:141: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6
in_crs_string = _prepare_from_proj_string(in_crs_string)
C:\Users\seong taek\anaconda3\lib\site-packages\pyproj\crs\crs.py:141: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6
in_crs_string = _prepare_from_proj_string(in_crs_string)
tag-local-identifier | geometry | |
---|---|---|
0 | 30048 | POINT (-90.12992 20.73242) |
1 | 30054 | POINT (-93.60861 46.50563) |
2 | 30198 | POINT (-80.31036 25.92545) |
3 | 30263 | POINT (-76.78146 42.99209) |
4 | 30275 | POINT (-76.78213 42.99207) |
다음 코드 셀을 사용하여 각 새의 최종 위치를 포함하는 GeoDataFrame end_gdf를 만듭니다.
- 형식은 start_gdf의 형식과 동일해야 하며 두 개의 열(“태그-로컬 식별자”) 및 (“기하학”)열에 점 객체가 포함되어 있어야 합니다. - end_gdf의 CRS를 {init}: ‘epsg:4326’}(으)로 설정합니다.
# Your code here
= birds.groupby("tag-local-identifier")['geometry'].apply(list).apply(lambda x: x[-1]).reset_index()
end_df = gpd.GeoDataFrame(end_df, geometry=end_df.geometry)
end_gdf = ['tag-local-identifier', 'geometry']
end_gdf.columns
end_gdf
tag-local-identifier | geometry | |
---|---|---|
0 | 30048 | POINT (-47.53632 -4.43758) |
1 | 30054 | POINT (-62.47914 -5.03840) |
2 | 30198 | POINT (-57.46417 -2.77617) |
3 | 30263 | POINT (-50.19230 -5.70504) |
4 | 30275 | POINT (-57.70404 -16.72336) |
5 | 30300 | POINT (-50.22547 -9.84450) |
6 | 30304 | POINT (-52.55503 -5.85648) |
7 | 30380 | POINT (-63.98744 -2.93250) |
8 | 30384 | POINT (-51.85126 -2.90570) |
9 | 30445 | POINT (-53.97454 -2.35599) |
10 | 30448 | POINT (-59.50059 -3.91805) |
2.4 Where does each bird start and end its journey? (Part 2)
위 질문(path_gdf, start_gdf 및 end_gdf)의 GeoDataFrames를 사용하여 모든 새의 경로를 단일 맵에 시각화합니다. 또한 미주 지역 데이터 프레임을 사용할 수도 있습니다.
### Your code here
# americas GeoDataFrame을 사용하여 미주 지역 지도를 만듭니다
= americas.plot(figsize=(12,8), color='white', edgecolor='black')
ax
# path_gdf, start_gdf, end_gdf GeoDataFrame을 사용하여 모든 새의 이동 경로, 출발점 및 도착점을 지도에 추가합니다
=ax, color='red', linewidth=0.5)
path_gdf.plot(ax=ax, color='blue', markersize=10)
start_gdf.plot(ax=ax, color='green', markersize=10)
end_gdf.plot(ax
# 제목, 범례 추가
"Migration of Purple Martins in Americas")
ax.set_title('Bird Path', 'Starting Point', 'Ending Point'])
ax.legend([
# 출력
plt.show()
2.5 Where are the protected areas in South America? (Part 1)
모든 새들이 결국 남아메리카 어딘가에 도착하는 것처럼 보입니다. 하지만 그들은 보호구역으로 갈 것입니까?
다음 코드 셀에서는 남미의 모든 보호 영역의 위치를 포함하는 GeoDataFrame protected_areas를 만듭니다. 해당 셰이프 파일은 filepath protected_filepath에 있습니다
# Path of the shapefile to load
= "C:/Users\seong taek/Desktop/archive/SAPA_Aug2019-shapefile/SAPA_Aug2019-shapefile/SAPA_Aug2019-shapefile-polygons.shp"
protected_filepath
# Your code here
= gpd.read_file(protected_filepath)
protected_areas protected_areas.head()
WDPAID | WDPA_PID | PA_DEF | NAME | ORIG_NAME | DESIG | DESIG_ENG | DESIG_TYPE | IUCN_CAT | INT_CRIT | … | GOV_TYPE | OWN_TYPE | MANG_AUTH | MANG_PLAN | VERIF | METADATAID | SUB_LOC | PARENT_ISO | ISO3 | geometry | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 14067.0 | 14067 | 1 | Het Spaans Lagoen | Het Spaans Lagoen | Ramsar Site, Wetland of International Importance | Ramsar Site, Wetland of International Importance | International | Not Reported | Not Reported | … | Not Reported | Not Reported | Not Reported | Management plan is not implemented and not ava… | State Verified | 1856 | Not Reported | NLD | ABW | POLYGON ((-69.97523 12.47379, -69.97523 12.473… |
1 | 14003.0 | 14003 | 1 | Bubali Pond Bird Sanctuary | Bubali Pond Bird Sanctuary | Bird Sanctuary | Bird Sanctuary | National | Not Reported | Not Applicable | … | Not Reported | Not Reported | Not Reported | Not Reported | State Verified | 1899 | Not Reported | NLD | ABW | POLYGON ((-70.04734 12.56329, -70.04615 12.563… |
2 | 555624439.0 | 555624439 | 1 | Arikok National Park | Arikok National Park | National Park | National Park | National | Not Reported | Not Applicable | … | Non-profit organisations | Non-profit organisations | Fundacion Parke Nacional Arikok | Not Reported | State Verified | 1899 | Not Reported | NLD | ABW | MULTIPOLYGON (((-69.96302 12.48384, -69.96295 … |
3 | 303894.0 | 303894 | 1 | Madidi | Madidi | Area Natural de Manejo Integrado | Natural Integrated Management Area | National | Not Reported | Not Applicable | … | Federal or national ministry or agency | Not Reported | Not Reported | Not Reported | State Verified | 1860 | BO-L | BOL | BOL | POLYGON ((-68.59060 -14.43388, -68.59062 -14.4… |
4 | 303893.0 | 303893 | 1 | Apolobamba | Apolobamba | Area Natural de Manejo Integado Nacional | National Natural Integrated Management Area | National | Not Reported | Not Applicable | … | Federal or national ministry or agency | Not Reported | Not Reported | Not Reported | State Verified | 1860 | BO-L | BOL | BOL | POLYGON ((-69.20949 -14.73334, -69.20130 -14.7… |
5 rows × 29 columns
2.6 Where are the protected areas in South America? (Part 2)
protected_areas GeoDataFrame을 사용하여 남미의 보호지역 위치를 표시하는 플롯을 만듭니다. (일부 보호지역은 육지에, 다른 보호지역은 해양에 있음을 알 수 있습니다.)
# Country boundaries in South America
= americas.loc[americas['continent']=='South America']
south_america
### Your code here: plot protected areas in South America
# 보호지역 데이터를 지도에 그리기
= south_america.plot(color='white', edgecolor='black')
ax =ax, alpha=0.5, color='green')
protected_areas.plot(ax
# 플롯 출력하기
plt.show()
2.7 What percentage of South America is protected?
여러분은 남아메리카의 몇 퍼센트가 보호되고 있는지를 결정하는 데 관심이 있습니다. 그래서 여러분은 남아메리카의 어느 정도가 새들에게 적합한지를 알 수 있습니다
첫 번째 단계로 남아메리카의 모든 보호 지역(해양 지역 제외)의 총 면적을 계산합니다. 이렇게 하려면 “REP_AREA” 및 “REP_M_AREA” 열을 사용합니다. 여기에는 각각 총 면적과 총 해양 면적이 평방 킬로미터로 포함됩니다.
아래 코드 셀을 변경하지 않고 실행합니다.
= sum(protected_areas['REP_AREA']-protected_areas['REP_M_AREA'])
P_Area print("South America has {} square kilometers of protected areas.".format(P_Area))
South America has 5396761.9116883585 square kilometers of protected areas.
그런 다음 계산을 마치려면 south_america GeoDataFrame을 사용합니다.
south_america.head()
pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
---|---|---|---|---|---|---|
9 | 44938712.0 | South America | Argentina | ARG | 445445 | MULTIPOLYGON (((-68.63401 -52.63637, -68.25000… |
10 | 18952038.0 | South America | Chile | CHL | 282318 | MULTIPOLYGON (((-68.63401 -52.63637, -68.63335… |
20 | 3398.0 | South America | Falkland Is. | FLK | 282 | POLYGON ((-61.20000 -51.85000, -60.00000 -51.2… |
28 | 3461734.0 | South America | Uruguay | URY | 56045 | POLYGON ((-57.62513 -30.21629, -56.97603 -30.1… |
29 | 211049527.0 | South America | Brazil | BRA | 1839758 | POLYGON ((-53.37366 -33.76838, -53.65054 -33.2… |
다음 단계를 수행하여 남미의 총 면적을 계산합니다: - 각 폴리곤의 면적 속성(CRS로 EPSG 3035 사용)을 사용하여 각 국가의 면적을 계산하고 결과를 합산합니다. 계산된 면적은 제곱미터 단위가 될 것입니다. - 제곱킬로미터의 단위를 갖도록 답변을 변환합니다
### Your code here: Calculate the total area of South America (in square kilometers)
# CRS를 EPSG 3035로 설정합니다.
= south_america.to_crs(epsg=3035)
south_america
# 남미의 면적을 계산합니다.
= south_america.area/10**6 # 제곱미터에서 제곱킬로미터로 변환
south_america_area
# 결과를 합산합니다.
= south_america_area.sum()
total_area
print("남미의 총 면적은 {:.2f} 제곱킬로미터입니다.".format(total_area))
남미의 총 면적은 17759005.82 제곱킬로미터입니다.
아래 코드 셀을 실행하여 보호되는 남미의 백분율을 계산합니다.
# What percentage of South America is protected?
= P_Area/total_area
percentage_protected print('Approximately {}% of South America is protected.'.format(round(percentage_protected*100, 2)))
Approximately 30.39% of South America is protected.
2.8 Where are the birds in South America?
그렇다면, 그 새들은 보호 구역에 있을까요?
모든 새들과 남아메리카에서 발견된 모든 위치를 보여주는 플롯을 만듭니다. 또한 남미의 모든 보호 지역의 위치를 표시합니다
순수하게 해양 영역(육지 구성 요소가 없음)인 보호 영역을 제외하려면 “MARINE” 열을 사용하면 됩니다 - To exclude protected areas that are purely marine areas (with no land component), you can use the “MARINE” column (and plot only the rows in protected_areas[protected_areas[‘MARINE’]!=‘2’], instead of every row in the protected_areas GeoDataFrame).
### Your code here
# 플롯 만들기
= plt.subplots(figsize=(12,8))
fig, ax
# 보호구역 (protected_areas[protected_areas['MARINE']!='2'])
'MARINE']!='2'].plot(ax=ax, color='green', alpha=0.2)
protected_areas[protected_areas[
# 새 위치
=ax, markersize=10, color='blue', alpha=0.5)
birds.plot(ax
# 제목
"Bird locations and protected areas in South America")
ax.set_title("Longitude")
ax.set_xlabel("Latitude")
ax.set_ylabel(
# 그래프 출력
plt.show()
3 Keep going
Create stunning interactive maps with your geospatial data.
Have questions or comments? Visit the course discussion forum to chat with other learners.