Skip to content

Commit

Permalink
r.horizon: always include distance output in JSON output (#3768)
Browse files Browse the repository at this point in the history
Given the [discussion](#3744 (comment)) about what should be part of JSON output, I changed the behavior to include the distance whether or not the -l flag is used. There is no computational reason not to.
  • Loading branch information
petrasovaa authored Jun 7, 2024
1 parent f13b379 commit 87847c2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
12 changes: 4 additions & 8 deletions raster/r.horizon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ int main(int argc, char *argv[])
flag.horizonDistance = G_define_flag();
flag.horizonDistance->key = 'l';
flag.horizonDistance->description =
_("Include horizon distance in the output");
_("Include horizon distance in the plain output");
flag.horizonDistance->guisection = _("Point mode");

flag.degreeOutput = G_define_flag();
Expand Down Expand Up @@ -889,8 +889,7 @@ void calculate_point_mode(const Settings *settings, const Geometry *geometry,
case JSON:
json_array_append_number(azimuths, tmpangle);
json_array_append_number(horizons, shadow_angle);
if (settings->horizonDistance)
json_array_append_number(distances, horizon.length);
json_array_append_number(distances, horizon.length);
break;
}
}
Expand All @@ -905,8 +904,7 @@ void calculate_point_mode(const Settings *settings, const Geometry *geometry,
case JSON:
json_array_append_number(azimuths, printangle);
json_array_append_number(horizons, shadow_angle);
if (settings->horizonDistance)
json_array_append_number(distances, horizon.length);
json_array_append_number(distances, horizon.length);
break;
}
}
Expand All @@ -928,9 +926,7 @@ void calculate_point_mode(const Settings *settings, const Geometry *geometry,
if (format == JSON) {
json_object_set_value(json_origin, "azimuth", azimuths_value);
json_object_set_value(json_origin, "horizon_height", horizons_value);
if (settings->horizonDistance)
json_object_set_value(json_origin, "horizon_distance",
distances_value);
json_object_set_value(json_origin, "horizon_distance", distances_value);
}
}

Expand Down
35 changes: 31 additions & 4 deletions raster/r.horizon/testsuite/test_r_horizon.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@
340.000000,0.196863,5004.018385
"""

ref6 = """azimuth,horizon_height,horizon_distance
180.000000,0.023101,420.000000
200.000000,0.034850,436.577599
220.000000,0.050549,184.390889
240.000000,0.048211,197.230829
260.000000,0.053101,162.788206
280.000000,0.039774,253.179778
300.000000,0.032360,277.848880
320.000000,0.014804,262.488095
340.000000,0.000000,0.000000
360.000000,0.004724,2780.017986
20.000000,0.012612,1148.259553
40.000000,0.015207,1334.166406
60.000000,0.014344,1867.966809
80.000000,0.011044,2964.203097
100.000000,0.012192,1828.223181
120.000000,0.007462,4270.667395
140.000000,0.004071,5659.231397
160.000000,0.015356,1666.883319
"""


class TestHorizon(TestCase):
circle = "circle"
Expand Down Expand Up @@ -206,15 +227,18 @@ def test_point_mode_multiple_direction_json(self):
stdout = json.loads(module.outputs.stdout)
azimuths = []
horizons = []
distances = []
reference = {}
for line in ref2.splitlines()[1:]:
azimuth, horizon = line.split(",")
for line in ref6.splitlines()[1:]:
azimuth, horizon, distance = line.split(",")
azimuths.append(float(azimuth))
horizons.append(float(horizon))
distances.append(float(distance))
reference["x"] = 634720.0
reference["y"] = 216180.0
reference["azimuth"] = azimuths
reference["horizon_height"] = horizons
reference["horizon_distance"] = distances

self.assertListEqual([reference], stdout)

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

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

Expand Down

0 comments on commit 87847c2

Please sign in to comment.