Using model builder for summary statistics and setting ranges to group by

550
4
Jump to solution
03-23-2019 04:50 AM
CiaranHiggins1
New Contributor II

Hi

I am trying to perform a summary on a poly line dataset. This data contains an attribute for the year the section was laid and the diameter of the section. I want to sum the length of these sections group them by both year and band. However I want to group the bands and years e.g  year laid 2000-2010 and diameter 150-300 mm. I have created a model which does what I want but I was wondering if there was an easier way to carry out this task. I was also wondering if there was a way to modify the model so that the user could can the year bands and diameters in a table of spreadsheet. I am new to the model builder. Maybe there is a better way to do what I am trying to do. See attached images. 

Thanks

Ciaran 

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

You can make an model input variable a multi value list, one variable for each of your classify variables.

create multi value model variable           multi value model builder variable

Then you can use that multi value list in Calculate Field, inside the code block splitting it into a Python list with “;”.split(). 

Here is a Calculate Field expression I came up with that seems to do what you want. Dan Patterson‌ numpy is pretty handy!

# Calculate Field expression, specifying to update field "class"
# Source field is the name of the field we want to classify
classify(!%Source field%!)

# Code block
# breaks is a multivalue model variable I created of type Double
import numpy as np
breaks = sorted([float(k) for k in "%breaks%".split(";")])
breaklab = ["<{}".format(breaks[0])]
breaklab += ["{} up to {}".format(breaks[k], breaks[k+1]) for k in range(len(breaks)-1)]
breaklab += [">={}".format(breaks[-1])]
breaks = np.array(breaks)
def classify(vv):
p = np.searchsorted(breaks, vv, 'right')
return breaklab[p]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

results of lookup

View solution in original post

4 Replies
curtvprice
MVP Esteemed Contributor

You can make an model input variable a multi value list, one variable for each of your classify variables.

create multi value model variable           multi value model builder variable

Then you can use that multi value list in Calculate Field, inside the code block splitting it into a Python list with “;”.split(). 

Here is a Calculate Field expression I came up with that seems to do what you want. Dan Patterson‌ numpy is pretty handy!

# Calculate Field expression, specifying to update field "class"
# Source field is the name of the field we want to classify
classify(!%Source field%!)

# Code block
# breaks is a multivalue model variable I created of type Double
import numpy as np
breaks = sorted([float(k) for k in "%breaks%".split(";")])
breaklab = ["<{}".format(breaks[0])]
breaklab += ["{} up to {}".format(breaks[k], breaks[k+1]) for k in range(len(breaks)-1)]
breaklab += [">={}".format(breaks[-1])]
breaks = np.array(breaks)
def classify(vv):
p = np.searchsorted(breaks, vv, 'right')
return breaklab[p]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

results of lookup

View solution in original post

CiaranHiggins1
New Contributor II

Curtis,

Thanks for the information but i must be doing something stupid. I have tried to simplify this to test. So i created a new feature class with the same structure as your example and with the same names. When i try to run the model i get an error about value type. I have set the to type double. See attached images which will hopefulyl help.

RegardsAttributesTable StructureModel LayoutVariablePythonError

Ciaran

0 Kudos
CiaranHiggins1
New Contributor II

I have got this working now thanks.

I didn't realise the coulmn that is being updated has to be of datatype text/string.

Thanks again

HussainMalik1
New Contributor III

Hi @curtvprice 

i know this is an old post but i am doing something similar, but instead of multivalue variable i am using Table of Values variable. and i need to split the variables so that i could use in them for field calculation. 

I have posted a question here in communities would love to pick your brains on the topic 

Thanks 

Hussain 

 

 

0 Kudos