Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extracts nodes by number of line connections
#!/usr/bin/env python3
import grass.script as gs
def parse_node(line):
node, n_lines, xyz, y, z = line.split(",")
_, cat = node.split("=")
cat = int(cat.strip())
_, n_lines = n_lines.split("=")
n_lines = int(n_lines.strip())
_, x = xyz.split("=")
x = float(x.strip())
y = float(y.strip())
z = float(z.strip())
return(cat, n_lines, x, y, z)
def main(map_name, nlines, output):
dump = gs.read_command('v.build', map=map_name, option="dump").strip()
read = False
points = []
for line in dump.splitlines():
if read:
if line.startswith("node"):
cat, n_lines, x, y, z = parse_node(line.strip())
if n_lines in nlines:
points.append(f"{cat}|{x}|{y}|{z}")
else:
if line.startswith("Nodes"):
read = True
if points:
gs.write_command("v.in.ascii", input="-", output=output,
stdin="\n".join(points), cat=1, x=2, y=3, z=4)
if __name__ == '__main__':
main(map_name="test_clean2", nlines=(4, ), output="nodes")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment