如何在Geodjango中将request.FILES读入数据源

xzlaal3s  于 2022-12-14  发布在  Go
关注(0)|答案(2)|浏览(132)

所以,我们的目标是创建一个网页来加载一个.shp文件,并以JsonResponse的形式获得一些计算的摘要。我已经准备好了计算和所有的东西,当我手动添加一个路径到有问题的文件时,它工作得很好。然而,我们的目标是让其他人能够上传数据并获得响应,这样我就不能硬编码我的路径。
总体做法:

  • 通过forms.FileField()和request.FILES ['file_name']读取。在此之后,我需要将此request.FILES对象传输到DataSource以便读取它。如果可能,我宁愿不将文件上载到PC上,而是直接从内存工作。

forms.py

from django import forms
from django.core.files.storage import FileSystemStorage

class UploadFileForm(forms.Form):
    # title = forms.CharField(max_length=50)
    file = forms.FileField()

views.py

import json
import os

from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import render

from django.template import loader
from django.contrib import messages
from django.views.generic import TemplateView
from django.http import JsonResponse

from django.conf import settings
from .forms import UploadFileForm
from . import models
from django.shortcuts import redirect
from gisapp.functions.functions import handle_uploaded_file, handle_uploaded_file_two
from django.contrib.gis.gdal import DataSource
from django.core.files.uploadedfile import UploadedFile, TemporaryUploadedFile
import geopandas as gpd
import fiona

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            f = request.FILES['file']
            # geo2 = gpd.read_file(f)
            # print(geo2)
            # f_path = os.path.abspath(os.path.join(os.path.dirname(f), f))

            # f_path = TemporaryUploadedFile.temporary_file_path(UploadedFile(f))
            # print(f_path)
            # f_path = f.temporary_file_path()
            # new_path =  request.FILES['file'].temporary_file_path
            # print(f'This is file path: {f_path}')
            # print(f'This is file path: {new_path}')
            # data = DataSource(f'gisapp/data/{f}') -- given an absolute path it works great
            data = DataSource(f) -- constantly failing
            # data = DataSource(new_path)
            # print(f'This is file path: {f_path}')
            layer = data[0]
            if layer.geom_type.name == "Polygon" or layer.geom_type.name == "LineString":
                handle_uploaded_file(request.FILES['file'])
            elif layer.geom_type.name == "Point":
                handle_uploaded_file_two(request.FILES['file'])

            return JsonResponse({"Count": f"{handle_uploaded_file_two(request.FILES['file'])[0]}", "Bounding Box": f"{handle_uploaded_file_two(request.FILES['file'])[1]}"})
            
            # return JsonResponse({"Count": f"{handle_uploaded_file(request.FILES['file'])[0]}", "Minimum": f"{handle_uploaded_file(request.FILES['file'])[1]}", "Maximum": f"{handle_uploaded_file(request.FILES['file'])[1]}"})
            # instance = models.GeometryUpload(file_field=request.FILES['file'])
            # instance.save()
            # # return HttpResponseRedirect('/success/')
    else:
        form = UploadFileForm()
    return render(request, 'upload.html', {'form': form})

我得到的错误:

django.contrib.gis.gdal.error.GDALException: Invalid data source input type: <class 'django.core.files.uploadedfile.InMemoryUploadedFile'>

现在您可以从www.example.com中的upload_file()看到views.py,我尝试了许多操作,当我添加绝对路径时,它工作了,但除此之外,我似乎无法将文件上载到DataSource,以便在以后的分析中使用它。

ahy6op9u

ahy6op9u1#

看看Django是如何处理这个问题的,它似乎不可能处理内存中的文件,文件的路径被传递给C API for OGR,然后由C API for OGR打开并阅读文件。

kokeuurv

kokeuurv2#

一个可能的解决方案,我正在尝试自己是有用户压缩他们的形状文件(.shp,. shx,dbf等)事先。压缩文件,然后上传和解压。shp文件,然后可以读取。希望这有助于

相关问题