Vector maps have different geometry features, like:
All these geometry features are define in:
from grass.pygrass.vector import geometry as geo
The point is the simpler type of geometry, it is composed by at least 2 coordinates.
pnt00 = geo.Point(0, 0)
We can calculate the distance between two points
pnt00.distance(geo.Point(1, 1))
Check if it is 2D or 3D
pnt00.is2D
pnt00.z = 0
pnt00.is2D
It is also possible to create a buffer around the point, and obtain an area object
buff = pnt00.buffer(2)
buff
buff.boundary
buff.centroid
buff.isles
A line is created from several points.
line = geo.Line([(0, 0), (0, 3)])
We can perform some simple analysis like:
line.bbox()
line.length()
line.distance(geo.Point(4, 1))
line.distance?
line.is_with_topology()
buff = line.buffer(2)
buff
buff.boundary
buff.centroid
buff.isles
area = geo.Area(boundary=geo.Line([(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]), centroid=geo.Point(1, 1))
area.is_with_topology()
We saw how to:
Below, we define a function to create random point
# import the random function
import random
# import the Point and Region functions?
from grass.pygrass.vector.geometry import Point
from grass.pygrass.gis.region import Region
# define a function to produce random points
def get_random_points(num):
# inside current GRASS' region of course
reg = Region()
# loop over a series of numbers from 0 up to...
for _ in xrange(0, num):
# use the function randrange() to get both x and y random numbers
x = random.randrange(reg.south, reg.north)
y = random.randrange(reg.west, reg.east)
# at the end of each loop we need to explicitly catch the pairs of x and y numbers
# in order to produce a point
yield Point(x, y)
Using the get_random_points function write two functions:
rand_pnts = rand_vect_points('random_points', npoints=10)