0

I am rerunning code in GRASS that has worked in the past (about six months ago) and am now having problems.

In the GUI, I have created a new mapset and imported the 'SimplifedCoastline' file to the location (and set the default region based of the coastline's extent) before entering the code below into the Python Shell within GRASS. The objective is to create a visibility graph of shortest paths/lines between the points (centroids.txt) while going around the coastline as an obstacle.

The code:

import os
import grass.script as gscript

directory = r"E:\CollabProject\VisibilityAnalysis\VisibilityInputs\IntegerInputs"
os.listdir(directory)
centroids = open(os.path.join(directory, "Centroids.txt"), 'r')
data = centroids.read()
vn = "Centroids_visibility"

gscript.run_command("v.net.visibility",input="SimplifiedCoastline", output=vn, coordinate=data)

outname = "E:\CollabProject\VisibilityAnalysis\VisibilityOutputs"
gscript.run_command("v.out.ogr", input=vn, type="line", dsn=outname, output="visibility_output.shp")

The v.out.ogr command results in:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\OSGEO4~1\apps\grass\grass-7.2.2\etc\python\grass\script\core.py", line 410, in run_command 
return handle_errors(returncode, returncode, args, kwargs)
File "C:\OSGEO4~1\apps\grass\grass-7.2.2\etc\python\grass\script\core.py", line 329, in handle_errors
returncode=returncode)
CalledModuleError: Module run None ['v.out.ogr', 'dsn=E:\\CollabProject\\VisibilityAnalysis\\VisibilityOutputs', 'input=Centroids_visibility', 'type=line', 'output=visibility_output.shp'] ended with error
Process ended with non-zero return code 1. See errors in the (error) output.

I'm not sure if it is failing because of an issue with v.out.ogr or perhaps because v.net.visibility isn't running correctly and creating an empty output, so therefore v.out.ogr can't run properly?

I have tried:

  • running this code on several other computers, as some forum posts suggested this error could result from GDAL conflicts from downloading through OSGeo
  • downloading GRASS alone onto a computer and running the code; downloading GRASS and QGIS onto a computer and running the code
  • importing the data from a shapefile rather than text file
  • running the code on a test dataset with only 27 points so it shouldn't be maxing out data limits
  • going through the documentation for v.net.visibility and v.out.ogr and searching on forums

I seek any suggestions on how to correct this, as well as view the output of v.net.visibility to confirm it is being created correctly.

I can view the visibility graph that is being created in GRASS' Map Display so I know it exists, but I can't export it using v.out.ogr. The error I receive when trying to export it through the Python Shell is above; the error I receive when trying to export it through the point and click method is:

v.out.ogr input=inputdatatestttt@PERMANENT output=E:\CollabProject\VisibilityAnalysis\VisibilityOutputs\visgraphout format=ESRI_Shapefile
WARNING: No attribute table found -> using only category numbers as attributes
Exporting 2470954 features...
WARNING: 2470954 features without category were skipped. Features without category are written only when -c flag is given.
WARNING: Output layer is empty, no features written
v.out.ogr complete. 0 features (Line String type) written to <inputdatatestttt> (ESRI_Shapefile format).

I'm not sure why the visibility graph is being created without attributes or how to fix this.

0

Looks like you have tried possibly everything to rectify any installation problems. So I ditto Micha on the doubt that the text file you are reading in could be the source of the problem.

I have struggled quite a bit with text files read in Python as a variable and use that to get it working in GRASS modules. I might looks funny, but one of my desperate workaround was writing the data file from within Python and then read it in the GRASS module which worked in my case. If v.net.visibility works with what Micha suggested, then you can try something like:

with open('centroids.txt', 'w+') as rules:
        centroid_text ='lat1, long1\nlat2, long2\nlat3, long3'
        centroids.write(centroid_text)
gscript.run_command("v.net.visibility",input="SimplifiedCoastline", output=vn, coordinate=os.path.join(os.getcwd(),'rules.txt'))

Keep trying, this one or something else will work :)

  • Thank you, Hasnein. I did create this text file using a separate Python script, and there doesn't appear to be any problems with it when I read it in, but I'll try running the lines as one script without writing and reading the file back in. One difference between your code and mine is that I did not create a new line for each coordinate pair, so I'll look into that (although I have successfully run this code with this format in the past). My data is in the format: 'lat1, long1, lat2, long2, ...' – Sarah Aug 21 '18 at 18:29
  • Thanks Sarah. I think working with a pair of lat long at the beginning would make sure that the code and the module is working.Good luck. – Hasnein Tareque Aug 23 '18 at 7:11
  • Thanks Hasnein. When I create the visibility graph with the code, then use the point and click method in the GUI to export the file with v.out.ogr, it works fine (for the test data and my real data). So I think there is just an issue with the way that I am coding the v.out.ogr command. – Sarah Aug 24 '18 at 18:04
  • hmm.. if that is working, the next thing I would check is if you are providing a valid path to v.out.ogr. In your example, the r is missing in the outfile path. Sometime the path implementation within Python might behave funny, specially if you are in a windows machine. If putting the 'r' in front of the path does not do any change, try also os.path.join to define your output path. – Hasnein Tareque Aug 27 '18 at 2:10
0

The first place I would look to find the problem is the data variable. When you read a text file, you are probably getting end-of-line characters, etc. I would stick in a print(data) to see what the list looks like.

Also, an initial test could be to replace data with a coordinate pair that you enter manually, to be sure it works as expected.

Be aware that the coordinate parameter needs to be a list, so, i.e.:

gscript.run_command("v.net.visibility",input="SimplifiedCoastline", output=vn, coordinate=(200000,500000))

Multiple coordinate pairs need to be separated by commas.

  • Thank you for the suggestion, Micha. The input data is a comma separated list and there are no extra characters being added. I have not tried running the function with only one coordinate pair though. – Sarah Aug 21 '18 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.