
Folium is an excellent python library that makes it easy to visualise geospatial data on interactive maps using the power of Leaflet.js. In my previous article, I covered how to display individual markers on a Folium map, but we can use folium also display data that has been stored within a GeoJSON file.
GeoJSON is a commonly used file format for storing geospatial data and uses JavaScript Object Notation. These files can store location data, shapes, points, surfaces, etc.
Within this article, we will see how to display polygons of UK North Sea oil and gas fields that have been stored within a GeoJSON file format.
The data for this article is sourced from data.gov.uk and is licenced under the Open Government Licence. The data can be viewed and downloaded from the following website.
Setting up the Libraries and Displaying a Basic Folium Map
Installing Folium
If you don’t already have folium installed, you can install it via pip:
pip install folium
Or Anaconda:
conda install folium
Once you have the library installed, the first step is to import the folium library into our Jupyter Notebook:
import folium
To keep things simple, we will assign the location of the GeoJSON file to a variable. This makes things simpler when we want to call upon it later or change it for another file without changing the rest of the code directly.
field_locations = 'geodata/NSTA_Offshore_Fields_WGS84.geojson'
Next, we need to initialise a folium map with some basic parameters.
The created map will be centred around a latitude of 58 degrees N and a longitude of 5 degrees E. We will also set the zoom_start
to 6 to allow us to view most of the UK North Sea.
There are several other parameters we can use when creating a folium map. If you want to learn more, check out the help documentation on the folium.map class.
m = folium.Map(location=[58, 5],
zoom_start=6, control_scale=True)
m
When we call upon the folium map object using the variable m
we will get the following map displayed:

This generated map looks bare, so the next step is to add some shapes to identify the UK offshore oil and gas fields.
Adding GeoJSON Data to a Folium Map
To display the data from a GeoJSON file, we need to call upon folium.geojson
and pass in a few parameters.
The first parameter is the location of the GeoJSON file, which we assigned to the variable field_locations
. This is then followed by the tooltip
parameter, which allows us to display information in the tooltip when any of the shapes are hovered over. In this example, we will set the fields to be displayed to the name of the oil or gas field (FIELDNAME
).
Finally, to make the objects appear, we need to add the extension add_to(m)
at the end.
folium.GeoJson(field_locations,
tooltip=folium.GeoJsonTooltip(fields=['FIELDNAME'])
).add_to(m)
We are presented with the following map when we call upon the folium map object: m
.
From the generated map, we can see the locations of all of the UK North Sea oil and gas fields.

Applying Custom Colours to GeoJson Shapes in Folium
If we want to add some styling to the shapes on the map, we can do so by first creating a function which will be used to control the colour of the shape.
From this dataset, we will be using the type of field to choose the colours that we want to display. Any oil fields will be displayed in green, gas fields shown in red, and condensate fields will be displayed in orange.
The following code was derived from this StackOverflow post.
def field_type_colour(feature):
if feature['properties']['FIELDTYPE'] == 'COND':
return 'orange'
elif feature['properties']['FIELDTYPE'] == 'OIL':
return 'green'
elif feature['properties']['FIELDTYPE'] == 'GAS':
return 'red'
Now that we have the conditional function set up, we need to add a new parameter to our call to folium.GeoJson
called style_function
. Within this parameter, we pass in a lambda function containing a dictionary for the styling properties.
When it comes to the fillColor
property, we call upon the function we created above and pass in our feature
.
We can also pass additional styling properties to the dictionary. For this example, we will set the fillOpacity
to 0.9 and the weight, which controls the line around the shapes, to 0.
If we don’t want to include the shapes from the previous section, we need to recreate the folium map again.
m = folium.Map(location=[58, 5],
zoom_start=6, control_scale=True)
folium.GeoJson(field_locations, name='geojson',
tooltip=folium.GeoJsonTooltip(fields=['FIELDNAME']),
style_function= lambda feature: {'fillColor':field_type_colour(feature),
'fillOpacity':0.9, 'weight':0}
).add_to(m)
m
When we call upon m
, we are presented with the following map.

We can now see that each shape/field that is on the map is coloured based on the primary type of hydrocarbon that is contained within the reservoir section.
Adding Additional Information to the Folium Tooltip
Within the GeoJSON file, we have several additional properties for each shape. These can easily be added to the existing tooltip by including them within the list for the fields
parameter.
m = folium.Map(location=[58, 5],
zoom_start=6, control_scale=True)
folium.GeoJson(field_locations, name='geojson',
tooltip=folium.GeoJsonTooltip(fields=['FIELDNAME',
'PROD_DATE',
'CURR_OPER']),
style_function= lambda feature: {'fillColor':field_type_colour(feature),
'fillOpacity':0.9, 'weight':0}).add_to(m)
m
When we rerun the code, we can now see that we get the extra properties when we hover over each of the shapes. In this case, we can see the field name, the date production started, and the current operator of the field.

Summary
Within this short article, we have seen how easy it is to display data stored within a GeoJSON file on an interactive folium map. This allows us to quickly display shapes and outlines that may be stored within this type of file.
Thanks for reading. Before you go, you should definitely subscribe to my content and get my articles in your inbox. You can do that here! Alternatively, you can sign up for my newsletter to get additional content straight into your inbox for free.
Secondly, you can get the full Medium experience and support me and thousands of other writers by signing up for a membership. It only costs you $5 a month, and you have full access to all of the fantastic Medium articles and the chance to make money with your writing. If you sign up using my link, you will support me directly with a portion of your fee, and it won’t cost you more. If you do so, thank you so much for your support!