diff --git a/raster/r.profile/r.profile.html b/raster/r.profile/r.profile.html index a2e75eb0cc7..b5801a28055 100644 --- a/raster/r.profile/r.profile.html +++ b/raster/r.profile/r.profile.html @@ -219,6 +219,41 @@

JSON Output

] +

Using JSON output with Python for plotting data

+ +The JSON output makes for ease of integration with popular python data science libraries. For instance, here +is an example of creating a scatterplot of distance vs elevation with color coding. + +
+import grass.script as gs
+import pandas as pd
+import matplotlib.pyplot as plt
+
+# Run r.profile command
+elevation = gs.read_command(
+    "r.profile",
+    input="elevation",
+    coordinates="641712,226095,641546,224138,641546,222048,641049,221186",
+    format="json",
+    flags="gc"
+)
+
+# Load the JSON data into a dataframe
+df = pd.read_json(elevation)
+
+# Convert the RGB color values to hex format for matplotlib
+df["color"] = df.apply(lambda x: "#{:02x}{:02x}{:02x}".format(int(x["red"]), int(x["green"]), int(x["blue"])), axis=1)
+
+# Create the scatter plot
+plt.figure(figsize=(10, 6))
+plt.scatter(df['distance'], df['elevation'], c=df['color'], marker='o')
+plt.title('Profile of Distance vs. Elevation with Color Coding')
+plt.xlabel('Distance (meters)')
+plt.ylabel('Elevation')
+plt.grid(True)
+plt.show()
+
+

SEE ALSO

diff --git a/raster/r.profile/read_rast.c b/raster/r.profile/read_rast.c index afc051571cc..7d2a5e77ecb 100644 --- a/raster/r.profile/read_rast.c +++ b/raster/r.profile/read_rast.c @@ -55,8 +55,10 @@ int read_rast(double east, double north, double dist, int fd, int coords, switch (format) { case JSON: - json_object_set_number(object, "easting", east); - json_object_set_number(object, "northing", north); + if (coords) { + json_object_set_number(object, "easting", east); + json_object_set_number(object, "northing", north); + } json_object_set_number(object, "distance", dist); break; case PLAIN: