Better GPT-4 Prompting For Interactive Python Plotly GIS Maps
Source: tai_blog

There are some terrific sources for data sets out there on the internet  including historical shipwreck data.   One of the weekly updates I receive as part of expanding my knowledge on available datasets comes from Data is Plural:   This site provides a weekly newsletter on interesting data sets.   In the most recent version (20240710)  there is a dataset on ancient shipwrecks (from Harvard University). This data set containse records from approximately 1500BC to 1500AD and is focused around the Mediterranean Sea and northern Europe. Super cool!   I was curious if I could upload this dataset to GPT-4 and have it give me some Python code that places each piece of point data onto a map.   The answer? Yes  it can. But not without some careful prompting. Heres the steps I went through  so that you dont have to.   Starting With A Generic GPT-4 PromptTo start off with  I just wanted to see if GPT-4 could interpret the data set in a meaningful way. A generic prompt to see if it can generate GIS map code.   Prompt to GPT-4: Using the attached dataset  give me the python code for a global map that displays each shipwreck as a point on the map.   GPT-4 Response:   OK  thats a chart  interestingly  each point is accurate if we put in a map layer underneath. But this is not what we are looking for.   So the first generic prompt was semi-successful. At this point  I needed to get more specific.   Prompt to GPT-4: That is not a map. Please generate Python code that displays each point on an actual map display.   This time around  GPT-4 creates the Python code using the matplotlib and shapely libraries. The result:   OK  we now have a map  but this map is completely static. It does not allow zooming in and out and there is no interactivity.   It is also extremely difficult to interpret each individual point. I can see that the points are mostly in Europe.   Prompting for Interactive MappingLets get even more specific and ask GPT-4 for some interactivity options.   Prompt to GPT-4: I need interactivity. I want to be able to zoom in on the map and hover over each data point to display the ship name and the cargo.   GPT-4 creates the Python code for a folium map using tooltips:   Wow  a lot more detail here. We can more clearly see data points that identify individual shipwrecks. Unfortunately  the map is created as an HTML file  which is not ideal. Very nice to look at  but this is a bit of a kludgy approach. What GPT-4 needs is some even tighter guidance.   Prompt to GPT-4: This doesnt seem like a good approach. Why wouldnt you use Plotly to do this?   Response from GPT-4: What a great idea! Lets rewrite the code using Plotly for better interactivity:   import pandas as pd import plotly.express as px # Load the dataset file_path = 'Geodatabase of Shipwrecks Hardard.csv' shipwreck_data = pd.read_csv(file_path) # Fix column headers shipwreck_data.columns = shipwreck_data.iloc[0] shipwreck_data = shipwreck_data.drop(0) # Filter out rows without valid coordinates shipwreck_data = shipwreck_data.dropna(subset=['Latitude'  'Longitude']) # Convert Latitude and Longitude to numeric shipwreck_data['Latitude'] = pd.to_numeric(shipwreck_data['Latitude']  errors='coerce') shipwreck_data['Longitude'] = pd.to_numeric(shipwreck_data['Longitude']  errors='coerce') # Filter out rows with invalid coordinates after conversion shipwreck_data = shipwreck_data.dropna(subset=['Latitude'  'Longitude']) # Create an interactive map with Plotly and Mapbox fig = px.scatter_mapbox(shipwreck_data  lat='Latitude'  lon='Longitude'  hover_name='NAME'  hover_data={'Longitude': False  'Latitude': False  'Cargo_1': True}  title='Global Map of Shipwrecks'  mapbox_style='carto-positron') # Using mapbox # Update layout for better appearance fig.update_traces(marker=dict(color='red'  size=10)) # Set color size fig.update_layout( mapbox=dict( accesstoken='your_mapbox_access_token'  center={'lat': 44  'lon': 18}  # Center the map for Europe zoom=4 )  margin=dict(l=0  r=0  t=0  b=0)  title_font_size=24  font=dict(size=14) ) # Display the interactive map fig.show()And this code works just great.   The result:   Awesome  now were getting somewhere. And I can hover over each point data to find out the shipwreck name and and the cargo it was carrying (if known):   GPT-4 really can be leveraged effectively for creating Python code that displays GIS point data on a map.   The information to provide GPT-4 to ensure that it produces the results you are looking for with this example:   Load the dataset and fix the column headers.Filter out rows without valid coordinates.Convert Latitude and Longitude to numeric values.Create an interactive map using Plotlys scatter_mapbox function.Set each points color to red and size to 5 pixels.Display the map zoomed in to the Mediterranean Sea.Use the carto-positron style for Mapbox tiles.Center the map on latitude 34 and longitude 18 with a zoom level of 4.Add your Mapbox access token in the appropriate place in the code.Thats all there is to it.   In SummaryGPT-4 can easily create Python code to display GIS point data on a map. And it can do it from a simple CSV file.   The caveat here is that you need to carefully create your prompts in order to optimize this LLMs capabilities  as it may not give you the best response in its first (or second) attempt.   Having extensively used GPT-4  I know that there may be multiple paths to go down to before I find an optimal result. It is worth asking GPT-4 what approaches are available  or if you know of a better approach (as I knew in this example)  specifically prompt GPT-4 to utilize it.   And as with any LLM prompting exercise  it is important to note that GPT-4 is designed in a way to provide variance in the results  so the code that you receive from your prompting may not line up exactly with what I am showing here.   Leave me a comment and let me know how your prompting went!   If this type of story is right up your alley  and you want to support me as a writer  subscribe to my Substack.   Subscribe to Data at DepthWelcome to Data at Depth! Our site offers articles on data analysis and visualization with Python/AI. Learn to createjohnloewen.substack.com   On Substack  I publish a bi-weekly newsletter and articles that you wont find on the other platforms where I create content.