-1

I have a project that uses GeoDjango to store GPS routes. The geometry is stored in a GeometryField. This works great when data is imported with geospatial information, but it is frustrating when I have a model which needs user-supplied data. I would like to have a widget in the Admin that will let me upload a file, and then use that file to essentially import the geospatial information.

The FileField field doesn't seem appropriate, since I don't want the file stored on the file system. I want it processed and stored in the geospatial DB field so I can run geospatial functions on the data.

Ideally the admin interface would contain a file upload widget and the geospatial field, shown with the typical map.

Travis Prescott
  • 407
  • 2
  • 8

1 Answers1

0

There are a couple of options for importing geo data files into DB. If you want to use a zipped shapefile, Geodjango comes with a nice solution, LayerMapping.

Before importing the file, you should implement the workflow for uploading zip file with a form, checking the required extensions ([".shp", ".shx", ".dbf", ".prj"]) and saving the files for reading.

Then you have to define a mapping to match field names across the file and Django model.

After you completed these steps, you can save the geometries into the DB with:

from django.contrib.gis.utils.layermapping import LayerMapping

layer = uploaded_and_extracted_file

mapping = {"id": "district", "name": "dis_name", "area": "shape_area", "geom": "MULTIPOLYGON"}

lm = LayerMapping(ModelName, layer, mapping, transform=True, encoding="utf-8")

lm.save(verbose=True, strict=True, silent=True)
Deniz
  • 291
  • 1
  • 3
  • 5