2016-03-22 16 views
0

Ich schreibe etwas Code, um eine Symbolleiste zu erstellen, die eine Map in ArcMap bearbeitet, und ich habe einige Probleme beim Abrufen von Variablenwerten aus anderen Funktionen in anderen Klassen, die ich verwende .Warum funktioniert nicht selbst in meinem Code

Alle Funktionen sind vordefiniert, so kann ich die int Argumente nicht ändern oder der Code wird einen Fehler werfen. Ich habe die dir() überprüft und keine der Variablen, die ich mit self definieren, sind in den Funktionen. Ich glaube nicht, dass ich einen Syntaxfehler gemacht habe und der Code innerhalb der anderen Klassen korrekt funktioniert.

Hier ist mein Code:

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 

Was mache ich falsch? Ist dies einer der seltenen Fälle, in denen ich eine globale Variable verwenden sollte? Warum zeigt das Verzeichnis nicht die Variablen an, die ich mit self definiert habe?

+0

Was erwarten Sie von Ihrem Code und was macht er? – glibdud

+0

Was ist die Eingabe und was ist der Fehler? – JulienD

+0

@muraveill Es ist Benutzereingaben. Das funktioniert gut. Der Fehler, der ausgelöst wird, wenn ich versuche, es in der dritten Klasse aufzurufen, ist ein Attributfehler. Es besagt, dass es in findingCoordinates.onMouseDownMap.x kein 0 gibt. – Steve

Antwort

1

self bezieht sich auf eine Instanz der Klasse, die Sie definiert haben. Um auf diese Werte zuzugreifen, müssen Sie eine Instanz der Klasse erstellen, die Methode aufrufen und dann auf die Werte der Instanz zugreifen.

Zum Beispiel:

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: Es scheint, wie Sie auch einige Verwirrung über Scoping/Namensräume hatten. Es gibt keine global definierte x oder y; Sie existieren nur innerhalb der Klasseninstanzen. Dadurch können Sie auch separate x und y Werte für verschiedene Instanzen der Klasse haben.

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

Ist es wichtig, wo ich die Funktion gleich einer Variablen setze? – Steve

+0

Wie gibt In [11] mir die Werte von In [9]? – Steve

+0

onMouseDownMap passiert, wenn die Maus auf einer Karte angeklickt wird. Ich bin kein Genie, aber das sieht so aus, als würdest du nur Variablen in die Funktion einbauen und sie dann sofort aufrufen. Wie liefert dies die Variablen, die in erster Instanz in der Klasse selbst berechnet wurden? – Steve

Verwandte Themen