<commit_before>"""
Import Greenwich
"""
from django.contrib.gis.geos import Point

from data_collection.management.commands import BaseCsvStationsCsvAddressesImporter
from data_finder.helpers import geocode, geocode_point_only, PostcodeError
from addressbase.models import Address


class Command(BaseCsvStationsCsvAddressesImporter):
    """
    Imports the Polling Station data from Greenwich Council
    """
    council_id = 'E09000011'
    addresses_name = 'Addresses.csv'
    stations_name = 'PollingStations.csv'
    csv_delimiter = ','
    elections = [
        'ref.2016-06-23'
    ]

    def station_record_to_dict(self, record):
        # format address
        address = record.pollingstation
        while "\n\n" in address:
            address = address.replace("\n\n", "\n").strip()
        postcode = " ".join(address.split(' ')[-2:])

        location = None
        if float(record.lat) and float(record.lng):
            location = Point(
                float(record.lng),
                float(record.lat),
                srid=4326)
        else:
            # no points supplied, so attempt to attach them by geocoding
            try:
                location_data = geocode_point_only(postcode)
            except PostcodeError:
                pass

            if location_data:
                location = Point(
                    location_data['wgs84_lon'],
                    location_data['wgs84_lat'],
                    srid=4326)

        return {
            'internal_council_id': record.stationcode,
            'postcode'           : postcode,
            'address'            : address,
            'location'           : location
        }

    def address_record_to_dict(self, record):
        if record.pao.strip() == '0':
            address = record.streetname.strip()
        else:
            address = " ".join([
                record.pao.strip(),
                record.streetname.strip(),
                record.locality.strip(),
                record.posttown.strip(),

            ])

        return {
            'address'           : address,
            'postcode'          : record.postcode.strip(),
            'polling_station_id': record.stationcode
        }<commit_msg>update import script for Greenwich for parl.2017-06-08<commit_after>from data_collection.management.commands import BaseHalaroseCsvImporter

class Command(BaseHalaroseCsvImporter):
    council_id      = 'E09000011'
    addresses_name  = 'parl.2017-06-08/Version 1/polling_station_export-2017-05-09 2.csv'
    stations_name   = 'parl.2017-06-08/Version 1/polling_station_export-2017-05-09 2.csv'
    elections       = ['parl.2017-06-08']
    csv_encoding    = 'windows-1252'
{'fraction_non_alphanumeric': 0.09903917220990392, 'fraction_numerical': 0.03473762010347376, 'mean_word_length': 1.9264864864864866, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6442806', 'n_tokens_mistral': 844, 'n_tokens_neox': 767, 'n_words': 149}