In this blog post, I’ll explore how you can use ChatGPT to create complex code that downloads and processes satellite imagery in just minutes. Specifically, I’ll show you how to create Python code for analyzing changes in NDVI (Normalized Difference Vegetation Index) between two date ranges to identify deforestation or urbanization.
Step 1: Break the Problem Down into Sections
To get started, break the problem down into sections. ChatGPT works best when you provide it with a step-by-step list, as opposed to a long paragraph. This allows ChatGPT to optimize the code for specific tasks that you may not have considered. For instance, the method for pulling data may differ depending on whether you’re using a simple bounding box or a complex GeoJSON shape.
For our example, I’ll break the problem down into the following steps:
- Take two date ranges from the user.
- Take a GeoJSON from the user.
- Download one Sentinel 2 image from each date range that is cloud-free within the GeoJSON bounds. If no cloud-free images are available within a date range, print a message to the console and stop the script.
- Compute NDVI from both downloaded Sentinel 2 images.
- Subtract the later date NDVI from the earlier date NDVI and save the resulting file as a TIFF.
Step 2: Pick a Data Source
You can either choose your own data source or let ChatGPT choose one for you. If you prefer to use a specific data source, ChatGPT can write all the code using it. However, if you let ChatGPT choose the data source, it will optimize the code for the task at hand. For example, if you’re just downloading NDVI data, ChatGPT will use SentinelHub. On the other hand, if you need to work with cloud cover, ChatGPT will use Earth Engine because it has tools to check for cloud cover in your area of interest before downloading.
Step 3: Ask ChatGPT to Write the Code
To have ChatGPT write the Python code, simply prompt it with the following command: “Please write me some Python code that does the following steps.” If you’d like to specify a data source, use this command instead: “Please write me some Python code that does the following steps and uses SentinelHub:”
Copy and paste the list of steps from Step 1 into the chat box, and ChatGPT will start writing the code.
Step 4: Use “Continue Code”
If ChatGPT stops writing mid-code, ask it to continue writing the code. This happens because the AI algorithm has a character limit for the prompt and response. To continue, simply prompt ChatGPT with “continue code.” Avoid using “continue” because it may cause issues with formatting making the code difficult to work with.
Step 5: Ask for Code Explanation, API Key, and Dependencies
Ask ChatGPT to explain the code it generated, which will help you understand what it did and identify any logical errors. You can also ask for instructions on how to connect with the API used and how to install dependencies for your operating system. If you need a full explanation on how to run the code from beginning to end, all you have to do is ask!
Step 6: Running and Troubleshooting
Once you have reviewed the code and obtained any necessary API keys and dependencies, you can start running the code. If you encounter any errors, don’t panic! Simply copy and paste the error message into ChatGPT and it will provide you with suggestions to fix the issue. Continue to troubleshoot and make any necessary changes until you have successfully run the code and obtained your desired results.
No errors were in the code this time but I made a small one on one of the first lines just to demonstrate what to do in the event of an error. The error was copy and pasted straight from the terminal with no context into ChatGPT.
Overall, ChatGPT is an exciting new tool for professionals in the GIS industry. With its ability to generate code, audit code, provide advice on analysis techniques, and more, it can help streamline workflows and save valuable time. By breaking down complex problems into manageable steps and prompting ChatGPT to generate code, professionals can easily process satellite imagery, analyze data, and perform a variety of other tasks.
Always keep in mind though, that an understanding of the code and the GIS concepts are still very important to make sure that the generated products deliver correct information. ChatGPT is still limited in its reasoning and it does make coding mistakes occasionally. This means that you may get code that doesn’t work or code that works but doesn’t do exactly what you were looking for. Since ChatGPT is still so new it’s likely that we’ll see lots of these shortcomings fixed over time, but regardless, at the moment whether you are a seasoned GIS professional or just starting out, ChatGPT is a valuable resource that can help you achieve your goals.