|
POST
|
I think a sort is required between line 7 and 8. Interesting code, James. Thanks for sharing. # line 7
df = df.sort(['area_id','length'],ascending=[True,False])
# line 8
... View more
11-20-2017
09:24 PM
|
1
|
1
|
2794
|
|
POST
|
I am still getting the folder instead of the feature with 10.5.0. I added a few lines to check the working directory for Python and found if I change the cwd to the arcpy workspace then describe finds the feature. Perhaps there's a problem with the Python path/environment? print "Arcpy: {}".format(arcpy.env.workspace)
print "Python: {}".format(os.getcwd())
for fc in arcpy.ListFeatureClasses("*"):
desc = arcpy.Describe(fc)
print "{} - {}".format(fc, desc.dataType)
os.chdir(arcpy.env.workspace)
print "\nNow: {}".format(os.getcwd())
for fc in arcpy.ListFeatureClasses("*"):
desc = arcpy.Describe(fc)
print "{} - {}".format(fc, desc.dataType)
# Output (using IDLE):
Arcpy: C:\Temp\describe\Default.gdb
Python: C:\Temp\describe
point_fc - Folder
line_fc - FeatureClass
polygon_fc - FeatureClass
Now: C:\Temp\describe\Default.gdb
point_fc - FeatureClass
line_fc - FeatureClass
polygon_fc - FeatureClass
... View more
11-20-2017
07:47 PM
|
0
|
0
|
2941
|
|
POST
|
Since you don't know the specific glac_id, it might work to read the data from a SearchCursor into an array or dictionary and process the results from there. Untested code: sourceFC = r"C:\Users\adam\Desktop\testarcpy\cont_int.shp"
sourceFieldsList = ["glac_id", "length"]
# Use list comprehension to build a dictionary from a da SearchCursor
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(sourceFC, sourceFieldsList)}
for k, v in valueDict.items():
print k, max(v)
... View more
11-20-2017
12:50 PM
|
3
|
0
|
6304
|
|
POST
|
Try this one: import arcpy
arcpy.env.overwriteOutput=True
testVector = r"C:\Users\adam\Desktop\testarcpy\cont_int.shp"
result = arcpy.da.SearchCursor(testVector, ["glac_id","length"],
where_clause = "glac_id = 'G012039E78878N'",
sql_clause = (None, 'ORDER BY length DESC')
).next()
print result[0] # glac_id
print result[1] # length
... View more
11-20-2017
12:34 PM
|
1
|
2
|
2794
|
|
POST
|
Try this for the where clause (double quotes around entire where clause, single quotes only around the id code): where_clause = "glac_id = 'G012039E78878N'",
... View more
11-20-2017
12:26 PM
|
1
|
2
|
3510
|
|
POST
|
In your original question, you were wanting to "group by" area_id. My thoughts were to use the 3 area_id values to feed the where_clause in 3 SearchCursors. You would use only the first row of each search.
... View more
11-20-2017
12:09 PM
|
0
|
7
|
3510
|
|
POST
|
I did try this on a computer running ver 10.2.1, and describe gave the correct results. I will try again with the 10.5.0 version later today.
... View more
11-20-2017
11:40 AM
|
0
|
0
|
2941
|
|
POST
|
In my latest test I was using "point_fc" for a folder name, with the same name used for a feature inside the geodatabase. I first noticed it when I moved a folder using the same name as a polygon feature that was part of a dataset into the directory with the file geodatabase. I was using describe to look at a feature's shapeType property - which is not a property when describing a folder!
... View more
11-19-2017
03:12 PM
|
0
|
0
|
2941
|
|
POST
|
Perhaps something like (to get only first row): OID = arcpy.da.SearchCursor(myFile, ["OBJECTID","length"],
where_clause = "area_id = 'some_code'",
sql_clause = (None, 'ORDER BY length DESC')
).next()[0] # use [0] to return just OBJECTID, remove for tuple
... View more
11-19-2017
02:25 PM
|
0
|
0
|
3510
|
|
POST
|
Are you using a file geodatabase? Or an SQL server? Some commands do not work with a file geodatabases. TOP is only supported by SQL Server and MS Access databases. See: SearchCursor
... View more
11-19-2017
12:03 PM
|
1
|
12
|
3510
|
|
POST
|
There are several ways to work around this issue. Renaming the folder is one option. Or this adjustment to the code will also work: for fc in arcpy.ListFeatureClasses("*"):
desc = arcpy.Describe(gdb+'\\'+fc) # include gdb/workspace
I was expecting Describe to use the declared workspace in a stand-alone script. But, apparently, it starts with the folder that contains the geodatabase. This is an example where scripts work slightly different between ArcMap's Python window and a stand-alone script. Using hasattr to check for properties is a good suggestion.
... View more
11-19-2017
11:34 AM
|
0
|
0
|
2941
|
|
POST
|
Does Describe work differently in a stand-alone script vs. a script run inside ArcMap? More specifically, does Describe use the workspace if one is specified? Here's the script: import arcpy
gdb = r'C:\Path\To\Default.gdb' # path to a file geodatabase
arcpy.env.workspace = gdb
for fc in arcpy.ListFeatureClasses("*"):
desc = arcpy.Describe(fc)
print "{} - {}".format(fc, desc.dataType) Here's the folder structure: Note that there is a folder outside the file geodatabase with the same name as a feature inside it - in this case "point_fc". Here are the results: # Outside ArcMap in stand-alone script:
point_fc - Folder
line_fc - FeatureClass
polygon_fc - FeatureClass
# In ArcMap's Python window:
point_fc - FeatureClass
line_fc - FeatureClass
polygon_fc - FeatureClass The stand-alone script picked up the folder, whereas the script when ran in ArcMap picked up the feature class. Is this expected behavior for Describe?
... View more
11-18-2017
11:00 PM
|
0
|
9
|
3415
|
|
POST
|
Have you considered the Add Geometry Attributes tool (Data Management > Features)? It will add a Point_X and Point_Y to an attribute table and populate it with the geometry values. You will need to select GCS_WGS_1984 as the coordinate system, and the tool will populate the fields with longitude and latitude.
... View more
11-17-2017
12:14 PM
|
2
|
0
|
1284
|
|
POST
|
Using ListFeatureClasses to loop through a geodatabase: import arcpy
import os
gdb = r'C:\Path\To\geodatabase.gdb'
arcpy.env.workspace = gdb
sfDir = 'C:/Temp/' # directory to put shapefiles, subdirectories will be created
for fc in arcpy.ListFeatureClasses("*","Polygon"):
print "Creating shapefile: {}".format(fc)
if not os.path.exists('{}{}'.format(sfDir, fc)):
os.makedirs('{}{}'.format(sfDir, fc))
arcpy.FeatureClassToShapefile_conversion(fc, '{}{}'.format(sfDir, fc))
arcpy.AddField_management('{}{}/{}.shp'.format(sfDir,fc,fc),
field_name = "LABEL",
field_type = "TEXT",
field_length = "25" )
arcpy.CalculateField_management('{}{}/{}.shp'.format(sfDir,fc,fc),
field = "LABEL",
expression = '"{}"'.format(fc) ) This puts shape files in a subdirectories to keep them grouped. It uses the feature's name for the subdirectory, the shape file name and to populate a "LABEL" field. This assumes that the feature name will be valid for such purposes. It has not been tested with datasets, but a version using ListDatasets as an outer loop could be created if this doesn't pick up all the desired features.
... View more
11-16-2017
10:23 PM
|
0
|
0
|
3225
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 10-27-2016 02:23 PM | |
| 1 | 09-09-2017 08:27 PM | |
| 2 | 08-20-2020 06:15 PM | |
| 1 | 10-21-2021 09:15 PM | |
| 1 | 07-19-2018 12:33 PM |
| Online Status |
Offline
|
| Date Last Visited |
02-12-2026
07:13 PM
|