Skip to content

Commit

Permalink
Updated notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-patrignani committed Apr 4, 2024
1 parent 9f5ba8f commit fd7a612
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 106 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file not shown.
Binary file modified datasets/spatial/county_ks_vegetation.tif
Binary file not shown.
94 changes: 45 additions & 49 deletions docs/exercises/raster_files.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/exercises/raster_files_files/figure-html/cell-13-output-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions docs/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -2475,21 +2475,21 @@
"href": "exercises/raster_files.html#band-properties",
"title": "85  Raster files",
"section": "Band properties",
"text": "Band properties\nIt is always useful to explore the number of bands in a raster, the data type, and dimensions of each band. The nodata and nodatavals indicate how the dataset encodes missing values or values outside a specific regions (e.g. pixels that fall on the ocean).\n\n# Print band attributes\nprint('Data type:', raster.dtypes)\nprint('Band width (number columns):', raster.width)\nprint('Band height (number of rows):', raster.height)\nprint('Band indexes:', raster.indexes)\nprint('No data placeholder:',raster.nodatavals)\n\nData type: ('uint8', 'uint8', 'uint8')\nBand width (number columns): 2733\nBand height (number of rows): 1465\nBand indexes: (1, 2, 3)\nNo data placeholder: (None, None, None)"
"text": "Band properties\nIt is always useful to explore the number of bands in a raster, the data type, and dimensions of each band. The nodata and nodatavals indicate how the dataset encodes missing values or values outside a specific regions (e.g. pixels that fall on the ocean).\n\n# Print band attributes\nprint('Data type:', raster.dtypes)\nprint('Band width (number columns):', raster.width)\nprint('Band height (number of rows):', raster.height)\nprint('Band indexes:', raster.indexes)\nprint('No data placeholder:',raster.nodatavals)\n\nData type: ('uint8', 'uint8', 'uint8')\nBand width (number columns): 2877\nBand height (number of rows): 1602\nBand indexes: (1, 2, 3)\nNo data placeholder: (None, None, None)"
},
{
"objectID": "exercises/raster_files.html#raster-bounding-box",
"href": "exercises/raster_files.html#raster-bounding-box",
"title": "85  Raster files",
"section": "Raster bounding box",
"text": "Raster bounding box\nAnother important attribute to have at hand is the bounding box. This information gives us a precise idea of the limits of the map and also shows whther we are working in geographic or projected coordinates.\n\n# Find map boundaries\nprint(raster.bounds)\n\nBoundingBox(left=-102.14937187828008, bottom=36.900982382252316, right=-94.47619487827995, top=40.12014738225228)"
"text": "Raster bounding box\nAnother important attribute to have at hand is the bounding box. This information gives us a precise idea of the limits of the map and also shows whther we are working in geographic or projected coordinates.\n\n# Find map boundaries\nprint(raster.bounds)\n\nBoundingBox(left=-102.30474587359757, bottom=36.73867052309612, right=-94.22721987359756, top=40.25910252309606)"
},
{
"objectID": "exercises/raster_files.html#raster-pixel-to-geographic-coordinates",
"href": "exercises/raster_files.html#raster-pixel-to-geographic-coordinates",
"title": "85  Raster files",
"section": "Raster pixel to geographic coordinates",
"text": "Raster pixel to geographic coordinates\nAn easy way to access the pixel coordiantes given the row and column of the raster. The offset allow us to specify the starting point. Selecting the Upper-Left corner makes things easy because that is also the beginning of the raster array in terms of rows and columns. So, if we request the geographic coordinates of row=0 and column=0 with offset='ul', we should obtain the coordinates of the top-left corner of the boundaing box.\n\n# Get geographic coordinate for specific row and column.\nraster.xy(row=0, col=0, offset='ul') # center by default. ul, ur, ll, lr.\n\n(-102.14937187828008, 40.12014738225228)\n\n\n\n# Another example\nraster.xy(row=60, col=1200, offset='ul') # center by default. ul, ur, ll, lr.\n\n(-98.78024915599683, 39.98830444709869)"
"text": "Raster pixel to geographic coordinates\nAn easy way to access the pixel coordiantes given the row and column of the raster. The offset allow us to specify the starting point. Selecting the Upper-Left corner makes things easy because that is also the beginning of the raster array in terms of rows and columns. So, if we request the geographic coordinates of row=0 and column=0 with offset='ul', we should obtain the coordinates of the top-left corner of the boundaing box.\n\n# Get geographic coordinate for specific row and column.\nraster.xy(row=0, col=0, offset='ul') # center by default. ul, ur, ll, lr.\n\n(-102.30474587359757, 40.25910252309606)\n\n\n\n# Another example\nraster.xy(row=60, col=1200, offset='ul') # center by default. ul, ur, ll, lr.\n\n(-98.93560051384783, 40.12725113732827)"
},
{
"objectID": "exercises/raster_files.html#explore-raster-bands",
Expand All @@ -2510,7 +2510,7 @@
"href": "exercises/raster_files.html#retrive-bands-from-a-specific-point",
"title": "85  Raster files",
"section": "Retrive bands from a specific point",
"text": "Retrive bands from a specific point\nImagine we are interested in knowing the information for the different bands at a specific point in the map. The index() method can help us retrieving this information by providing us with the row and column of a specific pair of geographic coordinates.\n\n# Add location to map\nmhk_lat = 39.193608\nmhk_lon = -96.584593\n\nplt.figure(figsize=(12,8))\nplt.scatter(mhk_lon, mhk_lat, marker='x', color='r', s=100) # this line needs to precede the raster.\nshow(raster, title='Terra MODIS 7-2-1 2019-09-05')\nplt.show()\n\n\n\n\n\n# Retrieve RGB values for specified location\npy,px = raster.index(mhk_lon, mhk_lat)\nprint('Row =',py, 'Col=',px)\nmhk_red = raster.read(1)[py,px]\nmhk_green = raster.read(2)[py,px]\nmhk_blue = raster.read(3)[py,px]\n\nprint('R=',mhk_red, 'G=',mhk_green, 'B=',mhk_blue)\n\nRow = 421 Col= 1982\nR= 136 G= 137 B= 104\n\n\nAll three RGB values can be obtained with a single statement: raster.read()[:,py,px]\n\n# Get a small area around selected point\nmhk_pixel = raster.read(window=rasterio.windows.Window(px,py,3,3))\n\n# Display window\nshow(mhk_pixel);\n\n\n\n\n\n# Create an NxN window\nN = 100\nwindow = rasterio.windows.Window(px-N//2, py-N//2, N, N)\nprint(window)\n\nWindow(col_off=1932, row_off=371, width=100, height=100)\n\n\n\n# Read the data in the window\nclip = raster.read(window=window)\n\n# Display window\nshow(clip);"
"text": "Retrive bands from a specific point\nImagine we are interested in knowing the information for the different bands at a specific point in the map. The index() method can help us retrieving this information by providing us with the row and column of a specific pair of geographic coordinates.\n\n# Add location to map\nmhk_lat = 39.193608\nmhk_lon = -96.584593\n\nplt.figure(figsize=(12,8))\nplt.scatter(mhk_lon, mhk_lat, marker='x', color='r', s=100) # this line needs to precede the raster.\nshow(raster, title='Terra MODIS 7-2-1 2019-09-05')\nplt.show()\n\n\n\n\n\n# Retrieve RGB values for specified location\npy,px = raster.index(mhk_lon, mhk_lat)\nprint('Row =',py, 'Col=',px)\nmhk_red = raster.read(1)[py,px]\nmhk_green = raster.read(2)[py,px]\nmhk_blue = raster.read(3)[py,px]\n\nprint('R=',mhk_red, 'G=',mhk_green, 'B=',mhk_blue)\n\nRow = 484 Col= 2037\nR= 86 G= 142 B= 58\n\n\nAll three RGB values can be obtained with a single statement: raster.read()[:,py,px]\n\n# Get a small area around selected point\nmhk_pixel = raster.read(window=rasterio.windows.Window(px,py,3,3))\n\n# Display window\nshow(mhk_pixel);\n\n\n\n\n\n# Create an NxN window\nN = 100\nwindow = rasterio.windows.Window(px-N//2, py-N//2, N, N)\nprint(window)\n\nWindow(col_off=1987, row_off=434, width=100, height=100)\n\n\n\n# Read the data in the window\nclip = raster.read(window=window)\n\n# Display window\nshow(clip);"
},
{
"objectID": "exercises/raster_files.html#east-west-transect",
Expand All @@ -2524,7 +2524,7 @@
"href": "exercises/raster_files.html#use-geopandas-to-clip-area",
"title": "85  Raster files",
"section": "Use GeoPandas to clip area",
"text": "Use GeoPandas to clip area\nIn this case we will use a polygon to clip a portion of the raster map.\n\n# Import additional modules\nimport geopandas as gpd\n\n\n# Read boundary of Riley county, KS\ncounties = gpd.read_file('../datasets/spatial/us_county_5m.geojson')\n\n# Select Riley county\nidx = counties['NAME'] == 'Geary'\ncounty = counties[idx]\n\n\n# Clip the raster to the polygon extent\nclipped_raster, clipped_transform = mask(raster, county.geometry, crop=True)\n\n# Inspect dimensions of clipped raster\nprint(clipped_raster.shape)\n\n# Note that Rasterio places the number of bands in the first axis\n\n(3, 161, 165)\n\n\n\n# Show clipped raster image\nshow(clipped_raster)\nplt.show()\n\n\n\n\n\n# Set background of pixels as white\n\n# Get the clipped raster as a numpy array\nclipped_raster = clipped_raster.transpose(1, 2, 0)\n\n# Replace nodata values with 255 (white)\nidx_mask = np.sum(clipped_raster, axis=2)==0\nclipped_raster[idx_mask,:] = 255\n\n\n# Restore order of dimensions \nclipped_raster = clipped_raster.transpose(2, 0, 1)\nprint(clipped_raster.shape)\n\n(3, 161, 165)\n\n\n\n# Update metadata of clipped raster\nnew_metadata = raster.meta.copy()\nnew_metadata.update({\n 'height': clipped_raster.shape[1],\n 'width': clipped_raster.shape[2],\n 'transform': clipped_transform\n})\n\n\n# Save the clipped raster to a new file\nclipped_raster_filename = '../datasets/spatial/county_ks_vegetation.tif'\nwith rasterio.open(clipped_raster_filename, 'w', **new_metadata) as f:\n f.write(clipped_raster)\n \n\n\n# Read saved clipped raster\nclipped_raster = rasterio.open(clipped_raster_filename)\n\n\n# Show clipped raster\n\nfig,ax = plt.subplots(figsize=(5,4))\nshow(clipped_raster, ax=ax)\ncounty.plot(ax=ax, facecolor='None', edgecolor='k')\nplt.title('Geary county')\nplt.xlabel('Longitude')\nplt.ylabel('Latitude')\nplt.show()"
"text": "Use GeoPandas to clip area\nIn this case we will use a polygon to clip a portion of the raster map.\n\n# Import additional modules\nimport geopandas as gpd\n\n\n# Read boundary of Riley county, KS\ncounties = gpd.read_file('../datasets/spatial/us_county_5m.geojson')\n\n# Select Riley county\nidx = counties['NAME'] == 'Geary'\ncounty = counties[idx]\n\n\n# Clip the raster to the polygon extent\nclipped_raster, clipped_transform = mask(raster, county.geometry, crop=True)\n\n# Inspect dimensions of clipped raster\nprint(clipped_raster.shape)\n\n# Note that Rasterio places the number of bands in the first axis\n\n(3, 161, 166)\n\n\n\n# Show clipped raster image\nshow(clipped_raster)\nplt.show()\n\n\n\n\n\n# Set background of pixels as white\n\n# Get the clipped raster as a numpy array\nclipped_raster = clipped_raster.transpose(1, 2, 0)\n\n# Replace nodata values with 255 (white)\nidx_mask = np.sum(clipped_raster, axis=2)==0\nclipped_raster[idx_mask,:] = 255\n\n\n# Restore order of dimensions \nclipped_raster = clipped_raster.transpose(2, 0, 1)\nprint(clipped_raster.shape)\n\n(3, 161, 166)\n\n\n\n# Update metadata of clipped raster\nnew_metadata = raster.meta.copy()\nnew_metadata.update({\n 'height': clipped_raster.shape[1],\n 'width': clipped_raster.shape[2],\n 'transform': clipped_transform\n})\n\n\n# Save the clipped raster to a new file\nclipped_raster_filename = '../datasets/spatial/county_ks_vegetation.tif'\nwith rasterio.open(clipped_raster_filename, 'w', **new_metadata) as f:\n f.write(clipped_raster)\n \n\n\n# Read saved clipped raster\nclipped_raster = rasterio.open(clipped_raster_filename)\n\n\n# Show clipped raster\n\nfig,ax = plt.subplots(figsize=(5,4))\nshow(clipped_raster, ax=ax)\ncounty.plot(ax=ax, facecolor='None', edgecolor='k')\nplt.title('Geary county')\nplt.xlabel('Longitude')\nplt.ylabel('Latitude')\nplt.show()"
},
{
"objectID": "exercises/inverse_distance_weighting.html",
Expand Down
105 changes: 53 additions & 52 deletions exercises/raster_files.ipynb

Large diffs are not rendered by default.

0 comments on commit fd7a612

Please sign in to comment.