def read_shapefileGeoPd(shp_path): ''' Read a shapefile into a Pandas dataframe Gets the current CRS and changes the CRS to '4326' to get Lat,Longs Returns df: Pandas dataframe with data and geometry column Initprj: Initial Projection of the data Newprj: New projection for the data (4326) ''' sf = gpd.read_file(shp_path) Initprj = pyepsg.get(sf.crs['init'].split(':')[1]) sf= sf.to_crs(epsg=4326) Newprj = pyepsg.get(sf.crs['init'].split(':')[1]) df = pd.DataFrame(sf) return [df,Initprj,Newprj]
def __init__(self, code): projection = pyepsg.get(code) if not isinstance(projection, pyepsg.ProjectedCRS): raise ValueError('EPSG code does not define a projection') self.epsg_code = code proj4_str = projection.as_proj4().strip() terms = [term.strip('+').split('=') for term in proj4_str.split(' ')] globe_terms = filter(lambda term: term[0] in _GLOBE_PARAMS, terms) globe = ccrs.Globe(**{_GLOBE_PARAMS[name]: value for name, value in globe_terms}) other_terms = [] for term in terms: if term[0] not in _GLOBE_PARAMS: if len(term) == 1: other_terms.append([term[0], None]) else: other_terms.append(term) super(_EPSGProjection, self).__init__(other_terms, globe) # Convert lat/lon bounds to projected bounds. # GML defines gmd:EX_GeographicBoundingBox as: # Geographic area of the entire dataset referenced to WGS 84 # NB. We can't use a polygon transform at this stage because # that relies on the existence of the map boundary... the very # thing we're trying to work out! ;-) x0, x1, y0, y1 = projection.domain_of_validity() geodetic = ccrs.Geodetic() lons = np.array([x0, x0, x1, x1]) lats = np.array([y0, y1, y1, y0]) points = self.transform_points(geodetic, lons, lats) x = points[:, 0] y = points[:, 1] self.bounds = (x.min(), x.max(), y.min(), y.max())
def epsg_to_dict(epsg): """Converts an EPSG code to a full proj4 dictionary. Parameters ---------- epsg : int e EPSG code to lookup Returns ------- dict """ p = pyepsg.get(epsg).as_proj4() return fiona.crs.from_string(p)
from dask.diagnostics import ProgressBar import geopandas as gpd from geopandas import GeoDataFrame import h5py import numpy as np import pandas as pd import pyepsg import regionmask import scipy.optimize from shapely.geometry import (Point, Polygon, box) from dask import compute CRS = pyepsg.get(4326).as_proj4() def extract_grace(fpath): """ Creates a file object for extracting GRACE data from NASA GSFC mascon product. Parameters ---------- fpath : string path to the GRACE hdf5 file Returns ------- f : file object h5py file object """
CmList = ['ProjID','CountyCode','SR','BegSeg','BegOff'] Data1 = Data1.rename(columns ={"HSIP Proj. ID":"HSIP_Proj_ID"}) FinDat = pd.merge(Data1, NewDfClean, left_on = CmList, right_on = CmList, how = 'left') FinDat1 = FinDat.set_index(['ProjID','HSIP_Proj_ID','County','CountyCode','SR','BegSeg','BegOff','EndSeg','EndOff']) maskOldDateUpd = FinDat1["Date Updated"].isna() FinDat1.loc[maskOldDateUpd,"Date Updated"] = "Old" FinDat1.drop(columns = 'LineSeg',inplace=True) FinDat_Plot = FinDat[~(FinDat.LineSeg.isna())].copy() FinDatGpd = GeoDataFrame(FinDat_Plot,geometry='LineSeg') FinDatGpd.geometry.name FinDatGpd.drop(columns = "Date Updated",inplace=True) FinDatGpd.rename(columns={"LineSeg":"geometry"},inplace=True) FinDatGpd.set_geometry("geometry",inplace=True) FinDatGpd.crs = {'init' :'epsg:4326'} pyepsg.get(FinDatGpd.crs['init'].split(':')[1]) try: os.makedirs("../ProcessedData") os.makedirs("../ProcessedData/ShapeFilesByYear2") except: "Failed to create dir" FinDatGpd.to_file("../ProcessedData/ShapeFilesByYear2/{}.shp".format(yr)) #Write the output #*********************************************************************************************************** FinDat1.to_excel(writer,yr,na_rep = "NoData", engine='xlsxwriter') FinDat1.loc[:,"Year"] = yr Data_AllYear = pd.concat([Data_AllYear,FinDat1]) Workbook = writer.book format1 = Workbook.add_format({'bg_color': '#FFC7CE', 'font_color': '#9C0006'})
def translate_epsgcode(code_number): pyepsg_crs = pyepsg.get(code_number) return pyepsg_crs.as_proj4()
SegInfoData.loc[:, "BegGeom"] = SegInfoData[["X_VALUE_BGN", "Y_VALUE_BGN" ]].apply(lambda x: Point( (x[0], x[1])), axis=1) SegInfoData.loc[:, "EndGeom"] = SegInfoData[["X_VALUE_END", "Y_VALUE_END" ]].apply(lambda x: Point( (x[0], x[1])), axis=1) SegInfoData.loc[:, "LineSeg"] = SegInfoData[["BegGeom", "EndGeom"]].apply( lambda x: LineString(x.tolist()), axis=1) SegInfoData = gpd.GeoDataFrame(SegInfoData, crs=crs, geometry="BegGeom") sum(SegInfoData.Y_VALUE_BGN.isna()) SegInfoData = SegInfoData[~SegInfoData.Y_VALUE_BGN.isna()] SegInfoData.geometry.name pyepsg.get(SegInfoData.crs["init"].split(":")[1]) Qaqc_dat = gpd.sjoin(SegInfoData, gdf, how="inner") Qaqc_dat.columns Qaqc_dat2 = Qaqc_dat[[ "CountyCode_left", "CountyCode_right", "SR_left", "SR_right", "BegSeg", "SegNo", "EndSeg", "ProjID", ]]
CrashDict[i].to_excel(writer, "Crash" + i + ".xlsx") MerDa1[i].to_excel(writer, "IncFact" + i + ".xlsx") FinDatCounty[i].to_excel(writer, "CountyDa" + i + ".xlsx") writer.save() #MerDa["I40"] #Get IMAP routes ######################################################################################################################## PathFi = os.path.join(os.path.expanduser('~'), 'OneDrive - Kittelson & Associates, Inc', 'Documents', 'IMAP_DashBoard', 'ShapeFiles', 'IMAP Routes', 'Statewide_IMAP_Routes.shp') IMAP_df = gpd.read_file(PathFi) Initprj = pyepsg.get(IMAP_df.crs['init'].split(':')[1]) my_map1 = folium.Map(location=[34.768897, -78.802428], tiles='cartodbpositron', zoom_start=10) IMAP_temp = IMAP_df.head(100) IMAP_json = IMAP_df.to_json() folium.GeoJson(IMAP_json, name='IMAP Routes', style_function=lambda feature: { 'color': 'black', 'fill': False, 'weight': 2, 'dashArray': '5, 5' }).add_to(my_map1)