2016-10-26 6 views
1

外部からGRASSGISモジュールを呼び出すためにpython関数Aを書きました。私は別のPython関数を書きましたB他のGRASSGISモジュールとPython関数を呼び出す文を含んでいます、エラーが発生しました。Python関数がGRASS GISモジュールと同じ種類の別のPython関数を呼び出すときにエラーが発生しました

関数A:

import os 
import sys 
import numpy 
from GRASSGIS_conn import GRASSGIS_conn 


def v_edit(map_name, tool, thresh, coords): 

    cor = [",".join(item) for item in coords.astype(str)] 
    no_of_cors = len(cor) 
    i = 0 
    while i <= no_of_cors - 1: 
     g.run_command('v.edit', map = map_name, tool = tool, threshold = thresh, coords = cor[i]) 
     i = i + 1 

関数B:

import sys 
import os 
import numpy 
from GRASSGIS_conn import GRASSGIS_conn 
from v_edit import v_edit 


def split_line(line_shape, out_name, thresh, point_cor): 

    g.run_command('v.in.ogr', overwrite = True, input = line_shape, output = out_name) 
    v_edit(out_name, 'break', thresh, point_cor) 



if __name__ == "__main__": 


    sys.path.append(os.path.join(os.environ['GISBASE'], 'etc', 'python')) 
    import grass.script as g 
    gisdb = 'C:\Users\Heinz\Documents\grassdata' 
    location = 'nl' 
    mapset = 'nl' 
    GRASSGIS_conn(gisdb, location, mapset) 
    point_cor = numpy.genfromtxt('proj_cor.csv', delimiter = ',') 
    split_line(r'C:\Users\Heinz\Desktop\all.shp', 'tctest', '50', point_cor) 

エラー:私は機能を呼び出す声明ことなく、その機能Bをテストしている

Traceback (most recent call last): 
    File "C:\Users\Heinz\Desktop\split_line.py", line 25, in <module> 
    split_line(r'C:\Users\Heinz\Desktop\all.shp', 'tctest', '50', point_cor) 
    File "C:\Users\Heinz\Desktop\split_line.py", line 11, in split_line 
    v_edit(out_name, 'break', thresh, point_cor) 
    File "C:\Users\Heinz\Desktop\v_edit.py", line 13, in v_edit 
    g.run_command('v.edit', map = map_name, tool = tool, threshold = thresh, coords = cor[i]) 
NameError: global name 'g' is not defined 

そして、うまく走った間違いなく。

なぜこれが起こったのか、それを解決する方法はわかりません。

答えて

1

ソリューション: はトップレベルの輸入のimport grass.script as gからファイル1(function Aからv_edit.pyを)すなわち持っては次のようになります。

import os 
import sys 
import numpy 
from GRASSGIS_conn import GRASSGIS_conn 
import grass.script as g 

def v_edit(map_name, tool, thresh, coords): 

    cor = [",".join(item) for item in coords.astype(str)] 
    no_of_cors = len(cor) 
    i = 0 
    while i <= no_of_cors - 1: 
     g.run_command('v.edit', map = map_name, tool = tool, threshold = thresh, coords = cor[i]) 
     i = i + 1 

原因: あなたはgを定義している(インポート)if __name__ == "__main__"ブロック - ファイルコードの一部としてカウントされません。

これを読んでください - what does if name == "main" do

関連する問題