![]() |
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:
{ Convert JSON to GeoJSON PythonBelow, are the methods of Converting JSON to GeoJSON Python.
Convert JSON to GeoJSON Using
|
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"
}
}
]
}
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.
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
}
]
}
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.
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]
]
]
}
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.
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |