Lab 5#
Exercise 1: Calculating Distances with Functions#
Define a function
calculate_distancethat takes two geographic coordinates (latitude and longitude) and returns the distance between them using the Haversine formula.Use this function to calculate the distance between multiple pairs of coordinates.
Exercise 2: Batch Distance Calculation#
Create a function
batch_distance_calculationthat accepts a list of coordinate pairs and returns a list of distances between consecutive pairs.Test the function with a list of coordinates representing several cities.
Exercise 3: Creating and Using a Point Class#
Define a
Pointclass to represent a geographic point with attributeslatitude,longitude, andname.Add a method
distance_tothat calculates the distance from one point to another.Instantiate several
Pointobjects and calculate the distance between them.
Exercise 4: Reading and Writing Files#
Write a function
read_coordinatesthat reads a file containing a list of coordinates (latitude, longitude) and returns them as a list of tuples.Write another function
write_coordinatesthat takes a list of coordinates and writes them to a new file.Ensure that both functions handle exceptions, such as missing files or improperly formatted data.
Exercise 5: Processing Coordinates from a File#
Create a function that reads coordinates from a file and uses the
Pointclass to createPointobjects.Calculate the distance between each consecutive pair of points and write the results to a new file.
Ensure the function handles file-related exceptions and gracefully handles improperly formatted lines.
# Create a sample coordinates.txt file
sample_data = """35.6895,139.6917
34.0522,-118.2437
51.5074,-0.1278
-33.8688,151.2093
48.8566,2.3522"""
output_file = "coordinates.txt"
try:
with open(output_file, "w") as file:
file.write(sample_data)
print(f"Sample file '{output_file}' has been created successfully.")
except Exception as e:
print(f"An error occurred while creating the file: {e}")
Sample file 'coordinates.txt' has been created successfully.
Exercise 6: Exception Handling in Data Processing#
Modify the
batch_distance_calculationfunction to handle exceptions that might occur during the calculation, such as invalid coordinates.Ensure the function skips invalid data and continues processing the remaining data.
Exercise 7: NumPy Array Operations and Geospatial Coordinates#
In this exercise, you will work with NumPy arrays representing geospatial coordinates (latitude and longitude) and perform basic array operations.
Create a 2D NumPy array containing the latitude and longitude of the following cities: Tokyo (35.6895, 139.6917), New York (40.7128, -74.0060), London (51.5074, -0.1278), and Paris (48.8566, 2.3522).
Convert the latitude and longitude values from degrees to radians using np.radians().
Calculate the element-wise difference between Tokyo and the other cities’ latitude and longitude in radians.
Exercise 8: Pandas DataFrame Operations with Geospatial Data#
In this exercise, you’ll use Pandas to load and manipulate a dataset containing city population data, and then calculate and visualize statistics.
Load the world cities dataset from this URL using Pandas: opengeos/datasets
Display the first 5 rows and check for missing values.
Filter the dataset to only include cities with a population greater than 1 million.
Group the cities by their country and calculate the total population for each country.
Sort the cities by population in descending order and display the top 10 cities.
Exercise 9: Creating and Manipulating GeoDataFrames with GeoPandas#
This exercise focuses on creating and manipulating GeoDataFrames, performing spatial operations, and visualizing the data.
Load the New York City building dataset from the GeoJSON file using GeoPandas: opengeos/datasets
Create a plot of the building footprints and color them based on the building height (use the
height_MScolumn).Create an interactive map of the building footprints and color them based on the building height (use the
height_MScolumn).Calculate the average building height (use the
height_MScolumn).Select buildings with a height greater than the average height.
Save the GeoDataFrame to a new GeoJSON file.
Exercise 10: Combining NumPy, Pandas, and GeoPandas#
This exercise requires you to combine the power of NumPy, Pandas, and GeoPandas to analyze and visualize spatial data.
Use Pandas to load the world cities dataset from this URL: opengeos/datasets
Filter the dataset to include only cities with latitude values between -40 and 60 (i.e., cities located in the Northern Hemisphere or near the equator).
Create a GeoDataFrame from the filtered dataset by converting the latitude and longitude into geometries.
Reproject the GeoDataFrame to the Mercator projection (EPSG:3857).
Calculate the distance (in meters) between each city and the city of Paris.
Plot the cities on a world map, coloring the points by their distance from Paris.
Submission Requirements#
Complete the exercises above and and upload the notebook to your GitHub repository. Make sure the notebook has a Colab badge at the top so that it can be easily opened in Google Colab. Submit the URL of the notebook to Canvas.