Skip to content

Commit

Permalink
address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kritibirda26 committed Jun 29, 2024
1 parent 08f05d0 commit aafad94
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
35 changes: 35 additions & 0 deletions raster/r.profile/r.profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,41 @@ <h3>JSON Output</h3>
]
</pre></div>

<h3>Using JSON output with Python for plotting data</h3>

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.

<div class="code"><pre>
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()
</pre></div>

<h2>SEE ALSO</h2>

<em>
Expand Down
6 changes: 4 additions & 2 deletions raster/r.profile/read_rast.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit aafad94

Please sign in to comment.