|
POST
|
used lmpy.py about a year ago Ha! That's so cool 😄 Doubt it would help, but when ArcMap starts acting wonkey on me, one of my last ditch efforts is to delete/rename the normal.mxt file (it's in your profile directory - might be called something else in v10 though). The benefit is ArcMap then behaves like brand new, the downside is ALL your customizations are deleted (buttons, toolboxes, everything). Maybe there is something akin to normal.mxt for arcpy? I have no idea... Could you have somehow tuned off auto complete in the ArcGIS python window, and it somehow stuck for other IDEs somehow? Worth a shot... Just make a backup of normal.mxt first if it doesn't work! I will say that some of us had to learn gp/arcpy/whatever scripting without auto complete. As within many things though, once you have something cool, it hard to go back! 😉
... View more
04-21-2011
02:15 PM
|
0
|
0
|
609
|
|
POST
|
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
... View more
04-21-2011
12:41 PM
|
0
|
0
|
2399
|
|
POST
|
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().
... View more
04-21-2011
09:03 AM
|
0
|
0
|
2399
|
|
POST
|
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...
... View more
04-21-2011
08:05 AM
|
0
|
0
|
2399
|
|
POST
|
Try this: rows = arcpy.UpdateCursor("c:/gis/temp.shp","","","", "POINT_X A; POINT_Y A") The correct sort field parameter delimiter is a ; and you were using a ,
... View more
04-20-2011
10:15 AM
|
0
|
0
|
1238
|
|
POST
|
Not much to tell, but it seems to work when shutil.copytree throw an error about some .lock file in a .fgdb folder having a file sharing issue. Try it and you may I say...
... View more
04-19-2011
11:06 AM
|
0
|
0
|
2765
|
|
POST
|
Are you using shutil.copytree()? I've notice that quite often lock files persist in the FGDB folders (even after all the featurelayers, tableviews, cursor refs, etc are deleted). In that case, I have had to rely on the (frankly much slower) gp.Copy_management() tool.
... View more
04-19-2011
10:28 AM
|
0
|
0
|
2765
|
|
POST
|
I have some questions pertaining to running large overlays in ArcGIS and examining overlapping features. Maybe someone from ESRI might want to share some corporate secrets? 1) When running an overlay, there is a little executable that fires off called TestGPram.exe. This is used as part of the ArcGIS tiling scheme used to chop up large overlay tasks. My question: Assuming everyone has at least 2 GB of RAM available, what are the conditions that cause a tile to be subdivided? Is it simply the number of vertices in the input FC(s) per tile - Or is it more complex? The reason I ask is that sometimes I will run a large overlay using the default tools (union for example) and the thing will sit there for hours happily subdiving tiles, create hundreds of tiles, and after 6 hours, finally finish. However, using the same input layers, I can create a four cell (2 x 2) fishnet polygon FC, set the gp.extent to one fishnet poly at a time, and run the union (one onion per fishnet poly) and be done with the union in just 10 minutes. What gives? What causes the large overlay tilling thing to go crazy creating hundreds of tiles for 6 hours when just four tiles will do the job and the thing takes only 10 minutes?!? 2. When running a "Spaghetti and Meatballs" analysis (that makes me hungry, so I'll call it an "overlapping polygon or polyline analysis"), there are at least a few tools that will "shatter" the geometry of overlapping features (aka "planarization"). For polygons: Union or FeatureToPolygon (intersect too I think), and for lines: Intersect or FeatureToLines. My question is: What is the "best" method to use for shattering polygons and/or lines? Are there other/better way of doing this that I am not aware of? For polygons, ESRI seems to like using the FeatureToPolygon tool instead of the Union tool... What is the reasoning? In my experience it is not faster, actually quite the contrary.
... View more
04-13-2011
04:11 PM
|
0
|
0
|
792
|
|
POST
|
Okay I get it. So maybe I'm wrong, but it looks like you are attempting to directly set the .firstpoint and .lastpoints coordinates. However these are read-only properties of an existing feature, and not directly "writable". You would have to cycle through all the vertices and alter them that way. Seems that maybe one method would be: 1. For each feature, you get the correct start/end coordinates (you got this from the near tool). 2. For each feature you retrieve their current (but offset) start/end coordinates 3. Cycle through the features points, and alter as necessary. Can you use the .replace method (part of the geometry Array) to quickly retrieve the geometry of the start and end? Obviously the start would be index of 0, but can you do an index of -1 like in Python (to get the last)? Dunno... Otherwise you could loop through all the vertices - the start point is obvious, but to find the end you might need to continuously compare each vertex with the known location (step #2). So this might help, but I wrote some code a long while back that did something similar. In my case, I had an stream layer that, for complicated reasons, had some small "skips" in it, that basically made for a bunch of unconnected stream segments. My approach was to just insert little "connector arcs" instead of move the existing vertices, but... Here is an excerpt of that code: #NOTE: Due to small inconsistencies in how the "SIMPLIFY" method of the StreamToFeature tool behaves, there are
# some occasional gaps in the stream network along the fishnet boundary. We need to fix these issues
# to preserve topological connectivity. My solution is to insert a connector arc (with the correct directionality)
# to bridge the gap (which is occasionally larger than gp.cellsize!). I explored other methods such as using the
# "integrate" tool (didn't fix everything with a tolerance of gp.cellsize, and many unintended problems as well).
# I also thought of just moving the start or end node of the gaped features (quite a bit more complex than just inserting
# a connector arc). Using the "SIMPLIFY" parameter is about a 25% disk space savings, but there would still be a few
# disconnected arcs using the "NO_SIMPLIFY" method (where the stream exactly parallels the fishnet tile boundary.
# Anyway, this is what I came up with...
#Process: Dissolves prelimStreamsFC by GRID_CODE (fixes 99% of the tile boundary issues)
streamDissolve1FC = gdbPath + "\\stream_dislv1"
gp.Dissolve_management(prelimStreamsFC, streamDissolve1FC, "GRID_CODE", "", "SINGLE_PART", ""); showGpMessage()
#Process: Make a baseline copy (before we mess with it!)
baseCopyFC = gdbPath + "\\baseline_copy"
gp.Select_analysis(streamDissolve1FC, baseCopyFC, ""); showGpMessage()
#Process: Figures out what stream reaches are still disconnected
streamIdFreqTbl = gdbPath + "\\stream_id_freq"
gp.Frequency_analysis(streamDissolve1FC, streamIdFreqTbl, "GRID_CODE", ""); showGpMessage()
#Process: Build an SQL string and also a Python dictionary so we can select the problem features
streamIdFreqString = ""
streamIdFreqDict = {}
searchRows = gp.searchcursor(streamIdFreqTbl, "FREQUENCY > 1")
searchRow = searchRows.next()
while searchRow:
streamIdFreqString = streamIdFreqString + str(searchRow.GRID_CODE) + ","
streamIdFreqDict[searchRow.GRID_CODE] = searchRow.FREQUENCY
searchRow = searchRows.next()
del searchRow
del searchRows
#Process: Loads the firstpoint and lastpoint XY pairs into a dictionary keyed by GRID_CODE
vectorSelectorDict = {}
searchRows = gp.searchcursor(streamDissolve1FC, "GRID_CODE in (" + streamIdFreqString[:-1] + ")")
searchRow = searchRows.next()
while searchRow:
firstPointX = searchRow.getvalue(shapeFieldName).firstpoint.x
firstPointY = searchRow.getvalue(shapeFieldName).firstpoint.y
lastPointX = searchRow.getvalue(shapeFieldName).lastpoint.x
lastPointY = searchRow.getvalue(shapeFieldName).lastpoint.y
if vectorSelectorDict.has_key(searchRow.GRID_CODE) == False:
vectorSelectorDict[searchRow.GRID_CODE] = []
vectorSelectorDict[searchRow.GRID_CODE].append([firstPointX,firstPointY,lastPointX,lastPointY])
else:
vectorSelectorDict[searchRow.GRID_CODE].append([firstPointX,firstPointY,lastPointX,lastPointY])
searchRow = searchRows.next()
del searchRow
del searchRows
#Process: Figures out what lastpoint and first point combinations are closest together (since we don't
#inherently know what arc segment is furthest upstream) Preserving correct directionality and connectivity is critical!!!
vectorConnectorDict = {}
for gridCode in vectorSelectorDict.keys():
gridCodeFeatureCount = streamIdFreqDict[gridCode]
if gridCodeFeatureCount == 2:
distance1 = math.sqrt(math.pow(vectorSelectorDict[gridCode][0][2] - vectorSelectorDict[gridCode][1][0],2) + math.pow(vectorSelectorDict[gridCode][0][3] - vectorSelectorDict[gridCode][1][1],2))
distance2 = math.sqrt(math.pow(vectorSelectorDict[gridCode][1][2] - vectorSelectorDict[gridCode][0][0],2) + math.pow(vectorSelectorDict[gridCode][1][3] - vectorSelectorDict[gridCode][0][1],2))
if distance1 > float(gp.cellsize) and distance2 > float(gp.cellsize):
message = "ERROR: Grid Code " + str(gridCode) + " - distance1 (" + str(distance1) + ") and distance2 (" + str(distance2) + ") are both more than gp.cellsize!"; showPyMessage()
if distance1 < distance2:
vectorConnectorDict[gridCode] = [vectorSelectorDict[gridCode][0][2],vectorSelectorDict[gridCode][0][3],vectorSelectorDict[gridCode][1][0],vectorSelectorDict[gridCode][1][1]]
else:
vectorConnectorDict[gridCode] = [vectorSelectorDict[gridCode][1][2],vectorSelectorDict[gridCode][1][3],vectorSelectorDict[gridCode][0][0],vectorSelectorDict[gridCode][0][1]]
else:
message = "ERROR: The frequency count of Grid Code " + str(gridCode) + " is " + str(gridCodeFeatureCount) + "!"; showPyMessage()
#Process: Construct the connector arcs!
gp.AddField_management(streamDissolve1FC, "CON_FLG", "SHORT"); showGpMessage()
insertRows = gp.insertcursor(streamDissolve1FC)
for gridCode in vectorConnectorDict.keys():
lineArray = gp.createobject("Array")
startPoint = gp.createobject("Point")
startPoint.x = vectorConnectorDict[gridCode][0]
startPoint.y = vectorConnectorDict[gridCode][1]
lineArray.add(startPoint)
endPoint = gp.createobject("Point")
endPoint.x = vectorConnectorDict[gridCode][2]
endPoint.y = vectorConnectorDict[gridCode][3]
lineArray.add(endPoint)
insertRow = insertRows.newrow()
insertRow.GRID_CODE = gridCode
insertRow.CON_FLG = 1
insertRow.setvalue(shapeFieldName,lineArray)
insertRows.insertrow(insertRow)
lineArray.removeall()
del insertRow
del insertRows
del startPoint
del endPoint
del lineArray
... View more
04-13-2011
11:26 AM
|
0
|
0
|
2025
|
|
POST
|
As far as I know in Python - in order to "flip" a line segment, you would have to regurgitate ALL the vertices in reverse order (not just the start/end nodes). Fortunately Python has some very convenient functions for doing just that. For example, after reading all the vertices into a list where the vertices are tuples, you can simply reverse the order, and then regurgitate. For example: test = [(0,0),(1,1),(2,2),(3,3),(4,4)]
test.reverse()
test
[(4, 4), (3, 3), (2, 2), (1, 1), (0, 0)] Another possible way is to select the feature you want to flip and than calc the Shape field to this using the field calculator (don't think you can use this via Python though): '=========================
'polyline_Flip.cal
'Author: Ianko Tchoukanski
'http://www.ian-ko.com
'=========================
Dim pCurve As ICurve
If (Not IsNull([Shape])) Then
Set pCurve = [Shape]
If (Not pCurve.IsEmpty) Then
pCurve.ReverseOrientation
End If
End If
__esri_field_calculator_splitter__
pCurve
... View more
04-13-2011
09:43 AM
|
0
|
0
|
2025
|
|
POST
|
This code should work: rasterList = gp.ListRasters("","GRID")
somaExp = "" #note that your code had somaExp = " " (and extra space)
for raster in rasterList:
somaExp = somaExp + raster + " + " #Note the SOMA language requires single spaces around the operations, so your "+" would be invalid
gpsa = outputFolder + "\\output" #Note in your code you were only naming the folder
gp.SingleOutputMapAlgebra_sa(soma[:-3], gpsa) #Note the [:-3] index
... View more
04-11-2011
08:18 AM
|
0
|
0
|
930
|
|
POST
|
The ArcMap Python window "claims" that Esc will do it, but... not so sure.
... View more
03-31-2011
01:22 PM
|
1
|
0
|
9171
|
|
POST
|
Not sure what you mean exactly... And you are hitting the enter button after typing 'import sys', right? Are you entering these commands at the prompt in: -python.exe (the DOS-looking window) -pythonwin.exe (the freebee python IDE) -the python window in ArcMap -something else?
... View more
03-31-2011
10:56 AM
|
0
|
0
|
9171
|
|
POST
|
I usually use sys.exit() to stop a script from running if some error condition is met. So for example, if a user put in a wrong input variable... import sys
var =gp.GetParameterAsText(0)
if var not in ("cat","dog","mouse"):
print "ERROR: You entered the wrong animal! Exiting script..."; sys.exit()
else:
print var + " is a valid entry. Good job!" You might be intersted in the pass or break statements too... pass is used to basically 'do nothing', so something like: if animal == "dog":
pass #aka don't do anything
else:
print "not a dog" break is used to break out of a while or for loop, so something like: for x in (1,2,3,4,5):
if x > 4:
break #aka stop looping
else:
print x
... View more
03-31-2011
09:50 AM
|
0
|
0
|
9171
|
|
POST
|
The Python import statement looks for the specified "module" (aka a .py/.pyc file) in all the directories that are listed in sys.path(). For me, these include: ['C:\\Program Files\\ArcGIS\\bin', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25\\Lib\\site-packages\\pythonwin', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages', 'C:\\Python25\\lib\\site-packages\\win32', 'C:\\Python25\\lib\\site-packages\\win32\\lib'] It easiest just to put your module in one of these default paths. However, if you make your own module, and say want to put it out on a network location somewhere: import sys networkPath = r"\\mynetwork\gis\py_files" #aka the folder where the mymodule.py file is located sys.append(networkPath) import mymodule mymodule.myFunction(arg1,arg2)
... View more
03-30-2011
03:57 PM
|
1
|
0
|
4237
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 03-25-2014 12:57 PM | |
| 1 | 08-29-2024 08:23 AM | |
| 1 | 08-29-2024 08:21 AM | |
| 1 | 02-13-2012 09:06 AM | |
| 2 | 10-05-2010 07:50 PM |
| Online Status |
Offline
|
| Date Last Visited |
08-30-2024
12:25 AM
|