#!/usr/bin/env python # coding: utf-8 # In this example, we will introduce more options for `TransBigData` grid processing, including: # - Adding rotation angle. # - Triangle and hexagon grids. # # Rotate the grids # In[1]: #Read taxi gps data import transbigdata as tbd import pandas as pd data = pd.read_csv('../example/data/TaxiData-Sample.csv',header = None) data.columns = ['VehicleNum','time','lon','lat','OpenStatus','Speed'] #Define the study area bounds = [113.75, 22.4, 114.62, 22.86] #Delete the data out of the study area data = tbd.clean_outofbounds(data,bounds = bounds,col = ['lon','lat']) # The grid coordinates system offer by `TransBigData` also support adding rotation angle for the grids. # you can also specify a rotation angle for the grids by adding the `theta` into gridding params: # In[7]: #Obtain the gridding parameters params = tbd.grid_params(bounds,accuracy = 1000) #Add a rotation angle theta = 5 params = [*params[:4],theta] print(params) # In[8]: #Map the GPS data to grids data['LONCOL'],data['LATCOL'] = tbd.GPS_to_grids(data['lon'],data['lat'],params) #Aggregate data into grids grid_agg = data.groupby(['LONCOL','LATCOL'])['VehicleNum'].count().reset_index() #Generate grid geometry grid_agg['geometry'] = tbd.gridid_to_polygon(grid_agg['LONCOL'],grid_agg['LATCOL'],params) #Change the type into GeoDataFrame import geopandas as gpd grid_agg = gpd.GeoDataFrame(grid_agg) #Plot the grids grid_agg.plot(column = 'VehicleNum',cmap = 'autumn_r',figsize=(10,5)) # # Triangle and Hexagon grids # In[9]: #Map the GPS data to grids data['loncol_1'],data['loncol_2'],data['loncol_3'] = tbd.GPS_to_grids_tri(data['lon'],data['lat'],params) #Aggregate data into grids grid_agg = data.groupby(['loncol_1','loncol_2','loncol_3'])['VehicleNum'].count().reset_index() #Generate grid geometry grid_agg['geometry'] = tbd.gridid_to_polygon_tri(grid_agg['loncol_1'],grid_agg['loncol_2'],grid_agg['loncol_3'],params) #Change the type into GeoDataFrame import geopandas as gpd grid_agg = gpd.GeoDataFrame(grid_agg) #Plot the grids grid_agg.plot(column = 'VehicleNum',cmap = 'autumn_r',figsize=(10,5)) # In[10]: #Map the GPS data to grids data['loncol_1'],data['loncol_2'],data['loncol_3'] = tbd.GPS_to_grids_hexa(data['lon'],data['lat'],params) #Aggregate data into grids grid_agg = data.groupby(['loncol_1','loncol_2','loncol_3'])['VehicleNum'].count().reset_index() #Generate grid geometry grid_agg['geometry'] = tbd.gridid_to_polygon_hexa(grid_agg['loncol_1'],grid_agg['loncol_2'],grid_agg['loncol_3'],params) #Change the type into GeoDataFrame import geopandas as gpd grid_agg = gpd.GeoDataFrame(grid_agg) #Plot the grids grid_agg.plot(column = 'VehicleNum',cmap = 'autumn_r',figsize=(10,5)) # In[ ]: