

# This program is only able to run on VIM PowerShell
# https://www.globalsino.com/ICs/

import matplotlib.pyplot as plt
import geopandas
import pandas as pd #================
import numpy as np #================
from shapely.geometry import Point, Polygon #================

df = pd.read_csv(r"C:\0Python\Michel\Distribution.csv") #================
df = df[["Price", "Latitude",  "Longitude"]] #================
df.head() #================
print(df.head())
# "shapefile" (".shp") to plot coordinates and depend on the associated ".shx" shape index format, and the ".dbf" attribute format files to properly function
# In order to utilize the ".shp" file in GeoPandas, you must also save the other two mandatory file extensions in the same directory.

idaho = geopandas.read_file(r"C:\0Python\Michel\Game_Management_Units.shp")#converts idaho into a geopandas dataframe
# print(type(idaho))
# print(idaho.head())
# The coordinates change below does not have to be made
# idaho = idaho.to_crs("EPSG:3395")#changes coordinates from WGS 84 (used in GIS) to CRS (web mapping coordinates)
idaho = idaho.to_crs(epsg=4326) #changes coordinates from WGS 84 (used in GIS) to CRS (web mapping coordinates)
# idaho.plot()
# The axis in the map is not referring to latitude and longitude coordinates, which can be adjusted using the Coordinate Reference System or CRS.
# The most common CRS used is WGS84 lat/long projection.
# See: https://geopandas.org/en/stable/docs/user_guide/projections.html

# To change to latitude and longitude coordinates
# idaho.to_crs(epsg=4326).plot()

# Reformat the csv data into a "GeoPandas Dataframe"
crs = {'init':'EPSG:4326'}
geometry = [Point(xy) for xy in zip(df['Longitude'], df['Latitude'])]
geo_df = geopandas.GeoDataFrame(df, crs = crs, geometry = geometry)
# To match the ‘shapefile’ we need specify the same CRS.
# Utilize Shapely to transform our latitude and longitude data into geometric points and then pass our original dataframe with our "crs"
# and "geometry" variables into the GeoDataFrame function will create our "geo_df" that is a copy of our original data frame but
# with the newly created "geometry" column.
geo_df.head()
print(geo_df.head())

# Both our shapefile and GeoPandas dataframe are properly formatted
# Next is visualizing our real data! >> plot the location of our property data on top of our shapefile map.
fig, ax = plt.subplots(figsize = (10,10))
# idaho.apply(lambda x: ax.annotate(s=x.NAME, xy=x.geometry.centroid.coords[0], ha='center', fontsize=7),axis=1)
idaho.apply(lambda x: ax.annotate(text=x.NAME, xy=x.geometry.centroid.coords[0], ha='center', fontsize=7),axis=1)
idaho.boundary.plot(ax=ax, color='Black', linewidth=.4)
idaho.plot(ax=ax, cmap='Pastel2', figsize=(12, 12))
ax.text(-0.05, 0.5, "xyz", transform=ax.transAxes,#not sure what this line of code does
        fontsize=1, color='gray', alpha=0.5,
        ha='center', va='center', rotation='90')

# idaho.to_crs(epsg=4326).plot(ax=ax, color='lightgrey')
geo_df.plot(ax=ax, color='red')
ax.set_title("Distribution in Idaho")

plt.show()


