From 74b0402ac249ac36a7a544198a96bb5e9b0faae1 Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Sun, 18 Aug 2024 11:50:56 -0400 Subject: [PATCH] Add more chapters --- _toc.yml | 6 +++++ book/labs/instructions.ipynb | 36 +++++++++++++++++++++++++ book/labs/instructions.md | 28 +++++++++++++++++++ book/python/01_get_started.md | 2 +- book/python/02_variables.ipynb | 22 +++++++++++++++ book/python/02_variables.md | 40 +++++++++++++++------------- book/python/03_data_structures.ipynb | 22 ++++++++++++++- book/python/03_data_structures.md | 23 +++++++++++++--- 8 files changed, 155 insertions(+), 24 deletions(-) create mode 100644 book/labs/instructions.ipynb create mode 100644 book/labs/instructions.md diff --git a/_toc.yml b/_toc.yml index d67edba..6a65d51 100644 --- a/_toc.yml +++ b/_toc.yml @@ -31,3 +31,9 @@ parts: maxdepth: 2 chapters: - file: book/geospatial/01_get_started + - caption: Labs + numbered: false + maxdepth: 2 + chapters: + - file: book/labs/instructions + - file: book/labs/lab_01 diff --git a/book/labs/instructions.ipynb b/book/labs/instructions.ipynb new file mode 100644 index 0000000..47279c5 --- /dev/null +++ b/book/labs/instructions.ipynb @@ -0,0 +1,36 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Submission Instructions\n", + "\n", + "1. Click on the ![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg) button at the top of each lab notebook. This will open the lab in Google Colab.\n", + "2. Copy the notebook to your Google Drive. The notebook will be saved to **My Drive -> Colab Notebooks -> Copy of lab_x.ipynb**. You can change the filename back to **lab_x.ipynb**.\n", + "\n", + " ![](https://i.imgur.com/1bxdiTz.png)\n", + "3. Follow the directions on the lab notebook and write code to complete the exercises.\n", + "4. When you are finished, Click on **Runtime -> Restart and run all**. Make sure that you notebook runs well and all code cell outputs are shown. \n", + "\n", + " ![](https://i.imgur.com/6juNLRG.png)\n", + "\n", + "5. Click on the **Share** button in the top right corner and change the notebook general access from Restrcted to **Anyone with the link**. \n", + "\n", + " ![](https://i.imgur.com/FmNZSdx.png)\n", + "6. Click on the **Copy link** button.\n", + "\n", + " ![](https://i.imgur.com/3v1DNoj.png)\n", + "7. Open Chrome Incognito mode, paste the link to the address bar and press Enter. Make sure that you can preview the notebook content. Otherwise, you might need to save a copy of the notebook from Colab to your Google Drive. \n", + "8. Copy the link and paste it to the Lab Submission page on Canvas and submit." + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/book/labs/instructions.md b/book/labs/instructions.md new file mode 100644 index 0000000..2b86eae --- /dev/null +++ b/book/labs/instructions.md @@ -0,0 +1,28 @@ +--- +jupytext: + text_representation: + extension: .md + format_name: myst + format_version: 0.13 + jupytext_version: 1.16.2 +--- + +# Submission Instructions + +1. Click on the ![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg) button at the top of each lab notebook. This will open the lab in Google Colab. +2. Copy the notebook to your Google Drive. The notebook will be saved to **My Drive -> Colab Notebooks -> Copy of lab_x.ipynb**. You can change the filename back to **lab_x.ipynb**. + + ![](https://i.imgur.com/1bxdiTz.png) +3. Follow the directions on the lab notebook and write code to complete the exercises. +4. When you are finished, Click on **Runtime -> Restart and run all**. Make sure that you notebook runs well and all code cell outputs are shown. + + ![](https://i.imgur.com/6juNLRG.png) + +5. Click on the **Share** button in the top right corner and change the notebook general access from Restrcted to **Anyone with the link**. + + ![](https://i.imgur.com/FmNZSdx.png) +6. Click on the **Copy link** button. + + ![](https://i.imgur.com/3v1DNoj.png) +7. Open Chrome Incognito mode, paste the link to the address bar and press Enter. Make sure that you can preview the notebook content. Otherwise, you might need to save a copy of the notebook from Colab to your Google Drive. +8. Copy the link and paste it to the Lab Submission page on Canvas and submit. diff --git a/book/python/01_get_started.md b/book/python/01_get_started.md index 6322bac..915640e 100644 --- a/book/python/01_get_started.md +++ b/book/python/01_get_started.md @@ -15,7 +15,7 @@ kernelspec: There are multiple ways to run Jupyter notebooks. You can either run them locally on your machine or use an online service like Google Colab or Binder. -```{code-cell} ipython3 +```{code-cell} print("Hello, Python!") ``` diff --git a/book/python/02_variables.ipynb b/book/python/02_variables.ipynb index 1549bf1..56c9585 100644 --- a/book/python/02_variables.ipynb +++ b/book/python/02_variables.ipynb @@ -310,6 +310,28 @@ "cell_type": "markdown", "id": "29", "metadata": {}, + "source": [ + "## Exercise\n", + "\n", + "1. Create a list of tuples, each representing the coordinates (latitude, longitude) of different cities you have visited.\n", + "2. Calculate the centroid of these coordinates.\n", + "3. Create a dictionary to store the centroid's latitude and longitude." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "30", + "metadata": {}, + "outputs": [], + "source": [ + "# Type your code here" + ] + }, + { + "cell_type": "markdown", + "id": "31", + "metadata": {}, "source": [ "## Conclusion\n", "\n", diff --git a/book/python/02_variables.md b/book/python/02_variables.md index 3dcf5f2..7d7ec14 100644 --- a/book/python/02_variables.md +++ b/book/python/02_variables.md @@ -25,7 +25,7 @@ In Python, a variable is a symbolic name that is a reference or pointer to an ob Let's start by creating a simple variable that represents the number of spatial points in a dataset. -```{code-cell} ipython3 +```{code-cell} num_points = 120 ``` @@ -33,7 +33,7 @@ This variable `num_points` now holds the integer value 120, which we can use in To view the value of the variable, we can use the `print()` function. -```{code-cell} ipython3 +```{code-cell} print(num_points) ``` @@ -43,42 +43,45 @@ Python supports various data types, which are essential to understand before wor **a) Integers (int):** These are whole numbers, e.g., 1, 120, -5 -```{code-cell} ipython3 +```{code-cell} num_features = 500 # Represents the number of features in a geospatial dataset ``` **b) Floating-point numbers (float):** These are numbers with a decimal point, e.g., 3.14, -0.001, 100.0 -```{code-cell} ipython3 +```{code-cell} latitude = 35.6895 # Represents the latitude of a point on Earth's surface ``` **c) Strings (str):** Strings are sequences of characters, e.g., "Hello", "Geospatial Data", "Lat/Long" -```{code-cell} ipython3 +```{code-cell} coordinate_system = "WGS 84" # Represents a commonly used coordinate system ``` **d) Booleans (bool):** Booleans represent one of two values: True or False -```{code-cell} ipython3 +```{code-cell} is_georeferenced = True # Represents whether a dataset is georeferenced or not ``` **e) Lists:** Lists are ordered collections of items, which can be of any data type. -```{code-cell} ipython3 -coordinates = [35.6895, 139.6917] # A list representing latitude and longitude of a point +```{code-cell} +coordinates = [ + 35.6895, + 139.6917, +] # A list representing latitude and longitude of a point ``` **f) Dictionaries (dict):** Dictionaries are collections of key-value pairs. -```{code-cell} ipython3 +```{code-cell} feature_attributes = { "name": "Mount Fuji", "height_meters": 3776, "type": "Stratovolcano", - "location": [35.3606, 138.7274] + "location": [35.3606, 138.7274], } ``` @@ -88,22 +91,23 @@ Now, let's do some basic operations with these variables. Adding a constant to the number of features: -```{code-cell} ipython3 +```{code-cell} num_features += 20 print("Updated number of features:", num_features) ``` Converting latitude from degrees to radians (required for some geospatial calculations): -```{code-cell} ipython3 +```{code-cell} import math + latitude_radians = math.radians(latitude) print("Latitude in radians:", latitude_radians) ``` Adding new coordinates to the list: -```{code-cell} ipython3 +```{code-cell} coordinates.append(34.0522) # Adding latitude of Los Angeles coordinates.append(-118.2437) # Adding longitude of Los Angeles print("Updated coordinates:", coordinates) @@ -111,7 +115,7 @@ print("Updated coordinates:", coordinates) Accessing dictionary elements: -```{code-cell} ipython3 +```{code-cell} mount_fuji_name = feature_attributes["name"] mount_fuji_height = feature_attributes["height_meters"] print(f"{mount_fuji_name} is {mount_fuji_height} meters high.") @@ -123,18 +127,18 @@ Let's say you are given a list of coordinates and need to calculate the centroid Example coordinates of four points (latitude, longitude): -```{code-cell} ipython3 +```{code-cell} points = [ [35.6895, 139.6917], # Tokyo [34.0522, -118.2437], # Los Angeles - [51.5074, -0.1278], # London - [48.8566, 2.3522] # Paris + [51.5074, -0.1278], # London + [48.8566, 2.3522], # Paris ] ``` Calculate the centroid: -```{code-cell} ipython3 +```{code-cell} centroid_lat = sum([point[0] for point in points]) / len(points) centroid_lon = sum([point[1] for point in points]) / len(points) centroid = [centroid_lat, centroid_lon] diff --git a/book/python/03_data_structures.ipynb b/book/python/03_data_structures.ipynb index 92a7d55..4096625 100644 --- a/book/python/03_data_structures.ipynb +++ b/book/python/03_data_structures.ipynb @@ -250,7 +250,27 @@ "id": "23", "metadata": {}, "source": [ - "### Conclusion\n", + "## Exercise\n", + "\n", + "Create a dictionary to store attributes of a geographic feature (e.g., a river or mountain). Include keys for the name, length, and location of the feature. Then, add an additional attribute (e.g., the source of the river or the height of the mountain) and print the dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24", + "metadata": {}, + "outputs": [], + "source": [ + "# Type your code here" + ] + }, + { + "cell_type": "markdown", + "id": "25", + "metadata": {}, + "source": [ + "## Conclusion\n", "\n", "Understanding and utilizing Python's data structures such as tuples, lists, sets, and dictionaries are fundamental skills in geospatial programming. These structures provide the flexibility and functionality required to manage and manipulate spatial data effectively.\n", "\n", diff --git a/book/python/03_data_structures.md b/book/python/03_data_structures.md index 425c2d9..db4f539 100644 --- a/book/python/03_data_structures.md +++ b/book/python/03_data_structures.md @@ -24,7 +24,10 @@ Tuples are immutable sequences, meaning that once a tuple is created, its elemen For example, a tuple can be used to store the coordinates of a geographic point (latitude, longitude). ```{code-cell} ipython3 -point = (35.6895, 139.6917) # Tuple representing a geographic point (latitude, longitude) +point = ( + 35.6895, + 139.6917, +) # Tuple representing a geographic point (latitude, longitude) ``` You can access elements in a tuple using indexing: @@ -42,7 +45,11 @@ Lists are ordered, mutable sequences, meaning you can change, add, or remove ele For example, you can store a list of coordinates representing a path or boundary. ```{code-cell} ipython3 -path = [(35.6895, 139.6917), (34.0522, -118.2437), (51.5074, -0.1278)] # List of tuples representing a path +path = [ + (35.6895, 139.6917), + (34.0522, -118.2437), + (51.5074, -0.1278), +] # List of tuples representing a path ``` You can add a new point to the path: @@ -94,7 +101,7 @@ For example, you can use a dictionary to store attributes of a geospatial featur city_attributes = { "name": "Tokyo", "population": 13929286, - "coordinates": (35.6895, 139.6917) + "coordinates": (35.6895, 139.6917), } # Dictionary storing attributes of a city ``` @@ -113,7 +120,15 @@ city_attributes["area_km2"] = 2191 # Adding the area of the city in square kilo print("Updated city attributes:", city_attributes) ``` -### Conclusion +## Exercise + +Create a dictionary to store attributes of a geographic feature (e.g., a river or mountain). Include keys for the name, length, and location of the feature. Then, add an additional attribute (e.g., the source of the river or the height of the mountain) and print the dictionary. + +```{code-cell} ipython3 +# Type your code here +``` + +## Conclusion Understanding and utilizing Python's data structures such as tuples, lists, sets, and dictionaries are fundamental skills in geospatial programming. These structures provide the flexibility and functionality required to manage and manipulate spatial data effectively.