diff options
Diffstat (limited to 'ishtar_common/views_item.py')
-rw-r--r-- | ishtar_common/views_item.py | 83 |
1 files changed, 81 insertions, 2 deletions
diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 8d2786d17..4126219fd 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -20,6 +20,7 @@ from collections import OrderedDict from copy import copy, deepcopy +import os import csv import datetime import json @@ -29,6 +30,8 @@ import requests # nosec: no user input used import subprocess # nosec from tempfile import NamedTemporaryFile +from osgeo import ogr, osr +import shutil from django.apps import apps from django.conf import settings @@ -3215,9 +3218,85 @@ def get_item( except UnicodeEncodeError: vals.append(unidecode(v).encode(ENCODING).decode(ENCODING)) writer.writerow(vals) - return response + #return response + elif data_type == "gpkg": + # Work in progress + # Creation of the .gpkg + driver = ogr.GetDriverByName("GPKG") + root = settings.LIB_BASE_PATH + "ishtar_common/tests/" + filename = os.path.join(root, "Finds.gpkg") + # Verification to delete it if already existing + if os.path.exists(filename): + os.remove(filename) + datasource = driver.CreateDataSource(filename) + srs = osr.SpatialReference() + srs.ImportFromEPSG(4326) + # Layer creation + layer = datasource.CreateLayer("Finds", srs, ogr.wkbPoint) + # Getting all the column names (copy from below) + if col_names: + col_names = [name for name in col_names] + else: + col_names = [] + for field_name in table_cols: + if type(field_name) in (list, tuple): + field_name = " & ".join(field_name) + if hasattr(model, "COL_LABELS") and field_name in model.COL_LABELS: + field = model.COL_LABELS[field_name] + col_names.append(str(field)) + continue + else: + try: + field = model._meta.get_field(field_name) + except: + col_names.append("") + logger.warning( + "**WARN get_item - csv export**: no col name " + "for {}\nadd explicit label to " + "COL_LABELS attribute of " + "{}".format(field_name, model) + ) + continue + col_names.append(str(field.verbose_name)) + # Creation of the columns + for name in col_names: + layer.CreateField(ogr.FieldDefn(name, ogr.OFTString)) + max = len(col_names) + # Looping on all the datas extracted + for data in datas: + # Creation of a new feature + feature = ogr.Feature(layer.GetLayerDefn()) + # Looping on the attributes to add them to the feature + for n in range (0, max) : + # +1 because the first value in the attributes is '' + m = n + 1 + feature.SetField(col_names[n], str(data[m])) + # First version to create the geometry of the feature + # Work in progress + if "x" in col_names[n] or "X" in col_names[n]: + try: + float(data[m]) + geom_x = data[m] + except: + pass + if "y" in col_names[n] or "Y" in col_names[n]: + try: + float(data[m]) + geom_y = data[m] + except: + pass + try: + point = ogr.Geometry(ogr.wkbPoint) + point.AddPoint(float(geom_x), float(geom_y)) + feature.SetGeometry(point) + layer.CreateFeature(feature) + except: + pass + feature = None + datasource = None + # Missing : Part where the new .gpkg is moved to a copy of the QField folder + # Work in progress return HttpResponse("{}", content_type="text/plain") - return func |