Is it possible to set up the Django Admin to be able to upload shapefiles to PostGIS? Right now I have it set up so that you can draw administrative areas but what I would like is to have an 'import shapefile' or 'upload shapefile' option, so that other admins can upload shapefiles instead of having to draw them.
1 Answer
You can not currently do that in a generalized way. To upload and parse shapefiles, you have to think about several aspects:
- Do you want to store the shapefile for later re-use?
- How do you want to handle different projections for the uploaded data?
- What are the geometry attributes that you would like to store, and how do you want to store them?
- How do you want to extract the geomeries from the shapefile? If the file is big, you can probably not do it in a normal web request, it will time out. So you might need an asynchronous task queue (with Celery for instance).
I am author of an django-raster-aggregation, an app that has an admin based shapefile upload functionality. It has a separate model for storing the shapefile, and it extracts only one attribute from one column (the name of the area). The geometries and their names are stored in a separate model with a foreign key to the shapefile model. The app does the shapefile parsing asynchronously with this task.
The app has a very specific focus (aggregating rasters by areas), but you might get some inspiration there for how to write a shapefile parser.
-
Nice to find someone who has done this already. I was also looking for a solution same as the original poster. If the form has some validation and the form uses the model's projection, I don't see the problem. It is all about asking the user to match the data type, projection, and number of columns. However, my hope is diminished because of your answer. I would like to discuss further with you on.– wondimCommented Jun 14, 2017 at 11:00
-
If you check the parse script on the mentioned package, you see that the geometries are being transformed to the desired model projection: github.com/geodesign/django-raster-aggregation/blob/master/… Note that there is a lot of trial/error, and a parse log. If you have users uploading files, you will almost certainly run into problems, GIS data is very diverse, there are invalid or empty geometries, unknown projections, etc. So its not trivial to make something generic, but dont get discouraged! Commented Jun 15, 2017 at 9:58
-
I understand the problems. However, if the shapefile is not done correctly, it is the fault of the user. The developer just need to show a correct error message. I will keep on trying. Thanks!– wondimCommented Jun 15, 2017 at 13:45