-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v.multi2singlepart: a new addon to split multipart to singlepart poly…
…gons (#1021) Addon to split multipart polygons to singlepart polygons Co-authored-by: Edouard Choinière <[email protected]>
- Loading branch information
Showing
5 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 polygon features generated by separating the multipart | ||
features of the input vector layer. The attributes of the input layer | ||
will be maintained in the output vector layer. Singlepart features 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 polygons.</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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#!/usr/bin/env python | ||
|
||
# | ||
############################################################################ | ||
# | ||
# MODULE: v.multi2singlepart | ||
# AUTHOR(S): Paulo van Breugel | ||
# PURPOSE: Split multipart polygon features into singlepart 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-part polygons into single-part polygons. | ||
# % 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) | ||
|
||
# Check topology | ||
top = gs.parse_command("v.info", flags="t", map=tmplayer) | ||
if int(top["areas"]) == 0: | ||
gs.fatal(_("The layer does not contain areas. Exiting...")) | ||
|
||
# Overlay the original and copied layer | ||
gs.run_command( | ||
"v.overlay", | ||
ainput=options["input"], | ||
binput=tmplayer, | ||
operator="and", | ||
atype="area", | ||
btype="area", | ||
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())) |