Explore coral and sponge distribution

In this section, you will explore some of the factors that control the distribution of corals and sponges in the waters around Catalina Island. First, you will download data from the National Oceanic and Atmospheric Administration (NOAA), select an area of interest, and prepare the data for exploration.

Download deep sea coral and sponge data

The NOAA data shows the known coral and sponge locations from around the world. Understanding how to download and work with public data is an important aspect of learning how to use GIS. You will start by downloading two datasets from the NOAA’s Deep Sea Coral Research and Technology Program. First, you’ll go to the Deep-Sea Coral and Sponge Map Portal. This site contains data that is updated quarterly and is useful for studying oceanic species. The steps in this section will show you how to download species for a specific area of the world. Since your study area is around Catalina Island, off the coast of California, you will download data for the Northern Pacific.

  1. On your computer, open File Explorer. In a location on your computer that is easy to find, create a folder and name it DeepSeaCoralSponge.

    In this lesson, the data is shown at C:\Lessons\DeepSeaCoralSponge\. You can use a different folder, but you will need to adjust the paths in the instructions that follow.

  2. Go to https://www.ncei.noaa.gov/maps/deep-sea-corals/mapSites.htm.

    The NOAA Deep-Sea Coral & Sponge Map Portal contains a map created and managed by the NOAA Deep Sea Coral Research and Technology Program. The data for this map is updated quarterly.

    NOAA Deep-Sea Coral and Sponge Map Portal page

  3. At the top of the page, click Data Query.

    Data Query on the ribbon of the portal page

  4. In the Data Query pane, in the Region section, click the drop-down menu and click North Pacific.

    North Pacific selected for Region in the Data Query pane

  5. Clear Search.

    Search button on the Data Query pane

    The search may take a few minutes to complete.

  6. Once the data has finished loading, at the top of the page, click Data Download.

    Data Download button on the ribbon of the portal page

    The data download pane appears.

  7. Accept the default values and click Customized Download.

    Customized Download button

    The customized download page opens with the data for the North Pacific region.

  8. Scroll down and click File type.
  9. For File type, choose the option that begins with .csv.

    File type set to .csv

  10. Click Submit.

    It may require several minutes to download the data.

  11. Locate the downloaded .csv file on your computer.
    Tip:

    The name for the file begins with deep_sea_corals_ followed by a string of characters, for example deep_sea_corals_f6a2_cb18_f6f1.csv.

  12. Drag the .csv file to your DeepSeaCoralSponge folder you created in an earlier step.

You have downloaded deep sea coral data for the North Pacific region. Next, you will download coastal relief models for your project area.

Download NOAA coastal relief models

The NOAA Coastal Relief Models are public raster datasets that integrate bathymetry and coastal terrain. You will download the model for Southern California.

  1. Go to the NCEI Geoportal page.

    Since your study area is Catalina Island, you’ll download data for the Southern California region.

  2. In the search box, type U.S. Coastal Relief Model - Southern California Version 2 and press Enter.

    U.S. Coastal Relief Model for Southern California searched in the NCEI Geoportal website

  3. In the search results, click U.S. Coastal Relief Model - Southern California Version 2.

    Search result for the U.S. Coastal Relief Model - Southern California Version 2

  4. Scroll down to the Download Data section and click the second Download NetCDF File link.

    NetCDF download link

    The crm_socal_3as_vers2.nc file downloads, which is the 3 arc second (90 meter) resolution version of the data. The other Download NetCDF File link points to the 1 arc second (30 meter) resolution version of the data. For the purposes of this lesson, the 90 meter resolution data is adequate.

    Tip:

    This is a 512 MB file. Depending on your connection speed, it may take several minutes to download to your computer.

  5. When the download is complete, locate the crm_socal_3as_vers2.nc file on your computer and drag it to your project folder.
  6. Right-click the crm_socal_3as_vers2.nc file and rename it SouthernCABathymetry.nc.

    The deep_sea_corals_ .csv file contains a row that explains the units of the latitude and longitude and depth columns. While this is useful for a researcher viewing the table as a spreadsheet, it will cause problems when you convert the table to point features. You’ll open the .csv file in a spreadsheet editor and delete this row.

  7. Open the deep_sea_corals_ .csv file in Microsoft Excel (or the spreadsheet editor of your choice).

    The second row contains text description of the units for columns K, L, M, and T. The data values that you want to import are in the rows after row 2.

    Second row in the csv file

  8. Click the row number for row 2 to select the row.

    Row number selected for row 2

  9. Right-click the row and click Delete.

    Delete in the right-click menu for the selected row

    The row is deleted. Now the data table is formatted correctly and may be imported to a geodatabase.

    Row has been deleted and the .csv file is ready to be imported as a geodatabase

  10. Click the Save button and close the file.

You have downloaded and adjusted the formatting of public data from NOAA so it can be imported to the geodatabase. Next, you will add the data to ArcGIS Pro, prepare the NetCDF data for analysis, and create points from the geodatabase table.


Import the coastal relief model

The datasets you downloaded are only a small part of the data that NOAA publishes. In this section, you will learn how to use the costal relief models to create derived rasters for environment around Santa Catalina Island. The derived information will then be incorporated into the coral and sponge location database.

Add data and change symbology

Next, you'll start a new project in ArcGIS Pro and add data to the project.

  1. Open ArcGIS Pro.
  2. In the New Project section, click Map.
  3. Name the project Deep Sea Coral and Sponge Database.

    New project named Deep Sea Coral and Sponge Database

  4. Click OK.
    Note:

    If the Catalog pane is not open, click the View tab and click Catalog Pane.

  5. In the Catalog pane, expand Databases.

    Expand arrow for Databases on the Catalog pane

    When you created the project, ArcGIS Pro created a new geodatabase to store project data. You will import the CSV table to this geodatabase.

  6. Right-click Deep Sea Coral and Sponge Database, point to Import, and click Table(s).

    Import tables for the Deep Sea Coral and Sponges database

  7. For Input Table, browse to and choose the .csv file you downloaded and edited earlier in the lesson.

    The CSV file that was downloaded and edited earlier in the lesson added to Input Table in the Table To Geodatabase tool pane

  8. Click Run.

    The tool will run for a minute or two as the CSV table is imported to the geodatabase.

  9. In the Catalog pane, expand the geodatabase, and drag the table onto the map.

    Drag table onto the map.

    The table is added to the Standalone Tables section of the Contents pane.

  10. In the Contents pane, right-click the deep_sea_corals table and click Display XY Data.

    Display XY Data for the deep_sea_corals under Standalone Tables

  11. In the Display XY Data pane, for Output Feature Class, type CoralandSpongeLocations.

    The tool detects that the table contains longitude and latitude fields and assigns them to the X Field and Y Field input parameters.

  12. Click OK.

    Display XY Data pane

    The tool runs for about a minute. When it finishes, the new CoralandSpongeLocations feature class is added to the map.

    CoralandSpongeLocations feature class added to the Contents pane and map

    The deep_sea_corals table is no longer needed.

  13. In the Standalone Tables section, right-click deep_sea_corals table and click Remove.

    Remove for the deep_sea_corals table

    Next, you will edit the symbology for the CoralandSpongeLocations layer.

  14. Right-click the CoralandSpongeLocations layer and click Symbology.

    Symbology for the CoralandSpongeLocations layer

    The Symbology pane appears.

  15. In the Symbology pane, click the Primary symbology drop-down list and click Unique Values.

    Unique Values for Primary symbology in the Symbology pane for the CoralandSpongeLocations feature layer

  16. Click the Field 1 drop-down list and click VernacularNameCategory.

    Field 1 set to VernacularNameCategory

  17. Close the Symbology pane.
  18. On the Quick Access Toolbar, click the Save button to save the project.

    Save on the Quick Access Toolbar

Set the coordinate system for your map

You are interested in studying factors controlling the distribution of corals and sponges in the waters around Catalina Island, off of the coast of California. You will set the coordinate system of the map to a projected coordinate system designed for California to ensure accuracy.

  1. In the Contents pane, double-click Map.

    The Map Properties window appears.

  2. In the Map Properties window, click Coordinate Systems.

    Coordinate Systems tab on the Map Properties window

  3. In the XY Coordinate Systems Available box, type NAD 1983 California (Teale) Albers and press Enter.
  4. Expand Projected Coordinated System, expand State Systems, and click NAD 1983 California (Teale) Albers (Meters).

    NAD 1983 California (Teale) Albers (Meters) searched and selected

  5. Click OK.

    The coordinate system of the map changes to the NAD 1983 California (Teale) Albers coordinate system and the XY units of the map are set to meters.

    Map coordinate system updated to NAD 1983 California (Teale) Albers

    This projection is optimized to support area calculations for the state of California. You can see that it distorts the rest of the world, but your area of interest is in the less-distorted area, so this is not a problem. You will extract the points that are relevant to your study, so the distortion of points in distant parts of the Pacific Ocean is also not a problem.

Select the data for the project

Now that you have downloaded the data for the entire North Pacific, you set an area of interest (AOI) for your project and then clip the data to this area. This allows you to work with a more manageable subset of the data.

  1. In the Catalog pane, click Portal and click ArcGIS Online. In the search bar, type California_Detail_Learn_ArcGIS.
    Note:

    To open the Catalog pane, on the ribbon, click the View tab, and in the Windows group, click Catalog Pane.

    Portal and ArcGIS Online tab in the Catalog pane

  2. In the search results, drag the California_Detail_Learn_ArcGIS layer, owned by Learn_ArcGIS, onto the map.
  3. In the Contents pane, right-click dtl_st_trim, click Rename, and name the layer California.
  4. On the ribbon, click the Map tab, and in the Inquiry group, click Locate.

    Locate in the Inquiry group on the Map tab.

  5. In the Locate pane, on the search bar, type Catalina Island, and click Catalina Island, CA, USA.

    Search result for Catalina Island in the Locate pane

    The map zooms to Catalina Island.

  6. In the lower corner of the map window, click the drop-down scale indicator and choose 1:500,000.

    Scale indicator set to 1:500,000

  7. On the ribbon, click the Map tab, and in the Navigate section, click Explore.

    Explore in the Navigate group on the Map tab

  8. Click the map and pan the map so that Catalina Island is in the center.
  9. In the Selection group, click Select.

    Select in the Selection group on the Map tab

    To only select the coral and sponge points, you will make the CoralandSpongeLocations layer the only selectable layer.

  10. In the Contents pane, right-click the CoralandSpongeLocations layer, point to Selection, and click Make this the only selectable layer.
  11. With the Select tool activated, drag a box around the coral and sponge points in the waters surrounding Catalina Island.

    Box dragged around coral and sponge points surrounding Catalina Island with the Select tool activated

    The selected points highlight in a cyan color to indicate that the points are selected.

    Selected points highlight on the map.

  12. In the Contents, right-click CoralandSpongeLocations, point to Data, and click Export Features.

    Export Features for Data options for the CoralandSpongeLocations layer

  13. In the Export Features window, for Output Feature Class, type CoralandSpongeCatalina.

    Output Feature Class set in the Export Features window.

    The tool has a message indicating that the input has a selection. Depending on the area you selected, you may have more or fewer points selected. Exactly how many is not important, but approximately 6,000 is a good number.

    Before you run the tool, you will set the projection of the output feature class.

  14. On the Export Features tool, click the Environments tab.
  15. In the Output Coordinates section, click the Output Coordinate System drop-down list and click Current Map [Map].

    Output Coordinate System set to Current Map [Map] on the Export Features window.

  16. Click OK.

    The tool exports the selected features for your AOI around Catalina Island. If you had more than one study area, you could use the same method of setting the coordinate system to an appropriate one for analysis at that location, zooming to the location, selecting features, and exporting them using the map coordinate system to create other datasets to analyze and compare.

  17. In the Contents pane, uncheck the CoralandSpongeLocations layer.

    Now only the features you selected around Catalina island are displayed.

    California corals

  18. Save the project.
    Tip:

    You can also save the project by pressing Ctrl+S.

Create a bounding rectangle

Next, you'll create a bounding rectangle for your AOI and import the coastal relief model as a raster.

  1. If necessary, open your project in ArcGIS Pro.
  2. On the ribbon, click the Analysis tab and click Tools.

    Tools in the Geoprocessing group on the Analysis tab

    The Geoprocessing pane appears.

  3. On the Geoprocessing pane, in the search bar, type Minimum Bounding Geometry. In the list of results, click the Minimum Bounding Geometry tool.

    Minimum Bounding Geometry tool

  4. In the Minimum Bounding Geometry tool pane, set the following parameters:
    • For Input Features, choose CoralandSpongeCatalina.
    • For Output Features, type CatalinaBoundary.
    • For Geometry Type, choose Envelope.

    Parameters set for the Minimum Bounding Geometry tool pane

  5. Click Run.
  6. In the Contents pane, click the symbol for the CatalinaBoundary layer.

    Symbol for CatalinaBoundary in the Contents pane

  7. In the Symbology pane, click the Gallery tab and choose Black Outline 2pt.

    Black Outline 2pt in the Gallery tab on the Symbology pane

    Now you have a polygon that represents your study area and a set of points for coral and sponge observations within it.

    Catalina boundary polygon

Add the bathymetry data

The Coastal Relief Model is in NetCDF format. NetCDF is a data format used for sharing array-oriented scientific data and is commonly used in oceanography. You will create a raster dataset from the NetCDF file for your analysis.

  1. On the geoprocessing pane, click the back arrow. Type Make NetCDF Raster Layer and click Make NetCDF Raster Layer to open the tool.

    Search and open the Make NetCDF Raster Layer tool

  2. In the Make NetCDF Raster Layer tool pane, for the Input netCDF File, click the Browse button.
  3. In the Input netCDF file window, browse to the C:\DeepSeaCoralSponge folder (or the location you saved the crm_socal_3as_vers2.nc file).

    SouthernCABathymetry.nc file selected in the Input netCDF File window

  4. Click SouthernCABathymetry.nc and click OK.

    The Variable parameter is set to Band1, and the X and Y Dimension values are set to lon and lat.

    Parameters populate the Make NetCDF Raster Layer tool.

  5. Click Run.
    Tip:

    This step may take a few minutes to process.

    Bathymetry layer

    The raster layer appears for the entire Southern California area.

  6. Save your project.

Prepare layers for analysis

To be able to accurately analyze the data that has been downloaded, you will project and clip the bathymetry layer. It is best to do analysis on terrain or bathymetry rasters that have been projected into a projected coordinate systems with units that match their elevation or depth units. In this case, the depths are in meters. You will project the raster to the projected California coordinate system that you set for the data frame and for the CoralSpongeCatalina layer. This coordinate system uses meters as the linear units, which is the same units as the depth values.

  1. On the Geoprocessing pane, search for and open the Project Raster tool.
  2. On the Project Raster pane, for Input Raster, choose Band1_Layer and for Output Raster Dataset, type CatalinaBathymetry.
  3. For the Output Coordinate System, click the drop-down menu and click Current Map [Map].

    Output Coordinate System set to Current Map [Map]

    The coordinate system updates to match that of the data frame.

    Coordinate system updates

    A geographic transformation is also automatically applied. A transformation is used when the input and output geographic coordinate systems have different geographic coordinate systems (GCS) to ensure that data is properly aligned. In this case, the transformation accounts for the difference between the WGS 1984 GCS used by the NetCDF data and the NAD 1983 GCS used by the California Teale Albers coordinate system.

  4. Click the Environments tab.
  5. On the Environments tab, in the Processing Extent section, click the Extent drop-down menu and click CatalinaBoundary.

    Extent set to CatalinaBoundary under Processing Extent in the Project Raster tool pane

    This will project only the bathymetry data in your AOI, resulting in a smaller raster that just covers the area you are studying. Limiting the processing extent to your AOI saves disk space and reduces processing time.

  6. Click Run.

    Bathymetry data limited to the AOI

    Projecting the entire bathymetry raster might take a couple of minutes; this is much faster.

  7. In the Contents pane, right-click CatalinaBathymetry and click Symbology.
  8. On the Symbology pane, click the options button and click Import from layer.

    Import from layer in the options menu on the Symbology pane

    The Apply Symbology From Layer tool appears.

  9. For Symbology Layer, choose Band1_Layer and click Run.

    Now that you have the bathymetry data clipped to the Catalina area, projected to a planar coordinate system, and symbolized, you no longer need the larger Band1_Layer on the map.

  10. In the Contents pane, right-click Band1_Layer and click Remove.

    CatalinaBathymetry layer symbology updated

  11. Save the project.

Perform raster analysis

Now that the bathymetry raster has been prepared for your AOI, you will derive slope and aspect rasters from it. You will use these rasters to explore some of the factors that may control coral and sponge distribution in your study area.

  1. In the Geoprocessing pane, in the search bar, type Slope (Spatial Analyst). Click the Slope tool.
  2. In the Slope tool pane, for Input raster, choose CatalinaBathymetry. For Output raster, type CatalinaSlope.

    Parameters set in the Slope tool pane

  3. Click Run.

    The slope is calculated for each cell in the AOI. Slope identifies the steepness at each cell of a raster surface. The lower the slope value, the flatter the terrain; the higher the slope value, the steeper the terrain.

    Slope layer

  4. On the Geoprocessing pane, click the back button. Search for and click the Aspect (Spatial Analyst) tool.
  5. In the Aspect tool pane, for Input raster, choose CatalinaBathymetry. For Output raster, type CatalinaAspect.

    Parameters set in the Aspect tool pane

  6. Click Run.

    The aspect is calculated for each cell in the AOI. The Aspect tool identifies the direction the downhill slope faces. The values of each cell in the output raster indicate the compass direction the surface faces at that location.

    Aspect layer

  7. Save the project.

Enhance the database

Now that the bathymetry, slope, and aspect rasters for the Santa Catalina Island AOI have been created, you will take the values and incorporate them into the coral and sponge database. You will use the Extract Multi Values to Points tool to add the values from these three rasters to each of the coral and sponge location points. This will allow you to create graphs to understand how these environmental parameters vary at the locations where the corals and sponges were observed.

  1. Open the Geoprocessing pane. Search for and open the Extract Multi Values to Points tool.
  2. In the Extract Multi Values to Points tool pane, for Input Features, choose CoralandSpongeCatalina.
  3. For Input rasters, choose the following:
    • CatalinaBathymetry
    • CatalinaSlope
    • CatalinaAspect
  4. For the corresponding Output field name, type the following:
    • Bathymetry
    • Slope
    • Aspect

    Parameters set in the Extract Multi Values to Points tool pane

  5. Click Run.
  6. In the Contents pane, right-click the CoralandSpongeCatalina layer and click Attribute table.
  7. In the attribute table, scroll to confirm that the Bathymetry, Slope, and Aspect values have been added for each feature.

    Bathymetry, Slope, and Aspect values added to the CoralandSpongeCatalina attribute table

  8. Close the table and save the project.

Now that you have enhanced the coral and sponge points with the environmental data, your next step is to visualize these values with charts.


Explore the data with Python

ArcGIS Pro includes ArcGIS Notebooks, used by the data science community for exploring data. You will use Python code in Notebooks to explore the relationships between the three environmental variables and the different coral and sponge species. This will help you understand their preferred habitats.

Make a chart using Notebooks

You have imported bathymetry, derived slope and aspect rasters, and added the resulting data to the coral and sponge observations. Although you can visually inspect the layers to derive insights, it is sometimes easier to understand your data by creating charts. Now you will create charts to explore the data.

  1. On the ribbon, click the Analysis tab. In the Geoprocessing group, click the drop-down arrow next to Python and click Python Notebook.

    Python Notebook in the menu for Python

    The Notebook window appears and a new notebook is listed under a new group, Notebooks, in the Catalog pane.

    Python notebooks and the Python window are two ways to run Python code.

    Code in the Python window is run immediately after you press Enter. Notebooks allow you to type multiple lines of code in different cells, run the code, edit it, and re-run it. Notebooks are useful for prototyping code and for data analysis.

  2. In the Catalog pane, expand Notebooks.
  3. Right-click New Notebook.ipynb and choose Rename.
  4. Type CoralSponge and press Enter.
  5. Click in the empty cell in the notebook.

    Empty first cell

  6. Copy and paste or type the following line of code to import the arcpy module into the cell:
    import arcpy

    Python is case sensitive, so the capitalization must match.

  7. On the toolbar above the first cell, click Run.

    Run on the toolbar

    While the cell is running, an asterisk * appears in the brackets [*]. When it finishes running, the asterisk is replaced by the number 1, and a new empty cell appears below the first cell.

    New cell appears below.

    The arcpy module contains Python code to support many desktop mapping and analysis tasks.

    Sometimes when you type code in a cell, there may be an error, such as a typo. In this case, you may see an error message after the cell.

  8. Click in the second cell and type import ARCPY, and click the Run button.

    Running import ARCPYreturn error

    This line of code causes an error, because there is not a module installed in Python named ARCPY. The original first line, import arcpy, runs correctly. Trying to import ARCPY (or arcPy, or ArcPy, or any other variation other than arcpy) will fail. This is an example of how case sensitivity can affect your code in Python. Minor typos that a human could skim over and understand will cause errors in Python code.

    Next, you will write some code to create a chart from your data. You will use the Chart class from arcpy.

  9. In the second cell, delete the import ARCPY line.

    You will replace the code in the second cell with code that does essentially the same thing you would do to make a chart using the ArcGIS Pro graphical user interface. However, when you make a chart using the arcpy.Chart class, you use a set of Python commands to specify the details of the chart, rather than clicking user interface elements.

  10. Copy and paste the following code into the second cell:
    my_chart = arcpy.Chart('my_chart')
    my_chart.type = 'bar'
    my_chart.title = 'CoralandSpongeCatalina'  
    my_chart.description = 'This chart shows the mean Slope values that correspond with the taxa in the CoralandSpongeCatalina layer.'

    The first line (my_chart = arcpy.Chart('my_chart')) creates a variable and sets it equal to a new object that belongs to the Chart class. This object is called my_chart. You can think of the Chart class as a template for charts. Now that you've created one, you must specify what kind of chart it is and what properties it has. The remaining lines of code do that.

    The next line (my_chart.type = 'bar') specifies that the chart will be a bar chart. It sets the type property of the chart to be bar. The notation my_chart.type accesses the type property of my_chart, and the equals sign is used to assign the value 'bar' to that property. The Chart object class supports various types of chart, and they are identified by names, such as bar, line, and scatter. These names are specified as Python strings in quotation marks.

    The next lines, which begin with my_chart.title and my_chart.description, specify a title and a description for the chart. The code does this the same way it set the type—by accessing properties of the my_chart object and setting them to specific string values. In this case, instead of typing a description in a text box, you have added a Python string in a line of code.

  11. Copy and paste the following code to the bottom of the cell:
    my_chart.xAxis.field = 'VernacularNameCategory'
    my_chart.xAxis.aggregation = 'VernacularNameCategory'
    my_chart.xAxis.title = 'Name'
    my_chart.yAxis.field = 'Slope'
    my_chart.yAxis.title = 'Mean Slope'
    my_chart.bar.aggregation = 'MEAN'

    The next three lines (which begin with my_chart.xAxis) specify the field in the layer that will be used for the X axis and how the values in that column will be aggregated. For these two lines, VernacularNameCategory is a field in the CoralandSpongeCatalina layer attribute table that contains names of the different corals and sponges. Specifying the VernacularNameCategory for the aggregation means that the values will be grouped by the unique values in that field. Next, it sets the title for the X axis to Name, to label that axis as coral and sponge names.

    The two lines beginning with my_chart.yAxis and my_chart.bar.aggregation specify the field, title, and aggregation method for the Y axis. It will use the Slope field from the data table and aggregate it using the mean value. The Y axis will be labeled Mean Slope, because it will display the mean value of the slope for each coral and sponge group.

    Note:

    If, when you ran the Extract Multi Values to Points tool, you accepted the default output field names, you must check the attribute table of the CoralandSpongeCatalina layer and determine the name of the field that holds the slope values. You must edit the my_chart.yAxis.field = 'Slope' line to include the correct field name, if it is not Slope.

  12. Copy and paste the following code to the bottom of the cell:
    my_chart.dataSource = 'CoralandSpongeCatalina'
    my_chart.addToLayer = 'CoralandSpongeCatalina'

    Finally, these last two lines specify the layer on the map that is the source of the data and adds the chart to the layer.

    Note:

    If you named the layer something other than CoralandSpongeCatalina, you must edit this line to match the layer name

    The cell is complete.

    Complete code to create the chart.

  13. Click Run.

    The chart is created, but nothing seems to happen. To see the chart, you must run Python code to show it.

  14. In the next new cell, copy and paste the following code and click Run:

    my_chart

    The chart is displayed below the cell.

    First chart output

  15. Copy and paste the following code into the next cell:
    outChartName = "CoralandSpongeCatalinaSlope.svg"
    outSVGPath = "C:/Lessons/DeepSeaCoralSponge/" + outChartName
    my_chart.exportToSVG(outSVGPath, width=1500, height=800)

    Export code in the cell

    Note:

    If you created the DeepSeaCoralSponge project folder in another location than C:/Lessons/DeepSeaCoralSponge/, edit the path in the cell to match the path on your computer. The path may look strange with forward slashes as the separators. Python uses the \ character as an escape character, which, in combination with other characters, can signal line breaks or tabs, so it is better to use the forward slash for this path.

    The first line of code (beginning with outChartName =) defines a file name, including file extension, for the chart file. The file name is a string, in quotation marks.

    The second line (outSVGPath =) defines a string that specifies the path where that file should be saved. It has the file name appended to it using the + sign, which can be used to join text strings together (this is called concatenation) and for adding numbers in Python.

    The third line (my_chart.exportToSVG) calls the exportToSVG method of the chart object and supplies the full path with the file name, the width in pixels, and the height in pixels, in parentheses, as parameters to the method.

  16. Run the cell.

    The chart file will be created in the folder.

  17. On your computer, in the Microsoft File Explorer, go to the folder and open the chart image.

    The chart is larger than the one displayed in the notebook, because you specified different dimensions for the output chart.

  18. Close the chart after you have inspected it.

While this approach may seem like it requires much effort, especially when compared to clicking through the ArcGIS Pro interface, using code provides an advantage that allows you to automate the process. In the next section, you will learn how to turn this code into a function that you can call to make multiple charts.

Make a charting function

You have seen how to make a single chart using Python code. Now you will adapt that code to make it more reusable. You'll make a Python function that allows you to make multiple charts.

  1. Copy and paste the following code into the next cell:
    def coral_and_sponge_chart(layer, field):

    This line starts with def. This tells Python that you are defining a new function. The next part is the name of the function, coral_and_sponge_chart. Function names are how you refer to the block of code inside the function when you want to run that code. The next part is in parentheses. These are the parameters that the function will take. Parameters are inputs to a function that are processed by the function to create some result. This function will take a layer and a field name as inputs. It will make a bar chart for that field. The line ends with a colon. This indicates that the next lines will be the code block for the function.

  2. Press Enter.

    When you press Enter after the line that starts the function definition, the next line is indented by four spaces.

  3. Copy and paste the following code into the cell:
    my_chart = arcpy.Chart('MyChart')
        my_chart.type = 'bar'

    Each of these two lines is the same as the code you used to create the previous chart, but indented four spaces. Python uses indentation to group code sections. The code block lines inside this function should all be indented by four spaces.

    Three lines of code entered in the same cell

    These lines create a chart from the Chart class and specify that it should be a bar chart.

  4. Press Enter and copy and paste the following code into the cell, after the chart.type line:
    my_chart.title = layer

    This line begins like the next line in your original chart code, but instead of specifying the title of the chart as a specific string, it specifies that the title should be equal to the input layer parameter. This means that the output charts title will match its layer name, no matter what that layer is.

  5. Copy and paste the following code into the cell, after the my_chart.title line:
    my_chart.description = f'This chart shows the mean {field} values that correspond with the taxa in the {layer} layer.'

    This line is similar to the line in your original chart code, but with a few differences. The first difference is, instead of setting the description equal to a specific string, this line sets it equal to an f-string, or a formatted string. Formatted strings are strings that you can substitute values into. They start with f and then a quotation mark. The f indicates to Python that this is a formatted string.

    The next difference is, instead of setting the description to include the text the mean Slope values, the code is written as the mean {field} values. What do you think this means?

    Since this is an f-string, the code {field} will be substituted with the field name from the function's input parameters. The same will be true for the end of the code that includes in the {layer} layer. The {layer} will be substituted with the layer name. This way, the output chart description will match to the input layer and field names.

  6. In the same cell, start a new line and copy and paste the following code:
    my_chart.xAxis.field = 'VernacularNameCategory'
        my_chart.xAxis.aggregation = 'VernacularNameCategory'
        my_chart.xAxis.title = 'Name'

    These three lines are identical to the lines in your original chart code but are indented four spaces to be part of the code block. The function will plot bars for each of the input layer's VernacularNameCategory field values and name the X axis of the chart Name.

    This line will fail and cause an error if you run the function on a layer that lacks a VernacularNameCategory field. However, all of the CoralandSpongeLocations points have this field, so it will work, regardless of what study area you choose.

  7. In the same cell, start a new line and copy and paste the following code:
    my_chart.yAxis.field = field
        my_chart.yAxis.title = 'Name'

    Here the code is setting properties for the Y axis.

  8. In the same cell, start a new line and copy and paste the following code:
    my_chart.bar.aggregation = 'MEAN'

    This line is identical to the line in your original code. It makes the bars show the mean values in the input field for each of the types of coral and sponge.

  9. In the same cell, start a new line and copy and paste the following code:
    my_chart.dataSource = layer 
        my_chart.addToLayer = layer

    These lines are similar to the lines in your original chart code, but instead of setting the data source and adding the chart to a specific named layer (CoralandSpongeCatalina in the original), it does that for the input layer of the function. If you created another subset of the coral and sponge points for a different study area (around Hawaii or off the coast of Alaska, for example); added the slope, aspect, and bathymetry values to it; and named it something else, the function would still work to create the chart and associate it with the input layer.

  10. In the same cell, start a new line and copy and paste the following code:
    outChartName =  layer + field + ".svg"
        outSVGPath = "C:/Lessons/DeepSeaCoralSponge/" + outChartName
        my_chart.exportToSVG(outSVGPath, width=1500, height=800)

    These lines are similar to the ones in your original code. The outChartName line constructs a name for the chart by concatenating the layer name and the field name that are passed to the function. If you created a layer for an Alaska study area, the output chart names would reflect that layer's name.

    Note:

    If you created the DeepSeaCoralSponge project folder in another location than C:/Lessons/DeepSeaCoralSponge/, edit the path to match the path on your computer.

  11. In the same cell, start a new line and copy and paste the following code:
    return my_chart

    The return statement makes the function return the chart object to the code that calls the function.

    The code block to create the next chart is complete.

    Completed code block for the next chart

    The first line, defining the function, should start at the left-most column, and every line after it should be indented by four spaces.

    
    def coral_and_sponge_chart(layer, field):
        my_chart = arcpy.Chart('MyChart')
        my_chart.type = 'bar'
        my_chart.title = layer
        my_chart.description = f'This chart shows the mean {field} values that correspond with the taxa in the {layer} layer.'
        my_chart.xAxis.field = 'VernacularNameCategory'
        my_chart.xAxis.aggregation = 'VernacularNameCategory'
        my_chart.yAxis.field = field
        my_chart.xAxis.title = 'Name'
        my_chart.yAxis.title = field
        my_chart.bar.aggregation = 'MEAN'
        my_chart.dataSource = layer 
        my_chart.addToLayer = layer
        outChartName = layer + field + ".svg"
        outSVGPath = "C:/Lessons/DeepSeaCoralSponge/" + outChartName
        my_chart.exportToSVG(outSVGPath, width=1500, height=800)
        return my_chart
  12. Click Run.

    The cell should run but not return anything.

    Note:

    If you see error messages, copy and paste the whole code block from here into the cell and run it. Be sure to update the path for the outSVGPathvariable to a real path on your computer, using forward slashes.

    Why does this code not return anything when you run the cell?

    It defines a function, but the function needs to be run, or called, by other Python code.

  13. In the next cell, copy and paste the following code:
    coral_and_sponge_chart()
  14. Run the cell.

    An error message appears.

    Error message

    The error tells you that the function is missing two required positional arguments: layer and field. The function needs these values to run correctly.

  15. Replace the contents of the cell with the following code and click Run:
    coral_and_sponge_chart('CoralandSpongeCatalina', 'Slope')

    The chart is displayed in the notebook, and an .svg graphic file of the chart is created in the folder you specified. It has the same file name as the output of the first code, but if you look at the file now, you will see that it has an updated Date Modified value.

    Chart appears showing mean Slope values corresponding with taxa in the CoralandSpongeCatalina layer

    The result is the same as the result you got from the first code block. Now, however, you have a function that you can pass a layer name and a field name to, and you will get a different chart.

  16. In the next cell, copy and paste the following code and click Run:
    myList = ['Slope', 'Aspect', 'Bathymetry']
  17. In the next cell, copy and paste the following code and click Run:
    print(myList)

    Code run to print myList

    In Python, lists are enclosed in square brackets, and they are a way to store a group of things, delimited by commas. In this case, the list myList contains three strings, with the names of the Slope, Aspect, and Bathymetry fields.

    If, when you ran the Extract Multi Values to Points tool, you accepted the default output field names, you must check the attribute table of the CoralandSpongeCatalina layer, determine what the field names are, edit the values in myList to match, and run that cell again to reset the myList values.

  18. In the next cell, copy and paste the following code and click Run:
    
    for envField in myList:
        outChart = coral_and_sponge_chart('CoralandSpongeCatalina', envField)

    This cell contains a new Python code construct, a for loop. A for loop allows you to loop, or iterate, through a sequence of items. The first line specifies that the loop will go through each item in myList and assign it to the temporary variable envField. The colon specifies that the next section will be the code block of the for loop. The next line is indented four spaces, like the lines in the function definition were. The indented lines in a for loop block all run for each cycle, or iteration, of the loop.

    In this case, there is only one line in the code block. This is very similar to the line you ran to test the coral_and_sponge_chart function earlier. It creates a variable and sets its value equal to running the coral_and_sponge_chart function on the CoralandSpongeCatalina layer, but instead of also specifying the Slope field, runs it on the field name that it gets from the envField variable. Since there are three items in the list, the loop will run three times and create a chart for each of the three fields.

  19. In your computer File Explorer, browse to the folder you specified in the function definition.

    Output charts in the folder you created at the beginning of the lesson

    You should see a chart for each of the attributes. These output files could be used in presentations, added to a story using ArcGIS StoryMaps, or placed on a map layout in ArcGIS Pro.

    It is a little faster to do this than it is to create the three charts manually. Suppose that you had six different environmental variables that you enriched the coral and sponge points with. You could make a list of those field values and call the function to create a chart for each of them in nearly the same amount of time that it took you to make these three.

    Suppose you had five study areas and wanted to make charts of all the values for each study area. Since the function also takes the layer name as a parameter, you could make a list of all of the layer names, make a for loop that looped through them, and then inside that for loop, call the loop you just made.

    The code to accomplish this is as follows:

    
    myLayersList = ['CoralandSpongeCatalina']
    # You would add comma separated layer names in
    # quotation marks to the myLayersList, such as:
    # ['CoralandSpongeCatalina', 'CoralandSpongeAlaska', 'CoralandSpongeHawaii']
    
    myList = ['Slope', 'Aspect', 'Bathymetry']
    # You could add comma separated field names to the list
    
    for lyrName in myLayersList:     # outer loop, iterate over the layer names in the list
        for envField in myList:     # inner loop, iterate over the field names for that layer
            outChart = coral_and_sponge_chart(lyrName, envField)

    With 6 variables and 5 study area layers, you could make 30 charts in about the same time it took to make three.

  20. In the next cell, copy and paste the following code and click Run:
    outChart

    Chart returned that shows mean Bathymetry values

    You saw that three charts were created by the loop. Why does only one display when you run this cell?

    The variable outChart is reset to the new chart in each cycle of the loop. When the loop finishes, the variable is assigned to the chart created in the last cycle. A common problem when writing output files from loops is to expect multiple outputs but only get one. Often, this is because the output file name is the same in each cycle of the loop. The result is produced for each cycle, but because the name is the same, the same file gets overwritten each cycle. Your coral_and_sponge_chart function avoids this problem by constructing the file name from the input variables.

    outChartName = layer + field + ".svg"

    There are other ways of dealing with this problem. One is to include a counter variable in the loop and concatenate it to the output file name, resulting in outfile_1, outfile_2, outfile_3 and so on. Another is to add the current time to the filename, which works well unless the processing time for each cycle is very short. Another is to create a unique identifier and add it to the file name, though this results in long, potentially confusing file names.

  21. Save the project.
Tip:

For an additional challenge, try choosing another area of interest and make a set of charts using the steps in this lesson.

In this tutorial, you have learned how to download bathymetry and coral and sponge data from NOAA. You learned how to project the data to an appropriate planar coordinate system for raster analysis. You learned how to create an area of interest, derive slope and aspect layers for the AOI, and append that information to the coral and sponge attributes. You learned how to use Python to create a chart and how to define a function to automate the production of multiple charts. You can use these techniques to analyze coral and sponge data in multiple areas around the world.

You can find more tutorials in the tutorial gallery.