[英]How to save data to PostGis database from uploaded GPX file in Django?
I have the following problem: my goal is to make an archive of gps tracks that can be displayed on a map.我有以下问题:我的目标是制作一个可以在地图上显示的 gps 轨迹存档。 (using Python, Django, PostgreSQL, PostGIS) I found a tutorial: https://web.archive.org/web/20160425053015/http://ipasic.com/article/uploading-parsing-and-saving-gpx-data-postgis-geodjango the file upload works fine, but I can't figure out how to save data from a file to the database and use them as models.
(使用Python、Django、PostgreSQL、PostGIS)我找到了一个教程: https ://web.archive.org/web/20160425053015/http://ipasic.com/article/uploading-parsing-and-saving-gpx-data -postgis-geodjango文件上传工作正常,但我不知道如何将文件中的数据保存到数据库并将它们用作模型。 My code:
我的代码:
forms.py表单.py
from django import forms
from .models import gpxFile
class UploadGpxForm(forms.ModelForm):
title = forms.CharField(max_length=100)
gpx_file = forms.FileField(required='FALSE')
class Meta:
model = gpxFile
fields = ['title', 'gpx_file']
models.py模型.py
from django.contrib.gis.db import models
from django.contrib import admin
from django.contrib.gis import admin as geoadmin
from django.db.models.manager import Manager
def GPX_Folder(instance, filename):
return "uploaded_gpx_files/%s" % (filename)
class gpxFile(models.Model):
title = models.CharField("Title", max_length=100)
gpx_file = models.FileField(upload_to=GPX_Folder, blank=True)
def __unicode__(self):
return self.title
class GPXPoint(models.Model):
name = models.CharField("Name", max_length=50, blank=True)
description = models.CharField("Description", max_length=250, blank=True)
gpx_file = models.ForeignKey(gpxFile, on_delete=models.CASCADE)
point = models.PointField()
objects = models.Manager()
def __unicode__(self):
return unicode(self.name)
class GPXTrack(models.Model):
track = models.MultiLineStringField()
gpx_file = models.ForeignKey(gpxFile, on_delete=models.CASCADE)
objects = models.Manager()
views.py视图.py
from django.shortcuts import render
from .forms import UploadGpxForm, Up
from .models import GPXPoint, GPXTrack, gpxFile
from django.http import HttpResponseRedirect
from django.contrib.gis.geos import Point, LineString, MultiLineString
from django.conf import settings
import gpxpy
import gpxpy.gpx
def home(request):
#context = {
#'notes': Note.objects.all()
#}
return render(request, 'gpsarchive/home.html')
def SaveGPXtoPostGIS(f, file_instance):
gpx_file = open(settings.MEDIA_ROOT+ '/uploaded_gpx_files'+'/' + f.name)
gpx = gpxpy.parse(gpx_file)
if gpx.waypoints:
for waypoint in gpx.waypoints:
new_waypoint = GPXPoint()
if waypoint.name:
new_waypoint.name = waypoint.name
else:
new_waypoint.name = 'unknown'
new_waypoint.point = Point(waypoint.longitude, waypoint.latitude)
new_waypoint.gpx_file = file_instance
new_waypoint.save()
if gpx.tracks:
for track in gpx.tracks:
print("track name:" +str(track.name))
new_track = GPXTrack()
for segment in track.segments:
track_list_of_points = []
for point in segment.points:
point_in_segment = Point(point.longitude, point.latitude)
track_list_of_points.append(point_in_segment.coords)
new_track_segment = LineString(track_list_of_points)
new_track.track = MultiLineString(new_track_segment)
new_track.gpx_file = file_instance
new_track.save()
def upload_gpx(request):
if request.method == 'POST':
file_instance = gpxFile()
form = UploadGpxForm(request.POST, request.FILES)
if form.is_valid():
form.save()
SaveGPXtoPostGIS(request.FILES['gpx_file'], file_instance)
return HttpResponseRedirect('success/')
else:
form = UploadGpxForm()
return render(request, 'gpsarchive/form.html', {'form':form})
def upload_success(request):
return render(request, 'gpsarchive/success.html')
As I mentioned before, file upload works, unfortunately the following error occurs: [Errno 2] No such file or directory: '/uploaded_gpx_files/3359239.gpx'正如我之前提到的,文件上传有效,不幸的是发生了以下错误: [Errno 2] No such file or directory: '/uploaded_gpx_files/3359239.gpx'
has no idea what could be causing this error because both the file and its directory exist,I will be extremly grateful for any suggestions
不知道是什么原因导致此错误,因为文件及其目录都存在,我将非常感谢任何建议
Try to open your filefield like that:尝试像这样打开您的文件字段:
gpx_file = django_model.filefield.open('r') gpx_file = django_model.filefield.open('r')
gpx = gpxpy.parse(gpx_file) gpx = gpxpy.parse(gpx_file)
instead of代替
gpx_file = open(django_model.filefield.url, 'r') gpx_file = open(django_model.filefield.url, 'r')
gpx = gpxpy.parse(gpx_file) gpx = gpxpy.parse(gpx_file)
That did the trick for me.那对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.