Automatic mapping a layer from uploaded file

I’m trying to map a kml file to the database which works fine for me. I followed Geodjango documentation tutorial. But I want to map an uploaded file to the database. I have created a form for uploading kml file which works for me as it stores the file in database but not sure how to map the same file. I’m running webdom in development mode using docker. My layer mapping code, model code and views are as follows.

Upload model code
image code

Can anyone provide any help or suggestion or should I post in some other forum ?

Once you are able to upload files, to retrieve their content just use:

from .models import Upload
from .models import Farm
import os

file = Upload.objects.last()
farm_shp = file.upload.path

lm = LayerMapping(Farm, farm_shp, ...)

For example this code retrieves the path of the last uploaded file. In one of your views this code will probably need to be modified to use Upload.objects.get(id=id_coming_from_url). If you’re new to Django I’d recommend looking at the tutorials to setup views and retrieve model instances.

Thanks a lot again. I am apparently new to django. I will surely follow as you said.I would appreciate if you have any link to book or tutorial about geodjango. :grinning:

The django docs are probably the most up to date resource. I’d follow the tutorials there. Django documentation | Django documentation | Django

Geodjango is just a package within Django. Once you understand Django concepts it should be easy to read the reference docs to get up to speed. GeoDjango | Django documentation | Django

As you mentioned I did try to change the code as mentioned above which worked for me on my laptop. I cloned my repo on aws and started development server but got an error as below

Not sure if I am missing something as this works fine on my laptop.

The problem was with my code where it was searching for the last uploaded file in database at first so had to change it a bit. The problem is now solved. Thank you :grinning:

1 Like