Re: [GRASS-user] GRASS SCRIPT Execute Error

2021-07-26 Thread Ayush Chauhan
Update:

So, the folder from which I was running the command was wrong, and when ran
the script from the correct folder, it ran just fine.

On Tue, Jul 20, 2021 at 10:55 PM Ayush Chauhan  wrote:

> Hi Veronica, everyone,
>
> I used the below URL to re-install the grass7.8, after deleting the
> previous grass6.4.4 and previous grass7.8.5 versions.
>
>
> https://grass.osgeo.org/grass78/source/snapshot/grass-7.8.git_src_snapshot_2021_07_17.tar.gz
>
> I am trying to run a simple grass script, that will count the number of
> points on the geojson file.
>
> ```
> # %module
>
> # % description: greets the user and prints the information of a spatial file
> # %end
> # %option
> # % key: test
> # % type: string
> # % required: yes
> # % multiple: no
> # % description: Geospatial test file
> # %end
>
> import sys
> from grass.pygrass.modules import Module
> import grass.script as grass
>
>
> def main():
> # Import raster and vector
> Module("v.in.ogr", input=opts["test"], output="test", overwrite=True)
> info = grass.vector_info("test")
> print("Number of points: %s" % info["points"])
>
>
> if __name__ == "__main__":
> opts, _ = grass.parser()
> sys.exit(main())
>
> ```
>
> Now, when I am running the following command:
> ```
> /home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78 -c EPSG:4326
> location --exec /home/ec2-user/volume_calc/.venv/bin/python3
> /home/ec2-user/volume_calc/simple_test.py
> test=/home/ec2-user/volume_calc/media/tour_id/tmpb1civ9id_grass_engine/test.geojson
> ```
>
> I receive the following error:
>
> ```
> Starting GRASS GIS...
> Creating new GRASS GIS location ...
> Traceback (most recent call last):
>   File "/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78", line
> 2348, in 
> main()
>   File "/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78", line
> 2248, in main
> geofile=params.geofile, create_new=True)
>   File "/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78", line
> 1002, in set_mapset
> create_location(gisdbase, location_name, geofile)
>   File "/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78", line
> 804, in create_location
> epsg=epsg, datum_trans=datum_trans)
>   File
> "/home/ec2-user/src/grass/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py",
> line 1541, in create_location
> gisdbase = gisenv()['GISDBASE']
> KeyError: 'GISDBASE'
> ```
>
> I have set all the environment variables for the binary file of grass78
> ("/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78")
>
> GISBASE: /home/ec2-user/src/grass/dist.x86_64-pc-linux-gnu
> GUI: text
> GRASSBIN: /home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78
> PYTHONPATH: /home/ec2-user/src/grass/dist.x86_64-pc-linux-gnu/etc/python
> GISDBASE: /home/ec2-user/volume_calc/media/tour_id
> LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$GISBASE/lib
> PATH = $PATH:/usr/bin:$GISBASE/bin:$GISBASE/scripts
> LC_ALL: C.UTF-8
>
> Still, the python grass
> script(/home/ec2-user/src/grass/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py)
> is giving me the GISDBASE error (I think it is env variable error, not
> sure).
> Please tell me, is there any environment variable that I am forgetting? Or
> this is something else.
>
> And this is happening after the new installation. In my previous version,
> I was able to run this basic script to test my grass installation (which is
> failing this time).
> Please let me know if you need more information.
> Thanks
>
> On Mon, Jul 19, 2021 at 8:45 PM Veronica Andreo 
> wrote:
>
>> Hi Ayush
>>
>> GRASS 7.8 already has the r.external module, so no need to patch it. I'd
>> try first by using a clean installation of grass 7.8.5 (or compilation of
>> 7.8.6 for that matter).
>>
>> Also the same way there's v.import for vector data, there's r.import for
>> quick raster import, so you could try that for importing your raster map.
>>
>> my 0.02 cents
>> Vero
>>
>> El lun, 19 jul 2021 a las 8:16, Ayush Chauhan ()
>> escribió:
>>
>>> Hi,
>>>
>>> I am using the below code in my grass script to calculate volume from
>>> the DSM file input into the script and the polygon points on the DSM.
>>> ```
>>>
>>> # %module
>>> # % description: Calculate volume of area and prints the volume to stdout
>>> # %end
>>> # %option
>>> # % key: area_file
>>> # % type: string
>>> # % required: yes
>>> # % multiple: no
>>> # % description: Geospatial file containing the area to measure
>>> # %end
>>> # %option
>>> # % key: points_file
>>> # % type: string
>>> # % required: yes
>>> # % multiple: no
>>> # % description: Geospatial file containing the points defining the area
>>> # %end
>>> # %option
>>> # % key: dsm_file
>>> # % type: string
>>> # % required: yes
>>> # % multiple: no
>>> # % description: GeoTIFF DEM containing the surface
>>> # %end
>>>
>>> import sys
>>> from grass.pygrass.modules import Module
>>> import grass.script as grass
>>>
>>>
>>> def main():
>>>
>>> Module(
>>> "v.import",
>>> input=opts['area_file']

Re: [GRASS-user] GRASS SCRIPT Execute Error

2021-07-20 Thread Ayush Chauhan
Hi Veronica, everyone,

I used the below URL to re-install the grass7.8, after deleting the
previous grass6.4.4 and previous grass7.8.5 versions.

https://grass.osgeo.org/grass78/source/snapshot/grass-7.8.git_src_snapshot_2021_07_17.tar.gz

I am trying to run a simple grass script, that will count the number of
points on the geojson file.

```
# %module
# % description: greets the user and prints the information of a spatial file
# %end
# %option
# % key: test
# % type: string
# % required: yes
# % multiple: no
# % description: Geospatial test file
# %end

import sys
from grass.pygrass.modules import Module
import grass.script as grass


def main():
# Import raster and vector
Module("v.in.ogr", input=opts["test"], output="test", overwrite=True)
info = grass.vector_info("test")
print("Number of points: %s" % info["points"])


if __name__ == "__main__":
opts, _ = grass.parser()
sys.exit(main())

```

Now, when I am running the following command:
```
/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78 -c EPSG:4326
location --exec /home/ec2-user/volume_calc/.venv/bin/python3
/home/ec2-user/volume_calc/simple_test.py
test=/home/ec2-user/volume_calc/media/tour_id/tmpb1civ9id_grass_engine/test.geojson
```

I receive the following error:

```
Starting GRASS GIS...
Creating new GRASS GIS location ...
Traceback (most recent call last):
  File "/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78", line
2348, in 
main()
  File "/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78", line
2248, in main
geofile=params.geofile, create_new=True)
  File "/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78", line
1002, in set_mapset
create_location(gisdbase, location_name, geofile)
  File "/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78", line
804, in create_location
epsg=epsg, datum_trans=datum_trans)
  File
"/home/ec2-user/src/grass/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py",
line 1541, in create_location
gisdbase = gisenv()['GISDBASE']
KeyError: 'GISDBASE'
```

I have set all the environment variables for the binary file of grass78
("/home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78")

GISBASE: /home/ec2-user/src/grass/dist.x86_64-pc-linux-gnu
GUI: text
GRASSBIN: /home/ec2-user/src/grass/bin.x86_64-pc-linux-gnu/grass78
PYTHONPATH: /home/ec2-user/src/grass/dist.x86_64-pc-linux-gnu/etc/python
GISDBASE: /home/ec2-user/volume_calc/media/tour_id
LD_LIBRARY_PATH: $LD_LIBRARY_PATH:$GISBASE/lib
PATH = $PATH:/usr/bin:$GISBASE/bin:$GISBASE/scripts
LC_ALL: C.UTF-8

Still, the python grass
script(/home/ec2-user/src/grass/dist.x86_64-pc-linux-gnu/etc/python/grass/script/core.py)
is giving me the GISDBASE error (I think it is env variable error, not
sure).
Please tell me, is there any environment variable that I am forgetting? Or
this is something else.

And this is happening after the new installation. In my previous version, I
was able to run this basic script to test my grass installation (which is
failing this time).
Please let me know if you need more information.
Thanks

On Mon, Jul 19, 2021 at 8:45 PM Veronica Andreo 
wrote:

> Hi Ayush
>
> GRASS 7.8 already has the r.external module, so no need to patch it. I'd
> try first by using a clean installation of grass 7.8.5 (or compilation of
> 7.8.6 for that matter).
>
> Also the same way there's v.import for vector data, there's r.import for
> quick raster import, so you could try that for importing your raster map.
>
> my 0.02 cents
> Vero
>
> El lun, 19 jul 2021 a las 8:16, Ayush Chauhan ()
> escribió:
>
>> Hi,
>>
>> I am using the below code in my grass script to calculate volume from the
>> DSM file input into the script and the polygon points on the DSM.
>> ```
>>
>> # %module
>> # % description: Calculate volume of area and prints the volume to stdout
>> # %end
>> # %option
>> # % key: area_file
>> # % type: string
>> # % required: yes
>> # % multiple: no
>> # % description: Geospatial file containing the area to measure
>> # %end
>> # %option
>> # % key: points_file
>> # % type: string
>> # % required: yes
>> # % multiple: no
>> # % description: Geospatial file containing the points defining the area
>> # %end
>> # %option
>> # % key: dsm_file
>> # % type: string
>> # % required: yes
>> # % multiple: no
>> # % description: GeoTIFF DEM containing the surface
>> # %end
>>
>> import sys
>> from grass.pygrass.modules import Module
>> import grass.script as grass
>>
>>
>> def main():
>>
>> Module(
>> "v.import",
>> input=opts['area_file'],
>> output="polygon_area",
>> overwrite=True
>> )
>> Module(
>> "v.import",
>> input=opts['points_file'],
>> output="polygon_points",
>> overwrite=True
>> )
>> Module(
>> "v.buffer",
>> input="polygon_area",
>> s=True,
>> type="area",
>> output="region",
>> distance=1,
>> minordistance=1,
>> overwrite=True
>>

Re: [GRASS-user] GRASS SCRIPT Execute Error

2021-07-19 Thread Veronica Andreo
Hi Ayush

GRASS 7.8 already has the r.external module, so no need to patch it. I'd
try first by using a clean installation of grass 7.8.5 (or compilation of
7.8.6 for that matter).

Also the same way there's v.import for vector data, there's r.import for
quick raster import, so you could try that for importing your raster map.

my 0.02 cents
Vero

El lun, 19 jul 2021 a las 8:16, Ayush Chauhan ()
escribió:

> Hi,
>
> I am using the below code in my grass script to calculate volume from the
> DSM file input into the script and the polygon points on the DSM.
> ```
>
> # %module
> # % description: Calculate volume of area and prints the volume to stdout
> # %end
> # %option
> # % key: area_file
> # % type: string
> # % required: yes
> # % multiple: no
> # % description: Geospatial file containing the area to measure
> # %end
> # %option
> # % key: points_file
> # % type: string
> # % required: yes
> # % multiple: no
> # % description: Geospatial file containing the points defining the area
> # %end
> # %option
> # % key: dsm_file
> # % type: string
> # % required: yes
> # % multiple: no
> # % description: GeoTIFF DEM containing the surface
> # %end
>
> import sys
> from grass.pygrass.modules import Module
> import grass.script as grass
>
>
> def main():
>
> Module(
> "v.import",
> input=opts['area_file'],
> output="polygon_area",
> overwrite=True
> )
> Module(
> "v.import",
> input=opts['points_file'],
> output="polygon_points",
> overwrite=True
> )
> Module(
> "v.buffer",
> input="polygon_area",
> s=True,
> type="area",
> output="region",
> distance=1,
> minordistance=1,
> overwrite=True
> )
> Module("r.external", input=opts['dsm_file'], output="dsm",
> overwrite=True)
> # Set Grass region and resolution to DSM
> Module("g.region", raster="dsm")
> # Set Grass region to vector bbox
> Module("g.region", vector="region")
> # Create a mask to speed up computation
> Module("r.mask", vector="region")
> # Transfer dsm raster data to vector
> Module("v.what.rast", map="polygon_points", raster="dsm",
> column="height")
> # Decimate DSM and generate interpolation of new terrain
> Module("v.surf.rst", input="polygon_points", zcolumn="height",
>elevation="dsm_below_pile", overwrite=True)
> # Compute difference between dsm and new dsm
> Module("r.mapcalc",
>expression='pile_height_above_dsm=dsm-dsm_below_pile',
> overwrite=True)
> # Set region to polygon area to calculate volume
> Module("g.region", vector="polygon_area")
> # Volume output from difference
> Module("r.volume", input="pile_height_above_dsm", f=True)
>
> return 0
>
>
> if __name__ == "__main__":
> opts, _ = grass.parser()
> sys.exit(main())
>
> ```
>
> The area file I am providing contains the below
> contents(area_file.geojson):
>
> ```
>
> {"type": "Feature", "properties": {}, "geometry": {"type": "Polygon",
> "coordinates": [[[73.31231666938123, 26.22297239890824],
> [73.31241572405983, 26.222379996744422], [73.3134282830031,
> 26.222577464467804], [73.31281194277611, 26.222906576595037],
> [73.31231666938123, 26.22297239890824]]]}}
>
> ```
>
> The points file I am providing contains the below
> contents(points_file.geojson):
>
> ```
>
> {"type": "FeatureCollection", "features": [{"type": "Feature",
> "properties": {}, "geometry": {"type": "Point", "coordinates":
> [73.31231666938123, 26.22297239890824]}}, {"type": "Feature", "properties":
> {}, "geometry": {"type": "Point", "coordinates": [73.31241572405983,
> 26.222379996744422]}}, {"type": "Feature", "properties": {}, "geometry":
> {"type": "Point", "coordinates": [73.3134282830031, 26.222577464467804]}},
> {"type": "Feature", "properties": {}, "geometry": {"type": "Point",
> "coordinates": [73.31281194277611, 26.222906576595037]}}, {"type":
> "Feature", "properties": {}, "geometry": {"type": "Point", "coordinates":
> [73.31231666938123, 26.22297239890824]}}]}
>
> ```
>
> When I run the below command,
>
> ```
>
> */home/ec2-user/grass/bin.x86_64-pc-linux-gnu/grass78 -c \*
> */home/ec2-user/volume_calc/media/tour_id/files/dsm_volume.tif location \*
> *--exec python3 /home/ec2-user/volume_calc/grass_calc_vol.py \*
> *area_file=/home/ec2-user/volume_calc/media/tour_id/tmpo99fe5bi_grass_engine/area_file.geojson
> \*
> *points_file=/home/ec2-user/volume_calc/media/tour_id/tmpo99fe5bi_grass_engine/points_file.geojson
> \*
> *dsm_file=/home/ec2-user/volume_calc/media/tour_id/files/dsm_volume.tif*
>
> ```
>
> I get below output.
> ```
>
> (.venv) [ec2-user@ip-10-0-3-14 volume_calc]$
> /home/ec2-user/grass/bin.x86_64-pc-linux-gnu/grass78 -c
> /home/ec2-user/volume_calc/media/tour_id/files/dsm_volume.tif location
> --exec python3 /home/ec2-user/volume_calc/grass_calc_vol.py
> area_file=/home/ec2-user/volume_calc/media/tour_id/tmpo99fe5bi_grass_engine/area_file.

[GRASS-user] GRASS SCRIPT Execute Error

2021-07-18 Thread Ayush Chauhan
Hi,

I am using the below code in my grass script to calculate volume from the
DSM file input into the script and the polygon points on the DSM.
```

# %module
# % description: Calculate volume of area and prints the volume to stdout
# %end
# %option
# % key: area_file
# % type: string
# % required: yes
# % multiple: no
# % description: Geospatial file containing the area to measure
# %end
# %option
# % key: points_file
# % type: string
# % required: yes
# % multiple: no
# % description: Geospatial file containing the points defining the area
# %end
# %option
# % key: dsm_file
# % type: string
# % required: yes
# % multiple: no
# % description: GeoTIFF DEM containing the surface
# %end

import sys
from grass.pygrass.modules import Module
import grass.script as grass


def main():

Module(
"v.import",
input=opts['area_file'],
output="polygon_area",
overwrite=True
)
Module(
"v.import",
input=opts['points_file'],
output="polygon_points",
overwrite=True
)
Module(
"v.buffer",
input="polygon_area",
s=True,
type="area",
output="region",
distance=1,
minordistance=1,
overwrite=True
)
Module("r.external", input=opts['dsm_file'], output="dsm",
overwrite=True)
# Set Grass region and resolution to DSM
Module("g.region", raster="dsm")
# Set Grass region to vector bbox
Module("g.region", vector="region")
# Create a mask to speed up computation
Module("r.mask", vector="region")
# Transfer dsm raster data to vector
Module("v.what.rast", map="polygon_points", raster="dsm",
column="height")
# Decimate DSM and generate interpolation of new terrain
Module("v.surf.rst", input="polygon_points", zcolumn="height",
   elevation="dsm_below_pile", overwrite=True)
# Compute difference between dsm and new dsm
Module("r.mapcalc",
   expression='pile_height_above_dsm=dsm-dsm_below_pile',
overwrite=True)
# Set region to polygon area to calculate volume
Module("g.region", vector="polygon_area")
# Volume output from difference
Module("r.volume", input="pile_height_above_dsm", f=True)

return 0


if __name__ == "__main__":
opts, _ = grass.parser()
sys.exit(main())

```

The area file I am providing contains the below contents(area_file.geojson):

```

{"type": "Feature", "properties": {}, "geometry": {"type": "Polygon",
"coordinates": [[[73.31231666938123, 26.22297239890824],
[73.31241572405983, 26.222379996744422], [73.3134282830031,
26.222577464467804], [73.31281194277611, 26.222906576595037],
[73.31231666938123, 26.22297239890824]]]}}

```

The points file I am providing contains the below
contents(points_file.geojson):

```

{"type": "FeatureCollection", "features": [{"type": "Feature",
"properties": {}, "geometry": {"type": "Point", "coordinates":
[73.31231666938123, 26.22297239890824]}}, {"type": "Feature", "properties":
{}, "geometry": {"type": "Point", "coordinates": [73.31241572405983,
26.222379996744422]}}, {"type": "Feature", "properties": {}, "geometry":
{"type": "Point", "coordinates": [73.3134282830031, 26.222577464467804]}},
{"type": "Feature", "properties": {}, "geometry": {"type": "Point",
"coordinates": [73.31281194277611, 26.222906576595037]}}, {"type":
"Feature", "properties": {}, "geometry": {"type": "Point", "coordinates":
[73.31231666938123, 26.22297239890824]}}]}

```

When I run the below command,

```

*/home/ec2-user/grass/bin.x86_64-pc-linux-gnu/grass78 -c \*
*/home/ec2-user/volume_calc/media/tour_id/files/dsm_volume.tif location \*
*--exec python3 /home/ec2-user/volume_calc/grass_calc_vol.py \*
*area_file=/home/ec2-user/volume_calc/media/tour_id/tmpo99fe5bi_grass_engine/area_file.geojson
\*
*points_file=/home/ec2-user/volume_calc/media/tour_id/tmpo99fe5bi_grass_engine/points_file.geojson
\*
*dsm_file=/home/ec2-user/volume_calc/media/tour_id/files/dsm_volume.tif*

```

I get below output.
```

(.venv) [ec2-user@ip-10-0-3-14 volume_calc]$
/home/ec2-user/grass/bin.x86_64-pc-linux-gnu/grass78 -c
/home/ec2-user/volume_calc/media/tour_id/files/dsm_volume.tif location
--exec python3 /home/ec2-user/volume_calc/grass_calc_vol.py
area_file=/home/ec2-user/volume_calc/media/tour_id/tmpo99fe5bi_grass_engine/area_file.geojson
points_file=/home/ec2-user/volume_calc/media/tour_id/tmpo99fe5bi_grass_engine/points_file.geojson
dsm_file=/home/ec2-user/volume_calc/media/tour_id/files/dsm_volume.tif
Starting GRASS GIS...
Creating new GRASS GIS location ...
Cleaning up temporary files...
Executing  ...
Check if OGR layer  contains polygons...
 100%
Creating attribute table for layer ...
Importing 1 features (OGR layer )...
 100%
-
Registering primitives...
-
Cleaning polygons
-
Breaking polygons...
Breaking polygons (pass 1: select break p