Exercise 1¶
Use two maps of points from your country.
Compute the distance matrix for both maps.
Select one row of the distance matrix, and plot the two points with the minimal distance on top of the country of your choosing.
import geopandas as gpd
import matplotlib.pyplot as plt
# Cargar GeoPackage directamente desde GitHub (raw)
usaMapsLink = "https://raw.githubusercontent.com/Derick047/PC6/main/maps/maps/usaMaps_5070.gpkg"
gpd.list_layers(usaMapsLink)
name | geometry_type | |
---|---|---|
0 | country | MultiPolygon |
1 | cities | Unknown |
2 | rivers | MultiLineString |
3 | airports | Point |
airports=gpd.read_file(usaMapsLink,layer='airports')
rivers=gpd.read_file(usaMapsLink,layer='rivers')
cities_url = "https://raw.githubusercontent.com/Derick047/PC6/refs/heads/main/maps/maps/US_GeoCode.csv"
df = pd.read_csv(cities_url)
print(df.columns)
Index(['state&teritory', 'latitude', 'longitude', 'Name'], dtype='object')
gdf_cities = gpd.GeoDataFrame(
df,
geometry=gpd.points_from_xy(df["longitude"], df["latitude"]),
crs="EPSG:4326"
).to_crs("EPSG:5070")
import pandas as pd
# Enlace al archivo CSV de puertos
portsFileLink = "https://github.com/CienciaDeDatosEspacial/GeoDataFrame_Analytics/raw/main/data/UpdatedPub150.csv"
infoseaports = pd.read_csv(portsFileLink)
infoseaports.columns.to_list()
['World Port Index Number', 'Region Name', 'Main Port Name', 'Alternate Port Name', 'UN/LOCODE', 'Country Code', 'World Water Body', 'IHO S-130 Sea Area', 'Sailing Direction or Publication', 'Publication Link', 'Standard Nautical Chart', 'IHO S-57 Electronic Navigational Chart', 'IHO S-101 Electronic Navigational Chart', 'Digital Nautical Chart', 'Tidal Range (m)', 'Entrance Width (m)', 'Channel Depth (m)', 'Anchorage Depth (m)', 'Cargo Pier Depth (m)', 'Oil Terminal Depth (m)', 'Liquified Natural Gas Terminal Depth (m)', 'Maximum Vessel Length (m)', 'Maximum Vessel Beam (m)', 'Maximum Vessel Draft (m)', 'Offshore Maximum Vessel Length (m)', 'Offshore Maximum Vessel Beam (m)', 'Offshore Maximum Vessel Draft (m)', 'Harbor Size', 'Harbor Type', 'Harbor Use', 'Shelter Afforded', 'Entrance Restriction - Tide', 'Entrance Restriction - Heavy Swell', 'Entrance Restriction - Ice', 'Entrance Restriction - Other', 'Overhead Limits', 'Underkeel Clearance Management System', 'Good Holding Ground', 'Turning Area', 'Port Security', 'Estimated Time of Arrival Message', 'Quarantine - Pratique', 'Quarantine - Sanitation', 'Quarantine - Other', 'Traffic Separation Scheme', 'Vessel Traffic Service', 'First Port of Entry', 'US Representative', 'Pilotage - Compulsory', 'Pilotage - Available', 'Pilotage - Local Assistance', 'Pilotage - Advisable', 'Tugs - Salvage', 'Tugs - Assistance', 'Communications - Telephone', 'Communications - Telefax', 'Communications - Radio', 'Communications - Radiotelephone', 'Communications - Airport', 'Communications - Rail', 'Search and Rescue', 'NAVAREA', 'Facilities - Wharves', 'Facilities - Anchorage', 'Facilities - Dangerous Cargo Anchorage', 'Facilities - Med Mooring', 'Facilities - Beach Mooring', 'Facilities - Ice Mooring', 'Facilities - Ro-Ro', 'Facilities - Solid Bulk', 'Facilities - Liquid Bulk', 'Facilities - Container', 'Facilities - Breakbulk', 'Facilities - Oil Terminal', 'Facilities - LNG Terminal', 'Facilities - Other', 'Medical Facilities', 'Garbage Disposal', 'Chemical Holding Tank Disposal', 'Degaussing', 'Dirty Ballast Disposal', 'Cranes - Fixed', 'Cranes - Mobile', 'Cranes - Floating', 'Cranes - Container', 'Lifts - 100+ Tons', 'Lifts - 50-100 Tons', 'Lifts - 25-49 Tons', 'Lifts - 0-24 Tons', 'Services - Longshoremen', 'Services - Electricity', 'Services - Steam', 'Services - Navigation Equipment', 'Services - Electrical Repair', 'Services - Ice Breaking', 'Services - Diving', 'Supplies - Provisions', 'Supplies - Potable Water', 'Supplies - Fuel Oil', 'Supplies - Diesel Oil', 'Supplies - Aviation Fuel', 'Supplies - Deck', 'Supplies - Engine', 'Repairs', 'Dry Dock', 'Railway', 'Latitude', 'Longitude']
# rename
infoseaports.rename(columns={'Main Port Name': 'portName'}, inplace=True)
# keep few columns
infoseaports = infoseaports.loc[:, ['portName', 'Country Code', 'Latitude', 'Longitude']]
infoseaports.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 3739 entries, 0 to 3738 Data columns (total 4 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 portName 3739 non-null object 1 Country Code 3739 non-null object 2 Latitude 3739 non-null float64 3 Longitude 3739 non-null float64 dtypes: float64(2), object(2) memory usage: 117.0+ KB
# some rows
infoseaports.head()
portName | Country Code | Latitude | Longitude | |
---|---|---|---|---|
0 | Maurer | United States | 40.533333 | -74.250000 |
1 | Mangkasa Oil Terminal | Indonesia | -2.733333 | 121.066667 |
2 | Iharana | Madagascar | -13.350000 | 50.000000 |
3 | Andoany | Madagascar | -13.400000 | 48.300000 |
4 | Chake Chake | Tanzania | -5.250000 | 39.766667 |
# spatial points (unprojected)
seaports = gpd.GeoDataFrame(data=infoseaports.copy(),
geometry=gpd.points_from_xy(infoseaports.Longitude,
infoseaports.Latitude),
crs=4326)
# keep USA
seaports_usa = seaports[seaports['Country Code'] == 'United States'].copy()
# reset indexes
seaports_usa.reset_index(drop=True, inplace=True)
# reprojecting
seaports_usa_5070 = seaports_usa.to_crs(5070)
seaports_usa_5070.head()
portName | Country Code | Latitude | Longitude | geometry | |
---|---|---|---|---|---|
0 | Maurer | United States | 40.533333 | -74.250000 | POINT (1810960.59 2154871.34) |
1 | New Harbor | United States | 43.866667 | -69.483333 | POINT (2095795.897 2613750.809) |
2 | Depere | United States | 44.450000 | -88.066667 | POINT (629077 2410752.321) |
3 | Leland | United States | 45.016667 | -85.766667 | POINT (804049.488 2490907.908) |
4 | Lazy Bay | United States | 56.900000 | -154.250000 | POINT (-3562521.023 4863569.942) |
airports.columns.to_list()
['name', 'kind', 'latitude_deg', 'longitude_deg', 'elevation_ft', 'municipality', 'geometry']
airports["kind"].unique()
array(['heliport', 'small_airport', 'seaplane_base', 'closed', 'balloonport', 'medium_airport', 'large_airport'], dtype=object)
largeAirports = airports[airports["kind"] == "large_airport"].copy()
largeAirports.head()
name | kind | latitude_deg | longitude_deg | elevation_ft | municipality | geometry | |
---|---|---|---|---|---|---|---|
12080 | Albuquerque International Sunport | large_airport | 35.039976 | -106.608925 | 5355.0 | Albuquerque | POINT (-957798.272 1384790.581) |
12099 | Joint Base Andrews | large_airport | 38.810799 | -76.866997 | 280.0 | Camp Springs | POINT (1634820.374 1918830.82) |
12198 | Hartsfield Jackson Atlanta International Airport | large_airport | 33.636700 | -84.428101 | 1026.0 | Atlanta | POINT (1063382.438 1239206.825) |
12207 | Austin Bergstrom International Airport | large_airport | 30.197535 | -97.662015 | 542.0 | Austin | POINT (-159797.998 792697.675) |
12254 | Bradley International Airport | large_airport | 41.938510 | -72.688066 | 173.0 | Hartford | POINT (1900280.232 2338281.793) |
seaports_usa_5070.set_index('portName').geometry.apply(
lambda g: largeAirports.set_index('name').geometry.distance(g) / 1000
).sort_index(axis=0).sort_index(axis=1)
name | Albuquerque International Sunport | Austin Bergstrom International Airport | Baltimore/Washington International Thurgood Marshall Airport | Bradley International Airport | Buffalo Niagara International Airport | Charlotte Douglas International Airport | Chicago Midway International Airport | Chicago O'Hare International Airport | Cincinnati Northern Kentucky International Airport | Cleveland Hopkins International Airport | ... | Savannah Hilton Head International Airport | Seattle–Tacoma International Airport | Southwest Florida International Airport | St. Louis Lambert International Airport | Syracuse Hancock International Airport | Tampa International Airport | Theodore Francis Green State Airport | Tulsa International Airport | Washington Dulles International Airport | Will Rogers World Airport |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
portName | |||||||||||||||||||||
Aberdeen | 1955.218374 | 2917.053204 | 3873.680975 | 4046.366522 | 3546.191447 | 3767.842201 | 2905.115628 | 2885.410740 | 3269.010075 | 3372.194006 | ... | 3945.844883 | 125.698724 | 4307.107879 | 2851.357290 | 3738.523782 | 4142.166582 | 4151.408990 | 2593.359769 | 3825.244484 | 2525.519305 |
Adak Naval Air Station | 5788.461930 | 6772.879313 | 7559.884368 | 7625.758811 | 7165.203085 | 7560.307173 | 6642.592359 | 6619.889403 | 7033.883229 | 7061.781686 | ... | 7774.059491 | 3957.407506 | 8171.269224 | 6666.551564 | 7324.691640 | 8003.580253 | 7722.017914 | 6460.734223 | 7524.134249 | 6397.728257 |
Ahukini Landing | 5287.844588 | 6172.371528 | 7848.675755 | 8110.785061 | 7604.813658 | 7580.724352 | 6885.145726 | 6871.043817 | 7177.174727 | 7375.130084 | ... | 7644.996873 | 4339.657223 | 7784.927824 | 6695.218455 | 7812.308890 | 7659.289325 | 8217.585891 | 6241.804343 | 7788.103226 | 6097.678805 |
Akutan Harbor | 5040.185103 | 6018.059598 | 6768.910303 | 6836.067660 | 6374.509520 | 6772.297420 | 5852.827869 | 5830.053651 | 6244.803417 | 6270.897247 | ... | 6989.146496 | 3186.314987 | 7394.037677 | 5881.287378 | 6534.614494 | 7225.344824 | 6932.628642 | 5686.393180 | 6733.229036 | 5628.155802 |
Alabaster | 2205.147234 | 1991.922126 | 797.963792 | 914.475669 | 411.518981 | 1028.071718 | 433.403612 | 430.108796 | 581.766222 | 339.135383 | ... | 1363.966504 | 3015.397111 | 1974.648738 | 829.891289 | 609.633798 | 1811.751125 | 1020.644477 | 1370.567938 | 773.020252 | 1543.380526 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
Yerbabuena Island | 1432.156139 | 2410.905455 | 3913.035559 | 4187.202731 | 3683.533360 | 3657.632657 | 2953.639914 | 2940.459782 | 3241.352545 | 3443.633502 | ... | 3750.027297 | 1074.956236 | 3974.739362 | 2761.288432 | 3893.951811 | 3829.634543 | 4293.794490 | 2333.502043 | 3852.144573 | 2205.397167 |
Yes Bay | 3013.418414 | 3926.971394 | 4487.558617 | 4559.803873 | 4093.981753 | 4506.752478 | 3578.948453 | 3555.782082 | 3974.332025 | 3990.207707 | ... | 4738.627893 | 1136.513068 | 5178.701237 | 3632.533642 | 4256.755578 | 5006.089128 | 4657.653737 | 3498.848809 | 4452.370118 | 3467.355286 |
Yonkers | 2905.291143 | 2439.560884 | 305.900399 | 150.860595 | 456.795934 | 885.668744 | 1152.949781 | 1165.897926 | 934.905327 | 663.714764 | ... | 1178.645167 | 3880.997185 | 1758.639793 | 1416.233637 | 303.732971 | 1644.976439 | 223.795205 | 1965.620776 | 374.599049 | 2141.861469 |
Yorktown | 2684.809826 | 2094.252361 | 218.155733 | 618.873195 | 666.374818 | 455.767552 | 1085.052792 | 1105.521738 | 737.442342 | 654.068323 | ... | 712.977845 | 3876.527625 | 1288.544927 | 1217.349143 | 659.058392 | 1174.759090 | 663.408168 | 1716.664358 | 208.989196 | 1884.415049 |
Youngstown | 2521.514138 | 2193.666588 | 497.206401 | 539.118412 | 43.096642 | 914.284454 | 728.934161 | 735.618728 | 663.103229 | 307.873182 | ... | 1258.659463 | 3390.485441 | 1879.831689 | 1069.698080 | 238.574028 | 1733.511931 | 645.565633 | 1630.772962 | 499.892997 | 1808.129592 |
666 rows × 68 columns
distanceMatrixKM_sea_air = seaports_usa_5070.set_index('portName').geometry.apply(
lambda g: largeAirports.set_index('name').geometry.distance(g) / 1000
).sort_index(axis=0).sort_index(axis=1)
# the mean distance from a seaport to all the large airports (sorted)
distanceMatrixKM_sea_air.mean(axis=1).sort_values(ascending=True)
0 | |
---|---|
portName | |
Tri-City Port | 1341.135681 |
Port Of Memphis | 1370.729138 |
Gary | 1379.989307 |
Buffington | 1380.664512 |
Indiana Harbor | 1381.873020 |
... | ... |
Kiska | 7134.249865 |
Tern Island | 7150.086162 |
Alcan Harbor | 7345.166116 |
Skoot Cove | 7345.662139 |
Massacre Bay | 7397.381404 |
666 rows × 1 columns
SomeStats = pd.DataFrame()
SomeStats['mean'] = distanceMatrixKM_sea_air.mean(axis=1)
SomeStats['min'] = distanceMatrixKM_sea_air.min(axis=1)
SomeStats['max'] = distanceMatrixKM_sea_air.max(axis=1)
# see some
SomeStats.head(10)
mean | min | max | |
---|---|---|---|
portName | |||
Aberdeen | 3012.229852 | 125.698724 | 4470.101933 |
Adak Naval Air Station | 6741.869448 | 3957.407506 | 8332.159431 |
Ahukini Landing | 6667.783926 | 3929.835483 | 8260.902021 |
Akutan Harbor | 5971.701458 | 3186.314987 | 7553.966404 |
Alabaster | 1534.423726 | 220.509810 | 3301.192750 |
Alameda | 2798.569117 | 8.124414 | 4345.365913 |
Albany | 1780.445335 | 117.887091 | 4098.868397 |
Alcan Harbor | 7345.166116 | 4581.360665 | 8946.524509 |
Alexandria | 1522.585107 | 5.851671 | 3897.561881 |
Alexandria Bay | 1758.358720 | 137.202449 | 3908.265427 |
distanceMatrixKM_sea_air.idxmax(axis=1)
0 | |
---|---|
portName | |
Aberdeen | Miami International Airport |
Adak Naval Air Station | Miami International Airport |
Ahukini Landing | Portland International Jetport |
Akutan Harbor | Miami International Airport |
Alabaster | San Francisco International Airport |
... | ... |
Yerbabuena Island | Portland International Jetport |
Yes Bay | Miami International Airport |
Yonkers | San Francisco International Airport |
Yorktown | San Francisco International Airport |
Youngstown | San Francisco International Airport |
666 rows × 1 columns
distanceMatrixKM_sea_air.idxmax(axis=0)
0 | |
---|---|
name | |
Albuquerque International Sunport | Massacre Bay |
Austin Bergstrom International Airport | Massacre Bay |
Baltimore/Washington International Thurgood Marshall Airport | Tern Island |
Bradley International Airport | Tern Island |
Buffalo Niagara International Airport | Tern Island |
... | ... |
Tampa International Airport | Massacre Bay |
Theodore Francis Green State Airport | Tern Island |
Tulsa International Airport | Massacre Bay |
Washington Dulles International Airport | Tern Island |
Will Rogers World Airport | Massacre Bay |
68 rows × 1 columns
# closest airport to each seaport
distanceMatrixKM_sea_air.idxmin(axis=1)
0 | |
---|---|
portName | |
Aberdeen | Seattle–Tacoma International Airport |
Adak Naval Air Station | Seattle–Tacoma International Airport |
Ahukini Landing | San Francisco International Airport |
Akutan Harbor | Seattle–Tacoma International Airport |
Alabaster | Detroit Metropolitan Wayne County Airport |
... | ... |
Yerbabuena Island | San Francisco Bay Oakland International Airport |
Yes Bay | Seattle–Tacoma International Airport |
Yonkers | LaGuardia Airport |
Yorktown | Norfolk International Airport |
Youngstown | Buffalo Niagara International Airport |
666 rows × 1 columns
# closest seaport to each airport
distanceMatrixKM_sea_air.idxmin(axis=0)
0 | |
---|---|
name | |
Albuquerque International Sunport | San Diego |
Austin Bergstrom International Airport | Port Lavaca |
Baltimore/Washington International Thurgood Marshall Airport | Baltimore |
Bradley International Airport | Norwich |
Buffalo Niagara International Airport | Buffalo |
... | ... |
Tampa International Airport | Tampa |
Theodore Francis Green State Airport | Providence |
Tulsa International Airport | Port Of Memphis |
Washington Dulles International Airport | Washington D.C. |
Will Rogers World Airport | Deer Park |
68 rows × 1 columns
!pip install mapclassify
Collecting mapclassify Downloading mapclassify-2.9.0-py3-none-any.whl.metadata (3.1 kB) Requirement already satisfied: networkx>=3.2 in /usr/local/lib/python3.11/dist-packages (from mapclassify) (3.5) Requirement already satisfied: numpy>=1.26 in /usr/local/lib/python3.11/dist-packages (from mapclassify) (2.0.2) Requirement already satisfied: pandas>=2.1 in /usr/local/lib/python3.11/dist-packages (from mapclassify) (2.2.2) Requirement already satisfied: scikit-learn>=1.4 in /usr/local/lib/python3.11/dist-packages (from mapclassify) (1.6.1) Requirement already satisfied: scipy>=1.12 in /usr/local/lib/python3.11/dist-packages (from mapclassify) (1.15.3) Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.11/dist-packages (from pandas>=2.1->mapclassify) (2.9.0.post0) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.11/dist-packages (from pandas>=2.1->mapclassify) (2025.2) Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.11/dist-packages (from pandas>=2.1->mapclassify) (2025.2) Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.11/dist-packages (from scikit-learn>=1.4->mapclassify) (1.5.1) Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.11/dist-packages (from scikit-learn>=1.4->mapclassify) (3.6.0) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.11/dist-packages (from python-dateutil>=2.8.2->pandas>=2.1->mapclassify) (1.17.0) Downloading mapclassify-2.9.0-py3-none-any.whl (286 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 286.7/286.7 kB 4.6 MB/s eta 0:00:00 Installing collected packages: mapclassify Successfully installed mapclassify-2.9.0
import geopandas as gpd
import pandas as pd
from shapely.geometry import LineString
import matplotlib.pyplot as plt
#Seleccionar un puerto y su aeropuerto más cercano
selected_port = distanceMatrixKM_sea_air.index[0]
closest_airport = distanceMatrixKM_sea_air.loc[selected_port].idxmin()
g_port = seaports_usa_5070.set_index("portName").loc[selected_port].geometry
g_airport = largeAirports.set_index("name").loc[closest_airport].geometry
pair = gpd.GeoDataFrame({
"name": [selected_port, closest_airport],
"geometry": [g_port, g_airport]
}, crs="EPSG:5070")
linea = gpd.GeoDataFrame(geometry=[LineString([g_port, g_airport])], crs="EPSG:5070")
usa = gpd.read_file("https://raw.githubusercontent.com/Derick047/PC6/main/maps/maps/usaMaps_5070.gpkg", layer="country")
# Crear mapa interactivo
m = usa.explore(color="black", style_kwds=dict(weight=1))
pair.explore(m=m, color="red", marker_kwds=dict(radius=12))
linea.explore(m=m, color="blue")
m
Exercise 2¶
Use a map of points and a map of lines from your country.
Compute the distance matrix for both.
Select one line of the distance matrix, and plot the closests and the farthest point to that line.
rivers.head()
NAME | SYSTEM | geometry | |
---|---|---|---|
0 | Rio Grande, North America | None | MULTILINESTRING ((-131925.05 320347.117, -1594... |
1 | Colorado | None | MULTILINESTRING ((-805978.284 1983620.777, -81... |
2 | Arkansas | Mississippi | MULTILINESTRING ((-865607.174 1869087.48, -847... |
3 | Mississippi | Mississippi | MULTILINESTRING ((522348.037 1777644.232, 5002... |
4 | Ohio | Mississippi | MULTILINESTRING ((1502511.356 2244437.863, 147... |
distanceMatrixKM_riv_air = rivers.set_index('NAME').geometry.apply(
lambda g: largeAirports.set_index('name').geometry.distance(g) / 1000
).sort_index(axis=0).sort_index(axis=1)
distanceMatrixKM_riv_air
name | Albuquerque International Sunport | Austin Bergstrom International Airport | Baltimore/Washington International Thurgood Marshall Airport | Bradley International Airport | Buffalo Niagara International Airport | Charlotte Douglas International Airport | Chicago Midway International Airport | Chicago O'Hare International Airport | Cincinnati Northern Kentucky International Airport | Cleveland Hopkins International Airport | ... | Savannah Hilton Head International Airport | Seattle–Tacoma International Airport | Southwest Florida International Airport | St. Louis Lambert International Airport | Syracuse Hancock International Airport | Tampa International Airport | Theodore Francis Green State Airport | Tulsa International Airport | Washington Dulles International Airport | Will Rogers World Airport |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
NAME | |||||||||||||||||||||
Arkansas | 391.950709 | 647.285434 | 1390.973447 | 1818.943166 | 1454.064175 | 914.991951 | 840.398835 | 853.207024 | 799.856120 | 1146.391019 | ... | 916.724671 | 1575.713467 | 1192.910089 | 436.919266 | 1639.738435 | 1027.172937 | 1905.583851 | 3.881391 | 1318.670591 | 148.030442 |
Colorado | 439.744899 | 1328.460807 | 2449.380252 | 2729.399126 | 2228.899578 | 2220.089581 | 1491.629992 | 1479.298433 | 1781.291076 | 1981.119320 | ... | 2349.164861 | 1414.336766 | 2668.978608 | 1309.422479 | 2441.128035 | 2506.854444 | 2835.651519 | 956.132242 | 2388.988111 | 883.185026 |
Columbia | 1599.979092 | 2540.282187 | 3410.425194 | 3558.737401 | 3063.621164 | 3348.738551 | 2454.583211 | 2433.473313 | 2833.138091 | 2907.142609 | ... | 3541.689509 | 139.957753 | 3905.785907 | 2445.900372 | 3250.416256 | 3740.153176 | 3662.739442 | 2192.776005 | 3365.707050 | 2129.700159 |
Mississippi | 1428.428136 | 606.355892 | 1088.947087 | 1418.076985 | 919.668516 | 738.591782 | 187.270631 | 170.633440 | 427.779661 | 674.658271 | ... | 816.972609 | 2083.223358 | 775.560272 | 23.220286 | 1132.933511 | 651.765388 | 1524.300601 | 512.213742 | 1018.271047 | 626.965690 |
Missouri | 1134.830001 | 1051.131200 | 1136.066643 | 1487.619443 | 1038.752683 | 890.368747 | 373.603530 | 386.650673 | 451.111475 | 738.014793 | ... | 1087.471820 | 801.247656 | 1569.514764 | 6.252284 | 1244.994683 | 1393.016848 | 1588.025518 | 362.019068 | 1069.903059 | 507.498675 |
Niagara | 2150.342944 | 1823.236197 | 427.484927 | 499.708021 | 2.952076 | 708.068181 | 371.384308 | 384.012599 | 316.864882 | 11.249394 | ... | 1047.785763 | 3133.040938 | 1663.634267 | 689.825403 | 210.374335 | 1504.612976 | 606.504482 | 1250.895396 | 417.680534 | 1428.286758 |
Ohio | 1593.766178 | 1110.186561 | 291.439876 | 408.689076 | 81.590164 | 383.806405 | 395.092520 | 419.537661 | 1.787837 | 144.159977 | ... | 716.456006 | 2949.444114 | 1329.116936 | 214.094392 | 185.601249 | 1152.605208 | 513.714950 | 625.349095 | 262.418870 | 795.386301 |
Rio Grande, North America | 11.153745 | 314.111558 | 2418.145981 | 2821.599720 | 2336.098127 | 1871.090567 | 1586.993113 | 1579.724221 | 1812.601469 | 2062.643091 | ... | 1710.925205 | 1623.820317 | 1566.510969 | 1322.221915 | 2549.739048 | 1491.706103 | 2925.772700 | 855.348640 | 2347.060698 | 716.663340 |
Snake | 984.838082 | 1853.094051 | 2824.392701 | 3045.956224 | 2539.479774 | 2674.675264 | 1848.549645 | 1831.180033 | 2191.092276 | 2331.362718 | ... | 2835.176098 | 297.295079 | 3188.130221 | 1754.416972 | 2743.598763 | 3022.950778 | 3152.627231 | 1474.877668 | 2770.733611 | 1414.706171 |
St. Clair | 1797.966800 | 1592.062152 | 655.550139 | 796.406633 | 289.988889 | 887.742252 | 32.390144 | 36.026327 | 351.753146 | 212.867078 | ... | 1185.377045 | 2310.262216 | 1755.748584 | 429.071515 | 499.648118 | 1583.222269 | 903.190332 | 966.135446 | 630.841615 | 1139.779689 |
St. Lawrence | 2521.066270 | 2194.373627 | 466.460304 | 328.577725 | 44.649547 | 916.729142 | 728.365586 | 734.957134 | 664.179577 | 308.841818 | ... | 1261.144552 | 3388.527834 | 1882.340368 | 1069.847269 | 44.322175 | 1735.937634 | 428.366884 | 1630.876640 | 489.044186 | 1808.213358 |
Yukon | 4034.041609 | 4908.311135 | 5182.810922 | 5176.528088 | 4754.961837 | 5292.775481 | 4350.872996 | 4326.395514 | 4752.511472 | 4706.427240 | ... | 5558.024564 | 2192.110289 | 6047.764925 | 4473.202879 | 4888.897528 | 5872.112548 | 5265.275147 | 4421.814994 | 5158.588895 | 4413.192804 |
12 rows × 68 columns
selected_river = distanceMatrixKM_riv_air.index[0]
distanceMatrixKM_riv_air.loc[selected_river].sort_values()
Arkansas | |
---|---|
name | |
Tulsa International Airport | 3.881391 |
Denver International Airport | 136.108124 |
Will Rogers World Airport | 148.030442 |
Memphis International Airport | 155.056902 |
Kansas City International Airport | 268.190988 |
... | ... |
John F Kennedy International Airport | 1678.706453 |
Bradley International Airport | 1818.943166 |
Theodore Francis Green State Airport | 1905.583851 |
Logan International Airport | 1964.303717 |
Portland International Jetport | 2074.760510 |
68 rows × 1 columns
closest_airport = distanceMatrixKM_riv_air.loc[selected_river].idxmin()
farthest_airport = distanceMatrixKM_riv_air.loc[selected_river].idxmax()
g_river = rivers.set_index("NAME").loc[selected_river].geometry
g_closest = largeAirports.set_index("name").loc[closest_airport].geometry
g_farthest = largeAirports.set_index("name").loc[farthest_airport].geometry
points = gpd.GeoDataFrame({
"name": ["Closest", "Farthest"],
"geometry": [g_closest, g_farthest]
}, crs="EPSG:5070")
line = gpd.GeoDataFrame({
"name": [selected_river],
"geometry": [g_river]
}, crs="EPSG:5070")
usa = gpd.read_file("https://raw.githubusercontent.com/Derick047/PC6/main/maps/maps/usaMaps_5070.gpkg", layer="country")
m = usa.explore(color="black", style_kwds=dict(weight=1))
line.explore(m=m, color="blue", style_kwds=dict(weight=3))
points.explore(m=m, color=["green", "red"], marker_kwds=dict(radius=12))
m
Exercise 3¶
Create a HULL for some set of line map.
Compute the distance matrix between the HULLS and a map of points.
Plot the HULLS and the points. Show the closest and farthest points to the HULL.
rivers[~rivers.SYSTEM.isna()]
NAME | SYSTEM | geometry | |
---|---|---|---|
2 | Arkansas | Mississippi | MULTILINESTRING ((-865607.174 1869087.48, -847... |
3 | Mississippi | Mississippi | MULTILINESTRING ((522348.037 1777644.232, 5002... |
4 | Ohio | Mississippi | MULTILINESTRING ((1502511.356 2244437.863, 147... |
6 | Missouri | Mississippi | MULTILINESTRING ((-1128037.492 2511630.909, -1... |
systems=rivers.dissolve(by='SYSTEM')
systems
geometry | NAME | |
---|---|---|
SYSTEM | ||
Mississippi | MULTILINESTRING ((-865607.174 1869087.48, -847... | Arkansas |
# format the GDF:
systems.reset_index(drop=False,inplace=True)
systems.drop(columns='NAME',inplace=True)
# we have
systems
SYSTEM | geometry | |
---|---|---|
0 | Mississippi | MULTILINESTRING ((-865607.174 1869087.48, -847... |
# polygon for each system
systems.convex_hull
0 | |
---|---|
0 | POLYGON ((675848.681 694420.882, 518374.448 79... |
# see them
systems.convex_hull.plot()
<Axes: >
systems_hulls = systems.convex_hull.to_frame()
systems_hulls['system'] = ['Mississippi']
systems_hulls.rename(columns={0: 'geometry'}, inplace=True)
systems_hulls = systems_hulls.set_geometry('geometry')
systems_hulls.crs = "EPSG:5070"
systems_hulls
geometry | system | |
---|---|---|
0 | POLYGON ((675848.681 694420.882, 518374.448 79... | Mississippi |
distanceMatrixKM_sysHull_air = systems_hulls.set_index('system').geometry.apply(
lambda g: largeAirports.set_index('name').geometry.distance(g) / 1000
).sort_index(axis=0).sort_index(axis=1)
distanceMatrixKM_sysHull_air
name | Albuquerque International Sunport | Austin Bergstrom International Airport | Baltimore/Washington International Thurgood Marshall Airport | Bradley International Airport | Buffalo Niagara International Airport | Charlotte Douglas International Airport | Chicago Midway International Airport | Chicago O'Hare International Airport | Cincinnati Northern Kentucky International Airport | Cleveland Hopkins International Airport | ... | Savannah Hilton Head International Airport | Seattle–Tacoma International Airport | Southwest Florida International Airport | St. Louis Lambert International Airport | Syracuse Hancock International Airport | Tampa International Airport | Theodore Francis Green State Airport | Tulsa International Airport | Washington Dulles International Airport | Will Rogers World Airport |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
system | |||||||||||||||||||||
Mississippi | 377.922709 | 394.299858 | 257.007933 | 408.689076 | 55.446912 | 238.456041 | 0.0 | 0.0 | 0.0 | 0.0 | ... | 427.246325 | 801.247656 | 755.472899 | 0.0 | 185.601249 | 598.962201 | 513.71495 | 0.0 | 221.362884 | 0.0 |
1 rows × 68 columns
mins = distanceMatrixKM_sysHull_air.idxmin(axis="columns")
mins
0 | |
---|---|
system | |
Mississippi | Chicago Midway International Airport |
# plotting
base = systems_hulls.explore()
largeAirports[largeAirports.name.isin(mins)].explore(m=base, color='red', marker_kwds=dict(radius=10))
largeAirports[~largeAirports.name.isin(mins)].explore(m=base, color='blue', marker_kwds=dict(radius=5))
Exercise 4¶
Select a line map and a point one.
Get the buffer for the lines, select a distance.
Keep the points that are within the buffer (you might need to play with differn distances until you show something interesting.
distanceMatrixKM_riv_air
name | Albuquerque International Sunport | Austin Bergstrom International Airport | Baltimore/Washington International Thurgood Marshall Airport | Bradley International Airport | Buffalo Niagara International Airport | Charlotte Douglas International Airport | Chicago Midway International Airport | Chicago O'Hare International Airport | Cincinnati Northern Kentucky International Airport | Cleveland Hopkins International Airport | ... | Savannah Hilton Head International Airport | Seattle–Tacoma International Airport | Southwest Florida International Airport | St. Louis Lambert International Airport | Syracuse Hancock International Airport | Tampa International Airport | Theodore Francis Green State Airport | Tulsa International Airport | Washington Dulles International Airport | Will Rogers World Airport |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
NAME | |||||||||||||||||||||
Arkansas | 391.950709 | 647.285434 | 1390.973447 | 1818.943166 | 1454.064175 | 914.991951 | 840.398835 | 853.207024 | 799.856120 | 1146.391019 | ... | 916.724671 | 1575.713467 | 1192.910089 | 436.919266 | 1639.738435 | 1027.172937 | 1905.583851 | 3.881391 | 1318.670591 | 148.030442 |
Colorado | 439.744899 | 1328.460807 | 2449.380252 | 2729.399126 | 2228.899578 | 2220.089581 | 1491.629992 | 1479.298433 | 1781.291076 | 1981.119320 | ... | 2349.164861 | 1414.336766 | 2668.978608 | 1309.422479 | 2441.128035 | 2506.854444 | 2835.651519 | 956.132242 | 2388.988111 | 883.185026 |
Columbia | 1599.979092 | 2540.282187 | 3410.425194 | 3558.737401 | 3063.621164 | 3348.738551 | 2454.583211 | 2433.473313 | 2833.138091 | 2907.142609 | ... | 3541.689509 | 139.957753 | 3905.785907 | 2445.900372 | 3250.416256 | 3740.153176 | 3662.739442 | 2192.776005 | 3365.707050 | 2129.700159 |
Mississippi | 1428.428136 | 606.355892 | 1088.947087 | 1418.076985 | 919.668516 | 738.591782 | 187.270631 | 170.633440 | 427.779661 | 674.658271 | ... | 816.972609 | 2083.223358 | 775.560272 | 23.220286 | 1132.933511 | 651.765388 | 1524.300601 | 512.213742 | 1018.271047 | 626.965690 |
Missouri | 1134.830001 | 1051.131200 | 1136.066643 | 1487.619443 | 1038.752683 | 890.368747 | 373.603530 | 386.650673 | 451.111475 | 738.014793 | ... | 1087.471820 | 801.247656 | 1569.514764 | 6.252284 | 1244.994683 | 1393.016848 | 1588.025518 | 362.019068 | 1069.903059 | 507.498675 |
Niagara | 2150.342944 | 1823.236197 | 427.484927 | 499.708021 | 2.952076 | 708.068181 | 371.384308 | 384.012599 | 316.864882 | 11.249394 | ... | 1047.785763 | 3133.040938 | 1663.634267 | 689.825403 | 210.374335 | 1504.612976 | 606.504482 | 1250.895396 | 417.680534 | 1428.286758 |
Ohio | 1593.766178 | 1110.186561 | 291.439876 | 408.689076 | 81.590164 | 383.806405 | 395.092520 | 419.537661 | 1.787837 | 144.159977 | ... | 716.456006 | 2949.444114 | 1329.116936 | 214.094392 | 185.601249 | 1152.605208 | 513.714950 | 625.349095 | 262.418870 | 795.386301 |
Rio Grande, North America | 11.153745 | 314.111558 | 2418.145981 | 2821.599720 | 2336.098127 | 1871.090567 | 1586.993113 | 1579.724221 | 1812.601469 | 2062.643091 | ... | 1710.925205 | 1623.820317 | 1566.510969 | 1322.221915 | 2549.739048 | 1491.706103 | 2925.772700 | 855.348640 | 2347.060698 | 716.663340 |
Snake | 984.838082 | 1853.094051 | 2824.392701 | 3045.956224 | 2539.479774 | 2674.675264 | 1848.549645 | 1831.180033 | 2191.092276 | 2331.362718 | ... | 2835.176098 | 297.295079 | 3188.130221 | 1754.416972 | 2743.598763 | 3022.950778 | 3152.627231 | 1474.877668 | 2770.733611 | 1414.706171 |
St. Clair | 1797.966800 | 1592.062152 | 655.550139 | 796.406633 | 289.988889 | 887.742252 | 32.390144 | 36.026327 | 351.753146 | 212.867078 | ... | 1185.377045 | 2310.262216 | 1755.748584 | 429.071515 | 499.648118 | 1583.222269 | 903.190332 | 966.135446 | 630.841615 | 1139.779689 |
St. Lawrence | 2521.066270 | 2194.373627 | 466.460304 | 328.577725 | 44.649547 | 916.729142 | 728.365586 | 734.957134 | 664.179577 | 308.841818 | ... | 1261.144552 | 3388.527834 | 1882.340368 | 1069.847269 | 44.322175 | 1735.937634 | 428.366884 | 1630.876640 | 489.044186 | 1808.213358 |
Yukon | 4034.041609 | 4908.311135 | 5182.810922 | 5176.528088 | 4754.961837 | 5292.775481 | 4350.872996 | 4326.395514 | 4752.511472 | 4706.427240 | ... | 5558.024564 | 2192.110289 | 6047.764925 | 4473.202879 | 4888.897528 | 5872.112548 | 5265.275147 | 4421.814994 | 5158.588895 | 4413.192804 |
12 rows × 68 columns
distanceMatrixKM_riv_air.loc['Mississippi'].min()
4.910878823627097
minMts = distanceMatrixKM_riv_air.loc['Mississippi'].min() * 1000 # convertir km a metros
# El buffer es un polígono
rivers[rivers.NAME == 'Mississippi'].buffer(distance=minMts)
0 | |
---|---|
3 | MULTIPOLYGON (((680702.441 695167.705, 680752.... |
bufferAroundMississippi = rivers[rivers.NAME == 'Mississippi'].buffer(distance=minMts)
bufferAsBase = bufferAroundMississippi.explore(color='red')
rivers[rivers.NAME == 'Mississippi'].explore(m=bufferAsBase, color='blue', style_kwds={'weight': 0.5})
rivers[rivers.NAME == 'Mississippi'].explore(m=bufferAsBase, color='blue', style_kwds={'weight': 0.5})
largeAirports.explore(m=bufferAsBase, color='black')
airportsWithinBuffer = largeAirports.clip(mask=bufferAroundMississippi)
airportsWithinBuffer
name | kind | latitude_deg | longitude_deg | elevation_ft | municipality | geometry | |
---|---|---|---|---|---|---|---|
13807 | Louis Armstrong New Orleans International Airport | large_airport | 29.993401 | -90.258003 | 4.0 | New Orleans | POINT (553137.075 785358.053) |
bufferAsBase = bufferAroundMississippi.explore(color='red')
rivers[rivers.NAME == 'Mississippi'].explore(m=bufferAsBase, color='blue', style_kwds={'weight': 0.5})
airportsWithinBuffer.explore(m=bufferAsBase, color='black')
# Usaando 50 km de buffer (50,000 metros)
bufferAroundMississippi = rivers[rivers.NAME == 'Mississippi'].buffer(distance=50000)
airportsWithinBuffer = largeAirports.clip(mask=bufferAroundMississippi)
airportsWithinBuffer = largeAirports.clip(mask=bufferAroundMississippi)
airportsWithinBuffer
name | kind | latitude_deg | longitude_deg | elevation_ft | municipality | geometry | |
---|---|---|---|---|---|---|---|
13807 | Louis Armstrong New Orleans International Airport | large_airport | 29.993401 | -90.258003 | 4.0 | New Orleans | POINT (553137.075 785358.053) |
13696 | Memphis International Airport | large_airport | 35.042400 | -89.976700 | 341.0 | Memphis | POINT (544546.621 1348807.58) |
14519 | St. Louis Lambert International Airport | large_airport | 38.748697 | -90.370003 | 618.0 | St Louis | POINT (484449.501 1761097.206) |
13804 | Minneapolis–Saint Paul International Airport /... | large_airport | 44.880081 | -93.221741 | 841.0 | Minneapolis | POINT (219127.901 2435577.768) |
bufferAsBase = bufferAroundMississippi.explore(color='red')
rivers[rivers.NAME == 'Mississippi'].explore(m=bufferAsBase, color='blue', style_kwds={'weight': 0.5})
airportsWithinBuffer.explore(m=bufferAsBase, color='black')
distanceMatrixKM_riv_air.min(axis=1).min()
1.7878365380436378
# Crear un buffer global alrededor de todos los ríos
minMinMts_5 = 20 * distanceMatrixKM_riv_air.min(axis=1).min() * 1000 # convertir km a metros
allMinBuffer = rivers.buffer(distance=minMinMts_5).explore(color='red')
rivers.explore(m=allMinBuffer, color='blue', style_kwds={'weight': 0.5})
riversAll_buf = rivers.buffer(distance=minMinMts_5)
riversAll_buf
0 | |
---|---|
0 | MULTIPOLYGON (((-559738.218 728637.769, -56136... |
1 | POLYGON ((-1732823.658 1266472.556, -1734067.3... |
2 | POLYGON ((-814162.18 1796946.935, -811244.259 ... |
3 | MULTIPOLYGON (((711189.522 699858.594, 711552.... |
4 | POLYGON ((643166.029 1640948.271, 643395.262 1... |
5 | POLYGON ((1045634.875 2212599.602, 1044932.36 ... |
6 | POLYGON ((-1143370.71 2468542.791, -1146597.69... |
7 | POLYGON ((-1613457.501 2568645.595, -1609207.2... |
8 | POLYGON ((1545518.634 2493975.476, 1545258.929... |
9 | POLYGON ((371894.027 2626025.122, 355260.515 2... |
10 | POLYGON ((-1796287.387 2809983.193, -1796291.6... |
11 | POLYGON ((-3712642.655 5682516.952, -3728593.6... |
allRiversWithinBuffs = largeAirports.clip(riversAll_buf)
allRiversWithinBuffs
name | kind | latitude_deg | longitude_deg | elevation_ft | municipality | geometry | |
---|---|---|---|---|---|---|---|
12080 | Albuquerque International Sunport | large_airport | 35.039976 | -106.608925 | 5355.0 | Albuquerque | POINT (-957798.272 1384790.581) |
13807 | Louis Armstrong New Orleans International Airport | large_airport | 29.993401 | -90.258003 | 4.0 | New Orleans | POINT (553137.075 785358.053) |
13696 | Memphis International Airport | large_airport | 35.042400 | -89.976700 | 341.0 | Memphis | POINT (544546.621 1348807.58) |
14419 | Louisville Muhammad Ali International Airport | large_airport | 38.174400 | -85.736000 | 501.0 | Louisville | POINT (888934.375 1730423.311) |
14662 | Tulsa International Airport | large_airport | 36.198399 | -95.888100 | 677.0 | Tulsa | POINT (9970.932 1460962.247) |
14023 | Eppley Airfield | large_airport | 41.303200 | -95.894096 | 984.0 | Omaha | POINT (8799.218 2032941.903) |
14519 | St. Louis Lambert International Airport | large_airport | 38.748697 | -90.370003 | 618.0 | St Louis | POINT (484449.501 1761097.206) |
13677 | Kansas City International Airport | large_airport | 39.297600 | -94.713898 | 1026.0 | Kansas City | POINT (109895.214 1809008.491) |
12554 | Cincinnati Northern Kentucky International Air... | large_airport | 39.048801 | -84.667801 | 896.0 | Cincinnati / Covington | POINT (969370.596 1838244.523) |
14142 | Pittsburgh International Airport | large_airport | 40.491501 | -80.232903 | 1203.0 | Pittsburgh | POINT (1319046.555 2051715.479) |
13691 | Chicago Midway International Airport | large_airport | 41.785999 | -87.752403 | 620.0 | Chicago | POINT (679722.636 2116484.657) |
12479 | Cleveland Hopkins International Airport | large_airport | 41.411701 | -81.849800 | 791.0 | Cleveland | POINT (1169552.954 2132315.39) |
12678 | Detroit Metropolitan Wayne County Airport | large_airport | 42.213770 | -83.353786 | 645.0 | Detroit | POINT (1034116.496 2203729.612) |
13735 | General Mitchell International Airport | large_airport | 42.947201 | -87.896599 | 723.0 | Milwaukee | POINT (656810.493 2244823.772) |
12347 | Buffalo Niagara International Airport | large_airport | 42.940498 | -78.732201 | 728.0 | Buffalo | POINT (1393753.292 2343029.073) |
13804 | Minneapolis–Saint Paul International Airport /... | large_airport | 44.880081 | -93.221741 | 841.0 | Minneapolis | POINT (219127.901 2435577.768) |
14114 | Portland International Airport | large_airport | 45.588699 | -122.598000 | 31.0 | Portland | POINT (-2049090.806 2799820.673) |
# Graficar los buffers de todos los ríos y los aeropuertos dentro (interactivo)
base = riversAll_buf.explore(color='yellow', style_kwds={'fillOpacity': 0.3})
allRiversWithinBuffs.explore(m=base, color='green', marker_kwds={'radius': 3})