from django.contrib.gis.geos import Point
from data_collection.management.commands import BaseCsvStationsShpDistrictsImporter

"""
Ashfield publish their data on data.gov.uk :D
..but they publish Excel and MapInfo binaries :(

I've converted these to CSV and SHP format
and uploaded that data to Amazon S3 for import purposes

Additionally there's a hashes only scraper at
https://morph.io/wdiv-scrapers/DC-PollingStations-Ashfield
polling the URLs to look for changes.
"""

class Command(BaseCsvStationsShpDistrictsImporter):
    council_id = 'E07000170'
    srid = 4326
    districts_srid = 27700
    districts_name = 'polling_districts_2016-12-18'
    stations_name = 'polling_stations_2016-12-18.csv'
    csv_delimiter = ','
    elections = ['local.nottinghamshire.2017-05-04']

    def district_record_to_dict(self, record):
        return {
            'internal_council_id': str(record[0]).strip(),
            'name': '%s - %s' % (str(record[1]).strip(), str(record[0]).strip()),
            'polling_station_id': str(record[3]).strip(),
        }

    def station_record_to_dict(self, record):
        location = Point(float(record.x), float(record.y), srid=self.srid)
        return {
            'internal_council_id': record.id,
            'address' : '%s\n%s' % (record.name, record.address),
            'postcode': '',
            'location': location,
        }

@@ -9,37 +9,62 @@
 ngo.
-contrib.gis.geos import Point
+db import transaction%0Afrom django.db import connection
 %0Afro
@@ -108,27 +108,27 @@
  import Base
-Csv
+Shp
 StationsShpD
@@ -143,16 +143,67 @@
 Importer
+%0Afrom pollingstations.models import PollingDistrict
 %0A%0A%22%22%22%0AAs
@@ -566,11 +566,11 @@
 Base
-Csv
+Shp
 Stat
@@ -640,12 +640,13 @@
 d = 
-4326
+27700
 %0A   
@@ -683,32 +683,45 @@
 stricts_name = '
+New data/shp/
 polling_district
@@ -721,27 +721,16 @@
 istricts
-_2016-12-18
 '%0A    st
@@ -744,16 +744,29 @@
 name = '
+New data/shp/
 polling_
@@ -777,47 +777,12 @@
 ions
-_2016-12-18.csv'%0A    csv_delimiter = ',
+.shp
 '%0A  
@@ -1157,83 +1157,8 @@
 d):%0A
-        location = Point(float(record.x), float(record.y), srid=self.srid)%0A
     
@@ -1209,17 +1209,30 @@
 d': 
+str(
 record
-.id
+%5B0%5D).strip()
 ,%0A  
@@ -1268,36 +1268,55 @@
 ' %25 
+(str
 (record
-.name, record.address
+%5B2%5D).strip(), str(record%5B3%5D).strip()
 ),%0A 
@@ -1342,10 +1342,30 @@
 e': 
-''
+str(record%5B4%5D).strip()
 ,%0A  
@@ -1374,40 +1374,412 @@
     
+%7D%0A%0A
     
-'location': location,%0A        %7D
+@transaction.atomic%0A    def post_import(self):%0A        # fix self-intersecting polygon%0A        print(%22running fixup SQL%22)%0A        table_name = PollingDistrict()._meta.db_table%0A%0A        cursor = connection.cursor()%0A        cursor.execute(%22%22%22%0A        UPDATE %7B0%7D%0A         SET area=ST_Multi(ST_CollectionExtract(ST_MakeValid(area), 3))%0A         WHERE NOT ST_IsValid(area);%0A        %22%22%22.format(table_name))
 %0A

update import script for Ashfield