headers={ 'Authorization': "Bearer " + response['access_token'], 'Accept': "application/vnd.networkfleet.api-v1+json", 'Content-Type': "application/vnd.networkfleet.api-v1+json" }).json() if rjson['count'] != 0: x = pd.DataFrame(rjson) Name = pd.io.json.json_normalize(x['gpsMessage']) Nameedit = Name[['latitude', 'longitude', 'messageTime']] Nameedit['truck_name'] = i Nameedit['timeedit'] = pd.to_datetime(Nameedit['messageTime']) #Nameedit['hour'] = (Nameedit["timeedit"]-timezone).dt.hour Nameedit['datetime'] = (Nameedit['timeedit'] - timezone).dt.strftime("%Y-%m-%d %H:00:00") geometry = [ Point(xy) for xy in zip(Nameedit.longitude, Nameedit.latitude) ] Nameedit1 = GeoDataFrame(Nameedit, geometry=geometry) Nameedit1 = Nameedit1.groupby([ 'truck_name', 'datetime' ])['geometry'].apply(lambda x: LineString(x.tolist())) Nameedit1 = GeoDataFrame(Nameedit1, geometry='geometry') Nameedit1 = pd.DataFrame(Nameedit1.to_records()) Nameedit1 = GeoDataFrame(Nameedit1, geometry='geometry') Nameedit1.crs = {'init': 'epsg:4326'} x1 = gpd.sjoin(Nameedit1, dataSrc, op='intersects') x1 = x1[['STREET_ID', 'datetime']] appended_data = appended_data.append(x1) print("success: " + i) else: print("0 count: " + i)
# Polygon-type DataFrame input. list_gaussian_polys = gplt.utils.gaussian_polygons(gplt.utils.gaussian_points(n=1000), n=2).append( gplt.utils.gaussian_multi_polygons(gplt.utils.gaussian_points(n=1000), n=2) ) series_gaussian_polys = gpd.GeoSeries(list_gaussian_polys) dataframe_gaussian_polys = gpd.GeoDataFrame(geometry=series_gaussian_polys).assign(hue_var=[1,2,3,4]) # Hue input. list_hue_values = [1, 2, 3, 4] series_hue_values = pd.Series(list_hue_values) def map_hue_values(): return map(lambda i: list_hue_values[i], list(range(len(list_hue_values)))) # (Sankey) point input. # Start and End variables. list_start_points = [Point(a + 2, a - 2) for a in range(0, 4)] list_end_points = [Point(a - 2, a + 2) for a in range(1, 5)] series_start_points, series_end_points = gpd.GeoSeries(list_start_points), gpd.GeoSeries(list_end_points) def map_start_points(): return map(lambda i: list_start_points[i], list(range(len(list_start_points)))) def map_end_points(): return map(lambda i: list_end_points[i], list(range(len(list_end_points)))) dataframe_gaussian_points = dataframe_gaussian_points.assign(starts=list_start_points, ends=list_end_points) # (Sankey) paths. list_paths = [LineString([[a.x, a.y], [b.x, b.y]]) for a, b in zip(list_start_points, list_end_points)] series_paths = gpd.GeoSeries(list_paths) def map_paths(): return map(lambda i: list_paths[i], list(range(len(list_paths)))) dataframe_gaussian_points = dataframe_gaussian_points.assign(paths=list_paths) # (Aggplot) geometry. dataframe_gaussian_points = dataframe_gaussian_points.assign(mock_category=np.random.randint(1, 5)) aggplot_geometries = dataframe_gaussian_polys.set_index('hue_var', drop=True)
def test_offset_seconds(self): traj = self.default_traj_metric_5 traj.apply_offset_seconds("value", -20) assert traj.df.iloc[2].value == 5 assert traj.df.iloc[2].geometry == Point(10, 0)
rivers_us.geom_type # geometry rivers_us.crs # check our CRS - coordinate reference system rivers_us.total_bounds # Check the spatial extent # %% Gages information # Take only AZ gages.columns gages.STATE.unique() gages_AZ = gages[gages['STATE'] == 'AZ'] gages_AZ.shape # %% Add specific points # UA: 32.22877495, -110.97688412 # STream gauge: 34.44833333, -111.7891667 point_list = np.array([[-111.7891667, 34.44833333]]) point_geom = [Point(xy) for xy in point_list] point_df = gpd.GeoDataFrame(point_geom, columns=['geometry'], crs=gages_AZ.crs) # project into gages_az GEOMETRY # %% Look at one region in rivers # Zoom in and just look at AZ/UTAH stat_ref.columns stat_ref.STATE.unique() stat_ref_AZ = stat_ref[stat_ref['STATE'] == 'UT'] # Utah and Arizona stat_ref_AZ.shape test = pd.DataFrame(stat_ref['STATE']) # aux to see regions name # super CRUCIAL step!!! # Project points into stat_ref CRS points_project = gages_AZ.to_crs(stat_ref_AZ.crs) # %% Plot map :D
def test_get_position_of_nearest_timestamp_2(self): pos = self.default_traj_metric.get_position_at(datetime( 1970, 1, 1, 0, 0, 15), method="nearest") assert pos == Point(10, 0)
def test_get_position_interpolated_at_timestamp_2(self): pos = self.default_traj_metric.get_position_at(datetime( 1970, 1, 1, 0, 0, 15), method="interpolated") assert pos == Point(6 + 4 / 10 * 5, 0)
def test_endlocation(self): assert self.default_traj_metric.get_end_location() == Point(10, 0)
def test_get_interpolated_position_at_existing_timestamp(self): pos = self.default_traj_metric.get_position_at(datetime( 1970, 1, 1, 0, 0, 10), method="interpolated") assert pos == Point(6, 0)
def calculate_hydro(self, interval=1000, head_distance=500, get_range=False): """ Start at the top of each river, get source GSCDxFlowAccxsquare size. Calculates flow rate in m3/s, head in m and power in Watts. Parameters ---------- interval : int, optional (default 1000.) Interval in metres between testing points. head_distance : int, optional (default 500.) How far upstream to look to calculate head. get_range : boolean, optional (default False.) Whether to attempt to calculate mean, median, max etc. """ # TODO: Make the contribution % dependent on slope (elevation change/distance). # Choose the point_interval in metres # This loops through each stream and adds points down it at the specified interval # And creates a dict with these new point geometries # At 20 deg South: # 1 degree = 110704 metres -> 1 minute = 1845 metres -> 1 second = 30.75 metres # River network is 15 second resolution = 461 metres # Therefore each up_cell size is cell_area = (110704/60/60*15)**2 proj = pyproj.Proj(init='epsg:3395') # Mercator rivers = self.rivers.to_crs(epsg=3395) rho = 998.57 # density of water, kg/m3 g = 9.81 # acceleration due to gravity, m/2 n = 0.5 # overall efficiency of system hydro_points_dicts = [] count = 0 for _, row in rivers.iterrows(): geom = shape(row['geometry']) length = geom.length for _, distance in enumerate(range(0, int(length), interval)): arcid = row['arcid'] up_cells = row['up_cells'] point = Point(proj(*list(geom.interpolate(distance).coords)[0], inverse=True)) upstream_point = Point(proj(*list(geom.interpolate(distance - head_distance).coords)[0], inverse=True)) elevation = next(self.dem.sample(list(point.coords))).tolist()[0] upstream_elevation = next(self.dem.sample(list(upstream_point.coords))).tolist()[0] head = upstream_elevation - elevation runoff = next(self.flow.sample(list(point.coords))).tolist()[0] flowrate = runoff * up_cells * cell_area * (1/1000) * (1/(8760*3600)) # convert mm/year to m3/s power = rho*g*n*flowrate*head if head > 0 and flowrate > 0: hydro_points_dicts.append({'arcid': arcid, 'elevation': elevation, 'head': head, 'flowrate': flowrate, 'power': power, 'geometry': point}) count += 1 if count % 10000 == 0: print(count) self.hydro_points = gpd.GeoDataFrame(hydro_points_dicts) self.hydro_points.crs = {'init' :'epsg:4326'} print('Calculated hydro potential') if get_range: eta_t=0.88 eta_g=0.96 conv=0.6 # transfer discharge values from the river to the points self.hydro_points['discharge_accum'] = -99 self.hydro_points['discharge_max'] = -99 self.hydro_points['discharge_mean'] = -99 self.hydro_points['discharge_min'] = -99 for index, row in self.nodes_out.iterrows(): self.hydro_points.loc[index, 'discharge_accum'] = \ rivers.loc[self.nodes_out.loc[index, 'arcid'], 'discharge_accum'] self.hydro_points.loc[index, 'discharge_max'] = \ rivers.loc[self.nodes_out.loc[index, 'arcid'], 'discharge_max'] self.hydro_points.loc[index, 'discharge_mean'] = \ rivers.loc[self.nodes_out.loc[index, 'arcid'], 'discharge_mean'] self.hydro_points.loc[index, 'discharge_min'] = \ rivers.loc[self.nodes_out.loc[index, 'arcid'], 'discharge_min'] # calculate the power in watts based on Alex's formula self.hydro_points['power_accum'] = rho * g * eta_t * eta_g * conv * self.nodes_out['discharge_accum'] * self.nodes_out['head'] self.hydro_points['power_max'] = rho * g * eta_t * eta_g * conv * self.nodes_out['discharge_max'] * self.nodes_out['head'] self.hydro_points['power_mean'] = rho * g * eta_t * eta_g * conv * self.nodes_out['discharge_mean'] * self.nodes_out['head'] self.hydro_points['power_min'] = rho * g * eta_t * eta_g * conv * self.nodes_out['discharge_min'] * self.nodes_out['head']
plt.scatter(df['Rating color'], df['Rating text']) # ##### The scatter plot shows which color corresponds to which rating sns.distplot(df['Aggregate rating']) import geopandas as gpd from shapely.geometry import Point india_shp = gpd.read_file( '../input/world-map-shape-files/TM_WORLD_BORDERS-0.3.shp') # ##### Restaurants plotting on World Map geometry = [Point(xy) for xy in zip(df['Longitude'], df['Latitude'])] crs = {'init': 'epsg:27700'} gdf = gpd.GeoDataFrame(df['Restaurant ID'], crs=crs, geometry=geometry) fig, ax = plt.subplots(figsize=(20, 10)) india_shp.plot(ax=ax, cmap='Pastel2') gdf.plot(ax=ax, color='red', markersize=12) # ##### Based on the map, few latitude nad longitude does not appear to be correct # ##### Country specific Data df.groupby('Country').agg({ 'Restaurant ID': np.count_nonzero,
def project_graph(G, to_crs=None): """ Project a graph from lat-long to the UTM zone appropriate for its geographic location. Parameters ---------- G : networkx multidigraph the networkx graph to be projected to_crs : dict if not None, just project to this CRS instead of to UTM Returns ------- networkx multidigraph """ G_proj = G.copy() start_time = time.time() # create a GeoDataFrame of the nodes, name it, convert osmid to str nodes = {node: data for node, data in G_proj.nodes(data=True)} gdf_nodes = gpd.GeoDataFrame(nodes).T gdf_nodes.crs = G_proj.graph['crs'] gdf_nodes.gdf_name = '{}_nodes'.format(G_proj.name) gdf_nodes['osmid'] = gdf_nodes['osmid'].astype(np.int64).map(make_str) # create new lat/lon columns just to save that data for later, and create a # geometry column from x/y gdf_nodes['lon'] = gdf_nodes['x'] gdf_nodes['lat'] = gdf_nodes['y'] gdf_nodes['geometry'] = gdf_nodes.apply( lambda row: Point(row['x'], row['y']), axis=1) log('Created a GeoDataFrame from graph in {:,.2f} seconds'.format( time.time() - start_time)) # project the nodes GeoDataFrame to UTM gdf_nodes_utm = project_gdf(gdf_nodes, to_crs=to_crs) # extract data for all edges that have geometry attribute edges_with_geom = [] for u, v, key, data in G_proj.edges(keys=True, data=True): if 'geometry' in data: edges_with_geom.append({ 'u': u, 'v': v, 'key': key, 'geometry': data['geometry'] }) # create an edges GeoDataFrame and project to UTM, if there were any edges # with a geometry attribute. geom attr only exists if graph has been # simplified, otherwise you don't have to project anything for the edges # because the nodes still contain all spatial data if len(edges_with_geom) > 0: gdf_edges = gpd.GeoDataFrame(edges_with_geom) gdf_edges.crs = G_proj.graph['crs'] gdf_edges.gdf_name = '{}_edges'.format(G_proj.name) gdf_edges_utm = project_gdf(gdf_edges, to_crs=to_crs) # extract projected x and y values from the nodes' geometry column start_time = time.time() gdf_nodes_utm['x'] = gdf_nodes_utm['geometry'].map(lambda point: point.x) gdf_nodes_utm['y'] = gdf_nodes_utm['geometry'].map(lambda point: point.y) gdf_nodes_utm = gdf_nodes_utm.drop('geometry', axis=1) log('Extracted projected node geometries from GeoDataFrame in {:,.2f} seconds' .format(time.time() - start_time)) # clear the graph to make it a blank slate for the projected data start_time = time.time() edges = list(G_proj.edges(keys=True, data=True)) graph_name = G_proj.graph['name'] G_proj.clear() # add the projected nodes and all their attributes to the graph G_proj.add_nodes_from(gdf_nodes_utm.index) attributes = gdf_nodes_utm.to_dict() for label in gdf_nodes_utm.columns: nx.set_node_attributes(G_proj, name=label, values=attributes[label]) # add the edges and all their attributes (including reconstructed geometry, # when it exists) to the graph for u, v, key, attributes in edges: if 'geometry' in attributes: row = gdf_edges_utm[(gdf_edges_utm['u'] == u) & (gdf_edges_utm['v'] == v) & (gdf_edges_utm['key'] == key)] attributes['geometry'] = row['geometry'].iloc[0] G_proj.add_edge(u, v, key=key, **attributes) # set the graph's CRS attribute to the new, projected CRS and return the # projected graph G_proj.graph['crs'] = gdf_nodes_utm.crs G_proj.graph['name'] = '{}_UTM'.format(graph_name) if 'streets_per_node' in G.graph: G_proj.graph['streets_per_node'] = G.graph['streets_per_node'] log('Rebuilt projected graph in {:,.2f} seconds'.format(time.time() - start_time)) return G_proj
def get_distance(config1, config2): if (config1 is not None and config2 is not None): point1 = Point(config1[0], config1[1]) point2 = Point(config2[0], config2[1]) return point1.distance(point2) return math.inf
def point(l): return Point(*l)