Skip to content

Commit

Permalink
Save centroid of geometry when the geometry is not a point (#3423)
Browse files Browse the repository at this point in the history
* Save centroid of geometry when the geometry is not a point

* Fix test
  • Loading branch information
dimasciput authored Jul 28, 2023
1 parent e28a160 commit 918ed19
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
17 changes: 12 additions & 5 deletions bims/models/location_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,10 @@ def data_name(self):
def get_centroid(self):
""" Getting centroid of location site """

if self.geometry_point:
if (
self.geometry_point and
self.location_type.allowed_geometry == 'POINT'
):
return self.geometry_point
else:
if self.get_geometry():
Expand All @@ -198,10 +201,11 @@ def get_geometry(self):
geometry = None
validation_error = ValidationError('Only one geometry allowed.')

if self.geometry_point:
if geometry:
raise validation_error
geometry = self.geometry_point
if (
self.geometry_point and
self.location_type.allowed_geometry == 'POINT'
):
return self.geometry_point

if self.geometry_polygon:
if geometry:
Expand Down Expand Up @@ -403,6 +407,9 @@ def save(self, *args, **kwargs):
if not lon_lat_changed:
self.geometry_point.x = self.longitude
self.geometry_point.y = self.latitude
if self.location_type.allowed_geometry != 'POINT':
self.geometry_point = self.get_centroid()

super(LocationSite, self).save(*args, **kwargs)
self.__original_centroid = self.get_centroid()
else:
Expand Down
11 changes: 5 additions & 6 deletions bims/static/js/forms/location-site-admin-form.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
(function ($) {
$(document).ready(function () {
var $geometryInput = $('[class*="geometry_"]');
var $geometryInput = $('[class*="geometry_"]').not(".geometry_point");
var init = true;
$geometryInput.hide();
function resetAllGeometries() {
geodjango_geometry_point.clearFeatures();
geodjango_geometry_line.clearFeatures();
geodjango_geometry_polygon.clearFeatures();
geodjango_geometry_multipolygon.clearFeatures();
Expand All @@ -18,14 +17,14 @@
if ($(this).val()) {
$.get("/api/location-type/" + $(this).val() + '/allowed-geometry/', function (data) {
var className = 'geometry_' + data.toLowerCase();
$geometryInput.not('.' + className).hide();
$geometryInput.not('.' + className).not(".geometry_point").hide();
if (!$('.' + className).is(":visible")) {
$('.' + className).show();
}
})
.fail(function () {
console.log('fail');
})
.fail(function () {
console.log('fail');
})
} else {
$geometryInput.hide();
}
Expand Down

0 comments on commit 918ed19

Please sign in to comment.