Skip to content

Commit

Permalink
r.horizon: change JSON format (OSGeo#3888)
Browse files Browse the repository at this point in the history
  • Loading branch information
petrasovaa authored Jun 28, 2024
1 parent 6748067 commit cb0e431
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 41 deletions.
35 changes: 18 additions & 17 deletions raster/r.horizon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,9 @@ void calculate_point_mode(const Settings *settings, const Geometry *geometry,

origin_point.maxlength = settings->fixedMaxLength;
/* JSON variables and formating */
JSON_Value *azimuths_value, *horizons_value, *distances_value;
JSON_Array *azimuths, *horizons, *distances;

JSON_Value *horizons_value;
JSON_Array *horizons;

switch (format) {
case PLAIN:
Expand All @@ -850,18 +851,15 @@ void calculate_point_mode(const Settings *settings, const Geometry *geometry,
fprintf(fp, "\n");
break;
case JSON:

json_object_set_number(json_origin, "x", xcoord);
json_object_set_number(json_origin, "y", ycoord);
azimuths_value = json_value_init_array();
azimuths = json_value_get_array(azimuths_value);
horizons_value = json_value_init_array();
horizons = json_value_get_array(horizons_value);
distances_value = json_value_init_array();
distances = json_value_get_array(distances_value);
break;
}
for (int i = 0; i < printCount; i++) {
JSON_Value *value;
JSON_Object *object;
OriginAngle origin_angle;
com_par(geometry, &origin_angle, angle, xp, yp);

Expand All @@ -872,7 +870,10 @@ void calculate_point_mode(const Settings *settings, const Geometry *geometry,
if (settings->degreeOutput) {
shadow_angle *= rad2deg;
}

if (format == JSON) {
value = json_value_init_object();
object = json_object(value);
}
if (settings->compassOutput) {
double tmpangle;

Expand All @@ -887,9 +888,10 @@ void calculate_point_mode(const Settings *settings, const Geometry *geometry,
fprintf(fp, "\n");
break;
case JSON:
json_array_append_number(azimuths, tmpangle);
json_array_append_number(horizons, shadow_angle);
json_array_append_number(distances, horizon.length);
json_object_set_number(object, "azimuth", tmpangle);
json_object_set_number(object, "angle", shadow_angle);
json_object_set_number(object, "distance", horizon.length);
json_array_append_value(horizons, value);
break;
}
}
Expand All @@ -902,9 +904,10 @@ void calculate_point_mode(const Settings *settings, const Geometry *geometry,
fprintf(fp, "\n");
break;
case JSON:
json_array_append_number(azimuths, printangle);
json_array_append_number(horizons, shadow_angle);
json_array_append_number(distances, horizon.length);
json_object_set_number(object, "azimuth", printangle);
json_object_set_number(object, "angle", shadow_angle);
json_object_set_number(object, "distance", horizon.length);
json_array_append_value(horizons, value);
break;
}
}
Expand All @@ -924,9 +927,7 @@ void calculate_point_mode(const Settings *settings, const Geometry *geometry,
} /* end of for loop over angles */

if (format == JSON) {
json_object_set_value(json_origin, "azimuth", azimuths_value);
json_object_set_value(json_origin, "horizon_height", horizons_value);
json_object_set_value(json_origin, "horizon_distance", distances_value);
json_object_set_value(json_origin, "horizons", horizons_value);
}
}

Expand Down
48 changes: 24 additions & 24 deletions raster/r.horizon/testsuite/test_r_horizon.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,20 +225,20 @@ def test_point_mode_multiple_direction_json(self):
)
self.assertModule(module)
stdout = json.loads(module.outputs.stdout)
azimuths = []
horizons = []
distances = []
reference = {}
for line in ref6.splitlines()[1:]:
azimuth, horizon, distance = line.split(",")
azimuths.append(float(azimuth))
horizons.append(float(horizon))
distances.append(float(distance))
horizons.append(
{
"azimuth": float(azimuth),
"angle": float(horizon),
"distance": float(distance),
}
)
reference["x"] = 634720.0
reference["y"] = 216180.0
reference["azimuth"] = azimuths
reference["horizon_height"] = horizons
reference["horizon_distance"] = distances
reference["horizons"] = horizons

self.assertListEqual([reference], stdout)

Expand All @@ -255,20 +255,20 @@ def test_point_mode_multiple_points_and_directions_json(self):
)
self.assertModule(module)
stdout = json.loads(module.outputs.stdout)
azimuths = []
horizons = []
distances = []
reference = {}
for line in ref6.splitlines()[1:]:
azimuth, horizon, distance = line.split(",")
azimuths.append(float(azimuth))
horizons.append(float(horizon))
distances.append(float(distance))
horizons.append(
{
"azimuth": float(azimuth),
"angle": float(horizon),
"distance": float(distance),
}
)
reference["x"] = 634720.0
reference["y"] = 216180.0
reference["azimuth"] = azimuths
reference["horizon_height"] = horizons
reference["horizon_distance"] = distances
reference["horizons"] = horizons

self.assertListEqual([reference, reference], stdout)

Expand Down Expand Up @@ -313,20 +313,20 @@ def test_point_mode_multiple_direction_artificial_distance(self):
)
self.assertModule(module)
stdout = json.loads(module.outputs.stdout)
azimuths = []
horizons = []
distances = []
reference = {}
for line in ref5.splitlines()[1:]:
azimuth, horizon, distance = line.split(",")
azimuths.append(float(azimuth))
horizons.append(float(horizon))
distances.append(float(distance))
horizons.append(
{
"azimuth": float(azimuth),
"angle": float(horizon),
"distance": float(distance),
}
)
reference["x"] = 637505.0
reference["y"] = 221755.0
reference["azimuth"] = azimuths
reference["horizon_height"] = horizons
reference["horizon_distance"] = distances
reference["horizons"] = horizons

self.assertListEqual([reference], stdout)

Expand Down

0 comments on commit cb0e431

Please sign in to comment.