Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --only-with to mapit_import so you can only import particular areas #200

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions mapit/management/commands/mapit_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# Not using LayerMapping as want more control, but what it does is what this does
# from django.contrib.gis.utils import LayerMapping
from django.contrib.gis.gdal import DataSource
from django.contrib.gis.gdal.error import OGRIndexError
from django.conf import settings
from django.utils import six
from django.utils.six.moves import input
Expand Down Expand Up @@ -113,6 +114,17 @@ class Command(LabelCommand):
dest='fix_invalid_polygons',
help="Try to fix any invalid polygons and multipolygons found"
),
make_option(
'--only-with',
action="append",
dest='only_with',
metavar='FIELD_NAME:VALUE',
help=(
"Ignore features without this field / value combination "
"(If specified multiple times, the feature must have all "
"such specified combinations.)"
)
),
)

def handle_label(self, filename, **options):
Expand Down Expand Up @@ -140,6 +152,14 @@ def handle_label(self, filename, **options):
code_field = options['code_field']
code_type_code = options['code_type']
encoding = options['encoding'] or 'utf-8'
only_with = {}
if options['only_with']:
for kv in options['only_with']:
m = re.search(r'^(.*?):(.*)', kv)
if not m:
message = "The --only-with value '{0}' did not contain a ':'"
raise CommandError(message.format(kv))
only_with[m.group(1)] = m.group(2)

if len(area_type_code) > 3:
raise CommandError("Area type code must be 3 letters or fewer, sorry")
Expand Down Expand Up @@ -245,6 +265,18 @@ def verbose(*args):
"Could not find code using code field '%s' - should it be something else? "
"It will be one of these: %s. Specify which with --code_field" % (code_field, choices))

# If some --only-with parameters were specified, the
# feature must have all such key / value combination:
if only_with:
try:
if not all(
feat.get(k) == str(v) for k, v in only_with.items()
):
continue
except OGRIndexError:
# This is raised if the key is not found in the feature:
continue

self.stdout.write(" looking at '%s'%s" % (name, (' (%s)' % code) if code else ''))

g = None
Expand Down