-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathagentic-coding-geospatial.Rmd
More file actions
1013 lines (670 loc) · 42.8 KB
/
Copy pathagentic-coding-geospatial.Rmd
File metadata and controls
1013 lines (670 loc) · 42.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Agentic Coding for Geospatial (Full Workshop)"
subtitle: "A practical guide to using AI coding agents for real-world geospatial problems."
author: "Ujaval Gandhi"
fontsize: 12pt
output:
html_document:
df_print: paged
toc: yes
toc_depth: 2
highlight: pygments
includes:
in_header:
- copy-code.html
- GA_Script.html
after_body: comment.html
# word_document:
# toc: false
# fig_caption: false
# pdf_document:
# latex_engine: xelatex
# toc: yes
# toc_depth: 3
header-includes:
- \usepackage{fancyhdr}
- \pagestyle{fancy}
- \renewcommand{\footrulewidth}{0.4pt}
- \fancyhead[LE,RO]{\thepage}
- \geometry{left=1in,top=0.75in,bottom=0.75in}
- \fancyfoot[CE,CO]{{\includegraphics[height=0.5cm]{images/cc-by-nc.png}} Ujaval Gandhi http://www.spatialthoughts.com}
classoption: a4paper
---
\newpage
***
```{r echo=FALSE, fig.align='center', out.width='75%', out.width='250pt'}
knitr::include_graphics('images/spatial_thoughts_logo.png')
```
***
\newpage
# Introduction
This course covers agentic coding workflows specializing in geospatial data science. You will get hands-on experience setting up your agent and understand how to use Skills and MCP servers to augment domain-specific capabilities. We cover best practices for using coding agents to rapidly solve complex problems - grounded in established science and human-in-the-loop validation. We cover hands-on examples using Claude Code - including crime hotspot mapping, route optimization, and object detection from aerial imagery. You will also learn how to use agents to manage a personal knowledge base and create a website that automatically updates with new information.
----
# Installation and Preparation
## Sign-up for GitHub
Visit [GitHub.com](https://github.com/) and create a free account. If you already have an account, skip this step.
## Install Git and GitHub CLI
Follow our [Git and GitHub CLI Installation Guide](install-git-github.html) to install and configure Git and GitHub CLI.
## Install Conda and Setup an Environment
We will use conda to install the required Python packages and manage local development environments.
Follow our step-by-step [Conda Installation Guide](install-conda.html) to install Miniconda for your operating system. Once you have a working conda installation, follow the steps below.
1. *(Windows users)*, search for Anaconda Powershell Prompt and launch it. *(Mac/Linux users)*: Launch a Terminal window. Run the following commands to create a fresh environment and activate it.
```{bash eval=FALSE}
conda create --name claude_code_workshop -y
conda activate claude_code_workshop
```
2. Now your environment is ready. We will install the required packages from `conda-forge`.
```{bash eval=FALSE}
conda install -c conda-forge pandas geopandas matplotlib jupyterlab rioxarray gdal rio-cogeo
```
Your local development environment is now ready.
## Install Visual Studio Code
We recommend Visual Studio Code (VS Code) editor for this workshop.
Follow our step-by-step [Visual Studio Code Installation Guide](install-vscode.html) to install and configure VS Code on your system. Make sure to complete the steps to set VS Code as your default editor.
## Install Claude Code
Follow our step-by-step [Claude Code Installation Guide](install-claude-code.html) to install and configure Claude Code on your system.
## Install Obsidian
[Obsidian](https://obsidian.md/) is a free app for creating and managing notes. We will be using Obsidian for setting up your personal knowledge base using Claude. Visit [Obsidian Download](https://obsidian.md/download) page and install Obsidian for your platform.
## Sign-up for OpenRouteService API
[OpenRouteService (ORS)](https://openrouteservice.org/) provides a free API for routing, distance matrix, geocoding, route optimization etc. using OpenStreetMap data. To obtain an API key, visit the [HeiGIT Sign Up page](https://account.heigit.org/signup) and create an account. Once your account is activated, you can visit the [Dashboard](https://account.heigit.org/manage/key) and copy the long string displayed under *Basic Key*.
# Get the Data Package
The code examples and reference materials used in this workshop are supplied to you in the `agentic_coding_geospatial.zip` file. Unzip this file to a directory - preferably to the `<home folder>/Desktop/agentic_coding_geospatial/` folder.
Download [agentic_coding_geospatial.zip](https://github.com/spatialthoughts/courses/releases/download/data/agentic_coding_geospatial.zip).
> Note: Certification and Support are only available for participants in our paid instructor-led classes.
# 1. Setting up Your Agent
A good practice is to always setup a [CLAUDE.md](https://code.claude.com/docs/en/memory) file for your project. This file contains instructions and rules to be followed when working inside the project directory. If you already have a directory with some code or have cloned a Git repository, Claude Code can automatically create the `CLAUDE.md` file using the `/init` command. Here we are starting with a blank project, so we will create a new `CLAUDE.md` file with instructions.
1. On your computer, launch *Visual Studio Code*. Click *Open Folder...* and select the `<home folder>/Desktop/agentic_coding_geospatial/` where you have unzipped the data package.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/init1.png')
```
2. We will create a new file at the root of this folder. Select *New File...*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/init2.png')
```
3. Enter the name of the file exactly as `CLAUDE.md`. Note that the filename is case sensitive and needs to have the `.md` extension.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/init3.png')
```
4. Paste the following content and save the file.
```
# CLAUDE.md
## Geospatial Stack Preferences
- Prefer Python-based approaches
- Use existing packages instead of building solutions from scratch
- My preference for geospatial packages are below
- Pandas for tabular data
- GeoPandas for vector geospatial data
- XArray ecosystem (rioxarray, xarray-spatial etc) for raster geospatial data
- Scikit-learn for Machine Learning
- GeoAI (geoai-py) for Deep Learning
- Web Apps
- Streamlit for data driven apps
- Leaflet for interactive mapping apps
- Self contained HTML for small apps
## Other Preferences
- Write code that is simple to understand and explain
- Always install python packages in a conda environment. Never install anything in the base environment. Ask the user for confirmation on their preferred conda environment before installing anything.
- Do not use emojis
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/init4.png')
```
5. Now we will start Claude Code. *(Windows users)*, search for Anaconda Powershell Prompt and launch it. *(Mac/Linux users)*: Launch a Terminal window. Use the `cd` command to change the current working directory to the project directory.
*Windows*
```{bash eval=FALSE}
cd Desktop\agentic_coding_geospatial
```
*Mac/Linux*
```{bash eval=FALSE}
cd Desktop/agentic_coding_geospatial
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/init5.png')
```
6. Once you are inside the project folder, launch Claude Code by entering the following command.
```{bash eval=FALSE}
claude
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/init6.png')
```
7. The first time you start Claude Code inside a directory, there will be a security notification. You can choose **Yes, I trust this folder**. At the prompt, you can invoke several [commands](https://code.claude.com/docs/en/commands) for configuration. You can type `/help` to see all available commands. Let's run a few commands. First enable [Plan Mode](https://code.claude.com/docs/en/permission-modes#analyze-before-you-edit-with-plan-mode) by entering the `/plan` command. Next choose the model using the `/model` command. The default model choice is good for now, and you can change the model at any time.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/init7.png')
```
Now we are ready to use Claude Code for our first data analysis task.
# 2. Geospatial Data Analysis (Mapping Crime Hotspots)
We will now use Claude Code to do some data exploration, cleaning and analysis. This will give you a feel of how agents work and how to guide them. We will take the Police.uk data on street-level crime for the City of London Police and identify crime hotspots.
## 2.1 Exploratory Data Analysis (EDA)
1. Enter the following prompt into Claude Code and press *Enter*. Make sure the *Plan mode* is on. This will ensure Claude Code will first make an implementation plan and wait for your approval before diving into coding.
```
I would like to do an exploratory data analysis of crime data in the folder london_crime_2024.
* Give me a summary of the rows, columns and any missing data.
* Flag any data quality issues.
* Create charts showing different types of crime and monthly patterns.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/eda1.png')
```
2. As the project-level `CLAUSE.md` states, all the work needs to be done in a conda virtual environment. Claude will prompt you to choose an environment. Choose the **claude_code_workshop** environment created for this class and press *Enter*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/eda2.png')
```
3. For the *Output format*, choose **Python script + saved PNGs**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/eda3.png')
```
4. Review the answers and select *Submit answers*. Press *Enter*.
> Depending on your operating system, Claude Code version, and selected model, you may see a slightly different set of questions.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/eda4.png')
```
5. Now Claude Code will go through the data and come up with a plan for building the required script for exploratory data analysis. You can review the plan and edit it as per your requirements. Use the **Ctrl+G** shortcut to open an editor with the plan and make any necessary edits. Once you are satisfied, confirm the execution by selecting **Yes, and use auto mode**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/eda5.png')
```
6. Claude Code will write the Python scripts and run it using the installed packages in the conda environment.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/eda6.png')
```
7. Once done, it will prints the summary of its findings.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/eda7.png')
```
8. The analysis will also produce some charts in the `output/` subfolder.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/london_crime_2024_eda.png')
```
## 2.2 Data Cleaning and Spatial Analysis
Now that we have a good understanding of the data structure, we can ask Claude to help us with analyzing the data. In this section, we will go through the workflow of identifying hotspots off thefts. Starting with data cleaning, we will iteratively improve our analysis and learn how we can use existing references (research papers, manuals etc.) to ground the behavior of the model.
1. Back in Claude Code, switch to the plan mode. You can use *Shift+Tab* till you see **plan mode on**. Enter the following prompt. Claude Code will ask you clarifying questions. Select **All 7 theft-family categories** for *Theft scope*.
```
I want to analyze thefts data. Suggest which categories should be merged.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot1.png')
```
2. For the *merge goal*, select **Align to official ONS groupings**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot2.png')
```
3. Select *Submit answers* and press *Enter*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot3.png')
```
4. Review the plan and make any adjustments if required. Select **Yes, and use auto mode** to proceed.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot4.png')
```
5. Review the summary and enter the following prompt to implement them.
```
Filter to data to only for the 7 theft-family categories and
write a single merged CSV with those records.
Remove missing data and unnecessary fields.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot5.png')
```
6. Claude will produce a merge script and the output CSV file.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot6.png')
```
7. This is the cleaned version of our original dataset. We can use this for mapping hotspots. Switch to the plan mode (*Shift+Tab*) and enter the following prompt.
```
I want to map the theft hotspots.
Suggest what would be the best technique for the analysis.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot7.png')
```
8. Answer the clarifying questions and select the **Kernel Density Estimation (KDE)** as the technique. Select *Submit answers* and press *Enter*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot8.png')
```
9. Once the analysis is planned, enter the following prompt to build the script.
```
Create a static visualiztion using matplotlib using gaussian_kde.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot9.png')
```
10. Claude Code will produce a script and a map with hotspot visualization.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot10.png')
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/theft_hotspot_kde.png')
```
11. While this is a good starting point, we can improve it by providing well-known and high quality reference material to guide the implementation. We have provided an extract of a research report [Mapping Crime: Understanding Hot Spots](https://www.ojp.gov/pdffiles1/nij/209393.pdf) in your data package that contains useful tips and best practices for crime mapping. We can improve our analysis by using this reference. Enter the plan mode (*Shift+Tab*) and write the following prompt.
```
I have some reference material for crime mapping in documents/hotspots.pdf.
Incorporate suggestions from that and give me an updated plan.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot11.png')
```
12. Select the option to use the **Quartic kernel** and deny option to extend the analysis beyond the scope of the original request.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot12.png')
```
13. Once the plan is ready, review it. We can be explicit about the desired output, so you can select **Tell Claude what to change**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot13.png')
```
14. Ask for the following change.
```
Save the updated script and chart with the _v2 suffix.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot14.png')
```
15. Claude Code will build the required script and produce the new hotspot visualization.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot15.png')
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/theft_hotspot_kde_v2.png')
```
16. The new map is much more interpretable and based in scientifically validated technique. Let's extract the hotspots. Enter the following prompt.
```
Use the criteria "Greater than 5x mean" to extract hotspots and convert then to polygons.
Save the results as a GeoJSON london_theft_hotspots.geojson.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot16.png')
```
17. The raster hotspots will be vectorized using the given threshold and saved as a GeoJSON file.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot17.png')
```
18. We can visualize and explore the results in GeoLibre. Open https://web.geolibre.app/ in a browser. Drag and drop the GeoJSON file and verify the results.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/hotspot18.png')
```
## 2.3 Monitoring Usage
It's a good practice to keep an eye on your token usage and costs. You can use the following command to know the utilization in the current session and how much quota is left in your plan.
```
/usage
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/usage.png')
```
# 3 Using Web APIs (Route Optimization)
In this section, we will learn how to use Claude Code to build Jupyter notebooks for data analysis and interactively guide the agent to reach the desired solution and validate the results. We will be implementing a real-world scenario of optimizing delivery routes based on a route optimization algorithm.
In your data package, you will find the following files in the `data/route_optimization/` folder.
* `Delivery_Locations.geojson`: A set of 100 addresses in Washington DC area representing delivery locations.
* `Grocery_Store_Locations.geojson`: A set of 14 grocery store locations which needs to fulfill the delivery to these locations.
We will use Claude Code to build a Jupyter Notebook that builds an end-to-end workflow that loads the data, finds optimal delivery routes and writes PDF manifests with delivery schedule for each store.
## 3.1 Using Claude Code in VS Code
Let's install the **Claude Code for VS Code** extension.
1. Click the *Extensions* button on the left-hand panel. Search for **Claude** and select the **Claude Code for VS Code by Anthropic** extension.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/vscode1.png')
```
2. Click *Install*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/vscode2.png')
```
3. Once installed, it can be invoked from the *Toggle Chat* button and selecting the *Claude Code* tab.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/vscode3.png')
```
## 3.2 Building Notebooks
Now that we have Claude Code running inside VS Code, we can prompt it to build a Jupyter notebook for us.
1. Ensure the workshop folder `agentic_coding_geospatial` is open in VS Code. Enter the following prompt.
```
I want to build a notebook to find optimal delivery routes.
Create a new notebook scripts/route_optimization.ipynb
Read the .geojson files in data/route_optimization/ folder and
display the data on an interactive map.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/notebook1.png')
```
2. This is a simple and straightforward task. Our project level `CLAUDE.md` already has preferences listed for how to setup the environment and what packages to use. Claude Code will start creating the notebook and will pause periodically for confirmation. You can choose **Yes** to proceed.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/notebook2.png')
```
3. Once the task finishes, a new notebook `route_optimization.ipynb` will be created in the `scripts/` folder as instructed.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/notebook3.png')
```
4. Locate the notebook in the *Explorer* tab and double-click to open it. The notebook will have the datasets visualized on an interactive map.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/notebook4.png')
```
## 3.3 Interactive Data Analysis
Now, let's build the core of our data analysis to find the optimal delivery routes. This is a complex problem that requires a lot more context and direction. We will use the *Plan mode* and guide Claude Code towards building the code that solves the problem.
1. Make sure the notebook `route_optimization.ipynb` is open. Open Claude Code from the *Toggle Chat* button.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive1.png')
```
2. Switch to the **Plan** mode.
```{r echo=FALSE, fig.align='center', out.width='50%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive2.png')
```
3. Enter the following prompt.
```
I want to use OpenRouteService API.
Give me options on different approaches to plan for the most optimal delivery routes from each store.
Here are the constrants:
- A single trip will consist of 10 deliveries at maximum.
- The delivery will originate at a grocery store and end at the same delivery store.
- All stores are capable of fulfilling the order but can do a maximum of 5 delivery trips each day.
I want to know the most optimal route to minimize the distance traveled along with a schedule of the trips.
```
```{r echo=FALSE, fig.align='center', out.width='50%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive3.png')
```
4. Claude Code will trigger the *AskUserQuestion* tool to collect more information. For the Vehicle Routing Problem (VRS) approach, select the **ORS Optimization API (VROOM)**.
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive4.png')
```
5. We need to enter our API key. Select **Other** and enter the API key in the response below, replacing the `<your-api-key>` with your actual API key. If you do have your key handy, you can login to your [HeiGIT Account](https://account.heigit.org/manage/key) and copy the long string displayed under *Basic Key*.
```
Yes, I have a key. Save the API key in a separate cell in the notebook.
ORS_API_KEY='<your-api-key>'
```
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive5.png')
```
6. For the *Conda env*, select the `claude_code_workshop` conda environment that we have setup. You can then click *Submit answers*.
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive6.png')
```
7. As we are asking for delivery schedules, you may be prompted to choose store operating hours. We keep things simple and choose **Trip order per store** listing.
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive7.png')
```
8. After obtaining the necessary information, Claude Code will kick-off an agent to do the planning. Approve any permissions it may need.
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive8.png')
```
9. The OpenRouteService API has limits on how many Origin-Destination pairs it can solve. If you get prompted to choose for an approach, choose **Two-stage: assign + batch, then ORS Optimization per trip** approach.
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive9.png')
```
10. Once the plan it ready, it will be opened in VS Code. Review the plan and suggest any changes by adding comments. You can select any text and enter comments on what you would like changed. For example, in the initial plan that Claude Code came up with - it wanted to use the `openrouteservice` Python package. We can instead ask it to use the API directly instead of a client library by adding a comment like below.
```
Instead, use the REST API directly.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive10.png')
```
11. After adding the comment, choose **Send feedback and keep planning**.
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive11.png')
```
12. Once the plan is modified and you are satisfied, choose **Yes, and auto-accept**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive12.png')
```
13. Claude Code will now start implementing the plan.
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive13.png')
```
14. Once the notebook is built, it will also test it to ensure it runs without any errors. Once everything is done, it will print a summary.
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive14.png')
```
15. Open the notebook and review the results. You will see the map with the planned routes for all the delivery locations.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/interactive15.png')
```
## 3.4 Validation and Export
When building with AI - you need to spend most of your time validating the results and guiding the model. Let's learn some techniques for validation.
1. When reviewing the initial output of the delivery route, you will notice that some of the routes are very long and does not look optimal. We can ask Claude Code to fix this using the following prompt.
```
I see that some stores are not assigned any deliveries even if they are quite close to an address.
The assignment tries to assign 10 deliveries in each route -
making some of them quite long while nearby stores end up without any deliveries.
Update the plan to address this problem.
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/validation1.png')
```
2. Claude Code will go through the notebook and come up with a fix. Give your approval to apply the fix.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/validation2.png')
```
3. Your prompt will also force Claude to do a more thorough validation of the results.
```{r echo=FALSE, fig.align='center', out.width='50%'}
knitr::include_graphics('images/agentic_coding_geospatial/validation3.png')
```
4. The updated routes look much better, but we still need to do a closer inspection. We can export the results in any vector data format, so we can use a GIS to view and verify it better. Enter the below prompt.
```
Save the routes as a single GeoJSON file in the outputs/route_optimization/
folder so I can validate the results.
```
```{r echo=FALSE, fig.align='center', out.width='50%'}
knitr::include_graphics('images/agentic_coding_geospatial/validation4.png')
```
5. A new file `routes.geojson` will be created. Locate it in the VS Code *Explorer*. Right-click and select *Reveal in File Explorer* to open the folder on your computer.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/validation5.png')
```
6. We can visualize and explore the results in GeoLibre. Open https://web.geolibre.app/ in a browser. Drag and drop `routes.geojson`, along with the source data `Delivery_Locations.geojson` and `Grocery_Store_Locations.geojson` and verify the results.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/validation6.png')
```
7. Once we are satisfied with the output, we can generate PDF manifests with the planned routes. These files are the output from our analysis that can be handed over to the operations team for implementation. Enter the following prompt. If prompted for a PDF library, choose **Install reportlab** and click *Submit answers*.
```
Generate PDF manifests in the outputs/route_optimization/ folder containing
delivery order and addresses for each trip.
1 manifest per store.
```
```{r echo=FALSE, fig.align='center', out.width='50%'}
knitr::include_graphics('images/agentic_coding_geospatial/validation7.png')
```
8. The code will be added to the notebook and PDFs will be generated.
```{r echo=FALSE, fig.align='center', out.width='50%'}
knitr::include_graphics('images/agentic_coding_geospatial/validation8.png')
```
9. The PDFs now contain delivery routes for each store.
```{r echo=FALSE, fig.align='center', out.width='60%'}
knitr::include_graphics('images/agentic_coding_geospatial/validation9.png')
```
# Assignment
Now you are ready to put your agentic coding skills to test by solving a spatial analysis problem from scratch.
**Problem Statement**
You are a data scientist for a real-estate company who wants to enhance their property listings by assigning a **livability score** to each of their properties to help buyers pick properties that suit their lifestyle.
Your task is to calculate such a score for all the *Apartment Buildings* in your city. Consider the factors that are important to home buyers. You can design the score using metrics such as:
* Total number of parks within a certain distance
* Total number of restaurants within a certain distance
* Distance to the nearest metro/rail/bus stop
* Distance to the nearest school
* ...
These are some examples and you can pick the criteria that make sense to you based on the knowledge of your city. You can use OpenStreetMap to obtain the data for apartment building polygons and the required amenities.
**Required output**
* A GeoPackage containing the apartment buildings layer enriched with attributes for each factor and the final livability score.
* A 1-page report on your analysis describing the methodology and steps you have taken to validate the results.
**Tips**
* You can prompt the agent to use the Python package `osmnx` which can easily extract the required data from OpenStreetMap.
* Instead of trying to one-shot the whole analysis, build your notebook by prompting the agent to plan and build one step at a time. Validate the output from each step before moving to the next.
* Start simply and use a simple buffer / linear distance. You may add an option to use an `network distance`/`isochrone` instead of simple circular buffer for a more realistic distances.
* Look at methodology followed by existing livability scores on websites such as [AreaVibes](https://www.areavibes.com/madison-wi/capitol/) and [PropTiger](https://www.proptiger.com/buzz/livability-score).
# 4. Using Skills
## 4.1 Installing a Skill
*Coming soon*
## 4.2 Using a Skill
*Coming soon*
## 4.3 Useful Geospatial Skills
*Coming soon*
# 5. Building a Personal Knowledge Website
## 5.1 Understanding the AI Second Brain
*Coming soon*
## 5.2 Setting up your personal knowledge base
*Coming soon*
## 5.3 Creating and publishing your website
*Coming soon*
# 6. Using GeoAI (Object Detection)
## 6.1 Configuring an MCP Server
MCP (Model Context Protocol) is an open-source standard for connecting agents to external systems. MCP servers are similar to APIs - but are designed specifically for use by AI Agents. MCP servers allow your agents to access specialized data sources and applications. We will setup and configure a connection to the [Google Colab MCP](https://github.com/googlecolab/colab-mcp) server which will allow Claude Code to create, modify and run Google Colab notebooks.
1. Start Claude Code in the Terminal. *(Windows users)*, search for Anaconda Powershell Prompt and launch it. *(Mac/Linux users)*: Launch a Terminal window. Use the `cd` command to change the current working directory to the project directory.
```{bash eval=FALSE}
claude
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/mcp1.png')
```
2. Enter the prompt below to setup the MCP server. In the world of agentic coding, you can point your agent to install instructions like these and it will configure itself instead of you doing this yourself.
```
Setup an MCP server according to the instructions at
https://github.com/googlecolab/colab-mcp
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/mcp2.png')
```
3. You can select the workshop Conda environment when prompted to select an environment for installing dependencies.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/mcp3.png')
```
4. Claude Code will install the dependency and update the local configuration file `.claude.json` with the required information. Restart Claude Code for the changes to take effect. You can exit the session using **Ctrl + C** and start it again by typing `claude`.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/mcp4.png')
```
5. Once the session is restarted, enter the following command.
```
/mcp
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/mcp5.png')
```
6. If everything went fine, you should see the `colab-mcp` server connected.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/mcp6.png')
```
## 6.2 Building Notebooks in the Cloud
Now that we have configured the MCP server, we can prompt Claude Code to create cloud-hosted notebooks using Google Colab.
1. Enter the following prompt.
```
Create a new colab notebook
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/colabnotebook1.png')
```
2. Claude Code will using the `colab-mcp` server to open a connection. Approve the execution requests.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/colabnotebook2.png')
```
3. A new tab will open in Google Chrome and you will be prompted to connect to the Colab MCP server. Click **Connect**. Ensure that you are signed-in to your Google account.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/colabnotebook3.png')
```
4. If this round-trip command was successful, a new notebook will be created. If you get errors, see the steps outlined in [Debugging Colab MCP Connection](#debugging-colab-mcp-connection).
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/colabnotebook4.png')
```
5. Next, let's add some code and execute it. Enter the following prompt.
```
Add a cell to print "hello world" and run it
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/colabnotebook5.png')
```
6. The MCP server will add the cell and run the notebook in a cloud-hosted machine.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/colabnotebook6.png')
```
### Debugging Colab MCP Connection
Colab MCP server can be a bit flaky and you may see errors when establishing the connection. Here are a few things you can try.
1. Type `/mcp` and check whether the `colab-mcp` server is connected. If it is disconnected, select it and press *Enter* to reconnect.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/colab_debug1.png')
```
2. If you have a Google Colab tab open but the Claude Code is not able to connect to it, you can go to **Tools → Command palette**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/colab_debug2.png')
```
3. Search for **mcp** and select *Connect to a local Colab MCP server* to connect that notebook to the MCP server.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/colab_debug3.png')
```
## 6.3 Using Deep Learning Models
1. We will now build a notebook to detect building footprints from an aerial image. Enter the following prompt.
```
Create a new Colab Notebook named "building_detection".
This notebook will use a pre-trained model for building footprint extraction from GeoAI and run it on the provided image.
Structure the notebook following this example https://opengeoai.org/examples/building_footprints_africa/
The provided image can be large. So use the GeoAI package to split the input image into tiles
Reproject the tiles to a suitable local UTM crs.
The model is trained on images around 50cm-1m resolution images. Check the resolution of the provided image and if it is higher resolution (i.e. 5cm, 10cm) - resample the tiles to 50cm resolution before running the inference.
Save the results as a GeoJSON file in EPSG:4326 CRS.
Use this image from OpenAerialMap to test the notebook https://oin-hotosm-temp.s3.us-east-1.amazonaws.com/69493c8084a859b011c94266/0/69493c8084a859b011c94267.tif
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building1.png')
```
2. Claude Code will now build the workflow and populate the cells in the Colab notebook. Once done, we need to change the runtime to use a GPU.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building2.png')
```
3. Open the notebook and click on the dropdown for *Additional connection options* and select *Change runtime type*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building3.png')
```
4. Select **T4 GPU** and click *Save*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building4.png')
```
5. Back in Claude Code, enter the following prompt.
```
Run the notebook
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building5.png')
```
6. The inference pipeline will be executed.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building6.png')
```
7. View the notebook and you will get the detected building footprints as a polygon layer. Let's download and view the results. Click on the **Files** tab from the left-hand panel in Colab.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building7.png')
```
8. Browse to the directory where the output is saved and locate the `building_footprint.geojson` file.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building8.png')
```
9. Click on the *:* button next to it and select *Download*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building9.png')
```
10. We can open the source imagery and the detections in GeoLibre Web for detailed QA. Visit https://web.geolibre.app/ and select **Add Data → Raster Layer**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building10.png')
```
11. Paste the URL of the image that we used for the notebook and click *Load*
```
https://oin-hotosm-temp.s3.us-east-1.amazonaws.com/69493c8084a859b011c94266/0/69493c8084a859b011c94267.tif
```
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building11.png')
```
12. Next, go to **Add Data → Vector Layer**. Click on the *Drop file or click to browse* panel. Browse to the downloaded GeoJSON file and select it and click *Load*.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building12.png')
```
13. Check how well the model performed in detecting and extracting the buildings.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building13.png')
```
14. Save the completed notebook by going to **File → Save a copy in Drive**.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/building14.png')
```
If you want to catch up to this step and use a sample notebook generated in this step, click the button below.
[](https://colab.research.google.com/github/spatialthoughts/courses/blob/master/code/agentic_coding_geospatial/buiding_detection.ipynb)
## Exercise
You will notice that since the footprints were vectorized from a raster layer, they have jagged edges. We can make the output polygons much better by *Regularizing* the polygons. GeoAI has several algorithms for [Building Regularization](https://opengeoai.org/examples/building_regularization/). Prompt Claude Code to add a step at the end to regularize the polygons and save them.
```{r echo=FALSE, fig.align='center', out.width='75%'}
knitr::include_graphics('images/agentic_coding_geospatial/regularization.png')
```