Convert JSON to GeoJSON Python
Last Updated :
19 Feb, 2024
GeoJSON has become a widely used format for representing geographic data in a JSON-like structure. If you have data in a standard JSON format and need to convert it into GeoJSON for mapping or analysis, Python provides several methods to make this conversion seamless. In this article, we will explore some commonly used methods to convert JSON to GeoJSON using practical code examples.
What is GeoJSON?
, short for Geographic JavaScript Object Notation, is an open standard format designed for representing simple geographical features along with their non-spatial attributes. It is based on JSON (JavaScript Object Notation) and is commonly used for encoding geographic data structures. GeoJSON is widely supported and can be easily parsed by various programming languages.
Here's an example of a simple GeoJSON Point feature, In this example:
- The "type" property indicates that it's a GeoJSON Feature.
- The "geometry" property specifies that the feature is a Point, and its coordinates are given in the "coordinates" property.
- The "properties" property contains non-spatial attributes like the name and population of the location.
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [ -73.9857, 40.7484 ]
},
"properties": {
"name": "New York City",
"population": 8175133
}
}
Convert JSON to GeoJSON Python
Below, are the methods of Converting JSON to GeoJSON Python.
- Using
JSON
& GeoJSON
libraries - Using LineString GeoJSON
- Using Polygon GeoJSON
Convert JSON to GeoJSON Using JSON
and GeoJSON
libraries
In this example, below Python script defines a function convert_json_to_geojson
that takes JSON data as input, parses it, extracts the "geometry" and "properties" fields, and converts it into a GeoJSON Feature with a Point geometry. The script then creates a GeoJSON FeatureCollection containing the converted Feature and prints the resulting GeoJSON with proper indentation.
Python
import json
import geojson
def convert_json_to_geojson(json_data):
#Parsing JSON data
parsed_json = json.loads(json_data)
# Converting to GeoJSON feature
feature = geojson.Feature(geometry=parsed_json["geometry"], properties=parsed_json["properties"])
#Create a GeoJSON FeatureCollection
feature_collection = geojson.FeatureCollection([feature])
return feature_collection
example_json_data = '''
{
"type": "Feature",
"properties": {
"name": "Example Feature"
},
"geometry": {
"type": "Point",
"coordinates": [0, 0]
}
}
'''
# Converting JSON to GeoJSON
geojson_result = convert_json_to_geojson(example_json_data)
#Printing the GeoJSON result.
print(geojson.dumps(geojson_result, indent=2))
Output
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
0,
0
]
},
"properties": {
"name": "Example Feature"
}
}
]
}
Convert JSON to GeoJSON Using LineString GeoJSON
In this example ,below Python script uses the geojson
library to create a GeoJSON LineString from a set of coordinates [(0, 0), (1, 1), (2, 2)]. It then constructs a GeoJSON Feature using the LineString and creates a FeatureCollection containing that Feature. Finally, the script prints the resulting GeoJSON with proper indentation.
Python
import geojson
linestring_data = {"coordinates": [(0, 0), (1, 1), (2, 2)]}
# Creating a GeoJSON LineString
linestring_geojson = geojson.LineString(coordinates=linestring_data["coordinates"])
# Creating a FeatureCollection with the LineString
feature_collection = geojson.FeatureCollection(features=[geojson.Feature(geometry=linestring_geojson)])
# Printing the output
print(geojson.dumps(feature_collection, indent=2))
Output :
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[0, 0],
[1, 1],
[2, 2]
]
},
"properties": null
}
]
}
Convert JSON to GeoJSON Using Polygon GeoJSON
In this example, below Python script uses the geojson
library to create a GeoJSON Polygon from a set of coordinates representing a square [(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]. It then prints the resulting GeoJSON Polygon with proper indentation.
Python
import geojson
# JSON data representing coordinates of a Polygon
polygon_data = {"coordinates": [[(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]]}
# Creating a GeoJSON Polygon geometry
polygon_geojson = geojson.Polygon(coordinates=polygon_data["coordinates"])
#Printing the Output...
print(geojson.dumps(polygon_geojson, indent=2))
Output
{
"type": "Polygon",
"coordinates": [
[
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0]
]
]
}
Conclusion
In conclusion, the provided Python scripts demonstrate the process of converting JSON data into GeoJSON format using the geojson
library. The scripts showcase the creation of GeoJSON objects, such as Point, LineString, and Polygon, from corresponding JSON structures. These examples illustrate how to extract relevant geometry and properties information, construct GeoJSON features, and organize them into FeatureCollections for spatial representation.
Similar Reads
Convert JSON to PNG in Python We are given JSON data and our task is to convert JSON to PNG in Python using different approaches. In this article, we will explore how to convert JSON data into PNG images using Python. Convert JSON to PNG in PythonBelow are some of the ways by which we can convert JSON to PNG in Python: Using pil
3 min read
Convert CSV to JSON using Python Converting CSV to JSON using Python involves reading the CSV file, converting each row into a dictionary and then saving the data as a JSON file. For example, a CSV file containing data like names, ages and cities can be easily transformed into a structured JSON array, where each record is represent
2 min read
Convert Excel To Json With Python In the realm of data manipulation and analysis, the ability to convert data between different formats is a valuable skill. Excel and JSON are two widely used formats, and Python, with its powerful libraries, provides a seamless way to convert Excel files into JSON. In this article, we will see how w
4 min read
Convert JSON to string - Python Data is transmitted across platforms using API calls. Data is mostly retrieved in JSON format. We can convert the obtained JSON data into String data for the ease of storing and working with it. Python provides built-in support for working with JSON through the json module. We can convert JSON data
2 min read
Convert String to JSON Object - Python The goal is to convert a JSON string into a Python dictionary, allowing easy access and manipulation of the data. For example, a JSON string like {"name": "John", "age": 30, "city": "New York"} can be converted into a Python dictionary, {'name': 'John', 'age': 30, 'city': 'New York'}, which allows y
2 min read