-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correctly calculate area (fixes #1689)
- Loading branch information
Anthony Lukach
committed
Aug 3, 2017
1 parent
60a42c9
commit 219365c
Showing
5 changed files
with
88 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.10.7 on 2017-08-03 17:18 | ||
from __future__ import unicode_literals | ||
|
||
import django.contrib.gis.db.models.fields | ||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
TABLE_NAME = 'spatial_spatialunit' | ||
FUNC_NAME = 'calculate_area' | ||
TRIGGER_NAME = '{}_trigger'.format(FUNC_NAME) | ||
|
||
dependencies = [ | ||
('spatial', '0004_area_location_field'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='historicalspatialunit', | ||
name='geometry', | ||
field=django.contrib.gis.db.models.fields.GeometryField(geography=True, null=True, srid=4326), | ||
), | ||
migrations.AlterField( | ||
model_name='spatialunit', | ||
name='geometry', | ||
field=django.contrib.gis.db.models.fields.GeometryField(geography=True, null=True, srid=4326), | ||
), | ||
migrations.RunSQL( | ||
( | ||
"UPDATE {table} SET area = ST_Area(geography(geometry)) " | ||
"WHERE geometrytype(geometry) LIKE '%POLYGON';" | ||
).format(table=TABLE_NAME), | ||
reverse_sql=migrations.RunSQL.noop | ||
), | ||
migrations.RunSQL( | ||
""" | ||
CREATE FUNCTION {func}() RETURNS trigger AS $$ | ||
BEGIN | ||
IF geometrytype(NEW.geometry) LIKE '%POLYGON' | ||
THEN | ||
NEW.area := (SELECT ST_Area(geography(NEW.geometry))); | ||
ELSE | ||
NEW.area := null; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ language plpgsql; | ||
CREATE TRIGGER {trigger} | ||
BEFORE INSERT OR UPDATE | ||
ON {table} | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE {func}(); | ||
""".format(func=FUNC_NAME, trigger=TRIGGER_NAME, table=TABLE_NAME), | ||
reverse_sql=""" | ||
DROP TRIGGER {trigger} ON {table}; | ||
DROP FUNCTION {func}(); | ||
""".format(func=FUNC_NAME, trigger=TRIGGER_NAME, table=TABLE_NAME) | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To be honest, I think this was overkill. Running
instance.refresh_from_db()
would likely be clearer.