def test_extract(): import georasters as gr raster = os.path.join(DATA, 'pre1500.tif') data = gr.from_file(raster) (xmin,xsize,x,ymax,y,ysize)=data.geot (x,y)=(xmin+2507*xsize, ymax+1425*ysize) assert data.raster[gr.map_pixel(x,y,data.x_cell_size,data.y_cell_size,data.xmin,data.ymax)]==data.extract(x,y).max() assert data.raster[gr.map_pixel(x,y,data.x_cell_size,data.y_cell_size,data.xmin,data.ymax)]==data.map_pixel(x,y)
def test_extract(): import georasters as gr raster = os.path.join(DATA, 'pre1500.tif') data = gr.from_file(raster) (xmin, xsize, x, ymax, y, ysize) = data.geot (x, y) = (xmin + 2507 * xsize, ymax + 1425 * ysize) assert data.raster[gr.map_pixel(x, y, data.x_cell_size, data.y_cell_size, data.xmin, data.ymax)] == data.extract(x, y).max() assert data.raster[gr.map_pixel(x, y, data.x_cell_size, data.y_cell_size, data.xmin, data.ymax)] == data.map_pixel(x, y)
def lu_extract(row): try: c, r = gr.map_pixel(row['gpsLongitude'], row['gpsLatitude'], GeoT[1], GeoT[-1], GeoT[0], GeoT[3]) lu = esa[c, r] return lu except IndexError: print('coordinates {} {} at sea!'.format(row['gpsLongitude'], row['gpsLatitude']))
def lu_extract(row): try: c, r = gr.map_pixel(row[lon_col], row[lat_col], GeoT[1], GeoT[-1], GeoT[0], GeoT[3]) lu = pop[c, r] return lu except IndexError: pass
def gps2pixel(points_coord, geot): # transform gps coordinate to pixel return np.flip(np.vstack(gr.map_pixel(points_coord[:,0], points_coord[:,1],geot[1],geot[-1], geot[0],geot[3])).T,1)
def watershed(flow_dir_file, lake_data, val=47): """ __orig_author__ = Rdebbout Description ---------- given a flow direction grid, develop tif representing lake watershed Parameters ---------- flow_dir_file : grid file representing flow direction. DIR file within HydroSHEDS lake_data : object representing lake information including id and pour point lat/lon val : value to set watershed cells Output --------- tif file of a lake's watershed including value attribute table """ #get information about original flow direction raster #NDV = no data values , xysize= raster size , tf = geotransform NDV, xsize, ysize, tf, Projection, DataType = gr.get_geo_info( flow_dir_file) #print (tf) #load into georaster from source file arr = gr.from_file(flow_dir_file) (xmin, xsize, x, ymax, y, ysize) = arr.geot #get pour point lon and lat from lake object pp_lon, pp_lat = lake_data.pour_pt_lon, lake_data.pour_pt_lat # Function to map location in pixel of raster array: https://github.com/ozak/georasters/blob/master/georasters/georasters.py here = gr.map_pixel(pp_lon, pp_lat, xsize, ysize, xmin, ymax) init = [] init = ring_cells(here) hold = [] temp_hold = eval_ring(init, arr) hold = temp_hold #Altered this to deal with an issue when iterating across multiple lakes in original code #for every cell in ring evaluate if it is flowing to the pour point, if so add it to the hold, temp_hold continues until exhausted while len(temp_hold) > 0: for h in temp_hold: init = ring_cells(h) temp_hold = eval_ring(init, arr) hold += temp_hold #Add target cell hold.append(here) hold = list(set(hold)) # try to update transform info(tf) and shrink array dd = np.array(hold) r_min, col_min = np.amin(dd, axis=0) r_max, col_max = np.amax(dd, axis=0) #added step for WGS84 data that is not projected lon, lat = gr.map_pixel_inv(r_min, col_min, tf[1], tf[-1], tf[0], tf[3]) # shift transform to reduce NoData in the output raster shifted_tf = (lon, tf[1], tf[2], lat, tf[4], tf[5]) # insert val into the cells of the raster that represent watershed out = np.zeros(arr.shape) for idx in hold: out[idx] = val #plus 1 deals with index starting at 0, these are counts of rows, columns new = out[r_min:(r_max + 1), col_min:(col_max + 1)] go = gr.GeoRaster(new, shifted_tf, 0) go.projection = Projection go.datatype = DataType #Set name of files and create lk_id = str(lake_data.id) #create tif file go.to_tiff(f"output/lkshed/{lk_id}_watershed") df = make_rat(f"output/lkshed/{lk_id}_watershed.tif") #export value attribute table df2dbf(df, f"output/lkshed/{lk_id}_watershed.tif.vat.dbf") # #cleanup to help take care of issues on iterative processing # arr = 0 go = None here = None arr = None out = None new = None dd = None shifted_tf = None gc.collect()
def map_indexes_geot(geot, point_x, point_y): row, col = gr.map_pixel(point_x, point_y, geot[1], geot[-1], geot[0], geot[3]) return row, col
def map_indexes(raster, point_x, point_y): row, col = gr.map_pixel(point_x, point_y, raster.x_cell_size, raster.y_cell_size, raster.xmin, raster.ymax) return row, col