Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v.multi2singlepart: a new addon to split multipart to singlepart polygons #1021

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/vector/v.multi2singlepart/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MODULE_TOPDIR = ../..

PGM = v.multi2singlepart

include $(MODULE_TOPDIR)/include/Make/Script.make

default: script
Binary file added src/vector/v.multi2singlepart/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
198 changes: 198 additions & 0 deletions src/vector/v.multi2singlepart/example.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions src/vector/v.multi2singlepart/v.multi2singlepart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<h2>DESCRIPTION</h2>

<em>v.multi2singlepart</em> creates a vector layer containing
singlepart polygons generated by separating the multipart polygons of
the input vector layer. The attributes of the input layer will be
maintained in the output vector layer. Singlepart polygons will not be
affected.

<p>
<div align="center" style="margin: 10px"> <a href="example.png">
<img src="example.png" alt="From multipart to singlepart features"
border="0" width=400 height="auto"> </a><br> <i>From multipart to singlepart features.</i> </div>


<h2>NOTES</h2>

To go from singlepart to multipart features, use the
<em>v.dissolve</em> function (see example below).


<h2>EXAMPLES</h2>

The example uses the layer <i>boundary_municp</i> from the North
Carolina dataset. You can download the sample data set from the <a
href="https://grass.osgeo.org/download/data/">grass gis website</a>

<div class="code">
<pre>
v.dissolve input=boundary_municp column=MB_NAME output=bnd_municp_dis
v.multi2singlepart input=bnd_municp_dis output=bnd_municp_split
</pre>
</div>

<h2>SEE ALSO</h2>

<em> <a
href="https://grass.osgeo.org/grass-stable/manuals/v.dissolve.html">v.dissolve</a>
</em>



<h2>AUTHOR</h2>

Paulo van Breugel, paulo at ecodiv.earth
110 changes: 110 additions & 0 deletions src/vector/v.multi2singlepart/v.multi2singlepart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/usr/bin/env python

#
############################################################################
#
# MODULE: v.multi2singlepart
# AUTHOR(S): Paulo van Breugel
# PURPOSE: Split multipart polygons into singlepart polygon features
#
# COPYRIGHT: (C) 2024 Paulo van Breugel and the GRASS Development Team
# http://ecodiv.earth
#
# This program is free software under the GNU General Public
# License (>=v2). Read the file COPYING that comes with GRASS
# for details.
#
#############################################################################
#
# REQUIREMENTS:
# -
# %module
# % description: Split multi-polygon features into single polygon features.
# % keyword: vector
# % keyword: geometry
# %end

# %option G_OPT_V_INPUT
# % description: Input vector layer
# % required : yes
# %end

# %option G_OPT_V_OUTPUT
# % description: Output vector layer
# % required : yes
# %end

# ----------------------------------------------------------------------------
# Standard
# ----------------------------------------------------------------------------

# import libraries
import sys
import atexit
import sys
import uuid
import grass.script as gs


CLEAN_LAY = []


def create_temporary_name(prefix):
tmpf = f"{prefix}{str(uuid.uuid4().hex)}"
CLEAN_LAY.append(tmpf)
return tmpf


def cleanup():
"""Remove temporary maps specified in the global list"""
maps = reversed(CLEAN_LAY)
mapset = gs.gisenv()["MAPSET"]
for map_name in maps:
for element in ("raster", "vector"):
found = gs.find_file(
name=map_name,
element=element,
mapset=mapset,
)
if found["file"]:
gs.run_command(
"g.remove",
flags="f",
type=element,
name=map_name,
quiet=True,
)


def main(options, flags):

# Copy layer and remove all but cat column in new layer
tmplayer = create_temporary_name("tmp")
gs.run_command("g.copy", vector=[options["input"], tmplayer], overwrite=True)
cols = gs.read_command("db.columns", table=tmplayer).split("\n")
cols = [x for x in cols[1:] if x != ""]
gs.run_command("v.db.dropcolumn", map=tmplayer, columns=cols)

# Overlay the original and copied layer
gs.run_command(
"v.overlay",
ainput=options["input"],
binput=tmplayer,
operator="and",
output=options["output"],
)

# Drop old cat columns
gs.run_command("v.db.dropcolumn", map=options["output"], columns=["a_cat", "b_cat"])

# Change the column names to the original names
for colname in cols:
oldcol = f"a_{colname}"
gs.run_command(
"v.db.renamecolumn", map=options["output"], column=[oldcol, colname]
)


if __name__ == "__main__":
atexit.register(cleanup)
sys.exit(main(*gs.parser()))
Loading