Create MicroJSON models
In this tutorial, we explain how a Python script is used to convert a pandas DataFrame into a MicroJSON FeatureCollection object.
Example dataframe to MicroJSON conversion
Transforms a pandas DataFrame into a FeatureCollection model.
This function is designed to convert geometries stored in a pandas DataFrame into a FeatureCollection model, based on the MicroJSON schema.
Parameters: - df: The pandas DataFrame to transform. Each row should represent a feature.
Returns: - A FeatureCollection object that aggregates the individual features.
Source code in src/microjson/examples/df_to_microjson.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
Here's a breakdown of the steps involved:
-
Initialize an empty list of Features: A list
features
is initialized to holdFeature
objects. -
Iterate over DataFrame rows: The function iterates through each row of the DataFrame, performing the following operations for each row:
-
Create a Geometry Object: It dynamically generates a Geometry object based on the geometry type specified in the row.
-
Create a Properties Object: It then creates a
Properties
object to hold metadata about the feature like the name, value, and an array of values. -
Combine into a Feature: It combines both the geometry and properties into a MicroJSON
Feature
object.
-
-
Calculate Value Ranges: For each numeric attribute, a range of values (min, max) is calculated.
-
Create a FeatureCollection Object: Finally, it aggregates all the features into a MicroJSON
FeatureCollection
object, including the calculated value ranges and other optional metadata.
Example dataframe creation, conversion and MicroJSON output
Below, we convert a pandas DataFrame into a MicroJSON FeatureCollection object. It includes an example of creating a DataFrame with two features, one Point and one Polygon, and converting it into a FeatureCollection model using the df_to_microjson
function as described above, and then serializing the model to a JSON string.
if __name__ == "__main__":
# Example DataFrame with two features: one Point and one Polygon
data = [
{
"type": "Feature",
"geometryType": "Point",
"coordinates": [0, 0],
"name": "Point 1",
"value": 1,
"values": [1, 2, 3],
},
{
"type": "Feature",
"geometryType": "Polygon",
"coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]],
"name": "Polygon 1",
"value": 2,
"values": [4, 5, 6],
},
]
# Convert this list of dictionaries into a DataFrame
df = pd.DataFrame(data)
# Convert the DataFrame into a FeatureCollection model
feature_collection_model = df_to_microjson(df)
# Serialize the FeatureCollection model to a JSON string
print(
feature_collection_model.model_dump_json(indent=2, exclude_unset=True)
)