Tutorial

Python GeoJSON | Tutorial to Read, Write, Parse & Modify

6 min read

The big data era integrates geographical data as a crucial part of data analysis. GeoJSON, a popular open standard format, represents simple geographical features and their non-spatial attributes. Learn GeoJSON basics, Python setup, reading, writing, parsing, and manipulating data with the GeoJSON library in Python.

Understanding GeoJSON Data

GeoJSON is a format for encoding geographic data structures. It is built upon the JSON (JavaScript Object Notation) format and incorporates a diverse set of geometrical objects. These objects include Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.

In the GeoJSON structure, a Feature represents a fusion of a geometry object and additional properties, while a Feature Collection is an array or list of Feature objects.

Let’s explore these components in more detail:

  • Point: This represents a single location in a two-dimensional space, defined by its coordinates. Points are the simplest form of geographical data and can represent anything from a city to a parking spot.
  • LineString: This is a series of points that form a line in space. LineStrings can represent roads, paths, or any linear geographical feature.
  • Polygon: This is a closed shape in a two-dimensional space, defined by a series of points. Polygons can represent areas such as countries, states, or parks.
  • MultiPoint, MultiLineString, and MultiPolygon: These are collections of multiple Points, LineStrings, and Polygons, respectively. They allow for the representation of complex geographical features that cannot be represented by a single Point, LineString, or Polygon.
  • GeometryCollection: This is a collection of different types of geometrical objects, allowing for the representation of complex geographical features that consist of different types of geometries.

In GeoJSON, each of these geometrical objects can be associated with a set of properties, providing additional information about the geographical feature they represent. For example, a Point representing a city could have properties such as population, area, or GDP.

Setting Up the Python Environment for GeoJSON

To manipulate GeoJSON data in Python, it’s essential to have at least Python version 3.7 installed on the system. The geojson library, which provides tools for working with GeoJSON data, can be installed using pip, a widely used package installer for Python. Here’s the command to do it:

pip install geojson

Once the geojson library is installed, you can import it into your Python script using the following line of code:

import geojson

With the geojson library, you can read, write, and manipulate GeoJSON data right in your Python environment. It provides classes for GeoJSON’s geometrical objects (Point, LineString, Polygon, etc.), allowing you to create, manipulate, and analyze geographical features in your Python scripts.

Exploring the geojson Library Features

Let’s start with an example of how to read GeoJSON data  in Python with the geojson library:

Before reading the data, let’s understand the GeoJSON file we are going to read.

The file data.geojson is a GeoJSON file that you have stored locally.

This is what it looks like:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}

In this GeoJSON data, we have:

  • The type field indicates that this data represents a Feature.
  • The geometry field contains the geographical data. In this case, it’s a Point with coordinates [125.6, 10.1].
  • The properties field contains additional information about the geographical feature. In this case, it’s the name of the location, which is “Dinagat Islands”.

Parsing GeoJSON data in Python

Now, let’s read the data from this file:

import geojson
import pprint as pp

with open('data.geojson', 'r') as file:
    geojson_data = geojson.load(file)

# Now, let's print the GeoJSON data to understand its structure
pp.pprint(dict(geojson_data))

The geojson.load(file) function reads the GeoJSON data from the file. The printed output will be the GeoJSON data contained in the file.

See the output below for the printout:

output for geojson data file

Writing GeoJSON data to a file

Now, let’s see how you can write GeoJSON data to a file:

import geojson

# Create a Point object
point = geojson.Point((-3.68, 40.41))

with open('output.geojson', 'w') as file:
    geojson.dump(point, file)

# Now, let's read the data back from the file to verify
with open('output.geojson', 'r') as file:
    geojson_data = geojson.load(file)
print(geojson_data)

In this code, geojson.Point((-3.68, 40.41)) create a Point object with the specified coordinates. The geojson.dump(point, file) function writes the GeoJSON data to the file.

The resulting output.geojson file will contain the GeoJSON representation of the point.

The printed output will be the GeoJSON data that was written to the file:

output for geojson save point data to output geojson file and display geojson file

This data represents a point with the coordinates (-3.68, 40.41).

Manipulating GeoJSON Data in Python

The geojson library in Python provides a robust set of tools for working with GeoJSON data.

It allows for the extraction of specific data from GeoJSON objects, modification of the data, and creation of new GeoJSON objects.

Extracting Specific Data from GeoJSON

Data extraction is a crucial aspect of working with GeoJSON data. The geojson library makes this process straightforward.

For instance, to extract the 'name' property from each feature in a GeoJSON file, the following code can be used:

“`python
import geojson

with open(‘data2.geojson’, ‘r’) as file:
geojson_data = geojson.load(file)

for feature in geojson_data[‘features’]:
print(feature[‘properties’][‘name’])
“`

This code opens a GeoJSON file (data2.json), that resembles the following:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [125.6, 10.1]
            },
            "properties": {
                "name": "Dinagat Islands"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [123.5, 7.1]
            },
            "properties": {
                "name": "Siargao Islands"
            }
        }
    ]
}

The code loads the contents of data2.geojson into a Python object, and then iterates over the features in the GeoJSON data.

For each feature, it prints the value of the 'name' property.

See the output below:

output for reading geojson file having featurecollection of point features

Modifying Python GeoJSON Data

The geojson library provides the capability to modify GeoJSON data. This modification can occur by altering the properties of the features.

For instance, the 'name' property of the first feature can be changed with the following code:

geojson_data['features'][0]['properties']['name'] = 'New Name'

This code alters the 'name' property of the first feature in the GeoJSON data to 'New Name'.

Creating New GeoJSON Objects

The geojson library facilitates the creation of new GeoJSON objects.

For example, a new Point object can be instantiated with the following code:

new_point = geojson.Point((-3.68, 40.41))

This code results in the creation of a new Point object with the given coordinates.

Saving Changes

Saving changes after modifying GeoJSON data or creating new GeoJSON objects involves writing the data back to a file.

The Python code snippet below demonstrates this:

with open('data.geojson', 'w') as file:
    geojson.dump(geojson_data, file)

This code writes the modified GeoJSON data back to the original file. If using a different file is intended, replace ‘data.geojson’ with the desired filename.

Conclusion

Utilizing the geojson library for working with GeoJSON data in Python proves to be straightforward and efficient. It caters to a variety of needs, whether it’s geographical data for data analysis, machine learning, or map visualization. The combination of GeoJSON and Python simplifies the task significantly.

We hope you enjoyed the GeoJSON tutorial with Python! Please share your thoughts in the comments below.

If you’re interested in more Python guides, consider hiring an expert tutor for personalized online Python lessons.

Enjoy Python coding!