# CSV2GEOJSON CONVERTER | CAMDEN COUNCIL | 31ST MARCH 2023 | LICENCE: https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/

import csv
import json
import datetime
import urllib.request

print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|conversion started')

#DOWNLOAD CSV (CSV RATHER THAN JSON AS THIS PREVENTS JSON PARSING ERRORS):
apiDownloadURL = 'https://opendata.camden.gov.uk/resource/7hiv-3r9k.csv?$select=last_uploaded,unique_identifier,postcode,restriction_type,times_of_operation,cashless_identifier,tariff,road_name,valid_parking_permits,epsg_27700_geojson_geometry,maximum_stay,epsg_4326_geojson_geometry,parking_spaces,controlled_parking_zone,nearest_machine,parking_bay_length_metres&$limit=10000'
print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|downloading data from Camden Open Data API...')
urllib.request.urlretrieve(apiDownloadURL, 'camden-council-parking-bays.csv')
print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|...done')

#PARSE CSV (THIS READS THE DOWNLOADED FILE, COULD BYPASS THE FILE AND READ THE HTTP RESPONSE BUT THIS WAY THE CSV CAN BE CHECKED IN THE EVENT OF AN ERROR, ETC.):
print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|parsing CSV...')
geojsonRaw = '{"type":"FeatureCollection","name":"Camden Council Parking Bays","features":[' #variable to store output GeoJSON
with open('camden-council-parking-bays.csv', mode ='r', encoding='utf-8')as csvFile:
    data = csv.DictReader(csvFile, delimiter=',')
    rowCount = 0
    for row in data:
        rowCount += 1
        try:
            geojsonRaw += '{"type":"Feature","geometry":'+row["epsg_27700_geojson_geometry"]+','
            geojsonRaw += '"properties":{'
            geojsonRaw += '"restriction_type":"'+row['restriction_type']+'",'
            geojsonRaw += '"parking_spaces":"'+row['parking_spaces']+'",'  
            geojsonRaw += '"times_of_operation":"'+row['times_of_operation']+'",'
            geojsonRaw += '"maximum_stay":"'+row['maximum_stay']+'",'  
            geojsonRaw += '"tariff":"'+row['tariff']+'",'  
            geojsonRaw += '"cashless_identifier":"'+row['cashless_identifier']+'",' 
            geojsonRaw += '"nearest_machine":"'+row['nearest_machine']+'",' 
            geojsonRaw += '"road_name":"'+row['road_name']+'",' 
            geojsonRaw += '"postcode":"'+row['postcode']+'",'  
            geojsonRaw += '"controlled_parking_zone":"'+row['controlled_parking_zone']+'",'
            geojsonRaw += '"valid_parking_permits":"'+row['valid_parking_permits']+'",' 
            geojsonRaw += '"unique_identifier":"'+row['unique_identifier']+'",'  
            geojsonRaw += '"last_uploaded":"'+row['last_uploaded']+'"'
            geojsonRaw += '}},'
            print(f'row {rowCount} - unique identifier {row["unique_identifier"]} status: converted')
        except:
            print(f'row {rowCount} - unique identifier {row["unique_identifier"]} status: ERROR')
            
#CLOSE OFF GEOJSON:
geojsonRaw = geojsonRaw[:-1]
geojsonRaw += ']}'
print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|...done')

#PRETTY PRINT JSON FOR READABILITY IN OUTPUT FILE:
print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|generating pretty printed GeoJSON...')
prettyPrint = json.loads(geojsonRaw)
geojsonOutput = json.dumps(prettyPrint, indent=4, sort_keys=False)
print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|...done')

#WRITE GEOJSON OUT TO FILE FOR USE IN A GIS:
print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|writing out GeoJSON data...')
with open('camden-council-parking-bays.geojson','w',encoding='utf-8') as dataExport:
    dataExport.write(geojsonOutput)
print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|...done')

print(f'{datetime.datetime.now():%Y-%m-%dT%H:%M:%S}|conversion finished')
