RSGISLib Vector Attributes Module

Basic Read and Writing Columns

rsgislib.vectorattrs.write_vec_column(out_vec_file: str, out_vec_lyr: str, att_column: str, att_col_datatype: int, att_col_data: List)

A function which will write a column to a vector file

Parameters
  • out_vec_file – The file / path to the vector data ‘file’.

  • out_vec_lyr – The layer to which the data is to be added.

  • att_column – Name of the output column

  • att_col_datatype – ogr data type for the output column (e.g., ogr.OFTString, ogr.OFTInteger, ogr.OFTReal)

  • att_col_data – A list of the same length as the number of features in vector file.

rsgislib.vectorattrs.write_vec_column_to_layer(out_vec_lyr_obj: osgeo.ogr.Layer, att_column: str, att_col_datatype: int, att_col_data: List)

A function which will write a column to a vector layer.

Parameters
  • out_vec_lyr_obj – GDAL/OGR vector layer object

  • att_column – Name of the output column

  • att_col_datatype – ogr data type for the output column (e.g., ogr.OFTString, ogr.OFTInteger, ogr.OFTReal)

  • att_col_data – A list of the same length as the number of features in vector file.

rsgislib.vectorattrs.read_vec_column(vec_file: str, vec_lyr: str, att_column: str) List

A function which will reads a column from a vector file

Parameters
  • vec_file – The file / path to the vector data ‘file’.

  • vec_lyr – The layer to which the data is to be read from.

  • att_column – Name of the input column

Returns

a list with the column values.

rsgislib.vectorattrs.read_vec_columns(vec_file: str, vec_lyr: str, att_columns: List[str]) List[Dict]

A function which will reads a number of column from a vector file

Parameters
  • vec_file – The file / path to the vector data ‘file’.

  • vec_lyr – The layer to which the data is to be read from.

  • att_columns – List of input attribute column names to be read in.

Returns

list of dicts with the column names as keys

rsgislib.vectorattrs.get_vec_cols_as_array(vec_file: str, vec_lyr: str, cols: List[str], lower_limit: Optional[float] = None, upper_limit: Optional[float] = None) numpy.array

A function returns an n x m numpy array with the values for the columns specified.

Parameters
  • vec_file – Input vector file.

  • vec_lyr – Input vector layer within the input file.

  • cols – list of columns to be read and returned.

  • no_data_val – no data value used within the column values. Rows with a no data value will be dropped. If None then ignored (Default: None)

  • lower_limit – Optional lower limit to define valid values. Note the same value is used for all the columns listed. If a value is found to be outside of the threshold the whole row is removed.

  • upper_limit – Optional upper limit to define valid values. Note the same value is used for all the columns listed. If a value is found to be outside of the threshold the whole row is removed.

Returns

a numpy array with the column values.

Add Columns

rsgislib.vectorattrs.add_fid_col(vec_file: str, vec_lyr: str, out_vec_file: str, out_vec_lyr: str, out_format: str = 'GPKG', out_col: str = 'fid')

A function which adds a numeric feature ID (FID) column with unique values per feature within the file.

Parameters
  • vec_file – Input vector file.

  • vec_lyr – Input vector layer within the input file.

  • out_vec_file – Output vector file

  • out_vec_lyr – output vector layer name.

  • out_format – output file format (default GPKG).

  • out_col – The output FID column name (Default: fid)

rsgislib.vectorattrs.add_numeric_col_lut(vec_file: str, vec_lyr: str, ref_col: str, val_lut: dict, out_col: str, out_vec_file: str, out_vec_lyr: str, out_format: str = 'GPKG')

A function which adds a numeric column based off an existing column in the vector file, using an dict LUT to define the values.

Parameters
  • vec_file – Input vector file.

  • vec_lyr – Input vector layer within the input file.

  • ref_col – The column within which the unique values will be identified.

  • val_lut – A dict LUT (key should be value in ref_col and value be the value outputted to out_col).

  • out_col – The output numeric column

  • out_vec_file – Output vector file

  • out_vec_lyr – output vector layer name.

  • out_format – output file format (default GPKG).

rsgislib.vectorattrs.add_numeric_col(vec_file: str, vec_lyr: str, out_col: str, out_vec_file: str, out_vec_lyr: str, out_val: float = 1, out_format: str = 'GPKG', out_col_int: bool = False)

A function which adds a numeric column with the same value for all the features.

Parameters
  • vec_file – Input vector file.

  • vec_lyr – Input vector layer within the input file.

  • out_col – The output numeric column

  • out_vec_file – Output vector file

  • out_vec_lyr – output vector layer name.

  • out_val – output numeric value

  • out_format – output file format (default GPKG).

  • out_col_int – Specify whether the output column should be an int datatype. If True (default: False) then the output column will be of type int. If False then it will be type float.

rsgislib.vectorattrs.add_string_col(vec_file: str, vec_lyr: str, out_col: str, out_vec_file: str, out_vec_lyr: str, out_val: str = 'str_val', out_format: str = 'GPKG')

A function which adds a string column with the same value for all the features.

Parameters
  • vec_file – Input vector file.

  • vec_lyr – Input vector layer within the input file.

  • out_col – The output numeric column

  • out_vec_file – Output vector file

  • out_vec_lyr – output vector layer name.

  • out_val – output numeric value

  • out_format – output file format (default GPKG).

rsgislib.vectorattrs.create_name_col(vec_file: str, vec_lyr: str, out_vec_file: str, out_vec_lyr: str, out_format: str = 'GPKG', out_col: str = 'names', x_col: str = 'MinX', y_col: str = 'MaxY', prefix: str = '', postfix: str = '', coords_lat_lon: bool = True, int_coords: bool = True, zero_x_pad: int = 0, zero_y_pad: int = 0, round_n_digts: int = 0, non_neg: bool = False)

A function which creates a column in the vector layer which can define a name using coordinates associated with the feature. Often this is useful if a tiling has been created and from this a set of images are to generated for example.

Parameters
  • vec_file – input vector file

  • vec_lyr – input vector layer name

  • out_vec_file – output vector file

  • out_vec_lyr – output vector layer name

  • out_format – The output format of the output file. (Default: GPKG)

  • out_col – The name of the output column

  • x_col – The column with the x coordinate

  • y_col – The column with the y coordinate

  • prefix – A prefix to the name

  • postfix – A postfix to the name

  • coords_lat_lon – A boolean specifying if the coordinates are lat / long

  • int_coords – A boolean specifying whether to integerise the coordinates.

  • zero_x_pad – If larger than zero then the X coordinate will be zero padded.

  • zero_y_pad – If larger than zero then the Y coordinate will be zero padded.

  • round_n_digts – If larger than zero then the coordinates will be rounded to n significant digits

  • non_neg – boolean specifying whether an negative coordinates should be made positive. (Default: False)

Calculate Column Values

rsgislib.vectorattrs.pop_bbox_cols(vec_file: str, vec_lyr: str, x_min_col: str = 'xmin', x_max_col: str = 'xmax', y_min_col: str = 'ymin', y_max_col: str = 'ymax')

A function which adds a polygons boundary bbox as attributes to each feature.

Parameters
  • vec_file – vector file.

  • vec_lyr – layer within the vector file.

  • x_min_col – output column name.

  • x_max_col – output column name.

  • y_min_col – output column name.

  • y_max_col – output column name.

rsgislib.vectorattrs.add_geom_bbox_cols(vec_file: str, vec_lyr: str, out_vec_file: str, out_vec_lyr: str, out_format: str = 'GPKG', min_x_col: str = 'MinX', max_x_col: str = 'MaxX', min_y_col: str = 'MinY', max_y_col: str = 'MaxY')

A function which adds columns to the vector layer with the bbox of each geometry.

Parameters
  • vec_file – input vector file

  • vec_lyr – input vector layer name

  • out_vec_file – output vector file

  • out_vec_lyr – output vector layer name

  • out_format – The output format of the output file. (Default: GPKG)

  • min_x_col – Name of the MinX column (Default: MinX)

  • max_x_col – Name of the MaxX column (Default: MaxX)

  • min_y_col – Name of the MinY column (Default: MinY)

  • max_y_col – Name of the MaxY column (Default: MaxY)

rsgislib.vectorattrs.add_unq_numeric_col(vec_file: str, vec_lyr: str, unq_col: str, out_col: str, out_vec_file: str, out_vec_lyr: str, out_format: str = 'GPKG')

A function which adds a numeric column based off an existing column in the vector file.

Parameters
  • vec_file – Input vector file.

  • vec_lyr – Input vector layer within the input file.

  • unq_col – The column within which the unique values will be identified.

  • out_col – The output numeric column

  • out_vec_file – Output vector file

  • out_vec_lyr – output vector layer name.

  • out_format – output file format (default GPKG).

Get Column Summaries

rsgislib.vectorattrs.get_unq_col_values(vec_file: str, vec_lyr: str, col_name: str) numpy.array

A function which splits a vector layer by an attribute value into either different layers or different output files.

Parameters
  • vec_file – Input vector file

  • vec_lyr – Input vector layer

  • col_name – The column name for which a list of unique values will be returned.

Returns

a numpy array as a list of the unique within the column.

Sort By Attributes

rsgislib.vectorattrs.sort_vec_lyr(vec_file: str, vec_lyr: str, out_vec_file: str, out_vec_lyr: str, sort_by: Union[str, List[str]], ascending: Union[bool, List[bool]], out_format: str = 'GPKG')

A function which sorts a vector layer based on the attributes of the layer. You can sort by either a single attribute or within multiple attributes if a list is provided. This function is implemented using geopandas.

Parameters
  • vec_file – the input vector file.

  • vec_lyr – the input vector layer name.

  • out_vec_file – the output vector file.

  • out_vec_lyr – the output vector layer name.

  • sort_by – either a string with the name of a single attribute or a list of strings if multiple attributes are used for the sort.

  • ascending – either a bool (True: ascending; False: descending) or list of bools if a list of attributes was given.

  • out_format – The output vector file format (Default: GPKG)