Goal: to store the sum of all values (lengths) of a vector map in a variable inside a Python script. This variable will later be shown to the user in a GUI. The computation time has to be as low as possible as well.
For the development of a tool I need to calculate certain parameters. I want to do this inside a Python script and all data is coming from GRASS GIS.
The data for this example is a vector map contain 26 lines, but this question is more about the general concept.
Here is a code snippet of my progress so far:
#import from grass.exceptions import CalledModuleError from grass.pygrass.vector import VectorTopo #create transect lines gscript.run_command('v.transects', overwrite=True, input=center_line, output=transect_line, transect_spacing=0.05, dleft=0.15 , dright=0.15, metric='along', transect_perpendicular='line') #compute width(sum(val_features)/ nFeatures) transects = VectorTopo(transect_line_clipped) transects.open(mode='r') #nFeatures nFeatures = transects.number_of("lines") #val_features <>
I have tried the following approaches but keep running into errors:
- Making a np.array as explained here: How to access vector coordinates in GRASS GIS from python?
- Trying vector attributes: http://wgbis.ces.iisc.ernet.in/grass/grass71/manuals/pygrass/vector_attributes.html
- Database connection (db): How to use python to set a variable to a value from a vector attribute table for GRASS
Which of these approaches is recommended taking into account my purpose and computation time?
The data has the following structure:
- Type: Line
- ID: unique
- Length: x (float)
- Layer: 1
- Category: unique
I can access my coordinates of the line via
test = gscript.run_command('v.out.ascii', overwrite=True, input=transect_line_clipped, output=attr)
Which I can put into a NumPy array:
But this does not provide me with the length.