|
POST
|
I'm wondering if one can change the location of the default geodatabase and the home folder too by using Python. The home folder is an ArcMap and ArcScene (not ArcCatalog) thing; it's where the current map or scene document is saved. I suppose in arcpy.mapping if you save the current map document, that would do it. The main purpose of the default geodatabase in ArcMap is to have a place to write scratch and output files if the user has not set up a current and scratch workspace (arcpy.env.workspace, arcpy.env.scratchWorkspace). In Python scripting, I think it's best to just set them directly. The tweak under discussion here is how to set the default workspace for new map documents. This option (in the ArcMap Catalog window) is only exposed in the user interface in 10.0 SP5 and 10.1.
... View more
01-17-2013
03:38 PM
|
0
|
0
|
3757
|
|
POST
|
Ah yes, the new symbol for doesn't equal is !=, though I find <> still works Learned something, thank you. [noparse] [/noparse]his is an obsolete usage kept for backwards compatibility only. New code should always use !=. Python docs: Built-in Types - Comparisons
... View more
01-16-2013
06:28 AM
|
0
|
0
|
1164
|
|
POST
|
I´m trying to write a Python script, that includes different ArcPy / ArcGIS commands and want to combine it with GRASS geoprocessing tools. This will only work if: 1) The versionsof Python are the same (and both 32 or 64) 2) The python libraries in use are the same version I have been pretty sucessful pulling in my EPD library by simply adding them to the Desktop10.pth file, or adding the paths I need to the PYTHONPATH. sys.path.append should work as well, but it is a more cumbersome way to go. However, I would imagine there are different flavors of GDAL and other libraries at play here, so I don't know if this is doable. Perhaps your arcpy script could write out a python script and then execute it using the GRASS python in its native environment through a system command line using the subprocess module. This would be ugly but may be the only way.
... View more
01-15-2013
10:44 AM
|
0
|
0
|
787
|
|
POST
|
I tried some runs after importing gc and enabling it, but I have the same problems. What still confuses me is that I thought gc was automatically enabled anyway. What does enabling gc do then? It seems redundant, unless I do not fully understand the intricacies of gc. Now that I re-read the documentation, I think you're absolutely correct. I thought gc was more than the "generic" garbage collection that Python does, but apparently not.
R:\>C:\Python26\ArcGIS10.0\python.exe
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import gc
>>> gc.isenabled()
True
... View more
01-15-2013
10:19 AM
|
0
|
0
|
1608
|
|
POST
|
This is something that has bugged me for a while. I've attached my test model, which runs two Calculate Value tools in a chain. If I give the inputs batch row 1: 10 11 batch row 2: 20 21 The model tool chain runs in "parallel", not in sequence: Messages
Executing: Model2 10 11
Start Time: Tue Jan 15 12:37:02 2013
Executing (Model 1): Model1 10 11
Start Time: Tue Jan 15 12:37:02 2013
Executing (Calculate Value): CalculateValue 10 # Variant
Start Time: Tue Jan 15 12:37:02 2013
Value = 10
Executing (Calculate Value): CalculateValue 20 # Variant
Value = 20
Succeeded at Tue Jan 15 12:37:02 2013 (Elapsed Time: 0.00 seconds)
Executing (Calculate Value (2)): CalculateValue 11 # Variant
Start Time: Tue Jan 15 12:37:02 2013
Value = 11
Executing (Calculate Value (2)): CalculateValue 21 # Variant
Value = 21
Succeeded at Tue Jan 15 12:37:02 2013 (Elapsed Time: 0.00 seconds)
Succeeded at Tue Jan 15 12:37:02 2013 (Elapsed Time: 0.00 seconds)
Succeeded at Tue Jan 15 12:37:02 2013 (Elapsed Time: 0.00 seconds)
I tried nesting the model in another model and batching that; I get the same results. The only workaround I can think of is to wrap the model in a script tool that calls the model. This does batch the model in proper order:
# script tool that wraps model
import sys
import os
import arcpy
Here = os.path.dirname(sys.argv[0])
arcpy.ImportToolbox(os.path.join(Here,"ModelBatchTest.tbx"),"mbt")
p1 = arcpy.GetParameterAsText(0)
p2 = arcpy.GetParameterAsText(1)
arcpy.Model1_mbt(p1,p2)
arcpy.AddMessage(arcpy.GetMessages(0))
Results of "batching" script tool: Executing: Script 10 11
Start Time: Tue Jan 15 12:56:45 2013
Running script Script...
Executing: Model1 10 11
Start Time: Tue Jan 15 12:56:46 2013
Executing (Calculate Value): CalculateValue 10 # Variant
Start Time: Tue Jan 15 12:56:46 2013
Value = 10
Succeeded at Tue Jan 15 12:56:46 2013 (Elapsed Time: 0.00 seconds)
Executing (Calculate Value (2)): CalculateValue 11 # Variant
Start Time: Tue Jan 15 12:56:46 2013
Value = 11
Succeeded at Tue Jan 15 12:56:46 2013 (Elapsed Time: 0.00 seconds)
Succeeded at Tue Jan 15 12:56:46 2013 (Elapsed Time: 0.00 seconds)
Completed script Script...
Executing: Script 20 21
Running script Script...
Executing: Model1 20 21
Start Time: Tue Jan 15 12:56:46 2013
Executing (Calculate Value): CalculateValue 20 # Variant
Start Time: Tue Jan 15 12:56:46 2013
Value = 20
Succeeded at Tue Jan 15 12:56:46 2013 (Elapsed Time: 0.00 seconds)
Executing (Calculate Value (2)): CalculateValue 21 # Variant
Start Time: Tue Jan 15 12:56:46 2013
Value = 21
Succeeded at Tue Jan 15 12:56:46 2013 (Elapsed Time: 0.00 seconds)
Succeeded at Tue Jan 15 12:56:46 2013 (Elapsed Time: 0.00 seconds)
Completed script Script...
Succeeded at Tue Jan 15 12:56:46 2013 (Elapsed Time: 1.00 seconds)
... View more
01-15-2013
09:04 AM
|
0
|
0
|
2043
|
|
POST
|
the "not equal" operator in Python is "!=" The names in quotes are (technically) raster layer names. Here's my shot at it: SetNull(("Ownership" != 200) | ("Soil" == 901), 1) Arc 10.1 help: Comparing Map Algebra between ArcGIS 9.x and 10 Building Expressions in Raster Calculator
... View more
01-15-2013
07:55 AM
|
0
|
0
|
1164
|
|
POST
|
Have you connected the output of Select By Attributes as the input to Calculate Field? Selected sets only work with layers (not feature classes).
... View more
01-15-2013
07:13 AM
|
1
|
0
|
1836
|
|
POST
|
I have done some tests and am convinced it's a memory issue. Would you be able to go into a little more depth into how one would use the gc module in an arcpy workflow? I suggest including the above two lines near the top of your script, before you import arcpy, and see if it helps. The gc module is part of the Python standard library, documented on the python.org website.
... View more
01-14-2013
09:09 AM
|
0
|
0
|
1608
|
|
POST
|
not sure how. but can we cancel the current process of the enabled background geoprocessing? Yes, you can do that be opening the Results window and right clicking on the running tool. This is also a way to see all the messages as the tool runs. ArcGIS Help 10.1: Foreground and background processing (See heading "Canceling a tool in the background")
... View more
01-14-2013
06:01 AM
|
0
|
0
|
1799
|
|
POST
|
Hi there, I have created a model that merges several dbfs then exports that final merged table to an excel file. All the results of the tool are contained in that exel file, I am just wondering if anyone knows how to format the resulting excel file such that its more clear to the end user. The tool outputs several spatial statistics for shapefiles, I would like to input labels into column 1: "Min, mean, within 600m, etc." so the end user knows exactly what all the numbers mean. You can control the output field names using the field mapping parameter in the Merge tool. If you aren't changing with the inputs, you can do this using the field mapping control in the Merge tool dialog within your model. Otherwise, you may have to use Python to set up your field mapping (you can do this with the Calculate Value tool). Programming field mappings in Python is challenging, but it does work.
... View more
01-11-2013
09:42 AM
|
0
|
0
|
604
|
|
POST
|
I agree with Bruce -- the last one will work even if the workspace is not set. 1. arcpy.env.workspace + os.sep + fc 2. os.path.join(arcpy.env.workspace,fc) 3. arcpy.Describe(fc).catalogPath
... View more
01-11-2013
09:34 AM
|
0
|
0
|
2673
|
|
POST
|
My theory is that the local RAM is spent, and if so there seems to be no good way to refresh the RAM mid-process. Anyone else experience something like this? If you are correct, the python garbage collection module may be helpful. Be sure any layers you are done with or in_memory datasets you don't need are deleted before you run the Union. (In Arc 10, you can run arcpy.Delete_management() on layers to free them up.)
import gc
gc.enable()
Another issue that may be in play is the scratch and current workspace. If the workspaces you were using when running interactively were different, that may make a difference. For example, .mdb is very size-limited, as you are probably aware.
... View more
01-11-2013
09:17 AM
|
0
|
0
|
6569
|
|
POST
|
Model builder absolutely *does* work in batch creator in 10.1, BUT only if each intermediary file is set as a "parameter." Could you please elaborate what you mean by "does not work?" I've never tested batch with a model that all the intermediate datasets set up as parameters; I stand corrected. When I've tried to batch a model (@ 10.0), my experience has been that the first tool in the chain runs for batch iterations, then the second, etc. Maybe your approach would get around that -- but it sure sounds cumbersome. Also, I thought you could only have one iterator per model. I'm not just iterating down a single list of input. It's more like: in1.tif + in2.tif = out1 in3.tif + in4.tif = out2 That alone would need two iterators. I'm not sure if that kind of iteration is supported, how to even approach it. This is true. However, if there is a pattern to your file names, you could use Calculate Value to generate the paths you need and use a for iterator. In your example: path1 = "in{}.tif".format(%n% * 2 + 1)
path2 = "in{}.tif".format(%n% * 2 + 2)
... View more
01-11-2013
08:51 AM
|
0
|
0
|
2043
|
|
POST
|
It's handy, and consistent with the UI (IMHO) that you can pass a feature class to this function. Layer inputs to tools can take a layer file, layer in the map, or a dataset. I agree it should be documented! If you find anything in the help you don't like, you're welcome (and invited) to provide feedback using the button at the upper right of the page. I know these comments gets read and used because when I provide my email address I get a personal email back from the doc team. I agree that GetLayer may be a better name for the function. If you add it to ideas, I'll vote for it!
... View more
01-11-2013
08:03 AM
|
0
|
0
|
1663
|
|
POST
|
Kim Ollivier a couple of years ago gave example Python code to create a polygon with a hole in it. This involved a null point between the outer and inner rings of data. The help does not go into detail on this; though there is a deprecated script tool Create Features From Text File, from the Samples Toolbox, which supports creation of donut holes (Inner Rings). Since this script is such a good short and detailed example for writing geometry, and tricky to locate in the install folder, I just went ahead and pasted it here, so people can find it in the forums when search for how to write polygons with donut holes or other geometry.
'''----------------------------------------------------------------------------------
Tool Name: CreateFeaturesFromTextFile
Source Name: CreateFeaturesFromTextFile.py
Version: ArcGIS 9.1
Author: Environmental Systems Research Institute Inc.
Required Argumuments: An Input Text File containing feature coordinates
An Input Character designating the decimal separator used in the text file.
An output feature class
Optional Arguments: A spatial reference can be specified. This will be the
spatial reference of the output fc.
Description: Reads a text file with feature coordinates and creates a feature class
from the coordinates.
----------------------------------------------------------------------------------'''
import string, os, sys, locale, arcgisscripting
gp = arcgisscripting.create()
gp.overwriteoutput = 1
msgErrorTooFewParams = "Not enough parameters provided."
msgUnknownDataType = " is not a valid datatype. Datatype must be point, multipoint, polyline or polygon."
msgErrorCreatingPoint = "Error creating point %s on feature %s"
# sets all the point properties
def createPoint(point, geometry):
try:
point.id = geometry[0]
point.x = geometry[1]
point.y = geometry[2]
# When empty values are written out from pyWriteGeomToTextFile, they come as 1.#QNAN
# Additionally, the user need not supply these values, so if they aren't in the list don't add them
if len(geometry) > 3:
if geometry[3].lower().find("nan") == -1: point.z = geometry[3]
if len(geometry) > 4:
if geometry[4].lower().find("nan") == -1: point.m = geometry[4]
return point
except:
raise Exception, msgErrorCreatingPoint
try:
# get the provided parameters
inputTxtFile = open(gp.getparameterastext(0))
fileSepChar = gp.getparameterastext(1)
outputFC = gp.getparameterastext(2)
# spatial reference is optional
outputSR = gp.getparameterastext(3)
# make sure the text type specified in the text file is valid.
inDataType = inputTxtFile.readline().strip().lower()
dataTypes = ["point", "multipoint", "polyline", "polygon"]
if inDataType.lower() not in dataTypes:
msgUnknownDataType = "%s%s" % (inDataType, msgUnknownDataType)
raise Exception, msgUnknownDataType
# create the new featureclass
gp.toolbox = "management"
gp.CreateFeatureclass(os.path.split(outputFC)[0], os.path.split(outputFC)[1], inDataType, "#", "ENABLED", "ENABLED", outputSR)
# create a new field to assure the id of each feature is preserved.
idfield = "File_ID"
gp.addfield(outputFC, idfield, "LONG")
# get some information about the new featureclass for later use.
outDesc = gp.describe(outputFC)
shapefield = outDesc.ShapeFieldName
# create the cursor and objects necessary for the geometry creation
rows = gp.insertcursor(outputFC)
pnt = gp.createobject("point")
pntarray = gp.createobject("Array")
partarray = gp.createobject("Array")
locale.setlocale(locale.LC_ALL, '')
sepchar = locale.localeconv()['decimal_point']
# loop through the text file.
featid = 0
lineno = 1
for line in inputTxtFile.readlines():
lineno += 1
# create an array from each line in the input text file
values = line.replace("\n", "").replace("\r", "").replace(fileSepChar, sepchar).split(" ")
# for a point feature class simply populate a point object and insert it.
if inDataType == "point" and values[0].lower() != "end":
row = rows.newrow()
pnt = createPoint(pnt, values)
row.SetValue(shapefield, pnt)
row.SetValue(idfield, int(values[0]))
rows.insertrow(row)
# for a multipoint the text file is organized a bit differently. Groups of points must be inserted at the same time.
elif inDataType == "multipoint":
if len(values) > 2:
pnt = createPoint(pnt, values)
pntarray.add(pnt)
elif (len(values) == 2 and lineno != 2) or values[0].lower() == "end":
row = rows.newrow()
row.SetValue(shapefield, pntarray)
# store the feature id just in case there is an error. helps track down the offending line in the input text file.
if values[0].lower() != "end":
row.SetValue(idfield, featid)
featid = int(values[0])
else:
row.SetValue(idfield, featid)
rows.insertrow(row)
pntarray.removeall()
elif (len(values) == 2 and lineno == 2):
featid = int(values[0])
# for polygons and lines. polygons have a bit of logic for interior rings (donuts).
# lines use the same logic as polygons (except for the interior rings)
elif inDataType == "polygon" or inDataType == "polyline":
#takes care of
#adds the point array to the part array and then part array to the feature
if (len(values) == 2 and float(values[1]) == 0 and lineno != 2) or values[0].lower() == "end":
partarray.add(pntarray)
row = rows.newrow()
row.SetValue(shapefield, partarray)
# store the feature id just in case there is an error. helps track down the offending line in the input text file.
if values[0].lower() != "end":
row.SetValue(idfield, featid)
featid = int(values[0])
else:
row.SetValue(idfield, featid)
rows.insertrow(row)
partarray.removeall()
pntarray.removeall()
#adds parts and/or interior rings to the part array
elif (len(values) == 2 and float(values[1]) > 0) or values[0].lower() == "interiorring":
partarray.add(pntarray)
pntarray.removeall()
#add points to the point array
elif len(values) > 2:
pnt = createPoint(pnt, values)
pntarray.add(pnt)
elif (len(values) == 2 and lineno == 2):
featid = int(values[0])
inputTxtFile.close()
del rows
del row
except Exception, ErrorDesc:
# handle the errors here. if the point creation fails, want to keep track of which point failed (easier to fix the
# text file if we do)
if ErrorDesc[0] == msgErrorCreatingPoint:
if inDataType.lower() == "point":
msgErrorCreatingPoint = msgErrorCreatingPoint % (values[0], values[0])
else:
msgErrorCreatingPoint = msgErrorCreatingPoint % (values[0], featid)
gp.AddError(msgErrorCreatingPoint)
elif ErrorDesc[0] != "":
gp.AddError(str(ErrorDesc))
gp.AddError(gp.getmessages(2))
# make sure to close up the fileinput no matter what.
if inputTxtFile: inputTxtFile.close()
Additional search keys: doughnut, polygon rings, writing polygon geometry
... View more
01-11-2013
07:24 AM
|
0
|
0
|
1026
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 08-11-2021 01:26 PM | |
| 5 | 12-10-2021 04:58 PM | |
| 1 | 02-27-2017 09:30 AM | |
| 2 | 12-04-2023 01:05 PM | |
| 1 | 04-12-2016 10:17 AM |
| Online Status |
Offline
|
| Date Last Visited |
06-19-2024
12:10 AM
|