On migre à une base de données géographique

This commit is contained in:
Jean-Marie Favreau 2024-10-10 23:23:25 +02:00
parent 80a7b4e57f
commit 7d98fe6020
12 changed files with 122 additions and 9 deletions

View File

@ -5,7 +5,7 @@ WORKDIR /usr/src/app
RUN --mount=type=cache,target=/var/cache/apt \
apt-get update && \
apt-get install --no-install-recommends -y build-essential libpq-dev gettext chromium-driver \
apt-get install --no-install-recommends -y build-essential libpq-dev gettext chromium-driver gdal-bin \
&& rm -rf /var/lib/apt/lists/*

View File

@ -23,7 +23,7 @@ services:
command: [ "/bin/bash", "/app/deployment/scripts/backend/start.sh" ]
db:
image: postgres:15.2-alpine
image: postgis/postgis:15-3.4-alpine
container_name: "${APP_NAME}-db"
hostname: "${POSTGRES_HOST:-db}"
volumes:

View File

@ -0,0 +1,20 @@
# Generated by Django 4.2.9 on 2024-10-10 20:34
import django.contrib.gis.geos.point
from django.db import migrations
import location_field.models.spatial
class Migration(migrations.Migration):
dependencies = [
('agenda_culturel', '0079_contactmessage_spam'),
]
operations = [
migrations.AddField(
model_name='place',
name='location_pt',
field=location_field.models.spatial.LocationField(default=django.contrib.gis.geos.point.Point(45.783329, 3.08333), srid=4326),
),
]

View File

@ -0,0 +1,31 @@
# Generated by Django 4.2.9 on 2024-10-10 20:35
from django.db import migrations
from agenda_culturel.models import Place
from django.contrib.gis.geos import Point
def change_coord_format(apps, schema_editor):
places = Place.objects.all()
for p in places:
l = p.location.split(',')
if len(l) == 2:
p.location_pt = Point(float(l[1]), float(l[0]))
else:
p.location_pt = Point(3.08333, 45.783329)
p.save()
class Migration(migrations.Migration):
dependencies = [
('agenda_culturel', '0080_place_location_pt'),
]
operations = [
migrations.RunPython(change_coord_format),
]

View File

@ -0,0 +1,20 @@
# Generated by Django 4.2.9 on 2024-10-10 21:15
import django.contrib.gis.geos.point
from django.db import migrations
import location_field.models.spatial
class Migration(migrations.Migration):
dependencies = [
('agenda_culturel', '0081_auto_20241010_2235'),
]
operations = [
migrations.AlterField(
model_name='place',
name='location_pt',
field=location_field.models.spatial.LocationField(default=django.contrib.gis.geos.point.Point(3.08333, 45.783329), srid=4326),
),
]

View File

@ -0,0 +1,17 @@
# Generated by Django 4.2.9 on 2024-10-10 21:15
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('agenda_culturel', '0082_alter_place_location_pt'),
]
operations = [
migrations.RemoveField(
model_name='place',
name='location',
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 4.2.9 on 2024-10-10 21:15
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('agenda_culturel', '0083_remove_place_location'),
]
operations = [
migrations.RenameField(
model_name='place',
old_name='location_pt',
new_name='location',
),
]

View File

@ -25,6 +25,8 @@ from django.utils.timezone import datetime
from django.utils import timezone
from location_field.models.plain import PlainLocationField
from location_field.models.spatial import LocationField
from django.contrib.gis.geos import Point
from .calendar import CalendarList, CalendarDay
from icalendar import Calendar as icalCal
@ -244,7 +246,7 @@ class Place(models.Model):
null=True,
)
city = models.CharField(verbose_name=_("City"), help_text=_("City name"))
location = PlainLocationField(based_fields=["name", "address", "city"], zoom=12)
location = LocationField(based_fields=["name", "address", "city"], zoom=12, default=Point(3.08333, 45.783329))
aliases = ArrayField(
models.CharField(max_length=512),

View File

@ -49,6 +49,7 @@ INSTALLED_APPS = [
"compressor",
"django_ckeditor_5",
"recurrence",
'django.contrib.gis',
"location_field.apps.DefaultConfig",
"django.contrib.postgres",
"robots",
@ -107,7 +108,7 @@ WSGI_APPLICATION = "agenda_culturel.wsgi.application"
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
'ENGINE': 'django.contrib.gis.db.backends.postgis',
"NAME": os_getenv("POSTGRES_DB", "postgres"),
"USER": os_getenv("POSTGRES_USER", "postgres"),
"PASSWORD": os_getenv("POSTGRES_PASSWORD", "postgres"),

View File

@ -8,7 +8,7 @@
{% load static %}
{% load cache %}
{% load i18n %}
{% load l10n %}
{% block entete_header %}
{% css_categories %}
@ -39,7 +39,7 @@
<div>
<ul>
<li><strong>Adresse&nbsp;:</strong> {{ object.address }}, {{ object.city }}</li>
<li><strong>Coordonnée GPS&nbsp;:</strong> <a href="geo:{{ object.location }}">{{ object.location }}</a></li>
<li><strong>Coordonnée GPS&nbsp;:</strong> <a href="geo:{{ object.location|tocoords }}">{{ object.location|tocoords }}</a></li>
{% with object.nb_events_future as nb %}
{% if nb > 0 %}
<li>{{ nb }} événement{{ nb|pluralize }} à venir</li>
@ -53,12 +53,12 @@
<div id="map_location" style="width: 100%; aspect-ratio: 16/16"></div>
<script>
L.Icon.Default.imagePath = "{% static "location_field/leaflet/images/" %}";
var map = L.map('map_location').setView([{{ object.location }}], 13);
var map = L.map('map_location').setView([{{ object.location|tocoords }}], 13);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
}).addTo(map);
var marker = L.marker([{{ object.location }}]).addTo(map);
var marker = L.marker([{{ object.location|tocoords }}]).addTo(map);
</script>
<p>Voir aussi <a href="{% url 'view_places' %}">les autres lieux</a></p>
</div>

View File

@ -74,7 +74,7 @@
window.mMapping = {};
{% if object_list %}
{% for place in object_list %}
markerArray.push(L.marker([{{ place.location }}]).bindPopup('<a href="{% url 'view_place' place.pk %}">{{ place.name }}</a><br />{% if place.address %}{{ place.address }}, {% endif %}{{ place.city }}'))
markerArray.push(L.marker([{{ place.location|tocoords }}]).bindPopup('<a href="{% url 'view_place' place.pk %}">{{ place.name }}</a><br />{% if place.address %}{{ place.address }}, {% endif %}{{ place.city }}'))
markers.addLayer(markerArray[markerArray.length - 1]);
window.mMapping[{{ place.id }}] = markerArray[markerArray.length - 1];
window.jQuery('a#open-map-{{ place.id }}').click(function(){

View File

@ -119,3 +119,7 @@ def get_item(dictionary, key):
@register.filter
def remove_id_prefix(value):
return int(value.replace("id_", ""))
@register.filter
def tocoords(c):
return str(c.coords[1]) + ", " + str(c.coords[0])