mysociety / mapit

A web service to map postcodes to administrative boundaries and more
Other
269 stars 88 forks source link

mapit_filter: Unable to filter by type and code #429

Closed torotil closed 4 months ago

torotil commented 4 months ago

Problem

When importing using mapit_import you can’t import codes while also filtering for an area type:

When one code is used in multiple area types this can lead to areas of a different type being modified.

Steps to reproduce

I observed this when importing the WMCF areas because there are a few scottish constituencies which keep their area code. Importing them with:

python manage.py mapit_import --area_type_code=WMCF --country_code=first-letter --name_type_code=O --code_field=gss_code --code_type=gss parl_constituencies_2025.gpkg

Will only create 632 WMCF areas although there are 650 areas in the dataset. Turns out the culprit is this bit of code in mapit/add_areas_from_file/core.py:

            if parameters.new:  # Always want a new area
                raise Area.DoesNotExist
            if code:
                matching_message = "code %s of code type %s" % (
                    code,
                    parameters.code_type,
                )
                areas = Area.objects.filter(
                    codes__code=code, codes__type=parameters.code_type
                ).order_by("-generation_high")
            else:
                matching_message = "name %s of area type %s" % (
                    name,
                    parameters.area_type,
                )
                areas = Area.objects.filter(
                    name=name, type=parameters.area_type
                ).order_by("-generation_high")

Workaround

Proposed change

If both codes and an area type is specified when importing the script should filter by both. In particular it should never touch areas of another type than the one specified as --area_type_code.