Skip to content

Commit

Permalink
Added Ben to authors. Tweaked code style. Added -d to startshapedefin…
Browse files Browse the repository at this point in the history
…itions.
  • Loading branch information
onyxfish committed Aug 17, 2011
1 parent 9bab0f1 commit fe7c730
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -4,3 +4,4 @@ Ryan Mark (@ryanmark)
Joe Germuska (@joegermuska)
Brian Boyer (@brianboyer)
Anders Eriksen (@anderseri)
Ben Welsh (@palewire)
8 changes: 4 additions & 4 deletions README.rst
Expand Up @@ -35,19 +35,19 @@ If you havn't already, you can create your definitions file using a management c

You can load all definitions like so::

$ python manage.py load_shapefiles
$ python manage.py loadshapefiles

You may also override the default location by passing the "-d" flag to the command or setting SHAPEFILES_DIR in settings.py::

$ python manage.py load_shapefiles -d data_dir
$ python manage.py loadshapefiles -d data_dir

You can load only a specified shapefile with the "-o" flag::

$ python manage.py load_shapefiles -o ShapeFileName
$ python manage.py loadshapefiles -o ShapeFileName

You can clear a particular shapefile from the database and reload it with the "-c" flag::

$ python manage.py load_shapefiles -c ShapeFileName
$ python manage.py loadshapefiles -c ShapeFileName

Advice
======
Expand Down
26 changes: 16 additions & 10 deletions boundaryservice/management/commands/startshapedefinitions.py
@@ -1,9 +1,12 @@
import os
import logging
log = logging.getLogger('boundaries.api.load_shapefiles')
from optparse import make_option
import os

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError


DEFAULT_SHAPEFILES_DIR = getattr(settings, 'SHAPEFILES_DIR', 'data/shapefiles')

class Command(BaseCommand):
"""
Expand All @@ -22,23 +25,25 @@ class Command(BaseCommand):
custom_options = (
make_option('-f', '--force',
action='store_true', dest='force',
help='Force the creation of a new defintions.py, even if it already exists.'
help='Force the creation of a new definitions.py, even if it already exists.'
),
make_option('-d', '--data-dir', action='store', dest='data_dir',
default=DEFAULT_SHAPEFILES_DIR,
help='Load shapefiles from this directory'
),
)
option_list = BaseCommand.option_list + custom_options

def handle(self, *args, **options):
shp_dir = getattr(settings, 'SHAPEFILES_DIR', 'data/shapefiles')
if not os.path.exists(shp_dir):
raise CommandError("The shapefiles directory does not exist. Create data/shapefiles/ or set SHAPEFILES_DIR in settings.py.")
def_path = os.path.join(shp_dir, "definitions.py")
if not os.path.exists(options['data_dir']):
raise CommandError("The shapefiles directory does not exist. Create it or specify a different directory.")
def_path = os.path.join(options['data_dir'], "definitions.py")
if os.path.exists(def_path) and not options.get("force"):
raise CommandError("Sorry, %s already exists." % def_path)
outfile = open(def_path, "w")
outfile.write(BOILERPLATE)
outfile.close()
self.stdout.write('Created definitions.py in %s\n' % shp_dir)

logging.info('Created definitions.py in %s' % options['data_dir'])

BOILERPLATE = """from datetime import date
Expand Down Expand Up @@ -72,4 +77,5 @@ def handle(self, *args, **options):
# This is normally not necessary and can be left undefined or set to an empty string to maintain the default behavior
'srid': ''
}
}"""
}
"""
1 change: 1 addition & 0 deletions boundaryservice/utils.py
Expand Up @@ -60,3 +60,4 @@ def __call__(self, feature):
name = normed

return name

0 comments on commit fe7c730

Please sign in to comment.