-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from Georiviere/impr_add_distance_to_source_g…
…eneration Add distance to source generation
- Loading branch information
Showing
37 changed files
with
658 additions
and
45 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
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
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 |
---|---|---|
@@ -1,7 +1,19 @@ | ||
from django.apps import AppConfig | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.apps import apps | ||
|
||
from django.db.models.signals import post_delete, post_save | ||
|
||
|
||
class MainConfig(AppConfig): | ||
name = 'georiviere.main' | ||
verbose_name = _("Main") | ||
|
||
def ready(self): | ||
from . import signals | ||
from mapentity.models import MapEntityMixin | ||
from georiviere.river.models import Stream | ||
for model in apps.get_models(): | ||
if issubclass(model, MapEntityMixin) and model != Stream: | ||
post_save.connect(signals.save_objects_generate_distance_to_source, sender=model) | ||
post_delete.connect(signals.delete_objects_remove_distance_to_source, sender=model) |
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,31 @@ | ||
# Generated by Django 3.1.14 on 2023-03-06 09:13 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('river', '0016_auto_20220324_1419'), | ||
('contenttypes', '0002_remove_content_type_name'), | ||
('main', '0009_auto_20230223_1541'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='DistanceToSource', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('distance', models.FloatField(default=0, verbose_name='Distance')), | ||
('object_id', models.PositiveIntegerField()), | ||
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')), | ||
('stream', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='river.stream')), | ||
], | ||
options={ | ||
'verbose_name': 'Distance to source', | ||
'verbose_name_plural': 'Distance to sources', | ||
'unique_together': {('content_type', 'object_id', 'stream')}, | ||
}, | ||
), | ||
] |
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,59 @@ | ||
from django.contrib.gis.db.models.functions import Distance, Length, LineLocatePoint | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.db.models import F, FloatField, Case, When | ||
|
||
from georiviere.functions import ClosestPoint, LineSubString | ||
from georiviere.river.models import Stream | ||
from georiviere.main.models import DistanceToSource | ||
|
||
|
||
def annotate_distance_to_source(streams, instance): | ||
if streams: | ||
streams = streams.annotate( | ||
locate_source=LineLocatePoint(F('geom'), F('source_location')), | ||
locate_object=LineLocatePoint(F('geom'), ClosestPoint(F('geom'), instance.geom)), | ||
locate=Length(LineSubString(F('geom'), | ||
Case( | ||
When(locate_source__gte=F('locate_object'), then=F('locate_object')), | ||
default=F('locate_source')), | ||
Case( | ||
When(locate_source__gte=F('locate_object'), then=F('locate_source')), | ||
default=F('locate_object')))) + Distance( | ||
instance.geom, | ||
F('geom'), | ||
output_field=FloatField()) + Distance( | ||
F('geom'), | ||
F('source_location'), | ||
output_field=FloatField()) | ||
) | ||
return streams | ||
|
||
|
||
def save_objects_generate_distance_to_source(sender, instance, **kwargs): | ||
if not hasattr(instance, 'get_topology'): | ||
streams = annotate_distance_to_source(instance.streams, instance) | ||
for stream in streams: | ||
DistanceToSource.objects.update_or_create( | ||
object_id=instance.pk, | ||
content_type=ContentType.objects.get_for_model(instance._meta.model), | ||
stream=stream, | ||
defaults={"distance": stream.locate.m} | ||
) | ||
if not kwargs.get('created'): | ||
DistanceToSource.objects.filter(object_id=instance.pk, | ||
content_type=ContentType.objects.get_for_model(instance._meta.model), | ||
).exclude(stream__in=streams).delete() | ||
|
||
elif hasattr(instance, 'topology'): | ||
stream = annotate_distance_to_source(Stream.objects.all(), instance).get(pk=instance.topology.stream.pk) | ||
DistanceToSource.objects.update_or_create( | ||
object_id=instance.pk, | ||
content_type=ContentType.objects.get_for_model(instance._meta.model), | ||
stream=stream, | ||
defaults={"distance": stream.locate.m} | ||
) | ||
|
||
|
||
def delete_objects_remove_distance_to_source(sender, instance, **kwargs): | ||
content_type = ContentType.objects.get_for_model(instance._meta.model) | ||
DistanceToSource.objects.filter(content_type=content_type, object_id=instance.pk).delete() |
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
Oops, something went wrong.