Skip to content

Commit

Permalink
Added function to rsgislib.vectorutils and option to rsgisvectools.py…
Browse files Browse the repository at this point in the history
… to print geometry type.
  • Loading branch information
petebunting committed Jun 20, 2024
1 parent 3379230 commit 705a705
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/python/source/rsgislib_vectorutils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Vector Info
.. autofunction:: rsgislib.vectorutils.get_vec_lyr_cols
.. autofunction:: rsgislib.vectorutils.get_ogr_vec_col_datatype_from_gdal_rat_col_datatype
.. autofunction:: rsgislib.vectorutils.get_vec_lyr_geom_type
.. autofunction:: rsgislib.vectorutils.get_geom_type_name


Vectors Utilities
Expand Down
33 changes: 30 additions & 3 deletions python/rsgislib/vectorutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,33 @@ def get_vec_feat_count(
return nFeats


def get_geom_type_name(geom_type: int) -> str:
"""
A function which returns a string with a human-readable name of the
geometry type.
:param geom_type: the numerical type (e.g., rsgislib.GEOM_POLY)
:return: the name of the geometry type
"""
geom_type_name = ""
if geom_type == rsgislib.GEOM_POLY:
geom_type_name = "Polygon"
elif geom_type == rsgislib.GEOM_PT:
geom_type_name = "Point"
elif geom_type == rsgislib.GEOM_LINE:
geom_type_name = "Line"
elif geom_type == rsgislib.GEOM_MPOLY:
geom_type_name = "Multi-Polygon"
elif geom_type == rsgislib.GEOM_MPT:
geom_type_name = "Multi-Point"
elif geom_type == rsgislib.GEOM_MLINE:
geom_type_name = "Multi-Line"
else:
raise Exception(f"Do not recognise Geometry Type: '{geom_type}'")
return geom_type_name


def get_vec_lyr_geom_type(vec_file: str, vec_lyr: str = None) -> int:
"""
A function which returns an rsgislib.GEOM_XX value related to the vector
Expand Down Expand Up @@ -344,7 +371,7 @@ def merge_vectors_to_gpkg(
:param in_vec_files: is a list of input files.
:param out_vec_file: is the output GPKG database (\*.gpkg)
:param out_vec_file: is the output GPKG database (*.gpkg)
:param out_vec_lyr: is the layer name in the output database (i.e., you can merge layers into single layer or write a number of layers to the same database).
:param exists: boolean which specifies whether the database file exists or not.
"""
Expand Down Expand Up @@ -428,7 +455,7 @@ def merge_vector_lyrs_to_gpkg(
:param vec_file: is a vector file which contains multiple layers which
are to be merged
:param out_vec_file: is the output GPKG database (\*.gpkg)
:param out_vec_file: is the output GPKG database (*.gpkg)
:param out_vec_lyr: is the layer name in the output database (i.e., you can
merge layers into single layer or write a number of layers
to the same database).
Expand Down Expand Up @@ -521,7 +548,7 @@ def merge_vectors_to_gpkg_ind_lyrs(
function wraps the ogr2ogr command.
:param in_vec_files: is a list of input files.
:param out_vec_file: is the output GPKG database (\*.gpkg)
:param out_vec_file: is the output GPKG database (*.gpkg)
:param rename_dup_lyrs: If False an exception will be throw if any input layers
has the same name. If True a layer will be renamed - with
a random set of letters/numbers on the end.
Expand Down
12 changes: 12 additions & 0 deletions tools/rsgisvectools.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ if __name__ == "__main__":
default=False,
help="Print the unique values within a column of the attribute table.",
)

parser.add_argument(
"--geom",
action="store_true",
default=False,
help="Print the geometry type of the vector layer.",
)
args = parser.parse_args()

if args.lyrs:
Expand Down Expand Up @@ -119,3 +126,8 @@ if __name__ == "__main__":
)
for i, val in enumerate(unq_vals):
print(f"\t{i}: '{val}'")

if args.geom:
geom_type = rsgislib.vectorutils.get_vec_lyr_geom_type(args.vecfile, args.veclyr)
geom_type_name = rsgislib.vectorutils.get_geom_type_name(geom_type)
print(geom_type_name)

0 comments on commit 705a705

Please sign in to comment.