![]() ![]() GetAuthorit圜ode ( None ))) print ( "Success: Wrote %s " % str ( out_shp_fn )) CreateFeature ( new_line_feat ) # create projection file srs = get_srs ( raster ) make_prj ( out_shp_fn, int ( srs. AddGeometry ( line ) # write multiline (wkbMultiLineString2shp) to shapefile new_shp = create_shp ( out_shp_fn, layer_name = "raster_pts", layer_type = "line" ) lyr = new_shp. Distance ( point2 ) if distance < max_distance : line = ogr. combinations ( points, 2 ): point1 = ogr. append ( offset2coords ( geo_transform, offset_x, offset_y )) count += 1 # create multiline (write points dictionary to line geometry (wkbMultiLineString) multi_line = ogr. count_nonzero ( trajectory ) is 0 : print ( "ERROR: The defined pixel_value ( %s ) does not occur in the raster band." % str ( pixel_value )) return None # convert pixel offset to coordinates and append to nested list of points points = count = 0 for offset_y in trajectory : offset_x = trajectory points. ![]() sqrt ( 2 * pixel_width ** 2 )) # extract pixels with the user-defined pixel value from the raster array trajectory = np. distance between points # ensures correct neighbourhoods for start and end pts of lines raster, array, geo_transform = raster2array ( raster_file_name ) pixel_width = geo_transform max_distance = np. ![]() The raster2line function is also implemented in the script.įrom flusstools.geotools import raster2array def raster2line ( raster_file_name, out_shp_fn, pixel_value ): """ Convert a raster to a line shapefile, where pixel_value determines line start and end points :param raster_file_name: STR of input raster file name, including directory must end on ".tif" :param out_shp_fn: STR of target shapefile name, including directory must end on ".shp" :param pixel_value: INT/FLOAT of a pixel value :return: None (writes new shapefile). prj projection file (recall descriptions in the shapefile section) using the spatial reference system of the input raster with the get_srs() function. If the distance between the points is smaller than max_distance, the function creates a line object from the two points and appends it to the multi_line object.Ĭreate a new shapefile (named out_shp_fn) using the create_shp() function (with integrated shapefile name length verification from flusstools geotools.create_shp()).Īdd the multi_line object as a new feature to the shapefile (according to the descriptions in the shapefile section).Ĭreate a. Point1 and point2 are required to get the distance between pairs of points. Iterate through all possible combinations of points (excluding combinations of points with themselves) with binations(iterable, r=number-of-combinations=2). Use the above-defined offset2coords function to append point coordinates to a points list.Ĭreate a multi_line object (instance of ogr.Geometry(ogr.wkbMultiLineString)), which represents the (void) final least cost path. Get the trajectory of pixels that have a user parameter-defined pixel_value (e.g., 1 to trace 1-pixels in the binary least_cost.tif) and throw an error if the trajectory is empty (i.e., np.count_nonzero(trajectory) is 0). Open a raster, its band as array, and geo_transform (geo-transformation) defined with the raster_file_name argument in the open_raster function from the raster section.Ĭalculate the maximum distance ( max_distance) between two pixels that are considered being connect-able, based on the hypothesis that the pixel height Δy and width Δx are the same: We name this function raster2line() and it builds on the following workflow: Next, we can write a core function to convert a raster dataset to a line shapefile. The offset is added 0.5 pixels in both x and y directions to meet the center of the pixel rather than the top-left pixel corner. Reservoir Volume (Sequent Peak Algorithm) ![]() Integrated Development Environments (IDEs)ġd Hydraulics (Manning-Strickler Formula) ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |