Skip to content

Commit

Permalink
Merge pull request #411 from dimagi/cz/maps-fixes
Browse files Browse the repository at this point in the history
fix bad data in admin map api
  • Loading branch information
calellowitz authored Oct 9, 2024
2 parents 2e0ef96 + 8cbe507 commit 48fa8b2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
46 changes: 45 additions & 1 deletion commcare_connect/reports/tests/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
PaymentUnitFactory,
UserVisitFactory,
)
from commcare_connect.reports.views import _get_table_data_for_quarter
from commcare_connect.reports.views import _get_table_data_for_quarter, _results_to_geojson


@pytest.mark.django_db
Expand Down Expand Up @@ -62,3 +62,47 @@ def test_delivery_stats(opportunity: Opportunity):
assert unknown_delivery_type_data[0]["users"] == 0
assert unknown_delivery_type_data[0]["services"] == 0
assert unknown_delivery_type_data[0]["beneficiaries"] == 0


def test_results_to_geojson():
# Test input
results = [
{"gps_location_long": "10.123", "gps_location_lat": "20.456", "status": "approved", "other_field": "value1"},
{"gps_location_long": "30.789", "gps_location_lat": "40.012", "status": "rejected", "other_field": "value2"},
{"gps_location_long": "invalid", "gps_location_lat": "50.678", "status": "unknown", "other_field": "value3"},
{"status": "approved", "other_field": "value4"}, # Case where lat/lon are not present
{ # Case where lat/lon are null
"gps_location_long": None,
"gps_location_lat": None,
"status": "rejected",
"other_field": "value5",
},
]

# Call the function
geojson = _results_to_geojson(results)

# Assertions
assert geojson["type"] == "FeatureCollection"
assert len(geojson["features"]) == 2 # Only the first two results should be included

# Check the first feature
feature1 = geojson["features"][0]
assert feature1["type"] == "Feature"
assert feature1["geometry"]["type"] == "Point"
assert feature1["geometry"]["coordinates"] == [10.123, 20.456]
assert feature1["properties"]["status"] == "approved"
assert feature1["properties"]["other_field"] == "value1"
assert feature1["properties"]["color"] == "#00FF00"

# Check the second feature
feature2 = geojson["features"][1]
assert feature2["type"] == "Feature"
assert feature2["geometry"]["type"] == "Point"
assert feature2["geometry"]["coordinates"] == [30.789, 40.012]
assert feature2["properties"]["status"] == "rejected"
assert feature2["properties"]["other_field"] == "value2"
assert feature2["properties"]["color"] == "#FF0000"

# Check that the other cases are not included
assert all(f["properties"]["other_field"] not in ["value3", "value4", "value5"] for f in geojson["features"])
35 changes: 22 additions & 13 deletions commcare_connect/reports/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,28 @@ def _results_to_geojson(results):
"rejected": "#FF0000",
}
for result in results:
feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [float(result["gps_location_long"]), float(result["gps_location_lat"])],
},
"properties": {
key: value for key, value in result.items() if key not in ["gps_location_lat", "gps_location_long"]
},
}
color = status_to_color.get(result["status"], "#FFFF00")
feature["properties"]["color"] = color
geojson["features"].append(feature)
# Check if both latitude and longitude are not None and can be converted to float
if result.get("gps_location_long") and result.get("gps_location_lat"):
try:
longitude = float(result["gps_location_long"])
latitude = float(result["gps_location_lat"])
except ValueError:
# Skip this result if conversion to float fails
continue

feature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [longitude, latitude],
},
"properties": {
key: value for key, value in result.items() if key not in ["gps_location_lat", "gps_location_long"]
},
}
color = status_to_color.get(result.get("status", ""), "#FFFF00")
feature["properties"]["color"] = color
geojson["features"].append(feature)

return geojson

Expand Down

0 comments on commit 48fa8b2

Please sign in to comment.