swectral.roi_to_shp#
- swectral.roi_to_shp(file_path, crs, name='', coordinates=None, roi_type='polygon', roi_list=None, return_path=True)[source]#
Write one or more polygon Regions of Interest (ROIs) to a Shapefile.
This function supports writing a single ROI via individual arguments or multiple ROIs via
roi_list. Whenroi_listis provided, all single-ROI arguments are ignored.- Parameters:
- file_path
str Path to the output ENVI XML ROI file.
- crs
strorCRS Coordinate reference system of the ROI(s).
Note: CRS must be provided. Non-georeferenced image may require additional alignment in GIS softwares.
- name
str,optional Name of the ROI when writing a single ROI. Ignored if
roi_listis provided.- coordinates
listoflistoftupleof2 (floatorint),optional Vertex coordinate pairs defining the ROI geometry when writing a single ROI.
Structure:
[ [ (x1, y1), (x2, y2), ..., (xn, yn), (x1, y1) ], # Polygon 1 [ (x1, y1), (x2, y2), ..., (xm, ym), (x1, y1) ], # Polygon 2 ... ]
Each inner list represents a polygon (for multipart geometries), and each tuple is a vertex coordinate.
Ignored if
roi_listis provided.- roi_type
str,optional Type of ROI to write.
Currently, only
"polygon"is supported. This parameter has no effect and is reserved for future extensions.Ignored if
roi_listis provided.- roi_list
listofdict,optional List of ROI definitions for writing multiple ROIs.
Each dictionary must contain the following keys:
{ "name": str, "type": str, "coordinates": list of list of tuple of float }
The
coordinatesentry represents one or more polygons as lists of vertex coordinate pairs.- return_pathbool,
optional If
True, return the path to the generated ENVI XML ROI file.
- file_path
- Returns:
- Return type:
Examples
For single ROI with one polygon:
>>> roi_to_shp( ... file_path="image_roi.shp", ... crs="EPSG:4326", ... name="roi1", ... coordinates=[[(2.0, 2.0), (3.0, 2.0), (3.0, 3.0), (2.0, 3.0)]] ... )
For single ROI with multiple polygons:
>>> roi_to_shp( ... file_path="image_roi.shp", ... crs="EPSG:4326", ... name="roi1", ... coordinates=[ ... [(2.0, 2.0), (3.0, 2.0), (3.0, 3.0), (2.0, 3.0)], ... [(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)], ... ] ... )
For multiple ROis using
roi_list:>>> roi_to_shp( ... file_path="image_roi.shp", ... roi_list=[ ... { ... "name": "test_roi_1", ... "type": "polygon", ... "coordinates": [[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)]], ... }, ... { ... "name": "test_roi_2", ... "type": "polygon", ... "coordinates": [[(2.0, 2.0), (3.0, 2.0), (3.0, 3.0), (2.0, 3.0)]], ... }, ... ] ... )