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):
parsed_json = json.loads(json_data)
feature = geojson.Feature(geometry = parsed_json[ "geometry" ], properties = parsed_json[ "properties" ])
feature_collection = geojson.FeatureCollection([feature])
return feature_collection
example_json_data =
geojson_result = convert_json_to_geojson(example_json_data)
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 )]}
linestring_geojson = geojson.LineString(coordinates = linestring_data[ "coordinates" ])
feature_collection = geojson.FeatureCollection(features = [geojson.Feature(geometry = linestring_geojson)])
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
polygon_data = { "coordinates" : [[( 0 , 0 ), ( 0 , 1 ), ( 1 , 1 ), ( 1 , 0 ), ( 0 , 0 )]]}
polygon_geojson = geojson.Polygon(coordinates = polygon_data[ "coordinates" ])
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.
Share your thoughts in the comments
Please Login to comment...