-
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 #519 from MTES-MCT/species_maps
Géolocalisation des espèces protégées
- Loading branch information
Showing
29 changed files
with
759 additions
and
111 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,20 @@ | ||
# Generated by Django 4.2.13 on 2025-01-10 10:17 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("geodata", "0016_alter_department_geometry"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="zone", | ||
name="attributes", | ||
field=models.JSONField( | ||
blank=True, null=True, verbose_name="Entity attributes" | ||
), | ||
), | ||
] |
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,27 @@ | ||
# Generated by Django 4.2.13 on 2025-01-10 10:19 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("geodata", "0017_zone_attributes"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="map", | ||
name="map_type", | ||
field=models.CharField( | ||
blank=True, | ||
choices=[ | ||
("zone_humide", "Zone humide"), | ||
("zone_inondable", "Zone inondable"), | ||
("species", "Espèces protégées"), | ||
], | ||
max_length=50, | ||
verbose_name="Map type", | ||
), | ||
), | ||
] |
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
Empty file.
Empty file.
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,65 @@ | ||
import csv | ||
import glob | ||
import io | ||
import os | ||
import zipfile | ||
from tempfile import TemporaryDirectory | ||
|
||
import requests | ||
from django.core.management.base import BaseCommand | ||
|
||
from envergo.hedges.models import Species | ||
|
||
# Download link can be found here | ||
# https://inpn.mnhn.fr/telechargement/referentielEspece/referentielTaxo | ||
TAXREF_URL = "https://inpn.mnhn.fr/docs-web/docs/download/454260" | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Update species data with TaxRef identifiers (cd_nom)." | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument("taxref_url", type=str, nargs="?", default=TAXREF_URL) | ||
|
||
def handle(self, *args, **options): | ||
|
||
# Read the taxref file | ||
taxref_url = options["taxref_url"] | ||
if os.path.exists(taxref_url): | ||
with open(taxref_url, "rb") as f: | ||
file_content = io.BytesIO(f.read()) | ||
else: | ||
r = requests.get(taxref_url, stream=True) | ||
file_content = io.BytesIO(r.content) | ||
|
||
with TemporaryDirectory() as tmpdir: | ||
zf = zipfile.ZipFile(file_content) | ||
zf.extractall(tmpdir) | ||
|
||
paths = glob.glob(f"{tmpdir}/TAXREFv*.txt") | ||
try: | ||
path = paths[0] | ||
except IndexError: | ||
self.stderr.write(self.style.ERROR("No TAXREF file found")) | ||
return | ||
|
||
# Reset taxref ids for all species | ||
Species.objects.update(taxref_ids=[]) | ||
species_names = Species.objects.all().values_list( | ||
"scientific_name", flat=True | ||
) | ||
|
||
with open(path) as csvfile: | ||
reader = csv.DictReader(csvfile, delimiter="\t") | ||
for row in reader: | ||
scientific_name = row["LB_NOM"] | ||
vernacular_name_id = row["CD_NOM"] | ||
kingdom = row["REGNE"].lower() | ||
if scientific_name in species_names: | ||
# AFAIK, there is still no way to update an array field in a | ||
# sigle query. Issue open since 9 years | ||
# https://code.djangoproject.com/ticket/26355 | ||
species = Species.objects.get(scientific_name=scientific_name) | ||
species.taxref_ids.append(vernacular_name_id) | ||
species.kingdom = kingdom | ||
species.save() |
Oops, something went wrong.