POST
|
I have developed a workaround that writes the raster to a layer file and retrieves this version for the map insertion: inRas = '<projectDir>/Risks/riskv0'
outAPRX = '<projectDir>/Reports/riskv0/riskv0.aprx'
templateFile = '<projectDir>/MapTemplates/risksTemplate/risksTemplate.aprx'
aprx = arcpy.mp.ArcGISProject(templateFile)
map = aprx.listMaps()[0]
refLyr = map.listLayers()[1]
arcpy.MakeRasterLayer_management(inRas, "insertRas")
arcpy.SaveToLayerFile_management("insertRas", '<tempDir>/insertRas.lyrx')
insertLyr = arcpy.mp.LayerFile('<tempDir>/insertRas.lyrx')
map.insertLayer(refLyr, insertLyr, "AFTER")
aprx.saveACopy(outAPRX)
del aprx I trust it doesn't have to be this complicated! Is there a way to work with the raster without writing/reading the temporary file?
... View more
05-06-2019
09:50 PM
|
0
|
0
|
439
|
POST
|
I'm converting Python standalone scripts from Desktop (10.6.1) to Pro (2.3.2). I can add a raster to an open Pro project file interactively by right clicking on the raster in the Catalog and choosing "Add to Current Map". I would like to do this in a script and have tried two ways (one commented out below): inRas = '<projectDir>/Risks/riskv0'
outAPRX = '<projectDir>/Reports/riskv0/riskv0.aprx'
templateFile = '<projectDir>/MapTemplates/risksTemplate/risksTemplate.aprx'
aprx = arcpy.mp.ArcGISProject(templateFile)
map = aprx.listMaps()[0]
map.addDataFromPath(inRas)
#refLyr = map.listLayers()[1]
#map.insertLayer(refLyr, inRas, "AFTER")
aprx.saveACopy(outAPRX)
del aprx When I use addDataFromPath, I receive a RuntimeError for line 6 with no further information. The listLayers/insertLayer approach results in a ValueErr at line 8 for the inRas file. I would be grateful for any advice on adding a raster to a map in Pro.
... View more
05-05-2019
04:07 AM
|
0
|
1
|
637
|
POST
|
As near as I can establish, it can't be done. I have used matplotlib ( https://matplotlib.org/ ) instead.
... View more
03-30-2019
09:06 PM
|
0
|
0
|
256
|
POST
|
I am building a multiple series vertical bar graph with arcpy.Graph in ArcMap 10.6.1. The short version is: import arcpy
TESTDIR = <some dir>
arcpy.env.workspace = TESTDIR
arcpy.env.scratchWorkspace = TESTDIR
arcpy.env.overwriteOutput = True
arcpy.env.qualifiedFieldNames = False
outGraphName = "VerticalBarGraph"
outGraphJpeg = "VerticalBarGraph.jpg"
inputTemplate = "SeriesComparison1.grf"
graph = arcpy.Graph()
inFieldNames = [f.name for f in arcpy.ListFields("OUT.dbf") if f.name != 'OID']
for i in range(1, len(inFieldNames)):
graph.addSeriesBarVertical("OUT.dbf", inFieldNames[i], "LABEL")
arcpy.MakeGraph_management(inputTemplate, graph, outGraphName)
arcpy.SaveGraph_management(outGraphName, outGraphJpeg, "MAINTAIN_ASPECT_RATIO", 1000) The output is just what I am after except that for three input fields it produces a legend with entries: Vertical Bar Vertical Bar 2 Vertical Bar 3 These can be changed interactively in ArcMap (see below) and I have done this to generate the template graph. I want to use Python to change these defaults, depending on user input, to the names of the input fields but I have had no success. I have been reduced to trying to reverse engineer what's going on in the interactive view: by adding a couple of lines to the for loop above: for i in range(1, len(inFieldNames)):
graph.addSeriesBarVertical("OUT.dbf", inFieldNames[i], "LABEL")
graph.graphSeries[i - 1].Custom = True
graph.graphSeries[i - 1].Text = inFieldNames[i] These instructions are accepted and I can print out their values but they have no effect on the graph output. Is what I am trying to do possible with arcpy? Perhaps I am looking in the wrong place. I would be grateful for any advice. Ian
... View more
03-25-2019
07:36 PM
|
0
|
1
|
402
|
POST
|
I have run this too and it works well. I found it a most instructive alternative to the join approach. Thank you
... View more
03-23-2019
10:54 PM
|
0
|
0
|
221
|
POST
|
Hi Curtis, Setting the environment in this way certainly helped to make sense of what was happening with the field names. All the field names still failed so I figured I should look at the other parameter in addInputField. It turns out I was trying to use the arcpy.mapping.TableView view of the input dataset rather than the dataset name itself. Thanks also for the 10-character limit explanation. Ian
... View more
03-23-2019
03:30 AM
|
0
|
0
|
931
|
POST
|
Thanks very much for this contribution, Randy. I'll do some work with it over the weekend.
... View more
03-22-2019
02:51 AM
|
0
|
0
|
221
|
POST
|
Yes, I had started that task when your suggestion arrived. It turns out all of them break it. I see when the TableToTable conversion is complete, the dbf has had its field names mangled with trailing "_" and digits and restricting them to 10 characters. I was thinking this happened at this step but perhaps it occurs at the AddJoin_management? If this is the case, I can understand the addInputField might get confused.
... View more
03-22-2019
02:49 AM
|
0
|
2
|
931
|
POST
|
Hi All, I'm having a go at both approaches. I need to gain some experience with field mappings, so ... After the join, tbl1 has the fieldnames: [u'AAA.OID', u'AAA.LABEL', u'AAA.AAA', u'BBB.OID', u'BBB.LABEL', u'BBB.BBB', u'CCC.OID', u'CCC.LABEL', u'CCC.CCC'] I determine which of these I want to keep for the TableToTable_conversion with: keepFields = [u'AAA.OID', u'AAA.LABEL', u'AAA.AAA', u'BBB.BBB', u'CCC.CCC'] and attempt to build the fieldMappings with: fieldMappings = arcpy.FieldMappings()
for field in fieldnames:
fMap = arcpy.FieldMap()
if field in keepFields:
fMap.addInputField(tbl1, field)
outField = fMap.outputField
outField.name = field
fMap.outputField = outField
fieldMappings.addFieldMap(fMap) which produces the error: RuntimeError: FieldMap: Error in adding input field to field map for line 5. Can you suggest how I might follow this up? My limited experience can find no help in the error message at all! Ian
... View more
03-21-2019
04:16 AM
|
0
|
4
|
931
|
POST
|
Yes, I only read that recently. Apparently, the fix didn't make it into 10.6 which is were I came in!
... View more
03-19-2019
10:35 PM
|
0
|
0
|
1141
|
POST
|
This problem was resolved for 32-bit implementations by ArcMap 10.6.1.
... View more
03-19-2019
06:00 PM
|
0
|
3
|
1141
|
POST
|
Thank you Dan, Randy and Curtis. I am most grateful for your ideas. I am unable to follow up on them this evening but I will let you know how I get on.
... View more
03-19-2019
02:18 AM
|
0
|
1
|
931
|
POST
|
I am seeking a method to merge table outputs from ZonalHistogram (ZH) using a Python script. These files have a simple two-column format. E.g., AAA.dbf has two fields: LABEL - the X-axis with values ranging from 0 to 100 AAA - the Y-axis counts produced by ZH Similarly, BBB.dbf has two fields, LABEL and BBB, CCC.dbf has LABEL and CCC. I am able to plot these individual table files with arcpy graph, or render them in Excel. I am trying to merge these files into a single output with the structure: LABEL AAA BBB CCC 0 - 100 ZH values ZH values ZH values I started simply with: tablesList = arcpy.ListTables()
arcpy.Merge_management(tablesList, "merged.dbf") which gave me the four columns I was after but the output was arranged as: rows 0 - 100: LABEL (0 - 100) ZH values 0 0 rows 101 - 201: LABEL (0 - 100) 0 ZH values 0 rows 202 - 302: Label (0 - 100) 0 0 ZH values I have attempted to use field mapping as follows: import arcpy
TESTDIR = <some dir>
arcpy.env.workspace = TESTDIR
arcpy.env.scratchWorkspace = TESTDIR
arcpy.env.overwriteOutput = True
tablesList = arcpy.ListTables()
inFields = [u'LABEL', u'AAA', u'BBB', u'CCC'] # Generated by arcpy.Describe()
fieldMappings = arcpy.FieldMappings()
# Deal with "LABEL" as special case (first field of first table)
fMap = arcpy.FieldMap()
fMap.addInputField(tablesList[0], inFields[0])
outField = fMap.outputField
outField.name = inFields[0]
fMap.outputField = outField
fieldMappings.addFieldMap(fMap)
# Add the second field of each file to the map
for i in range(len(tablesList)):
fMap = arcpy.FieldMap()
fMap.addInputField(tablesList[i], inFields[i + 1])
outField = fMap.outputField
outField.name = inFields[i + 1]
fMap.outputField = outField
fieldMappings.addFieldMap(fMap)
arcpy.Merge_management(tablesList, "merged.dbf", fieldMappings) The output was the same. It seems I have merely replicated the built-in Merge_management. I would be most grateful for any advice on modifying the script to generate the merged output I am after.
... View more
03-18-2019
03:53 AM
|
0
|
13
|
1623
|
POST
|
Yes, I was beginning to think memory management might be a problem. I have taken your advice to simplify my test system. It is now just a simple loop summing all the rasters. The erratic behaviour appears to have been resolved by using a 64-bit version of Python.
... View more
06-18-2018
12:41 AM
|
1
|
4
|
1141
|
POST
|
Hi Dan. Thanks for your prompt response. It was working, as far as the path definition was concerned (see the '\\' at the end of the inRasDir and outRasDir definitions). It was generating an output raster in the directory I expected and I could view it in ArcMap. I have taken your advice and tried both the raw formatting and forward slashes but the outcome is the same. I can produce an output raster from up to three inputs but not four. If it fails with four, and I immediately try with three, it fails. If I wait for about a minute, without altering the code, it succeeds with three.
... View more
06-17-2018
01:00 AM
|
0
|
6
|
1141
|
Title | Kudos | Posted |
---|---|---|
1 | 06-18-2018 12:41 AM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:25 AM
|