AEC Custom Reporting

How to leverage Notebooks and Python to access your data and create custom reports

THE NEED


We've all been in that frustrating situation where we need to create a report to share with stakeholders and don't have an easy, efficient way to get it done. Maybe our data isn't accessible in an app, like Survey123, that has built-in reporting capabilities or maybe our department has a small budget and doesn't have room to invest in a shiny new third party application.

What are some alternative methods for creating repeatable, scalable reports from our data, avoiding the laborious and monotonous task of creating a report from scratch each time. Aside from downloading a copy of the data and manipulating it manually, wouldn't it be great to automate report generation from any feature service?

In this tutorial we'll do just that! We'll walk through the process of using a  Jupyter Notebook  that will leverage the  ArcGIS for Python API  to grab a Feature Service to create a custom report from. We'll also work with  python-docx , a Python library for creating and manipulating Microsoft Word files, to automate the report creation process.

Once completed, you'll have the understanding of some basic concepts that will allow you to set up your own Notebook(s) for automated report creation.

Concepts to be covered:

1. Cloning your default environment in Pro

2. Installing additional Python packages 

3. Connecting to your Portal to access the data with the Python API

4. Setting up a Report Template in Word

5. Run Sample Notebook to produce your first report

6. Modify the Word template and Notebook to include additional data from Feature Service


GETTING STARTED

First, you'll create a new conda environment in order to install the Python library you'll use for creating and updating Microsoft Word (.docx) files. ArcGIS Pro includes a default  conda environment , arcgispro-py3. The default conda environment includes several common packages, like ArcPy, SciPy, NumPy, and Pandas, among others but is lacking the one we need. Since the default Python environment can't be modified, we'll need to clone it first in order to add any additional packages.

Cloning default environment

Open up Pro and follow along to create a new conda environment by cloning an existing environment using the Package Manager page in ArcGIS Pro.

Be sure to name your new environment something that will be easily remembered and distinguishable - i.e. "arcgispro-py3-docx"


Installing python-docx Library

With the new environment created, let's install the python-docx package we'll need to access the Word template and create each report.

Verify newly created environment is set as active under 'Package Manager'

Navigate to your start menu and open the 'Python Command Prompt' found under the ArcGIS folder:

Python Command Prompt

Once the prompt is open, check it is referencing the new environment, arcgispro-py3-docx, created in the previous steps and enter the following commands in succession:

pip install python-docx

Enter the command and press enter to initiate the install and verify successful installation (see above)

pip install docxtpl

Enter the command and press enter to initiate the install and verify successful installation (see above)

Close out of prompt when complete.

DOWNLOAD SAMPLE NOTEBOOK AND REPORT TEMPLATE

Now that our environment has been properly setup and configured, let's download the sample data and create our first report!

 Download 'Report Template' to your local "Downloads" folder on your computer 


INVESTIGATING THE DATA

In this example, we're going to be creating a simple daily weather report, for a specific location, using data from a Feature Layer that can be found in the Living Atlas.

Feel free to explore the data we'll be using in our example report. The Notebook is already hard wired to access this data but it will be good to get familiar with the schema as we'll be referencing it later on.

Current Weather and Wind Station Data

REPORT TEMPLATE

The report template we'll be using should look very familiar if you've ever worked with  Feature Report templates  from Survey123. If not, then it's essentially a Microsoft Word document with some keywords referencing field values from a layer. These keywords will ultimately be replaced with the data from the record(s), contained within the layer, you want to report on. You can still put all your Microsoft Word tricks to work to ensure your report looks exactly as you want and has that custom "feel". So in this case the template is being provided for you but you will quickly see how easy it is to create and use your own.

Feel free to take some time and open the report template document (.docx) you downloaded and explore.

Example Report Template

DISSECTING THE NOTEBOOK

Before running our Notebook and creating a report using the provided template, let's take a moment to look it over and become familiarized with what's happening. This way you can feel more comfortable modifying it and making your own changes if you'd like.

First, let's go back to Pro and get the sample Notebook that was downloaded added to our project.

1. Navigate to the 'Insert' tab located on your ribbon 2. Find 'New Notebook' and click the down arrow to open the menu options 3. Select 'Add Notebook' and navigate to your 'Downloads' folder to select the downloaded 'Demo Report Notebook' 4. In the Catalog pane, under Notebooks, right-click the 'Demo Report Notebook' and select 'Open Notebook'.

Now that the Notebook is open and accessible through Pro, let's take a look at what our code is doing.

The first cell is pretty standard and is  connecting  to your organization and creating an object representing that org and all it's content for us to work with. The "home" property of our  GIS  object is referencing the current user actively logged into Pro.

Sign-in Status can be found in the top right of the application

The second cell is simply importing all the packages we'll be working with to accomplish our goal including the  python-docx  library we installed earlier.

These can be explored further here:

The third cell is making reference to our report template. In the future if you'd like to use this  Notebook  to create your own workflow, this would be where you would change the template name to be one of your own creation. For example, instead of "Weather Report Template.docx" you could replace it with "Inspection Report Template.docx" or whatever the name of the template is that you've setup. Just make sure it's located in your 'Downloads' folder on your computer.

The fourth cell is retrieving and formatting the data we want to report on contained within the Feature Service from the Living Atlas. With just  3 lines of code  we are able to get the "Current Weather and Wind Station Data" Feature Layer by referencing its Item ID and the "Stations" sublayer position that is apart of this service. This is where you could modify the code to work with your own Feature Service outside of this exercise.

Item ID can be found in the Feature Service URL

Both ItemID and layer reference can be found through the Services Directory

The other 2 variables that have been defined within this cell are:

  • where_filter
  • out_fields

The "where_filter" is a  SQL statement  defining a condition to be met based on the field values within the Feature Layer. In this case, we're saying we'd only like the record(s) 'KGRR' found under the "ICAO" field. In this case it will only return a single record for the weather station located in Grand Rapids, MI.

The "out_fields" variable is filtering down the schema of the Feature Layer to only contain the field names within the defined list. In this way we're limiting how many fields will be returned and available for us to call on when writing values in the report. This will help with performance in cases when dealing with a large schema. As you can see, these names correspond directly to the ones used within the keyword brackets

"{{ }}"

found in our report template document.

Available  fields  from Feature Service

The fifth and last cell is where the values from the fields in the Feature Layer are mapped to the keywords contained within the word document (.docx). Lastly we save the report as a new document named "Generated Weather Report (demo).docx" to the local 'Downloads' folder.

This would be where you could modify the Notebook to work with your own chosen schema and .docx template.

Available  fields 

Fields used as keywords within template

RUN THE NOTEBOOK AND CREATE A REPORT

Now that we've set up our environment, downloaded our sample Notebook with report template, and gained a basic understanding of what it's doing, let's run it and create our first report!

With Pro still open make sure you're positioned on the first cell of the Notebook and click 'run'. Do this for all cells contained within the Notebook in till you reach the bottom and the messages "Report document export complete.." and "Check your 'Downloads' folder..." are displayed. Then check your 'Downloads' folder to view your report with data from the Grand Rapids, MI station.

Running the Notebook


MODIFYING THE NOTEBOOK AND TEMPLATE

In the previous example, a single report was created for a single station. But what if I wanted to create multiple reports for multiple stations?! How would I go about modifying the existing Notebook to create multiple reports at once? Let's take a look!

With the 'Demo Report Notebook' still open, navigate to the 4th cell in order to modify the current SQL statement.

4th cell in the Notebook

We're going to modify it to return multiple records using a more complex statement. Copy and replace the current statement with the one below:

where_filter = "COUNTRY LIKE '%Michigan, United States Of America%' And OBS_DATETIME >= timestamp '2022-11-16 18:56:00' "

This statement will now return any records that:

At the time of publishing '2022-11-16 00:00:00' was used but feel free to change the date to reflect a date closer to the current date of running through this tutorial as to not create too many reports

  1. Contain the text - 'Michigan, United States Of America' within the "COUNTRY" field
  2. AND fall on or after the date - '2022-11-16 00:00:00' within the "OBS_DATTETIME" field

While we're here let's also modify the fields being returned from the service to include an additional field, "R_HUMIDITY". Copy and replace the current statement with the one below:

out_fields = ["ICAO", "OBS_DATETIME", "STATION_NAME", "ELEVATION", "WIND_DIRECT", "WIND_SPEED", "TEMP", "SKY_CONDTN", "VISIBILITY", "WEATHER", "LONGITUDE", "LATITUDE", "R_HUMIDITY"]

Cell after changes have been made

Before moving on, let's modify the current report template to include a new keyword for the additional field we just added to our return. With the document open, append the following to the current line for temperature in the report template:

 with Relative Humidity at {{R_HUMIDITY}} %

Modified report template to include Relative Humidity

Moving back to the Notebook, navigate to the 5th cell in order to modify the code to work with multiple records. Copy and replace the current cell with the code snippet here:

Cell after changes have been made

That's it! Now you can save and re-run the Notebook. Check your downloads folder to see all the reports that were generated, including the new value for Relative Humidity.

Before and After

Download the modified Notebook and template here:

Now you can modify this and rewire it to work with any Feature Service to create any report. Happy creating!

Verify newly created environment is set as active under 'Package Manager'

Python Command Prompt

Enter the command and press enter to initiate the install and verify successful installation (see above)

Enter the command and press enter to initiate the install and verify successful installation (see above)

Example Report Template

4th cell in the Notebook

Cell after changes have been made

Modified report template to include Relative Humidity

Cell after changes have been made

Before and After

Sign-in Status can be found in the top right of the application

Item ID can be found in the Feature Service URL

Both ItemID and layer reference can be found through the Services Directory

Available  fields  from Feature Service

Available  fields 

Fields used as keywords within template