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

[Bexley][WW] Only skip parent addresses if they are marked as 'shell' properties #5203

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 16 additions & 15 deletions bin/bexley/make-bexley-ww-postcode-db
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ CREATE VIRTUAL TABLE postcodes USING fts4 (
sao_start_suffix TEXT,
sao_end_number TEXT,
sao_end_suffix TEXT,
sao_text TEXT
sao_text TEXT,

blpu_class TEXT
)
SQL

Expand Down Expand Up @@ -123,11 +125,14 @@ my $query_postcodes = $db->prepare(
sao_start_suffix,
sao_end_number,
sao_end_suffix,
sao_text
sao_text,

blpu_class
)
VALUES (?,?,?,
?,?,?,?,?,
?,?,?,?,?)
?,?,?,?,?,
?)
SQL
);

Expand Down Expand Up @@ -195,6 +200,8 @@ my @columns_lpi = (

say 'Populating tables...';

my %uprn_to_blpu_class;

# Cannot set column names / headers in usual way as CSV contains multiple
# record types
while( my $row = $csv->getline($fh) ) {
Expand All @@ -214,6 +221,8 @@ while( my $row = $csv->getline($fh) ) {
} elsif ( $record_id == $id_blpu ) {
@row_h{@columns_blpu} = @$row;

$uprn_to_blpu_class{ $row_h{UPRN} } = $row_h{BLPU_CLASS};

next if ( $row_h{LOGICAL_STATUS} || 0 ) != 1;
next unless $row_h{PARENT_UPRN};

Expand All @@ -230,6 +239,8 @@ while( my $row = $csv->getline($fh) ) {
# Remove whitespace from postcode
my $postcode = $row_h{POSTCODE} =~ s/ //gr;

my $blpu_class = $uprn_to_blpu_class{ $row_h{UPRN} } // '';

$query_postcodes->execute(
$postcode,
$row_h{UPRN},
Expand All @@ -246,20 +257,10 @@ while( my $row = $csv->getline($fh) ) {
$row_h{SAO_END_NUMBER},
$row_h{SAO_END_SUFFIX},
$row_h{SAO_TEXT},

$blpu_class,
);
}
}

# It is possible for UPRNs in child_uprns to not occur in the postcodes table
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out that the P/PP check fixes this issue as well, so have deleted this portion.

# (an 'empty' address).
# At least one address, UPRN 100020276242, was not being picked up
# by Bexley WW's address lookup, because it appeared as a parent_uprn in this
# table, but its 'child' addresses were empty.
$db->do(<<SQL);
DELETE FROM child_uprns
WHERE uprn NOT IN (
SELECT uprn FROM postcodes
)
SQL

say 'Tables populated';
17 changes: 12 additions & 5 deletions perllib/BexleyAddresses.pm
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ That script shows the setup of the database in more detail, but to explain brief

=item * postcodes

Stores postcode, UPRN, USRN, and address portions (e.g. house number and name) for each property (properties are uniquely identified by their UPRN)
Stores postcode, UPRN, USRN, and address portions (e.g. house number and name) for each property (properties are uniquely identified by their UPRN).

Also stores 'blpu_class'; if it is 'P' or 'PP', it means the property is a
'parent shell' and so should not be offered as a selectable address.

=item * street_descriptors

Stores address data (e.g. street & town name) for each USRN (street identifier)

=item * child_uprns

Captures mapping between parent and child properties (e.g. if a building contains multiple flats, the building is the parent, the children are the flats)
Captures mapping between parent and child properties (e.g. if a building contains multiple flats, the building is the parent, the children are the flats).

If a property has a parent, the parent's address details ('Secondary
Addressable Object' or sao_* fields) are included in the address.

=back

Expand Down Expand Up @@ -72,6 +78,8 @@ sub addresses_for_postcode {

my $address_fields = _address_fields();

# If blpu_class is 'P' or 'PP', it means the property is a
# 'parent shell' and so should not be offered as an option
my $addresses = $db->selectall_arrayref(
<<"SQL",
SELECT p.uprn uprn,
Expand All @@ -83,9 +91,8 @@ sub addresses_for_postcode {
LEFT OUTER JOIN child_uprns cu
ON cu.uprn = p.uprn
WHERE p.postcode = ?
AND p.uprn NOT IN (
SELECT parent_uprn FROM child_uprns
)
AND p.blpu_class != 'P'
AND p.blpu_class != 'PP'
SQL
{ Slice => {} },
$postcode,
Expand Down
Loading