|
POST
|
The ArcPy Data Access Walk function is robust enough to handle it without involving the ListFeatureClasses function: import arcpy, os
gdb_in = #path to input geodatabase
gdb_out = #path to output geodatabase
walk = arcpy.da.Walk(gdb_in, datatype="FeatureClass", type="Polyline")
for root, dirs, files in walk:
if root != gdb_in:
for f in files:
arcpy.CopyFeatures_management(
f,
os.path.join(gdb_out,"Polyline_" + os.path.split(root)[1]))
) The above assumes there is only 1 polyline feature class in each feature dataset, but the name of the polylines feature classes could also be dealt with in the for loop.
... View more
11-20-2015
09:24 AM
|
2
|
0
|
9935
|
|
POST
|
I will save any explanations until after I see whether this works for you: def FindLabel ( [stand_code], [Avg_TPA_Pi], [Avg_TPA_Cy], [Avg_TPA_Hd] ):
stand_code = [stand_code]
Avg_TPA_Pine = [Avg_TPA_Pi]
Avg_TPA_Cyp = [Avg_TPA_Cy]
Avg_TPA_Hdwd = [Avg_TPA_Hd]
label = ""
try:
if int(Avg_TPA_Pine) > 0:
label += "TPA P: " + Avg_TPA_Pine + " / "
if int(Avg_TPA_Cyp) > 0:
label += "TPA C: " + Avg_TPA_Cyp + " / "
if int(Avg_TPA_Hdwd) > 0:
label += "TPA H: " + Avg_TPA_Hdwd
return stand_code + '\n'+ label
except:
return stand_code
... View more
11-16-2015
11:31 AM
|
0
|
0
|
1265
|
|
POST
|
One problem I run into trying to reply during the day, I get distracted and my responses get drawn out, so other responses come in before I hit reply. I wish comments could be easily saved as draft and revisited later. Although there is auto-saving, I have run into cases where I lost content, and copying and pasting the content/html back and forth to the clipboard is such a hassle.
... View more
11-16-2015
10:49 AM
|
1
|
0
|
4735
|
|
POST
|
The problem with creating an "Unknown/Undefined" spatial reference has been around for years; unfortunately, Esri has made it clear they don't really see it as much of a problem and don't plan on addressing it. For example, see Bug NIM-087033: Arcpy.DefineProjection_management does not take 'Unknown' as a valid coordinate system. The SpatialReference.loadFromString works, but it relies on passing the Esri internal Class ID/GUID value for the unknown spatial reference. Although that Class ID/GUID has remained the same for many, many years, it is nonetheless an internal value that Esri could change. My preferred workaround is to create an empty point geometry with no spatial reference, which gets turned into the unknown spatial reference, and then pass that into a variable: >>> SR = arcpy.FromWKT('POINT EMPTY').spatialReference
>>> #which can be seen to use Unknown/Undefined spatial reference
>>> SR.exportToString()
u'{B286C06B-0879-11D2-AACA-00C04FA33C20};-450359962737.05 -450359962737.05 10000;#;#;0.001;#;#;IsHighPrecision'
... View more
11-16-2015
10:28 AM
|
1
|
2
|
4735
|
|
POST
|
Can you post some specific error messages? Also, what about a table/feature class with a subset of your data? I tested the code on some examples I created without issue, but obviously my guessing on your data structure wasn't right.
... View more
11-16-2015
09:59 AM
|
0
|
3
|
1265
|
|
POST
|
Honestly, I am a bit confused. You are using the Table to Table tool, right? And you are pasting the entire SQL above into the Expression box or are you pasting only the part after WHERE? If one looks at the Syntax table for the tool, the "Expression" box in the GUI tool is really a "where_clause," not a general SQL box.
... View more
11-16-2015
08:29 AM
|
0
|
0
|
1588
|
|
POST
|
Are you dealing with multipart polygons? If you have multipart polygons and one or more of the parts are not wholly within another polygon, the spatial selection will not return the multipart polygon.
... View more
11-16-2015
08:01 AM
|
0
|
2
|
2421
|
|
BLOG
|
I was on the fence whether to write a blog post or simply open a discussion in the Python place. It has been a while since I contributed to my blog, so I am going with the former.
The ArcPy Data Access Walk function (arcpy.da.Walk) is a real workhorse function, and I rely on it quite a bit. The ArcPy Walk function is an example where Esri got it right with their Python implementation. I can't say what their design goal was, but it seems rather apparent the aim was to create a geospatially-aware version of Python's os.walk function. The names are the same, many of the parameters are the same, the results are similar, etc.... I think emulating this long-standing, native Python functionality in ArcPy was a win because it didn't reinvent a perfectly good wheel, i.e., users familiar with os.walk can easily transition to using arcpy.da.Walk.
As much as I like the ArcPy Walk function, there are a couple of minor issues I have with it. The first is a documentation issue, or should I say lack of documentation. One of the Walk function's parameters is datatype. The documentation has a table that lists all of the acceptable arguments for datatype, but there is no actual documentation of the data types themselves. Looking over the data type names, it seems most of them are obvious, so maybe Esri decided they didn't need to document them.
As descriptive as a name might seem at first glance, lack of documentation usually leads to ambiguity and confusion. For example, what is covered under "FeatureClass"? Obviously one would assume a feature class in a geodatabase is covered but what about shape files? Are shape files feature classes? In Esri-land, the answer is usually "Yes" but not always. The real riddle is the "Geo" datatype since it includes shape files but not feature classes in a geodatabase. Feature classes aren't "geo?" As important as documentation is for libraries/APIs, it isn't the main reason for writing today.
One of my favorite ArcPy Walk patterns is to call Python's built-in next() function once to return a list of shape files in a folder or feature classes in a geodatabase or feature classes in a feature dataset.
>>> workspace = #path to folder or geodatabase or feature dataset
>>>
>>> _, _, filenames = next(arcpy.da.Walk(workspace, datatype="FeatureClass"))
>>> filenames
[u'canadwshed_p.shp', u'plots.shp']
>>>
>>> #or looping over feature classes or shape files
>>> for file in next(arcpy.da.Walk(workspace, datatype="FeatureClass"))[2]:
... print file
canadwshed_p.shp
plots.shp
>>>
In the first example, I create a throw-away Workspace Walker object that I don't have to bother keeping around or deconstructing. In the second example, calling next() in the for loop allows me to cut out an extra loop when I am only interested in one level of geospatial data.
As is the case with most things in life, there isn't just one way to list feature classes in a geodatabase or shape files in a folder. In fact, the ArcPy Walk function is the new kid on the block being introduced in ArcGIS 10.1 SP1. Prior to ArcPy Walk, a user could use one of the many ArcPy listing functions (ListDatasets, ListFeatureClasses, ListFiles, ListRasters, ListTables, and ListWorkspaces) or the ArcPy Describe function. I tend to prefer ArcPy Walk over the others because of its ease of use and similarity to built-in Python functionality.
The main reason for this blog post is to share one area where the ArcPy Walk function stumbles, i.e., in-memory data sources. Whereas the older methods of listing data sources work with in-memory workspaces, that is not the case with ArcPy Walk.
>>> arcpy.CreateFeatureclass_management('in_memory', 'test_fc')
<Result 'in_memory\\test_fc'>
>>> arcpy.CreateTable_management('in_memory', 'test_tbl')
<Result 'in_memory\\test_tbl'>
>>> arcpy.CreateRasterDataset_management('in_memory','test_rd')
<Result 'in_memory\\test_rd'>
>>>
>>> #using ArcPy Walk
>>> next(arcpy.da.Walk('in_memory'))[2]
[]
>>>
>>> #using ArcPy listing functions
>>> arcpy.env.workspace = 'in_memory'
>>> arcpy.ListFeatureClasses()
[u'test_fc']
>>> arcpy.ListTables()
[u'test_tbl']
>>> arcpy.ListRasters()
[u'test_rd']
>>> arcpy.ListDatasets()
[u'test_rd']
>>>
>>> #using ArcPy Describe function
>>> [child.name for child in arcpy.Describe('in_memory').children]
[u'test_tbl', u'test_fc', u'test_rd']
>>>
Not supporting in-memory workspaces isn't much more than a stumble, but it is a stumble nonetheless. After all, the documentation does say the first parameter is the "top-level workspace" and yet no mention is made that in-memory workspaces aren't supported. Fortunately for users, there are at least two other ways to list in-memory data sources.
UPDATE 06/2017:
Since writing this blog post nearly 18 months ago (I know "time flies" but still, 18 months already?), I have come to discover the issue with ArcPy Walk and in-memory workspaces is more nuanced than I originally thought. Let me demonstrate:
>>> arcpy.CreateFeatureclass_management('in_memory', 'test_fc')
<Result 'in_memory\\test_fc'>
>>> arcpy.CreateTable_management('in_memory', 'test_tbl')
<Result 'in_memory\\test_tbl'>
>>> arcpy.CreateRasterDataset_management('in_memory','test_rd')
<Result 'in_memory\\test_rd'>
>>>
>>> # using ArcPy Walk with "GPInMemoryWorkspace" rather than common "in_memory"
>>> next(arcpy.da.Walk('GPInMemoryWorkspace'))[2]
[u'test_tbl', u'test_fc', u'test_rd']
>>>
So, it turns out that ArcPy Walk works just fine with in-memory workspaces, when it actually knows you are pointing it to an in-memory workspace. The really frustrating part of this, and even a bit lamentable, is that this is simply about semantics and Esri still can't manage to fix it. Functionally, ArcPy Walk already works with in-memory workspaces, the function just doesn't know that everyone else and every other tool refers to those spaces as "in_memory" instead of "GPInMemoryWorkspace".
... View more
11-14-2015
05:14 PM
|
3
|
3
|
4016
|
|
POST
|
If my or any other user's comment addresses your question, please mark it correct to close out the thread so others know you are not looking for additional feedback. Thanks.
... View more
11-14-2015
09:37 AM
|
0
|
0
|
1376
|
|
POST
|
Can you provide the specific Python code? Although you have provided the SQL code that works directly in SQL Server, it is helpful to see how you are creating/building the arguments and passing them to the tool. And, please use Syntax Highlighting with your code, it makes it much easier to read, thanks.
... View more
11-14-2015
09:35 AM
|
0
|
0
|
1588
|
|
POST
|
If you log into My Esri, in the Downloads section under My Organizations, there is a menu option on the left-hand side for Backup Media. Once there, you can select "Download Media" to get access to ISO images of backup media. That said, you shouldn't need the ISO files to create an install package. The regular EXE installers extract the installation files, including MSI and CAB files, before installing the software. Those extracted files can be used to create an installation package.
... View more
11-13-2015
09:49 AM
|
2
|
2
|
1376
|
|
POST
|
Depending upon how you have indexing setup with ArcGIS Desktop, and how metadata is populated with your datasets, ArcCatalog does have an option to display projected and geographic coordinate systems of spatial datasets. It has a somewhat Windows Explorer "details" look to it in the Contents tab of ArcCatalog. The above example is for feature classes in a personal geodatabase, but it can work with shapefiles as well. I emphasize can because both indexing and metadata need to be in order for it all to work. Personally, it is a bit too fragile for me to rely on, but I wanted to throw it out for discussion purposes since it is built-in functionality. My guess is that Esri went with displaying coordinate systems in ArcCatalog through metadata because it is much quicker to look up information from metadata than it is to extract information from the data itself, like coordinate systems. If you are comfortable with Python and the Command Prompt, a fairly simple script could be written to output coordinate system information in a "dir" like format. The functional part of the code could look something like: >>> import os
>>> width = 24
>>> levels = 0
>>>
>>> workspace = r'D:\geodata'
>>> levels = len(workspace.split(os.path.sep)) + levels
>>>
>>> walk = arcpy.da.Walk(workspace, datatype="FeatureClass")
>>> for dirpath, dirnames, filenames in walk:
... if len(dirpath.split(os.path.sep)) >= levels:
... del dirnames[:]
...
... print " Workspace of {}\n".format(dirpath)
... for f in filenames:
... desc = arcpy.Describe(os.path.join(dirpath, f))
... SR = desc.spatialReference
... GCSName = SR.GCSName[:width-3] + (SR.GCSName[width-3:] and '.. ')
... PCSName = SR.PCSName[:width-3] + (SR.PCSName[width-3:] and '.. ')
... print "{: <{width}}{: <{width}}{}".format(GCSName, PCSName, f, width=width)
... print ""
...
Workspace of D:\geodata
GCS_North_American_19.. canadwshed_p.shp
GCS_North_American_19.. canadwshed_p_dissolve.shp
GCS_North_American_19.. plots.shp
GCS_North_American_19.. plots_44.shp
NAD_1983_UTM_Zone_15N plots_44_Project.shp
>>> Since arcpy.da.Walk doesn't include a parameter for limiting the recursion depth, I added some code to implement that functionality. The levels variable specifies the depth of subdirectories (including feature datasets) to walk. The width variable specifies how wide the columns are for displaying coordinate system information. The empty values in some columns are not caused by my code, per se, but how the spatial reference object handles coordinate systems. If a dataset is projected, the spatial reference object only shows the projected coordinate system and not the associated geographic coordinate system. If the dataset is geographic, then it makes sense to leave the projected information blank.
... View more
11-09-2015
10:16 AM
|
1
|
1
|
2402
|
|
POST
|
A few questions: What if you try to format all symbols, without sorting, does it error? What exact steps are you taking to try and sort the label? Can you elaborate about the field you are trying to format and sort? What data type? File geodatabase? SDE?
... View more
11-07-2015
01:11 PM
|
0
|
1
|
12886
|
|
POST
|
The major issue, as I see it, is with "permanently." As Wes Miller points out, you can use the Sort tool that will sort a dataset into a new dataset, but is that sorting "permanent?" The Sort tool creates a point-in-time sorted dataset, it doesn't create a dataset that will forever remain sorted the same as the data is modified over time. The relational model, from which relational DBMSes are founded, is based on set theory and predicate logic. Since mathematical sets are unordered collections of distinct/unique objects, sorting is more an issue of displaying the data rather than storing the data. Granted there is sorting of data within DBMSes, like Clustered Indexes in SQL Server or certain other indexes in other systems, but this type of sorting or need for it is driven by implementation/performance issues. One of the beauties of the SQL SELECT statement is that it allows people to view information in a way that makes sense to them, not based on other peoples' assumptions of how the data should or shouldn't be stored. If your question is more about changing the default sort order in ArcGIS than creating a new, sorted data snapshot; unfortunately, I don't have an answer for you. The question of changing the default sort order for layers and views in ArcGIS has been around for a long time, and I haven't seen a complete solution. One can sort a layer or view and export a layer file that will maintain that sort, at least for a while until the data starts getting modified by other users.
... View more
11-06-2015
07:53 AM
|
1
|
0
|
1469
|
|
POST
|
Mike Onzay, thanks for the follow up, much appreciated. In your case, I am glad there is a workaround, but I always have to shake my head when those 2+ year old bugs that are still open rear their ugly heads.
... View more
11-04-2015
01:37 PM
|
0
|
0
|
2225
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 3 weeks ago | |
| 2 | a month ago | |
| 1 | a month ago | |
| 2 | 06-05-2026 10:30 AM | |
| 1 | 05-29-2026 08:22 AM |
| Online Status |
Online
|
| Date Last Visited |
3 hours ago
|