def createBufferGeoPandas(inGDF, bufferDistanceMeters=5, bufferRoundness=1, projectToUTM=True): '''Create a buffer around the lines of the geojson''' # inGDF = gpd.read_file(geoJsonFileName) if len(inGDF) == 0: return [] # Transform gdf Roadlines into UTM so that Buffer makes sense if projectToUTM: tmpGDF = osmnx_funcs.project_gdf(inGDF) else: tmpGDF = inGDF gdf_utm_buffer = tmpGDF # perform Buffer to produce polygons from Line Segments gdf_utm_buffer['geometry'] = tmpGDF.buffer(bufferDistanceMeters, bufferRoundness) gdf_utm_dissolve = gdf_utm_buffer.dissolve(by='class') gdf_utm_dissolve.crs = gdf_utm_buffer.crs if projectToUTM: gdf_buffer = gdf_utm_dissolve.to_crs(inGDF.crs) else: gdf_buffer = gdf_utm_dissolve return gdf_buffer
def create_speed_gdf_v0(image_path, geojson_path, mask_path_out_gray, bin_conversion_func, mask_burn_val_key='burnValue', buffer_distance_meters=2, buffer_cap_style=1, dissolve_by='speed_m/s', bin_conversion_key='speed_mph', verbose=False): ''' Create buffer around geojson for speeds, use bin_conversion_func to assign values to the mask ''' # get gdf_buffer try: inGDF = gpd.read_file(geojson_path) except: print("Empty mask for path:", geojson_path) # create emty mask h, w = cv2.imread(image_path, 0).shape[:2] mask_gray = np.zeros((h, w)).astype(np.uint8) skimage.io.imsave(mask_path_out_gray, mask_gray) # cv2.imwrite(mask_path_out, mask_gray) return [] # project projGDF = osmnx_funcs.project_gdf(inGDF) if verbose: print("inGDF.columns:", inGDF.columns) gdf_utm_buffer = projGDF.copy() # perform Buffer to produce polygons from Line Segments gdf_utm_buffer['geometry'] = gdf_utm_buffer.buffer(buffer_distance_meters, buffer_cap_style) gdf_utm_dissolve = gdf_utm_buffer.dissolve(by=dissolve_by) gdf_utm_dissolve.crs = gdf_utm_buffer.crs gdf_buffer = gdf_utm_dissolve.to_crs(inGDF.crs) if verbose: print("gdf_buffer['geometry'].values[0]:", gdf_buffer['geometry'].values[0]) # set burn values speed_arr = gdf_buffer[bin_conversion_key].values burnVals = [bin_conversion_func(s) for s in speed_arr] gdf_buffer[mask_burn_val_key] = burnVals # create mask gdf_to_array(gdf_buffer, image_path, mask_path_out_gray, mask_burn_val_key=mask_burn_val_key, verbose=verbose) return gdf_buffer
def create_speed_gdf(image_path, geojson_path, mask_path_out_gray, bin_conversion_func, mask_burn_val_key='burnValue', buffer_distance_meters=2, buffer_roundness=1, dissolve_by='inferred_speed_mps', # 'speed_m/s', bin_conversion_key='speed_mph', # crs={'init': 'epsg:4326'}, zero_frac_thresh=0.05, verbose=False): '''Create buffer around geojson for speeds, use bin_conversion_func to assign values to the mask''' # get gdf_buffer try: inGDF = gpd.read_file(geojson_path) except: print("Can't read geojson:", geojson_path) # create emty mask h, w = skimage.io.imread(image_path).shape[:2] #h, w = cv2.imread(image_path, 0).shape[:2] mask_gray = np.zeros((h, w)).astype(np.uint8) skimage.io.imsave(mask_path_out_gray, mask_gray) # cv2.imwrite(mask_path_out, mask_gray) return [] if len(inGDF) == 0: print("Empty mask for path:", geojson_path) # create emty mask h, w = skimage.io.imread(image_path).shape[:2] # h, w = cv2.imread(image_path, 0).shape[:2] mask_gray = np.zeros((h, w)).astype(np.uint8) skimage.io.imsave(mask_path_out_gray, mask_gray) # cv2.imwrite(mask_path_out, mask_gray) return [] # project projGDF = osmnx_funcs.project_gdf(inGDF) #, to_crs=crs) if verbose: print("inGDF.columns:", inGDF.columns) gdf_utm_buffer = projGDF.copy() # perform Buffer to produce polygons from Line Segments gdf_utm_buffer['geometry'] = gdf_utm_buffer.buffer(buffer_distance_meters, buffer_roundness) gdf_utm_dissolve = gdf_utm_buffer.dissolve(by=dissolve_by) gdf_utm_dissolve.crs = gdf_utm_buffer.crs gdf_buffer = gdf_utm_dissolve.to_crs(inGDF.crs) if verbose: print("gdf_buffer['geometry'].values[0]:", gdf_buffer['geometry'].values[0]) # set burn values speed_arr = gdf_buffer[bin_conversion_key].values burnVals = [bin_conversion_func(s) for s in speed_arr] gdf_buffer[mask_burn_val_key] = burnVals # create mask gdf_to_array(gdf_buffer, image_path, mask_path_out_gray, mask_burn_val_key=mask_burn_val_key, verbose=verbose) # check to ensure no mask outside the image pixels (some images are # largely black) im_bgr = skimage.io.imread(image_path) # im_bgr = cv2.imread(image_path, 1) try: im_gray = np.sum(im_bgr, axis=2) # check if im_gray is more than X percent black zero_frac = 1. - float(np.count_nonzero(im_gray)) / im_gray.size if zero_frac >= zero_frac_thresh: print("zero_frac:", zero_frac) print("create_speed_gdf(): checking to ensure masks are null where " "image is null") # ensure the label doesn't extend beyond the image mask_gray = cv2.imread(mask_path_out_gray, 0) zero_locs = np.where(im_gray == 0) # set mask_gray to zero at location of zero_locs mask_gray[zero_locs] = 0 # overwrite cv2.imwrite(mask_path_out_gray, mask_gray) except: # something is wrong with the image... pass # h0, w0 = 100, 100 # mask_gray = np.zeros((h0, w0)).astype(np.uint8) # skimage.io.imsave(mask_path_out_gray, mask_gray) return gdf_buffer
def create_buffer_geopandas(inGDF, buffer_distance_meters=2, buffer_cap_style=1, dissolve_by='class', projectToUTM=True, verbose=False): """ Create a buffer around the lines of the geojson Arguments --------- inGDF : geodataframe Geodataframe from a SpaceNet geojson. buffer_distance_meters : float Width of buffer around geojson lines. Formally, this is the distance to each geometric object. Optional. Defaults to ``2``. buffer_cap_style : int Cap_style of buffer, see: (https://shapely.readthedocs.io/en/stable/manual.html#constructive-methods) Defaults to ``1`` (round). dissolve_by : str Method for differentiating rows in geodataframe, and creating unique mask values. Defaults to ``'class'``. projectToUTM : bool Switch to project gdf to UTM coordinates. Defaults to ``True``. verbose : bool Switch to print relevant values. Defaults to ``False``. Returns ------- gdf_buffer : geopandas dataframe Dataframe created from geojson """ # inGDF = gpd.read_file(geoJsonFileName) if len(inGDF) == 0: return [] # if we want a geojson instead of gdf for input # try: # inGDF = gpd.read_file(geoJsonFileName) # except: # return [] # Transform gdf Roadlines into UTM so that Buffer makes sense if projectToUTM: tmpGDF = osmnx_funcs.project_gdf(inGDF, inGDF.crs) else: tmpGDF = inGDF if verbose: print("inGDF.columns:", tmpGDF.columns) gdf_utm_buffer = tmpGDF.copy() # perform Buffer to produce polygons from Line Segments gdf_utm_buffer['geometry'] = tmpGDF.buffer(buffer_distance_meters, cap_style=buffer_cap_style) gdf_utm_dissolve = gdf_utm_buffer.dissolve(by=dissolve_by) gdf_utm_dissolve.crs = gdf_utm_buffer.crs if projectToUTM: gdf_buffer = gdf_utm_dissolve.to_crs(inGDF.crs) else: gdf_buffer = gdf_utm_dissolve if verbose: print("gdf_buffer['geometry'].values[0]:", gdf_buffer['geometry'].values[0]) # add the dissolve_by column back into final gdf, since it's now the index gdf_buffer[dissolve_by] = gdf_buffer.index.values return gdf_buffer