Select to view content in your preferred language

Keeping Empty ShapeFiles?

886
6
05-07-2018 10:32 PM
AaronChristiansen
Emerging Contributor

Hello All,

I am trying to create a tool in python that can potentially find areas of wind turbines by county. I have the correct process down, but in some counties there are parameters that don't exist. So for example I take into account airports, hydrology, slope, roads, urban areas, and more. But what if an area does not have that parameter ie. no military zones in some counties. But I have a code that requires the military area. Is there a way to use the tool so that it can ignore certain parameters when there is no data? Below is the piece of code that is giving me issues. I would still like to use this code, but I need it to work when there is no data in some of the shape files. I feel there is a way to do this without having to write a ton of if-then blocks. 

Thanks in advance!

arcpy.analysis.Union([Airports, Native, Parks, BufRail, Urban, BufRoads, Forests, Military, BufStreams, BufWaterBodies], Workings+"Avoid.shp", "ALL", None, "GAPS")

-Aaron

0 Kudos
6 Replies
AdrianWelsh
MVP Honored Contributor

Aaron,

This thread might be best placed in the Python‌ place.

Would it be acceptable to have null values in your code and/or attributes?

0 Kudos
AaronChristiansen
Emerging Contributor

Hey Adrian, 

Thank you for your feedback. Null values are fine for this analysis.

0 Kudos
XanderBakker
Esri Esteemed Contributor

How do you specify the input for the tool? Is each layer a separate parameter or do you let the user select a list of featureclasses with a single control?

0 Kudos
AaronChristiansen
Emerging Contributor

So each input has been specified in precursor code using a gui and each layer is a separate parameter.

0 Kudos
XanderBakker
Esri Esteemed Contributor

If each has it's own variable, you can validate to see if it is empty and exclude it from the list. Look at the snippet below:

Airports = r'blah 1'
Native = r'blah 2'
Parks = None  # not sure if it will be an empty string or null
BufRail = r'blah 4'
Urban = r'blah 5'
BufRoads = ''  # not sure if it will be an empty string or null
Forests = r'blah 6'
Military = r'blah 7'
BufStreams = None  # not sure if it will be an empty string or null
BufWaterBodies = r'blah 8'

lst = [Airports, Native, Parks, BufRail, Urban, BufRoads, Forests, Military, BufStreams, BufWaterBodies]

new_lst1 = [a for a in lst if a != '']
new_lst2 = [a for a in lst if a != None]
new_lst3 = [a for a in lst if a != None and a != '']

print lst
print new_lst1
print new_lst2
print new_lst3
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This will yield:

['blah 1', 'blah 2', None, 'blah 4', 'blah 5', '', 'blah 6', 'blah 7', None, 'blah 8']
['blah 1', 'blah 2', None, 'blah 4', 'blah 5', 'blah 6', 'blah 7', None, 'blah 8']
['blah 1', 'blah 2', 'blah 4', 'blah 5', '', 'blah 6', 'blah 7', 'blah 8']
['blah 1', 'blah 2', 'blah 4', 'blah 5', 'blah 6', 'blah 7', 'blah 8']

Use the new list in the Union:

arcpy.analysis.Union(new_lst3, Workings+"Avoid.shp", "ALL", None, "GAPS")

You will probably have to check to see if the list has at least 2 items to avoid problems.

AaronChristiansen
Emerging Contributor

Thank you so much!

This will definitely help me!

Best,

Aaron

0 Kudos