|
POST
|
Hello, I have the following script. It works as expected, but it takes around 1.5 seconds to run each time. Doesn't seem to matter if it's running against 20 parcels or 5000. Are there any suggestions as to how I can improve the performance of this script. Thanks import arcpy
import numpy as np
try:
arcpy.env.overwriteOutput = True
#Create in memory tables:
arcpy.CreateTable_management("in_memory", "tableSelRecs")
arcpy.AddField_management(r'in_memory\tableSelRecs', "USE_CATEGORY", "TEXT", field_length=35)
arcpy.AddField_management(r'in_memory\tableSelRecs', "SHAPE_Area", "Double")
arcpy.CreateTable_management("in_memory", "tableSumRecs")
arcpy.AddField_management(r'in_memory\tableSumRecs', "USE_CATEGORY", "TEXT", field_length=35)
arcpy.AddField_management(r'in_memory\tableSumRecs', "SHAPE_Area", "Double")
mxd = arcpy.mapping.MapDocument('CURRENT')
df = mxd.activeDataFrame
totalArea = 0
totalResArea = 0
totalParcels = 0
totalResParcels = 0
lu_layer = arcpy.mapping.ListLayers(mxd, "LandUse_UseCategory", df)[0]
#use da.SearchCursor to access necessary fields from selected records
with arcpy.da.SearchCursor(lu_layer,['USE_CATEGORY','SHAPE@AREA']) as cursor:
#Setup insert cursor to insert selected polygons into tableSelRecs
insCursor = arcpy.da.InsertCursor(r'in_memory\tableSelRecs', ['USE_CATEGORY','SHAPE_Area'])
#Isert selected rows into insCursor;
#Get total area of selected records;
#Get a count of total parcels
for row in cursor:
if row[0] != 'Invalid':
insCursor.insertRow((row[0],row[1]))
totalArea += row[1]
totalParcels += 1
#If the block is of a 'Residential' USE_CATEGORY type then add that value into the Residential DI calculation variable
if row[0] in('Residential - Low Density','Residential - Medium Density','Residential - High Density'):
totalResArea += row[1]
totalResParcels += 1
#use summarystatistics against tableSelRecs to get totals per USE_CATEGORY and write results to tableSumRecs
arcpy.Statistics_analysis(r'in_memory\tableSelRecs', r'in_memory\tableSumRecs', [["SHAPE_Area", "SUM"]], "USE_CATEGORY")
with arcpy.da.SearchCursor(r'in_memory\tableSumRecs',['USE_CATEGORY','SUM_SHAPE_Area']) as selCur:
#list to store calculation for each USE_CATEGORY value used in DI calculation
interimValue = [0]
#list to store calculation for each USE_CATEGORY value used in RDI calculation
interimResValue = [0]
for sRow in selCur:
arcpy.AddMessage('USE_CATEGORY: {}; AREA: {}'.format(sRow[0],sRow[1]))
#divide the USE_CATEGORY area by the total community area then square that number & append it to the list
interimValue.append(np.square(sRow[1]/totalArea))
if sRow[0] in('Residential - Low Density','Residential - Medium Density','Residential - High Density'):
interimResValue.append(np.square(sRow[1]/totalResArea))
#sum the values in the list then subtract that value from 1 to get the DI for the selected blocks
DI = 1 - sum(interimValue)
#sum the values in the list then subtract that value from 1 to get the RDI for the selected blocks
RDI = 1 - sum(interimResValue)
arcpy.AddMessage('Diversity Index: ' + str(DI))
arcpy.AddMessage('Residential Diversity Index: ' + str(RDI))
arcpy.AddMessage('Total Blocks Selected: ' + str(totalParcels))
arcpy.AddMessage('Total Blocks Area: ' + str(totalArea))
arcpy.AddMessage('Total Res Blocks Selected: ' + str(totalResParcels))
arcpy.AddMessage('Total Res Blocks Area: ' + str(totalResArea))
arcpy.AddMessage('NOTE: Any blocks with a use category of Invalid are not included in any calculations.')
#Delete in memory tables
arcpy.Delete_management(r'in_memory\tableSelRecs')
arcpy.Delete_management(r'in_memory\tableSumRecs')
except arcpy.ExecuteError:
print(arcpy.GetMessages(2))
... View more
02-05-2018
10:25 AM
|
0
|
20
|
9812
|
|
POST
|
So as a quick update. The addin continued to not display. And then all of a sudden one time today when the user opened the mxd, there the toolbar was docked and displaying. No ideas why, but it's working. Weird.
... View more
12-08-2017
11:36 AM
|
0
|
1
|
1727
|
|
POST
|
Hi Rebecca, The addin was installed by the user. Chris
... View more
12-07-2017
01:28 PM
|
0
|
0
|
1727
|
|
POST
|
Hello all, I created a python addin for ArcMap which works fine for me. Every time I open ArcMap the addin is displayed properly, ready to go. Yesterday I tested installing it for one of the end users. It installed fine, but it doesn't display. You have to right click the toolbar area then check the addin, then it shows up. Every time ArcMap is opened it's necessary to right click and check the toolbar. Any ideas why it isn't just always displaying as it does for myself? Thanks!
... View more
12-07-2017
12:52 PM
|
0
|
4
|
1844
|
|
POST
|
I've had no opportunity to date to look at doing this using dictionaries, but summary statistics is working great. So I think I'll just leave it at that. Thanks everyone!
... View more
11-29-2017
08:38 AM
|
0
|
0
|
7732
|
|
POST
|
What version of arcmap is pandas bundled? I’m learning pandas right now and find the documentation tough. At least there’s stackexchange.
... View more
11-14-2017
11:25 AM
|
0
|
1
|
7732
|
|
POST
|
Yes, summary statistics. I'm not sure why that didn't pop into my head as I've already used it previously. I think I was focused more on the list/dictionary. I'm working on using summary statistics now and trying to use in_memory tables. Once that's working I think I'll do it using the defaultdict, just for the learning. I'll update the thread as things progress. Thanks all.
... View more
11-14-2017
08:13 AM
|
0
|
0
|
7732
|
|
POST
|
Hello everyone, I have some data that looks like the attached pic. There's got to be an easy way to get a total area for each USE_CATEGORY and put the totals into a list or dictionary. Not sure if I need to maintain which total relates to which USE_CATEGORY. I don't need it for the current calculation I want to do, but may be worthwhile if I want to write it out to some external format later. Thanks for any help/pointers.
... View more
11-13-2017
01:18 PM
|
0
|
9
|
8823
|
|
POST
|
One last note. Even though in the Python Add-In Wizard I did select `Load Automatically` for the extension. It did not load the extension. It is listed in the Extensions list but is not being loaded automatically. I checked it out with both Windows 7 and 10, no difference.
... View more
09-20-2017
02:17 PM
|
0
|
0
|
1469
|
|
POST
|
Ok, figured this one out. It seems that before the extension will work you actually have to load it from Customize > Extensions (duh!):
... View more
09-19-2017
09:32 AM
|
1
|
0
|
1469
|
|
POST
|
Figured it out. The button createimagesbtn, I was referencing it as createimagebtn. So the code was puking out before it ever got to cleanupdatabtn. Only noticed it by accident when I happened to have the python window open in arcmap and the error showed there.
... View more
09-19-2017
08:05 AM
|
0
|
0
|
2425
|
|
POST
|
Hello, I have some code that I want to execute when an mdx file opens/closes. I'm trying to do it through an application extension because I also have a python addin which will be used with these files, so figure it makes sense to use the extension also. Before even trying to get my code working in the extension I just want to simply present a message via python addin messagebox and/or writing a message to the python window in arcmap. And basically this isn't working. Here is the code I'm trying on open/close: class MXDWatcher(object):
"""Implementation for PlanningTools_addin.mxdwatcher (Extension)"""
def __init__(self):
# For performance considerations, please remove all unused methods in this class.
self.enabled = True
def openDocument(self):
#pass
print('open doc')
pythonaddins.MessageBox('open doc','title',0)
def closeDocument(self):
#pass
print('close doc')
pythonaddins.MessageBox('close doc','title',0) Something else I notice in the Python Add-In Wizard, the "Methods to Implement" do not stay checked after I "Save" and close the Python Add-In Wizard. I'm assuming this is fine and that the methods I check are only referenced once when the wizard saves and creates those code blocks in the .py file. Any ideas on what I've got wrong here appreciated. Thanks, Chris
... View more
09-19-2017
07:37 AM
|
0
|
2
|
1643
|
|
POST
|
I got a script working so thought I'd share it. Seems to get the job done. Down the road when I have a better understanding of pandas I think I'll give that a try as it does seem the dataframe is the way to go. import arcpy
import numpy as np
updateFc = 'X:/gis/ludiversity/ludiversity.gdb/CommunityStats'
updateFields = ['NAME','SUM_Shape_Area','DIVERSITY_INDEX']
searchFc = 'X:/gis/ludiversity/ludiversity.gdb/Community_UseCategoryStats'
searchFields = ['NAME','USE_CATEGORY','SUM_Shape_Area']
with arcpy.da.UpdateCursor(updateFc,updateFields) as uCur:
#loop through each community record in updateFc
for uRow in uCur:
whereClause = "NAME = '" + uRow[0] + "'"
with arcpy.da.SearchCursor(searchFc,searchFields,whereClause) as selCur:
interimValue = [0] #list to store calculation for each USE_CATEGORY value
for sRow in selCur:
#divide the USE_CATEGORY area by the total community area then square that number & append it to the list
interimValue.append(np.square(sRow[2]/uRow[1]))
#sum the values in the list then subtract that value from 1 to get the diversity index for the community
uRow[2]= 1 - sum(interimValue)
uCur.updateRow(uRow)
... View more
08-28-2017
02:49 PM
|
0
|
0
|
2067
|
|
POST
|
I actually used Summary Statistics to create the table that has the 1495 records in it. The original table had around 35000 records. I used the Summary Statistics to group by NAME and USE_CATEGORY and SUM Area. Where I'm stumped with now is how to write the code to pull the records for each community, complete the calculation then move onto the next community in the list. Thanks!
... View more
08-23-2017
01:15 PM
|
0
|
2
|
2067
|
|
POST
|
Hello everyone, I have a feature class which looks like this: What I need to do is loop through each group of records in the NAME field where the value is the same (these are community names) and perform a calculation which is based on the Area values for each USE_CATEGORY and the Sum of all USE_CATEGORY Area values (which will give us the total area for the community). I think the following excel snapshot outlines the calculation to be done against each community (NAME): The end goal is to create a new table that will show the community along with the calculated diversity index for that community: I'm thinking some sort of loop would be used to select the records for each community, but I'm not sure of the best way to go about that and also how do you move on to select the records for the next community. Looking for some guidance and ideas on how to best get this done. Thanks everyone, Chris
... View more
08-23-2017
12:21 PM
|
0
|
4
|
2343
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 11-10-2020 03:34 PM | |
| 1 | 02-23-2021 11:53 AM | |
| 1 | 02-16-2021 09:15 AM | |
| 1 | 12-10-2020 07:46 AM | |
| 1 | 01-18-2019 09:54 AM |
| Online Status |
Offline
|
| Date Last Visited |
04-22-2021
04:31 PM
|