rcpch / rcpch-audit-engine

Epilepsy12 Audit Platform
https://e12.rcpch.ac.uk/
GNU Affero General Public License v3.0
5 stars 5 forks source link

Geocoordinates not saved by default #1028

Closed eatyourpeas closed 2 months ago

eatyourpeas commented 2 months ago

(note identical issue in RCPCH NHS Organisations repository) What happens: If a new organisation is added and longitude and latitude are provided, these do not automatically create a value in the geo_coordinates field. What should happen: If a new Organisation is created (for example through the admin), any latitude and longitude entered should be used to create geocoordinates that are automatically plotted.

Proposed solution and Gotchas This should be added to the save method in the Organisation model. Note that the geocoordinates saved will not be accurate unless they are saved using the correct SRID (spatial reference identifier), which is 4326. Also, if the longitude and latitude are added later as part of an update method, the save method will not be called.

Code would therefore be:

from django.contrib.gis.db.models import Point
# if there are no existing coordinates for this organisation but longitude and latitude do have values, create a Point object
# and add to the model. Otherwise this record is being updated so just save the exisiting fields.
# NOTE a small vulnerability of this is that if longitude and latitude are added using the update method, this function will not be 
# called.
def save(self, *args, **kwargs):
    if self.longitude and self.latitude and self.geocode_coordinates is None:
        longitude = self.longitude
        latitude = self.latitude
        geocode_coordinates = Point(x=longitude, y=latitude, srid=4326)
        self.geocode_coordinates=geo_coordinates

    super(Organisation, self).save(*args, **kwargs)