<jupyter_start><jupyter_text>https://www.earthdatascience.org/courses/intro-to-earth-data-science/file-formats/use-spatial-data/use-vector-data/   Lesson 3. Introduction to Spatial Vector Data File Formats in Open Source PythonPlotting a map with coastlines and country boundaries as well as cities.<jupyter_code># Import packages
import os

import matplotlib.pyplot as plt
import geopandas as gpd
import earthpy as et<jupyter_output><empty_output><jupyter_text>The data: \Coastlines:  https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/physical/ne_50m_coastline.zip \Cities: https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_populated_places_simple.zip \Countries: https://www.naturalearthdata.com/http//www.naturalearthdata.com/download/50m/cultural/ne_50m_admin_0_countries.zip \ Coastline<jupyter_code># Open and examine coastline data
coastlines_path = os.path.join("data", 
                               "earthpy-downloads",
                               "ne_50m_coastline",
                               "ne_50m_coastline.shp")

coastlines = gpd.read_file(coastlines_path)
coastlines.head()
# Plot the data
f, ax1 = plt.subplots(figsize=(12, 6))
coastlines.plot(ax=ax1)
ax1.set(title="Global Coastline Boundaries")
plt.show()
# Examine the data
coastlines.geom_type
# Show info
coastlines.info()<jupyter_output><class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 1428 entries, 0 to 1427
Data columns (total 4 columns):
 #   Column      Non-Null Count  Dtype   
---  ------      --------------  -----   
 0   scalerank   1428 non-null   int64   
 1   featurecla  1428 non-null   object  
 2   min_zoom    1428 non-null   float64 
 3   geometry    1428 non-null   geometry
dtypes: float64(1), geometry(1), int64(1), object(1)
memory usage: 44.8+ KB<jupyter_text>Cities<jupyter_code># Create a path to the populated places shapefile
populated_places_path = os.path.join("data", 
                                     "earthpy-downloads",
                                     "ne_50m_populated_places_simple",
                                     "ne_50m_populated_places_simple.shp")

cities = gpd.read_file(populated_places_path)
cities.head()
# Examine shape type
cities.geom_type
# Examine file info
cities.info()
# Plot coastlines and cities together
f, ax1 = plt.subplots(figsize=(10, 6))
coastlines.plot(ax=ax1,
                color="black")
cities.plot(ax=ax1)

# Add a title
ax1.set(title="Map of Cities and Global Lines")
plt.show()<jupyter_output><empty_output><jupyter_text>Countries<jupyter_code># Create a path to the countries shapefile
countries_path = os.path.join("data", 
                              "earthpy-downloads",
                              "ne_50m_admin_0_countries", 
                              "ne_50m_admin_0_countries.shp")

countries = gpd.read_file(countries_path)
countries.head()
# Plot coastlines, cities, and countries together
f, ax1 = plt.subplots(figsize=(15, 10))
coastlines.plot(ax=ax1,
                color="black")
countries.plot(ax=ax1, color='lightgrey', edgecolor='black')
cities.plot(ax=ax1, color='red', markersize=5)

# Add a title
ax1.set(title="Map of Cities, Countries, and Coastlines")
plt.show()
# Color the cities according to population
f, ax1 = plt.subplots(figsize=(15, 6))
coastlines.plot(ax=ax1,
                color="black")
countries.plot(ax=ax1, color='lightgrey', edgecolor='grey')
cities.plot(ax=ax1, markersize=10, column='pop_max', legend=True)

# Add a title
ax1.set(title="Map of Cities, Countries, and Coastlines")
plt.show()
# Examine population 
for i in range(0, len(cities['pop_max'])):
    if cities['pop_max'][i] > 15000000:
        print(cities['name'][i], cities['pop_min'][i], cities['pop_max'][i])

plt.hist(cities['pop_max'])
plt.show()
# --> Few cities with very high population
# Recolor cities using quantiles
f, ax1 = plt.subplots(figsize=(15, 6))
coastlines.plot(ax=ax1,
                color="black")
countries.plot(ax=ax1, color='lightgrey', edgecolor='grey')
cities.plot(ax=ax1, markersize=10, column='pop_max', legend=True, scheme='quantiles')
ax1.set(title="Map of Cities, Countries, and Coastlines")
plt.show()<jupyter_output><empty_output><jupyter_text>Focus on one country<jupyter_code># Subset the countries data to just a single
united_states_boundary = countries.loc[countries['SOVEREIGNT']
                                       == 'United States of America']

# Notice in the plot below, that only the boundary for the USA is in the new variable
f, ax = plt.subplots(figsize=(10, 6))
united_states_boundary.plot(ax=ax)
plt.show()

# Clip the cities data to the USA boundary
# Note -- this operation may take some time to run - be patient
cities_in_usa = gpd.clip(cities, united_states_boundary)

# Plot your final clipped data
f, ax = plt.subplots()
cities_in_usa.plot(ax=ax)
ax.set(title="Cities clipped to the USA Boundary")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12141992739007665, 'fraction_numerical': 0.02581686163775716, 'mean_word_length': 3.651969981238274, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 7, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '20428', 'license': 'MIT', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1666, 'n_words': 368, 'path': 'Intro-Textbook/Chapter5_Lesson3_Vector_Data.ipynb', 'repo_name': 'alma-pi/Earth-Science', 'size': '4958'}