<reponame>DomGonthier/PecheFantome
import GGlib
import mysql.connector
import gpxpy
#import gpxpy.gpx # needed if installed via : apt install python3-gpxpy
from xml.etree import ElementTree as ET

#############################################################
#      ******  *******       **     ******   ****     ** ******** **********
#     **////**/**////**     ****   /*////** /**/**   /**/**///// /////**///
#    **    // /**   /**    **//**  /*   /** /**//**  /**/**          /**
#   /**       /*******    **  //** /******  /** //** /**/*******     /**
#   /**       /**///**   **********/*//// **/**  //**/**/**////      /**
#   //**    **/**  //** /**//////**/*    /**/**   //****/**          /**
#    //****** /**   //**/**     /**/******* /**    //***/********    /**
#     //////  //     // //      // ///////  //      /// ////////     //
#
#
#         __       __
#        / <`     '> \
#       (  / @   @ \  )
#        \(_ _\_/_ _)/
#      (\ `-/     \-' /)
#       "===\     /==="
#        .==')___(`==.
#       ' .='     `=.
#
##############################################################
def get_elements(element_name,element_description):
	db = mysql.connector.connect(
	  host="cidco.ca",
	  user="crabnet",
	  password="<PASSWORD>$",
	  database="crabnet"
	)
	cursor = db.cursor()
	cursor.execute("SELECT * FROM crabnet.dfo_engins WHERE type='CASIER/CAGE - TRAP/POT'")
	result = cursor.fetchall()

	gpx = gpxpy.gpx.GPX()
	gpx.name = element_name
	gpx.description = element_description

	# Pour tous les casiers rapportes
	for trap in result:
		longitude = trap[6]
		latitude  = trap[7]
		waypoint = gpxpy.gpx.GPXWaypoint()
		waypoint.longitude = longitude
		waypoint.latitude  = latitude
		waypoint.name      = "Casier {}".format(trap[0])
		waypoint.description = trap[2]
		gpx.waypoints.append(waypoint)
	areas = gpx.to_xml()
	return areas

def extract_coordinates(areas):
	root = ET.fromstring(areas)
	liste_coordinates = []
	for i in range(1, len(root)):
		coord = root[i].attrib
		latitude = float(coord['lat'])
		longitude = float(coord['lon'])
		coordinate = [longitude, latitude]
		liste_coordinates += [coordinate]
	return liste_coordinates

def export_to(areas,export_format,filename):
	if export_format == "gpx":
		with open(filename+".gpx", "w") as f:
			f.write(areas)
	elif export_format == "shapefile":
		liste_coordinates = extract_coordinates(areas)
		GGlib.ecriture_points_2shp(liste_coordinates,filename)
	elif export_format == "csv":
		liste_coordinates = extract_coordinates(areas)
		GGlib.write_points_2csv(liste_coordinates,filename)
	else:
		error = "error: not a valid format ! \n options are : gpx, shapefile, csv"
		print(error)



import GGlib
import mysql.connector
import gpxpy
#import gpxpy.gpx # needed if installed via : apt install python3-gpxpy
from xml.etree import ElementTree as ET

#############################################################
#      ******  *******       **     ******   ****     ** ******** **********
#     **////**/**////**     ****   /*////** /**/**   /**/**///// /////**///
#    **    // /**   /**    **//**  /*   /** /**//**  /**/**          /**
#   /**       /*******    **  //** /******  /** //** /**/*******     /**
#   /**       /**///**   **********/*//// **/**  //**/**/**////      /**
#   //**    **/**  //** /**//////**/*    /**/**   //****/**          /**
#    //****** /**   //**/**     /**/******* /**    //***/********    /**
#     //////  //     // //      // ///////  //      /// ////////     //
#
#
#         __       __
#        / <`     '> \
#       (  / @   @ \  )
#        \(_ _\_/_ _)/
#      (\ `-/     \-' /)
#       "===\     /==="
#        .==')___(`==.
#       ' .='     `=.
#
##############################################################
def get_elements(element_name,element_description):
	db = mysql.connector.connect(
	  host="cidco.ca",
	  user="crabnet",
	  password="<PASSWORD>$",
	  database="crabnet"
	)
	cursor = db.cursor()
	cursor.execute("SELECT * FROM crabnet.dfo_engins WHERE type='CASIER/CAGE - TRAP/POT'")
	result = cursor.fetchall()

	gpx = gpxpy.gpx.GPX()
	gpx.name = element_name
	gpx.description = element_description

	# Pour tous les casiers rapportes
	for trap in result:
		longitude = trap[6]
		latitude  = trap[7]
		waypoint = gpxpy.gpx.GPXWaypoint()
		waypoint.longitude = longitude
		waypoint.latitude  = latitude
		waypoint.name      = "Casier {}".format(trap[0])
		waypoint.description = trap[2]
		gpx.waypoints.append(waypoint)
	areas = gpx.to_xml()
	return areas

def extract_coordinates(areas):
	root = ET.fromstring(areas)
	liste_coordinates = []
	for i in range(1, len(root)):
		coord = root[i].attrib
		latitude = float(coord['lat'])
		longitude = float(coord['lon'])
		coordinate = [longitude, latitude]
		liste_coordinates += [coordinate]
	return liste_coordinates

def export_to(areas,export_format,filename):
	if export_format == "gpx":
		with open(filename+".gpx", "w") as f:
			f.write(areas)
	elif export_format == "shapefile":
		liste_coordinates = extract_coordinates(areas)
		GGlib.ecriture_points_2shp(liste_coordinates,filename)
	elif export_format == "csv":
		liste_coordinates = extract_coordinates(areas)
		GGlib.write_points_2csv(liste_coordinates,filename)
	else:
		error = "error: not a valid format ! \n options are : gpx, shapefile, csv"
		print(error)
#import gpxpy.gpx # needed if installed via : apt install python3-gpxpy ############################################################# #      ******  *******       **     ******   ****     ** ******** ********** #     **////**/**////**     ****   /*////** /**/**   /**/**///// /////**/// #    **    // /**   /**    **//**  /*   /** /**//**  /**/**          /** #   /**       /*******    **  //** /******  /** //** /**/*******     /** #   /**       /**///**   **********/*//// **/**  //**/**/**////      /** #   //**    **/**  //** /**//////**/*    /**/**   //****/**          /** #    //****** /**   //**/**     /**/******* /**    //***/********    /** #     //////  //     // //      // ///////  //      /// ////////     // # # #         __       __ #        / <`     '> \ #       (  / @   @ \  ) #        \(_ _\_/_ _)/ #      (\ `-/     \-' /) #       "===\     /===" #        .==')___(`==. #       ' .='     `=. # ############################################################## # Pour tous les casiers rapportes