项目作者: Rodrigo-NH

项目描述 :
Django APP to automate GeoDjango shapefile importations and management without affecting migrations
高级语言: Python
项目地址: git://github.com/Rodrigo-NH/django-shapefileimport.git
创建时间: 2021-09-13T17:59:47Z
项目社区:https://github.com/Rodrigo-NH/django-shapefileimport

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Django-shapefileimport

GeoDjango includes a helper function to import ESRI shapefiles to PostGIS enabled DB: LayerMapping
It works as intended but it’s still a boring process. Moreover, the usual importing procedure means creating new migrations for each imported shapefile. This is problably OK for sparse importings of a couple of shapefiles. But as I needed to manage many shapefiles and/or keep adding/deleting them it doesn’t sounded to me very sane to keep afecting migration files (and migration history).
This APP proposes an alternative to manage such shapefiles:

  • Adds shapefile ZIP import option in admin page. Imports will include feature fields.
  • Loads shapefiles by name on demand without touching migrations (so you can use all nice GeoDjango API features)
  • Delete shapefiles from admin panel
  • Option to keep or discard uploaded ZIP files while importing to DB

Installation

Clone this repo:

  1. git clone https://github.com/Rodrigo-NH/django-shapefileimport

Or install with pip/pipenv (e.g. ‘pipenv install django-shapefileimport’) PiPy package
Enter the ‘django-shapefileimport’ directory, install de dependencies, enter shell and start a new project (For simplicity I reccomend keeping the ending dot at the django-admin startproject command, it makes project folder to be created at same level as the APP [not nested])

  1. pipenv install
  2. pipenv shell
  3. django-admin startproject mysite .

edit ‘./mysite/settings.py’ and add to INSTALLED_APPS:

  1. 'django.contrib.gis',
  2. 'shapefileimport'

Still at ‘./mysite/settings.py’, configure the database connection:

  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.contrib.gis.db.backends.postgis',
  4. 'NAME': 'shapeimportsDB',
  5. 'USER': 'geo',
  6. 'HOST': '192.168.0.40',
  7. 'PORT': '5432',
  8. 'PASSWORD': 'dbpassword',
  9. },
  10. }

Run migrations, create superuser and start the APP:

  1. python manage.py makemigrations
  2. python manage.py migrate
  3. python manage.py createsuperuser
  4. python manage.py runserver

It must be done at this point. Enter admin panel and start importing your shapefiles.
After importing, use ‘loadShape()’ function to load the shapefile and use. Example:

  1. from shapefileimport.shapetasks import loadShape
  2. from django.core.serializers import serialize
  3. ds = loadShape('MyShapeName')
  4. ff = serialize('geojson', ds.objects.all(), geometry_field='geom',)

Notes