Open In App

Convert JSON to GeoJSON Python

Last Updated : 19 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads