And again I had a little programming challenge I wanted to test ChatGPT with, and to hopefully get a result more quickly: I have an CSV input file that contains locations with GPS coordinates. The file is huge and I’m only interested in entries that are close to a given location. Such a kind of search didn’t sound like it should be done with a Bash script, so I decided that I wanted to approach this with Python. So how did ChatGPT do?
One way to filter the input file is to get the GPS coordinates of the location I’m interested in e.g. via OpenStreetMaps, and then search all lines with GPS coordinates within a certain radius, let’s say 10 or 20 kilometers. So here’s the question I asked ChatGPT to get going:
“I need a python program that reads a csv file with gps coordinates and filters all lines that contain coordinates around a gps location with a maximum radius given in km”
And indeed, right way, ChatGPT gave me code that looked quite sensible. It told me that I should install the ‘geopy’ library, as it already contains a function to calculate the distance between two GPS coordinates. The code looked good but didn’t work straight ‘out of the box’ for a number of reasons, so I had to iterate my way to a result:
- The CSV columns were separated with a ; instead of a , so the resulting dataset didn’t look like much. ChatGPT could not have ‘known’ that, so not its fault.
- I didn’t tell ChatGPT in which columns to find the GPS coordinates so it assumed they are in column 1 and 2. Of course they weren’t. My mistake.
- Some lines in the input file were empty or did not contain coordinates. This throws an exception and makes the program stop. So I asked ChatGPT to give me some code to account for that, which it did.
- At some point during the iterative development of the code, ChatGPT inserted a line in the code to remove the second column of each line. No ideas why it did that.
- ChatGPT assumed that the first line of the CSV file was a header. But it wasn’t, it just contained the first line with GPS coordinates. So that piece of code had to be removed.
So far so good. With these modifications I got a good looking output file. Great! So let’s take this to the next level:
For most locations that were filtered, the output file contained several entries. These, I wanted to sort in an ascending order based on a numerical value in one field, and then for a numerical value in another field, i.e. a subsequent sort of entries with the same value in the first field. And again ChatGPT came up with code to do this. However, it didn’t work. I tried for half an hour to ask different questions and explain what the result was and what I wanted to have. Unfortunately, I couldn’t get the program to work. In the end, I decided to proceed the old fashioned way: Read the description of the sorting function provided by a Pyhton library, insert debugging code that would output input and partial results and debug my way out of this. In the end I got things working and the code looked pretty much the same as what was delivered by ChatGPT. Not sure what the exact difference was, I didn’t bother to compare.
So, long story short: ChatGPT is great, it would have taken me longer to do it all on my own and searching the web for partial answers for everything. But the tool is definitely not a silver bullet. When confronted with a program that doesn’t deliver the desired results, it runs out of ideas to debug and fix very quickly. Without a good understanding of programming, I wouldn’t have gotten very far. I think this is an interesting perspective on the topic.