2016-03-22 9 views
0

で働いていません。なぜ自己私はArcMapでマップを編集ツールバーを作成するために、いくつかのコードを書いていると私は私が使用している他のクラス内の他の関数から変数の値を取得するといくつかの問題を抱えている私のコード

すべての関数は、私はint引数を変更することはできません事前定義されているかのコードは、エラーがスローされます。私はdir()をチェックし、selfを使って私が定義した変数はどれも関数にはありません。私は構文エラーを起こしたとは思わないし、他のクラスのコードは正しく動作します。ここで

は私のコードです:私は間違っ

import arcpy 
import math 
import pythonaddins 


class findingCoordinates(object): 
    """Implementation for leetScripts_addin.tool (Tool)""" 

    def __init__(self): 
     self.enabled = True 
     self.shape = "NONE" 

    def onMouseDownMap(self, x, y, button, shift): 
     print "onMouseDowMap executing" 
#this is where I declared the first two variables using self 
     self.x = x 
     self.y = y 
     print "Selected point is at %r, %r" % (self.x, self.y) 
     pass 


class squareFeetInput(object): 
    """Implementation for leetScripts_addin.combobox (ComboBox)""" 
    def __init__(self): 
     self.editable = True 
     self.enabled = True 
     #self.dropdownWidth = 'WWWWWW' 
     self.width = 'WWWWWW' 

    def onEditChange(self, text): 
     squareFeet = text 
#this is the other variable I defined that I need to use later 
     self.buffDist = (math.sqrt(float(squareFeet))/2) 
     print "Square size: %r ft^2 Buffer Distance: %r ft^2" % (squareFeet,self.buffDist) 
     print "self.buffdist is a %r type" % self.buffDist 
     return self.buffDist 
     pass 


class buildingTool(object): 
    """Implementation for leetScripts_addin.button (Button)""" 
    def __init__(self): 
     self.enabled = True 
     self.checked = False 
    def onClick(self): 
     print "building tool is executing" 
     #shows im_self, but no x or y 
     print "%r" % dir(findingCoordinates.onMouseDownMap) 
     # Get arguments: 
     # Input point feature class 
     # Output polygon feature class 
     # Buffer distance 
     # Boolean type: Maintain fields and field values of the input in the output 

#This is where the problem is. I can't get these values from the previous functions. 

     inPoints = (findingCoordinates.onMouseDownMap.x,findingCoordinates.onMouseDownMap.y) 
     outPolys = "U:\JackBuildingFootprints.gdb\BuildingFootprintsCopy" 
     bufDist = squareFeetInput.buffDist 
     keepFields = true 

     # Prepare the output based on whether field and field values are desired in the output 
     # 
     if keepFields: 
      # Create empty output polygon feature class that includes fields of the input 
      # 
      arcpy.CreateFeatureClass(os.path.dirname(outPolys), os.path.basename(outPolys), "POLYGON", 
            inPoints, "", "", inPoints) 

      # Create a short list of fields to ignore when moving fields values from 
      # input to output 
      # 
      ignoreFields = [] 

      # Use Describe properties to identify the shapeFieldName and OIDFieldName 
      # 
      desc = arcpy.Describe(inPoints) 
      ignoreFields.append(desc.shapeFieldName) 
      ignoreFields.append(desc.OIDFieldName) 

      # Create a list of fields to use when moving field values from input to output 
      # 
      fields = arcpy.ListFields(inPoints) 
      fieldList = [] 
      for field in fields: 
       if field.name not in ignoreFields: 
        fieldList.append(field.name) 
     else: 
      # Create empty output polygon feature class without fields of the input 
      # 
      arcpy.CreateFeatureclass(os.path.dirname(outPolys), os.path.basename(outPolys), "POLYGON", 
            "", "", "", inPoints) 

     # Open searchcursor 
     # 
     inRows = arcpy.SearchCursor(inPoints) 

     # Open insertcursor 
     # 
     outRows = arcpy.InsertCursor(outPolys) 

     # Create point and array objects 
     # 
     pntObj = arcpy.Point() 
     arrayObj = arcpy.Array() 

     for inRow in inRows: # One output feature for each input point feature 
      inShape = inRow.shape 
      pnt = inShape.getPart(0) 

      # Need 5 vertices for square buffer: upper right, upper left, lower left, 
      # lower right, upper right. Add and subtract distance from coordinates of 
      # input point as appropriate. 
      for vertex in [0,1,2,3,4]: 
       pntObj.ID = vertex 
       if vertex in [0,3,4]: 
        pntObj.X = pnt.X + bufDist 
       else: 
        pntObj.X = pnt.X - bufDist 
       if vertex in [0,1,5]: 
        pntObj.Y = pnt.Y + bufDist 
       else: 
        pntObj.Y = pnt.Y - bufDist 
       arrayObj.add(pntObj) 

      # Create new row for output feature 
      # 
      feat = outRows.newRow() 

      # Shift attributes from input to output 
      # 
      if keepFields == "true": 
       for fieldName in fieldList: 
        feat.setValue(fieldName, inRow.getValue(fieldName)) 

      # Assign array of points to output feature 
      # 
      feat.shape = arrayObj 

      # Insert the feature 
      # 
      outRows.insertRow(feat) 

      # Clear array of points 
      # 
      arrayObj.removeAll() 

     # Delete inputcursor 
     # 
     del outRows 



     pass 

何をしているのですか?これはまれなケースですが、グローバル変数を使用する必要がありますか? selfを使用して定義した変数がディレクトリに表示されないのはなぜですか?

+0

あなたのコードが何をしていると思いますか? – glibdud

+0

入力とは何か、発生したエラーは何ですか? – JulienD

+0

@muraveillユーザー入力です。それはうまくいきます。私が3番目のクラスでそれを呼び出そうとしたときにスローされるエラーは属性エラーです。これは、findingCoordinates.onMouseDownMap.xにxがないことを示します。 – Steve

答えて

1

selfは、定義したクラスのインスタンスを参照するため、これらの値にアクセスするには、クラスのインスタンスを作成し、メソッドを呼び出してインスタンスの値にアクセスする必要があります。例えば

In [9]: %paste 
class findingCoordinates(object): 
    """Implementation for leetScripts_addin.tool (Tool)""" 

    def __init__(self): 
     self.enabled = True 
     self.shape = "NONE" 

    def onMouseDownMap(self, x, y, button, shift): 
     print "onMouseDowMap executing" 
#this is where I declared the first two variables using self 
     self.x = x 
     self.y = y 
     print "Selected point is at %r, %r" % (self.x, self.y) 
     pass 

## -- End pasted text -- 

In [10]: f = findingCoordinates() 

In [11]: f.onMouseDownMap(x=1, y=2, button="button", shift="shift") 
onMouseDowMap executing 
Selected point is at 1, 2 

In [12]: f.x 
Out[12]: 1 

In [13]: f.y 
Out[13]: 2 

EDIT:あなたにも/名前空間をスコープに関するいくつかの混乱を持っていたように思えます。グローバルにはxまたはyが定義されていません。クラスインスタンス内に存在します。これにより、クラスの異なるインスタンスに対して別々のxyの値を持つことができます。

In [14]: x 
--------------------------------------------------------------------------- 
NameError         Traceback (most recent call last) 
<ipython-input-14-401b30e3b8b5> in <module>() 
----> 1 x 

NameError: name 'x' is not defined 

In [15]: y 
--------------------------------------------------------------------------- 
NameError         Traceback (most recent call last) 
<ipython-input-15-009520053b00> in <module>() 
----> 1 y 

NameError: name 'y' is not defined 

In [16]: g = findingCoordinates() 

In [17]: g.onMouseDownMap(100,200,0,0) 
onMouseDowMap executing 
Selected point is at 100, 200 

In [18]: f.x, f.y 
Out[18]: (1, 2) 

In [19]: g.x, g.y 
Out[19]: (100, 200) 
+0

関数を変数に等しい場所に設定しても問題ありませんか? – Steve

+0

In [11]はIn [9]からどのように値を返しますか? – Steve

+0

onMouseDownMapは、マップ上でマウスをクリックすると発生します。私は天才ではありませんが、あなたはファンクションに変数を入れてすぐに呼び出すように見えます。これは、クラス自体の最初のインスタンスで計算された変数をどのように返しますか? – Steve

関連する問題