POST
|
It looks like it is an indentation issue. The append statements and the writing to the csv happen only once after it has iterated through all of the layers. That is probably why you're only getting the last one. for services in parsed_json ["layers"]:
row = []
row.append(str(services['id']).encode('utf-8'))
row.append(str(services['name']).encode('utf-8'))
writer.writerow(row)
... View more
02-05-2015
11:08 AM
|
2
|
1
|
664
|
POST
|
To add a shapefile to an mxd, you first need to convert it to a layer, and then add it. Below is a stack exchange thread that deals with this. As far as the csv, just don't add it! :cool: http://gis.stackexchange.com/questions/26042/how-to-programmatically-add-shapefile-to-data-frame-in-arcmap
... View more
04-04-2014
07:16 AM
|
0
|
0
|
180
|
POST
|
Thanks for all of the helpful posts. This script works with Workflow Manager, and I think that with my current workflow(below), if I used GetCount, I would have to make 2 feature layers. So I'll go with len([r for r in arcpy.da.SearchCursor....]) Workflow -Use .sde connection file to connect to default version of database. -Get what should be a unique polygon attribute (but isn't always unique) from WMX for a SQL query. -Make feature layer of whole feature class without SQL query (The whole feature class is created because the polygon may or may not yet exist in default - rec and posts are scheduled nightly) -Change version (based on WMX user) -Create SearchCursor on version, using SQL query to limit records, and get len(SearchCursor) (Here I could create another feature layer using the SQL query, in order to be able to use GetCount. Seems inefficient?) -Based on number of records in search cursor(>1, 1, or 0), do stuff.
... View more
04-03-2014
04:55 AM
|
0
|
0
|
1205
|
POST
|
Thanks for the suggestions, but I'm looking to avoid a counter.
... View more
03-21-2014
12:20 PM
|
0
|
0
|
10449
|
POST
|
Does anyone know of a quick way to return the number of records in a query constrained Search Cursor using the da module? rows = arcpy.da.SearchCursor(someFC,someFields,someQuery) print (len(rows)) #I'm sure this used to work with the old cursors, but doesn't work with da.SearchCursor
... View more
03-21-2014
10:46 AM
|
2
|
13
|
42373
|
POST
|
You might try removing the "r" from in front of the coordinate system file path in the output_coordinate_system variable. You can also use arcpy.AddMessage(output_coordinate_system) to verify the file path is correct.
... View more
08-23-2013
02:45 AM
|
0
|
0
|
347
|
POST
|
In your previous code you don't account for well yield values of 721, 1086, or 1451 How about something like this:
def reclass(category,WellYield):
try:
if WellYield >= 0 and WellYield <= 720:
yld = 1
if WellYield >= 721 and WellYield <= 1085:
yld= 2
if WellYield >= 1086 and WellYield <= 1450:
yld = 3
if WellYield >= 1451:
yld = 4
catYield = ''.join([str(category),str(yld)])
reclassDict = {'11':10,'12':20,'13':30,'14':40,
'21':20,'22':30,'23':40,'24':50,
'31':30,'32':40,'33':50,'34':60,
'41':40,'42':50,'43':60,'44':70}
value = reclassDict[catYield]
return value
except:
pass
Expression
reclass(!CATEGORY!,WELL_YIELD!)
... View more
12-13-2012
01:42 PM
|
0
|
0
|
302
|
POST
|
That's really weird. I see no clear difference between records 959777 and 959778 that would cause one to calculate and the other to error out. I would highly recommend the try and except blocks. The calculation won't ever completely error out...if it can't perform the operation, it will default to 'Unable to calculate'. This will allow you to analyze what is different about those records that aren't calculated.
def setAPN2(parcel):
try:
parcel = parcel.strip()
strValue = parcel[0:3] + '-' + parcel[3:5] + '-'
if len(parcel) <= 8:
strValue += parcel[5:]
else:
strValue += parcel[5:8] + '-' + parcel[8:]
except:
strValue = 'Unable to calculate'
return strValue
... View more
11-07-2012
12:54 PM
|
0
|
0
|
967
|
POST
|
You should be able to run this script from idle. Just substitute the path to your mxd. You might have to tweak this some if the mxd has multiple data frames or grouped layers from arcpy import mapping mapDoc = r'enter mxd file path here' mxd = mapping.MapDocument(mapDoc) lyrs = mapping.ListLayers(mxd) lyrTitle = 'Map Layer' sourceTitle = 'Data Source' print('%-60s%s' % (lyrTitle,sourceTitle)) print('%-60s%s' % ('-' * len(lyrTitle), '-' * len(sourceTitle))) for lyr in lyrs: try: print('%-60s%s' % (lyr.name,lyr.dataSource)) except: print('Unable to retrieve layer information') del mxd
... View more
11-05-2012
08:04 AM
|
2
|
0
|
1069
|
POST
|
Actually.....looking at the code and the error, i'm pretty sure what is wrong. It is trying to strip the field name argument itself, which isn't a string. That's why you're getting the scanning string literal error. Try the solution below: Expression
setAPN2(str(!PG_APN!))
Then alter the function:
def setAPN2(parcel):
if parcel != 'None':
try:
parcel = parcel.strip()
strValue = parcel[0:3] + '-' + parcel[3:5] + '-'
if len(parcel) <= 8:
strValue += parcel[-3:]
else:
strValue += parcel[5:8] + '-' + parcel[-1:]
except:
strValue = 'Error - unable to calculate'
return strValue
... View more
11-04-2012
01:13 PM
|
0
|
0
|
967
|
POST
|
Oops...missed part of the logic for parcel strings with length > 8. Also added a calculation of error records to 'Error - Unable to calculate'
def setAPN2(parcel):
if parcel != 'None':
try:
strValue = parcel[0:3] + '-' + parcel[3:5] + '-'
if len(parcel) <= 8:
strValue += parcel[-3:]
else:
strValue += parcel[5:8] + '-' + parcel[-1:]
execept:
strValue = 'Error - unable to calculate'
return strValue
... View more
11-04-2012
01:00 PM
|
0
|
0
|
967
|
POST
|
It could be null values that are throwing it off. For 'Expression' change it to
setAPN2(str(!PG_APN!).strip())
This will convert any null values to a string representation of 'None'. Then alter your function like below Even if this isn't what is causing your specific error, this is good practice to ensure against null values breaking your code. You would then need to alter your function as below to only work on non-null values. Also, you can wrap the whole thing in try and except blocks to analyze which values aren't calculating and why. If none of the records get calculated, it's probably an error in the logic. Can you post a screenshot of some of the sample record values in the PG_APN field?
def setAPN2(parcel):
if parcel != 'None':
try:
strValue = parcel[0:3] + '-' + parcel[3:5] + '-'
if len(parcel) <= 8:
strValue += parcel[-3:]
else:
strValue += parcel[-1:]
return strValue
execept:
pass
... View more
11-04-2012
12:18 PM
|
0
|
0
|
967
|
POST
|
instead of copy features you'll probably want to use featureClass to featureClass. it looks like copy features requires that the feature class be in existence.
import arcpy
import os
folder = r'C:\Your Desired Output Path'
rows = arcpy.SearchCursor(lyr)
list = []
nullList = ['',' ','None']
for row in rows:
mesoID = str(row.getValue('MESO_ID'))
if mesoID not in nullList:
list.append(mesoID)
del row
del rows
for item in list:
item = ''.join(["'",item,"'"])
query = ''.join(['\"MESO_ID\" = ',item])
arcpy.FeatureClassToFeatureClass_conversion (lyr,folder, ''.join([item.strip("'"), '.shp']), query,'','')
... View more
11-03-2012
11:37 AM
|
0
|
0
|
324
|
POST
|
Mark's suggestion of using the cursor to build a list and iterating through the list will greatly improve the stability of the code. Something like below should work (haven't tested it)
import arcpy
import os
folder = r'C:\Your Desired Output Path'
rows = arcpy.SearchCursor(lyr)
list = []
nullList = ['',' ','None']
for row in rows:
mesoID = str(row.getValue('MESO_ID'))
if mesoID not in nullList:
list.append(mesoID)
del row
del rows
for item in list:
item = ''.join(["'",item,"'"])
query = ''.join(['\"MESO_ID\" = ',item])
arcpy.SelectLayerByAttribute_management("lyr","NEW_SELECTION",query)
outFC = os.path.join(folder,''.join([item.strip("'"),'.shp']))
arcpy.CopyFeatures_management("lyr", outFC)
... View more
11-02-2012
06:43 PM
|
0
|
0
|
324
|
POST
|
I seem to get this error when I kill a script while it's running. If you use task manager/processes to kill all python processes, it should go away.
... View more
11-02-2012
06:15 PM
|
0
|
0
|
393
|
Title | Kudos | Posted |
---|---|---|
2 | 11-05-2012 08:04 AM | |
2 | 03-21-2014 10:46 AM | |
2 | 02-05-2015 11:08 AM |
Online Status |
Offline
|
Date Last Visited |
10-18-2023
06:53 PM
|