1

I have the following problem: my goal is to make an archive of gps tracks that can be displayed on a map. (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. My code:

forms.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

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

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' Error 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

1 Answers1

-1

Try to open your filefield like that:

gpx_file = django_model.filefield.open('r')
gpx = gpxpy.parse(gpx_file)

instead of

gpx_file = open(django_model.filefield.url, 'r')
gpx = gpxpy.parse(gpx_file)

That did the trick for me.