Select to view content in your preferred language

trying to learn

855
12
04-21-2011 07:25 AM
TerrySilveus
Occasional Contributor III
In arcsscripting python code I see this statement all the time...

gp = arcgisscripting.create(9.3)

and then all the functions of the arcgisscripting package are addressed similar to this

something = gp.updateCursor(...

but in arcpy I rarely (if ever) see that same convention... its

something = arcpy.updateCursor(...

instead.  Why the difference?  Also, sometimes my autocomplete typing assist window after the . in arcpy. doesn't drop down a list even though the package has been imported and is in memory.  What would be the cause of that?
Tags (2)
0 Kudos
12 Replies
RDHarles
Occasional Contributor
Back in the old days :), that's they way you created the geoprocessing module.

arcgisscripting module, setting a 9.3 version:
import arcgisscripting
gp = arcgisscripting.create(9.3)

Using the native arcgisscripting module (before 9.3):
import arcgisscripting
gp = arcgisscripting.create()

...Then, going way back to the caveman days, it was done like this!...

Using COM IDispatch interface:
import win32com.client
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
0 Kudos
ChrisSnyder
Regular Contributor III
Back when things were fun, you could do stuff like:

import arcgisscripting
toeNails = arcgisscripting.create(9.3)
toeNails.Clip_analysis(inputFC, clipFC, outputFC)

Now we only have arcpy 😞

I guess standards are good though...
0 Kudos
TerrySilveus
Occasional Contributor III
ah yes...;) progress.

What about the autocomplete... why does it work only sometimes?
0 Kudos
ChrisSnyder
Regular Contributor III
Autocomplete in v9.3 or v10?

My understanding is that one of the reasons ESRI made arcpy an actual python sitepackage/module is so that autocomplete would rein. Not sure in v10 why it wouldn't work all the time no matter what IDE you were using (except of course Notepad or something like that).

In v9.3, there was some sort of bastard version of autocomplete that basically would only show just the gp methods (like .updatecursor or .setproduct). It would also display any tools/methods that you had used during that session - i.e. ones that were still in the memory (like Clip).

I know that one of the shortcomings of the autocomplete functionality in arcpy is that all the methods MUST now be proper case. Because of this I have a lot of code re-writing to do to be arcpy-compliant! For example, in v10 it must be arcpy.UpdateCursor() and not arcpy.updatecursor().
0 Kudos
RDHarles
Occasional Contributor
Back when things were fun, you could do stuff like:

import arcgisscripting
toeNails = arcgisscripting.create(9.3)
toeNails.Clip_analysis(inputFC, clipFC, outputFC)

Now we only have arcpy 😞

I guess standards are good though...


That's some funny stuff, Chris! 😄
R.D.
0 Kudos
TerrySilveus
Occasional Contributor III
Autocomplete in v9.3 or v10?

My understanding is that one of the reasons ESRI made arcpy an actual python sitepackage/module is so that autocomplete would rein. Not sure in v10 why it wouldn't work all the time no matter what IDE you were using (except of course Notepad or something like that).

In v9.3, there was some sort of bastard version of autocomplete that basically would only show just the gp methods (like .updatecursor or .setproduct). It would also display any tools/methods that you had used during that session - i.e. ones that were still in the memory (like Clip).

I know that one of the shortcomings of the autocomplete functionality in arcpy is that all the methods MUST now be proper case. Because of this I have a lot of code re-writing to do to be arcpy-compliant! For example, in v10 it must be arcpy.UpdateCursor() and not arcpy.updatecursor().


arcpy... but it seems to be exhibiting the behavior you describe for v9.3.  If it is something I've already typed out, it shows up in the list, but if I haven't typed it previously, it doesn't show up.  This is something that I just noticed yesterday.  Prior to that I'm sure the list was always complete.
0 Kudos
TerrySilveus
Occasional Contributor III
In addition to not giving me autocomplete, when I start the ( paranthesis it used to give me the parameters that the function expected, but it's not doing that either anymore
0 Kudos
ChrisSnyder
Regular Contributor III
What IDE (PythonWin, Eclipse, etc.) are you using? Maybe there is some setting somewhere in the IDE? I really don't know... Sadly, I'll admit that v10 is still too buggy for my likes, so I'm still doing most of my heavy lifting in 9.3.1 still, so my knowledge of auto complete functionality in v10 is really masked by ignorance.

However, here's a story though that may be enlightening. I made a hokey custom python module a long while back (called lmpy.py). It had a bunch of useful little functions in it like sending email, recursive searches, listing fields/field properties of a table, etc. It was basically set up like this:

#lmpy.py
def fieldExists(table, fieldName):
   """Returns a list indicating boolean if field exists, if True also the place"""
   blah
   blah

def doThat(param1,param2,param3)
   """Does this, that, and the other"""
   blah
   blah


And when placed in the proper folder (one of the folders that sys.path references), the lmpy module works like this:

>>> import lmpy
>>> lmpy.fieldExists(myTable, "FIELD1")
[True, 3]


The great thing I found, is without even trying, the thing had auto complete functionality "built in", so for example after I typed "lmpy." I got a pick list of all the functions available in the module, and upon picking one, it would tell you what the method did (that's the """" part).

With v10, there is a folder: C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy, and it is chock full of .py files that are basically what my module is... A bunch of functions that define/control the Python auto complete functionality. For example, a brief excerpt of arc.py (which references all the ArcInfo Workstation coverage tools):

@gptooldoc('CollapseDualLinesToCenterline_arc', None)
def CollapseDualLinesToCenterline(in_cover=None, out_cover=None, maximum_width=None, minimum_width=None):
    """CollapseDualLinesToCenterline_arc(in_cover, out_cover, maximum_width, {minimum_width})

        Derives centerlines (single lines) from dual-line features, such as road
        casings, based on specified width tolerances.

     INPUTS:
      in_cover (Coverage):
          The coverage containing near parallel dual lines, such as road casings, from
          which centerlines are derived.
      maximum_width (Double):
          Sets the maximum width in coverage units.
      minimum_width {Double}:
          Sets the minimum width in coverage units. The default is zero.

     OUTPUTS:
      out_cover (Coverage):
          The output coverage containing the derived centerlines. The output coverage name
          must be different from the input coverage name."""
    try:
        retval = convertArcObjectToPythonObject(gp.CollapseDualLinesToCenterline_arc(*gp_fixargs((in_cover, out_cover, maximum_width, minimum_width), True)))
        return retval
    except Exception, e:
        raise e

@gptooldoc('Dissolve_arc', None)
def Dissolve(in_cover=None, out_cover=None, dissolve_item=None, feature_type=None):
    """Dissolve_arc(in_cover, out_cover, dissolve_item, {feature_type})

        Creates a new coverage by merging adjacent polygons, lines, or regions that have
        the same value for a specified item.

     INPUTS:
      in_cover (Coverage):
          The coverage containing features to be dissolved.
      dissolve_item (String):
          The item in the in_cover feature attribute table that is used to dissolve
          features.

          * Dissolve_itemâ�?��?�An item name will be used to perform the dissolve. The item may
          be a redefined item.

          * #ALLâ�?��?�All items past the cover-ID in the PAT, AAT, or region subclass PAT will
          be used as a single dissolve item. If there are no items past the cover-ID, then
          the cover-ID will be used.
      feature_type {String}:
          The feature classes to be preserved in the output coverage:

          * POLYâ�?��?�Polygons will be dissolved; an AAT will not be created for the output
          coverage. This is the default option.

          * LINEâ�?��?�Nodes will be dissolved; a PAT will not be created for the output
          coverage.

          * NETâ�?��?�Polygons will be dissolved, and both a PAT and AAT will be created for the
          output coverage.

          * REGION.subclassâ�?��?�Region subclass will be dissolved, and all existing attributes
          of the input coverage will be maintained in the output coverage.

     OUTPUTS:
      out_cover (Coverage):
          The coverage to be created. The output coverage cannot already exist."""
    try:
        retval = convertArcObjectToPythonObject(gp.Dissolve_arc(*gp_fixargs((in_cover, out_cover, dissolve_item, feature_type), True)))
        return retval
    except Exception, e:
        raise e

@gptooldoc('Eliminate_arc', None)
def Eliminate(in_cover=None, out_cover=None, info_express=None, polygon_boundary=None, feature_type=None, selection_file=None, polygon_option=None):
    """Eliminate_arc(in_cover, out_cover, info_express;info_express..., {polygon_boundary}, {feature_type}, {selection_file}, {polygon_option})

        Merges the selected polygons with neighboring polygons if they have the largest
        shared border or the largest area.Eliminate is often used to remove sliver
        polygons created during polygon overlay
        or buffering. With the LINE option, Eliminate merges selected arcs separated by
        pseudo nodes into single arcs.

     INPUTS:
      in_cover (Coverage):
          The coverage whose selected polygons or arcs will be merged into neighboring
          features.
      info_express (INFO Expression):
          An INFO query containing one or more logical expressions to select features from
          the input coverage.

          * Reselectâ�?��?�Reduces the selected set of records with a selection expression to
          those that meet its criteria. If no selection expression follows, the selected
          set will be empty.

          * Aselectâ�?��?�Adds unselected records that meet the selection expression criteria to
          the currently selected set. If no selection expression follows, the selected set
          will contain all features.

          * Nselectâ�?��?�Reverses the current selection to the unselected set.
      polygon_boundary {Boolean}:
          Ensures that polygons along the coverage boundary are not altered.

          * NO_KEEP_EDGEâ�?��?�Allows the elimination of outer polygon boundaries. This is the
          default.

          * KEEP_EDGEâ�?��?�Is only used with the POLYGON option. Any polygon which is a
          neighbor of the background polygon will not be eliminated when KEEP_EDGE is
          specified.
      feature_type {String}:
          The feature class(es) to be eliminated in the Output Coverage. This parameter is
          only used for polygon coverages.

          * POLYâ�?��?�Polygon features will be eliminated; an AAT will not be created for the
          Output Coverage.

          * LINEâ�?��?�Line features will be eliminated; a PAT will not be created for the
          Output Coverage.
      selection_file {File}:
          A Selection File is a preexisting file that identifies which features will be
          used.
      polygon_option {Boolean}:
          Specifies which method will be used for eliminating polygons. This parameter is
          only used for polygon coverages.

          * BORDERâ�?��?�Merges a selected polygon with a neighboring unselected polygon by
          dropping an Arc. The neighboring polygon is the one with the longest shared
          border. This is the default and the way Eliminate worked with the POLY option in
          all pre-6.1.1 releases.

          * AREAâ�?��?�Merges a selected polygon with a neighboring unselected polygon by
          dropping an Arc. The neighboring polygon is the one with the largest area.

     OUTPUTS:
      out_cover (Coverage):
          The new coverage with all the selected sliver polygons merged into larger
          features. There should be a smaller number of polygons than the Input Coverage
          contains."""
    try:
        retval = convertArcObjectToPythonObject(gp.Eliminate_arc(*gp_fixargs((in_cover, out_cover, info_express, polygon_boundary, feature_type, selection_file, polygon_option), True)))
        return retval
    except Exception, e:
        raise e
0 Kudos
TerrySilveus
Occasional Contributor III
PythonWin is what I'm using.  I haven't made any changes to my desktop, no setting changes, no installs, no upgrades, no maintenance (within the time period)... it worked fine a couple of days ago, and now it doesn't.

I think I used lmpy.py about a year ago in a class I took on programming for arcGIS.  I found it somewhere and it had what I needed for my project.  Thanks for that.  I haven't really used python since that class, however I do use VBA on a daily basis (not for arcGIS though) so not only am I trying to learn new syntax in python, but applying that to arcGIS projects as well, and then having it not do what I expect is slowing me down a bit.
0 Kudos