# import pandas with shortcut 'pd'
import pandas as pd  
import os
from pyproj import Transformer, transform

transformer = Transformer.from_crs(2154, 4326)

headers = [
    'section',
    'nofinesset',
    'nofinessej',
    'rs',
    'rslongue',
    'complrs',
    'compldistrib',
    'numvoie',
    'typvoie',
    'voie',
    'compvoie',
    'lieuditbp',
    'commune',
    'departement',
    'libdepartement',
    'ligneacheminement',
    'telephone',
    'telecopie',
    'categetab',
    'libcategetab',
    'categagretab',
    'libcategagretab',
    'siret',
    'codeape',
    'codemft',
    'libmft',
    'codesph',
    'libsph',
    'dateouv',
    'dateautor',
    'maj',
    'numuai',
    'coordxet',
    'coordyet',
    'sourcecoordet',
    'datemaj'
]

# read_csv function which is used to read the required CSV file
data = pd.read_csv('./finess-clean.csv', sep=",", dtype='str', names=headers)
  
# display 
#print("Original 'input.csv' CSV Data: \n")
#print(data)

header_drop = [
    'section',
    # 'nofinesset',
    'nofinessej',
    #'rs',
    #'rslongue',
    'complrs',
    'compldistrib',
    'numvoie',
    'typvoie',
    'voie',
    'compvoie',
    'lieuditbp',
    'commune',
    #'departement',
    'libdepartement',
    #'ligneacheminement',
    #'telephone',
    'telecopie',
    'categetab',
    'libcategetab',
    'categagretab',
    'libcategagretab',
    #'siret',
    'codeape',
    'codemft',
    'libmft',
    'codesph',
    'libsph',
    'dateouv',
    'dateautor',
    'maj',
    'numuai',
    #'coordxet',
    #'coordyet',
    'sourcecoordet',
    'datemaj'
]

data = data.query('categetab == "603"')

# drop function which is used in removing or deleting rows or columns from the CSV files
data.drop(header_drop, inplace=True, axis=1)

def convertCoord (row):
    row.coordxet, row.coordyet = transformer.transform(row.coordxet, row.coordyet)
    return row

data.transform(convertCoord, axis=1)

data.to_json('../static/data.json', orient='values') #https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_json.html?highlight=to_json#pandas.DataFrame.to_json
