Techno Blender
Digitally Yours.

How to Display Data From GeoJSON Files Using the Folium Python Library | by Andy McDonald | Feb, 2023

0 49


Photo by Aksonsat Uanthoeng, downloaded from Pexels.

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.

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:

Base folium map centred over the North Sea. Image by the author.

This generated map looks bare, so the next step is to add some shapes to identify the UK offshore oil and gas fields.

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.

Folium map showing the outlines of the UK North Sea oil and Gas fields. Image by the author.

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.

Folium map of the UK North Sea oil and gas fields, coloured by field type. Image by the author.

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.

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.

Folium map showing the outlines of the UK Oil and Gas fields with additional properties appearing when each area is hovered over—image by the author.

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.


Photo by Aksonsat Uanthoeng, downloaded from Pexels.

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.

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:

Base folium map centred over the North Sea. Image by the author.

This generated map looks bare, so the next step is to add some shapes to identify the UK offshore oil and gas fields.

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.

Folium map showing the outlines of the UK North Sea oil and Gas fields. Image by the author.

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.

Folium map of the UK North Sea oil and gas fields, coloured by field type. Image by the author.

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.

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.

Folium map showing the outlines of the UK Oil and Gas fields with additional properties appearing when each area is hovered over—image by the author.

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.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment