-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from DemocracyClub/import_southwark
Import Southwark
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
polling_stations/apps/data_collection/management/commands/import_southwark.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
""" | ||
Import Southwark | ||
""" | ||
from time import sleep | ||
|
||
from django.contrib.gis.geos import Point | ||
|
||
from data_collection.management.commands import BaseAddressCsvImporter | ||
from data_finder.helpers import geocode, geocode_point_only, PostcodeError | ||
from addressbase.models import Address | ||
|
||
|
||
class Command(BaseAddressCsvImporter): | ||
""" | ||
Imports the Polling Station data from Southwark Council | ||
""" | ||
council_id = 'E09000028' | ||
addresses_name = 'PropertyPostCodePollingStationWebLookup-2016-06-15.TSV' | ||
stations_name = 'PropertyPostCodePollingStationWebLookup-2016-06-15.TSV' | ||
csv_delimiter = '\t' | ||
elections = [ | ||
'ref.2016-06-23' | ||
] | ||
|
||
def get_station_hash(self, record): | ||
return "-".join([ | ||
record.pollingplaceaddress7, | ||
record.pollingplaceid, | ||
record.pollingdistrictreference, | ||
]) | ||
|
||
def station_record_to_dict(self, record): | ||
# format address | ||
address = "\n".join([ | ||
record.pollingplaceaddress1, | ||
record.pollingplaceaddress2, | ||
record.pollingplaceaddress3, | ||
record.pollingplaceaddress4, | ||
record.pollingplaceaddress5, | ||
record.pollingplaceaddress6, | ||
record.pollingplaceaddress7, | ||
]) | ||
while "\n\n" in address: | ||
address = address.replace("\n\n", "\n").strip() | ||
|
||
location = None | ||
if int(record.pollingplaceeasting) and int(record.pollingplacenorthing): | ||
location = Point( | ||
float(record.pollingplaceeasting), | ||
float(record.pollingplacenorthing), | ||
srid=27700) | ||
else: | ||
# no points supplied, so attempt to attach them by geocoding | ||
try: | ||
location_data = geocode_point_only(record.pollingplaceaddress7) | ||
except PostcodeError: | ||
pass | ||
|
||
if location_data: | ||
location = Point( | ||
location_data['wgs84_lon'], | ||
location_data['wgs84_lat'], | ||
srid=4326) | ||
|
||
return { | ||
'internal_council_id': record.pollingplaceid, | ||
'polling_district_id': record.pollingdistrictreference, | ||
'postcode' : record.pollingplaceaddress7, | ||
'address' : address, | ||
'location' : location | ||
} | ||
|
||
def address_record_to_dict(self, record): | ||
if record.propertynumber.strip() == '0': | ||
address = record.streetname.strip() | ||
else: | ||
address = '%s %s' % ( | ||
record.propertynumber.strip(), record.streetname.strip()) | ||
|
||
return { | ||
'address' : address, | ||
'postcode' : record.postcode.strip(), | ||
'polling_station_id': record.pollingplaceid | ||
} |