-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcibule_analysis_TOOL.py
156 lines (131 loc) · 14.7 KB
/
cibule_analysis_TOOL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-11-06 11:17:09
"""
import arcpy
def Model(): # Model
# To allow overwriting outputs change overwriteOutput option to True.
arcpy.env.overwriteOutput = False
Workspace = "C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb"
INPUT_Corine_ = "U2018_CLC2018_V2020_20u_Clip"
INPUT_FLOODS = "C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\CIBULE\\D01_ZaplUzemi5Vody_Clip\\D01_ZaplUzemi5Vody_Clip.shp"
INPUT_SOILS = "BPEJ_2022_3035"
INPUT_DMR = arcpy.Raster("DMR_reprojected")
# Process: Select (Select) (analysis)
Agricultural_areas = fr"{Workspace}\corine_selection"
arcpy.analysis.Select(in_features=INPUT_Corine_, out_feature_class=Agricultural_areas, where_clause="Code_18 LIKE '2%'")
# Process: Intersect (Intersect) (analysis)
flooded_areas = fr"{Workspace}\flooded_areas"
arcpy.analysis.Intersect(in_features=[[Agricultural_areas, ""], [INPUT_FLOODS, ""]], out_feature_class=flooded_areas, join_attributes="ALL", cluster_tolerance="", output_type="INPUT")
# Process: Clip (Clip) (analysis)
soils_clip = fr"{Workspace}\soils_clip"
arcpy.analysis.Clip(in_features=INPUT_SOILS, clip_features=Agricultural_areas, out_feature_class=soils_clip, cluster_tolerance="")
# Process: Add Field (2) (Add Field) (management)
soils_add_type = arcpy.management.AddField(in_table=soils_clip, field_name="soil_type", field_type="FLOAT", field_precision=None, field_scale=None, field_length=None, field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")[0]
# Process: Calculate Field (2) (Calculate Field) (management)
soils_type = arcpy.management.CalculateField(in_table=soils_add_type, field="soil_type", expression="type(!B5!)", expression_type="PYTHON3", code_block="""def type(b5):
if (b5[1] == '0')or(b5[1]=='1')and(b5[2]<='7'):
return 0.3
elif (b5[1] == '1')or(b5[1] == '2')and(b5[2] == '8')or(b5[2] == '9')or(b5[2] == '0'):
return 9999
elif (b5[1] == '2')and(b5[2] <= '4'):
return 0.2
elif (b5[1] == '2')and(b5[2]>='5')and(b5[2] <= '9')or(b5[1] == '3')and(b5[2]>='0')and(b5[2]<='3'):
return 0.1
elif (b5[1] == '3')and(b5[2]>='4')and(b5[2]<='8'):
return 0
elif (b5[1] == '3')and (b5[2]=='9')or (b5[1]=='4')and(b5[2]>='0')and(b5[2]<='1'):
return 0.1
elif (b5[1]=='4') and (b5[2]>='2') and (b5[2]<='4'):
return 0
elif (b5[1]=='4') and (b5[2]>='4') and (b5[2]<='9') or (b5[1]=='5') and (b5[2]>='0')and (b5[2]<='4'):
return 0
else:
return 9999""", field_type="TEXT", enforce_domains="NO_ENFORCE_DOMAINS")[0]
# Process: Add Field (Add Field) (management)
soils_clip_2_ = arcpy.management.AddField(in_table=soils_type, field_name="precip", field_type="FLOAT", field_precision=None, field_scale=None, field_length=None, field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")[0]
# Process: Calculate Field (Calculate Field) (management)
soils_precipitation = arcpy.management.CalculateField(in_table=soils_clip_2_, field="precip", expression="uhrn(!B5!)", expression_type="PYTHON3", code_block="""def uhrn(b5):
if (b5[0]=='0'):
return 0.2
elif (b5[0]=='1'):
return 0.1
elif (b5[0]=='2'):
return 0.2
elif (b5[0]=='3'):
return 0.15
elif (b5[0]=='4'):
return 0.15
elif (b5[0]=='5'):
return 0.15
elif (b5[0]=='6'):
return 0.05
elif (b5[0]=='7'):
return 0.1
elif (b5[0]=='8'):
return 0.05
elif (b5[0]=='9'):
return 0""", field_type="TEXT", enforce_domains="NO_ENFORCE_DOMAINS")[0]
# Process: Clip Raster (Clip Raster) (management)
DMR_clip = fr"{Workspace}\DMR_clip"
arcpy.management.Clip(in_raster=INPUT_DMR, rectangle="4675769,8106 2885292,1525 4785178,1554 2986953,7451", out_raster=DMR_clip, in_template_dataset=Agricultural_areas, nodata_value="32767", clipping_geometry="NONE", maintain_clipping_extent="NO_MAINTAIN_EXTENT")
DMR_clip = arcpy.Raster(DMR_clip)
# Process: Raster to Polygon (Raster to Polygon) (conversion)
dmr_polyg = fr"{Workspace}\dmr_polyg"
with arcpy.EnvManager(outputMFlag="Disabled", outputZFlag="Disabled"):
arcpy.conversion.RasterToPolygon(in_raster=DMR_clip, out_polygon_features=dmr_polyg, simplify="SIMPLIFY", raster_field="Value", create_multipart_features="SINGLE_OUTER_PART", max_vertices_per_feature=None)
# Process: Spatial Join (Spatial Join) (analysis)
aoi_joined = fr"{Workspace}\aoi_joined"
arcpy.analysis.SpatialJoin(target_features=soils_precipitation, join_features=dmr_polyg, out_feature_class=aoi_joined, join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_ALL", field_mapping="B5 \"B5\" true true false 5 Text 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,B5,0,5;BPEJ \"BPEJ\" true true false 7 Text 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,BPEJ,0,7;TO_ZPF \"TO_ZPF\" true true false 1 Text 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,TO_ZPF,0,1;Cena \"Cena\" true true false 4 Float 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,Cena,-1,-1;LastUpdate \"LastUpdate\" true true false 8 Date 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,LastUpdate,-1,-1;Shape_Length \"Shape_Length\" true true true 8 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,Shape_Length,-1,-1;Shape_Area \"Shape_Area\" true true true 8 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,Shape_Area,-1,-1;Shape_length_1 \"Shape_length\" true true false 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,Shape_length,-1,-1;Shape_area_1 \"Shape_area\" true true false 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,Shape_area,-1,-1;soil_type \"soil_type\" true true false 0 Float 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,soil_type,-1,-1;precip \"precip\" true true false 0 Float 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\soils_clip,precip,-1,-1;Shape_Length_12 \"Shape_Length\" false false true 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\dmr_polyg,Shape_Length,-1,-1;Shape_Area_12 \"Shape_Area\" false false true 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\dmr_polyg,Shape_Area,-1,-1;ID \"ID\" true true false 0 Long 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\dmr_polyg,ID,-1,-1;GRIDCODE \"GRIDCODE\" true true false 0 Long 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\dmr_polyg,GRIDCODE,-1,-1", match_option="INTERSECT", search_radius="", distance_field_name="")
# Process: Slope (Slope) (3d)
Slope_DMR = fr"{Workspace}\Slope_DMR"
arcpy.ddd.Slope(in_raster=DMR_clip, out_raster=Slope_DMR, output_measurement="DEGREE", z_factor=1, method="PLANAR", z_unit="METER")
Slope_DMR = arcpy.Raster(Slope_DMR)
# Process: Int (Int) (sa)
DMR_slope_int = fr"{Workspace}\DMR_slope_int"
Int = DMR_slope_int
DMR_slope_int = arcpy.sa.Int(in_raster_or_constant=Slope_DMR)
DMR_slope_int.save(Int)
# Process: Raster to Polygon (2) (Raster to Polygon) (conversion)
DMR_slope_polyg = fr"{Workspace}\DMR_slope_polyg"
with arcpy.EnvManager(outputMFlag="Disabled", outputZFlag="Disabled"):
arcpy.conversion.RasterToPolygon(in_raster=DMR_slope_int, out_polygon_features=DMR_slope_polyg, simplify="SIMPLIFY", raster_field="VALUE", create_multipart_features="SINGLE_OUTER_PART", max_vertices_per_feature=None)
# Process: Spatial Join (2) (Spatial Join) (analysis)
all_joined = fr"{Workspace}\all_joined"
arcpy.analysis.SpatialJoin(target_features=aoi_joined, join_features=DMR_slope_polyg, out_feature_class=all_joined, join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_ALL", field_mapping="Join_Count \"Join_Count\" true true false 0 Long 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,Join_Count,-1,-1;TARGET_FID \"TARGET_FID\" true true false 0 Long 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,TARGET_FID,-1,-1;B5 \"B5\" true true false 5 Text 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,B5,0,5;BPEJ \"BPEJ\" true true false 7 Text 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,BPEJ,0,7;TO_ZPF \"TO_ZPF\" true true false 1 Text 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,TO_ZPF,0,1;Cena \"Cena\" true true false 4 Float 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,Cena,-1,-1;LastUpdate \"LastUpdate\" true true false 8 Date 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,LastUpdate,-1,-1;Shape_Length \"Shape_Length\" true true true 8 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,Shape_Length,-1,-1;Shape_Area \"Shape_Area\" true true true 8 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,Shape_Area,-1,-1;Shape_length_1 \"Shape_length\" true true false 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,Shape_length_1,-1,-1;Shape_area_1 \"Shape_area\" true true false 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,Shape_area_1,-1,-1;soil_type \"soil_type\" true true false 0 Float 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,soil_type,-1,-1;precip \"precip\" true true false 0 Float 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,precip,-1,-1;Shape_Length_12 \"Shape_Length\" false false true 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,Shape_Length_12,-1,-1;Shape_Area_12 \"Shape_Area\" false false true 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,Shape_Area_12,-1,-1;ID \"ID\" true true false 0 Long 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,ID,-1,-1;GRIDCODE \"GRIDCODE\" true true false 0 Long 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\aoi_joined,GRIDCODE,-1,-1;Shape_Length_12_13 \"Shape_Length\" false false true 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\DMR_slope_polyg,Shape_Length,-1,-1;Shape_Area_12_13 \"Shape_Area\" false false true 0 Double 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\DMR_slope_polyg,Shape_Area,-1,-1;ID_1 \"ID\" true true false 0 Long 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\DMR_slope_polyg,ID,-1,-1;GRIDCODE_1 \"GRIDCODE\" true true false 0 Long 0 0,First,#,C:\\Users\\dance\\OneDrive\\Dokumenty\\ArcGIS\\Projects\\CIBULE\\cibulezkouska.gdb\\DMR_slope_polyg,GRIDCODE,-1,-1", match_option="INTERSECT", search_radius="", distance_field_name="")
# Process: Add Field (3) (Add Field) (management)
all_joined_2_ = arcpy.management.AddField(in_table=all_joined, field_name="elevation", field_type="FLOAT", field_precision=None, field_scale=None, field_length=None, field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")[0]
# Process: Calculate Field (3) (Calculate Field) (management)
all_joined_3_ = arcpy.management.CalculateField(in_table=all_joined_2_, field="elevation", expression="elev(!GRIDCODE!)", expression_type="PYTHON3", code_block="""def elev(g):
if g >= 0 and g <= 400:
return 0.1
elif g > 400 and g <= 800:
return 0.05
elif g > 800 and g <= 1200:
return 0
else:
return 9999""", field_type="TEXT", enforce_domains="NO_ENFORCE_DOMAINS")[0]
# Process: Add Field (4) (Add Field) (management)
all_joined_4_ = arcpy.management.AddField(in_table=all_joined_3_, field_name="slope_index", field_type="FLOAT", field_precision=None, field_scale=None, field_length=None, field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")[0]
# Process: Calculate Field (4) (Calculate Field) (management)
all_joined_5_ = arcpy.management.CalculateField(in_table=all_joined_4_, field="slope_index", expression="slope_idx(!GRIDCODE_1!)", expression_type="PYTHON3", code_block="""def slope_idx(s):
if s >=0 and s<=3:
return 0.4
elif s>3 and s<= 7:
return 0.3
elif s>7 and s<= 12:
return 0.2
elif s>12 and s<= 17:
return 0.1
elif s>17 and s<= 25:
return 0
else:
return 9999""", field_type="TEXT", enforce_domains="NO_ENFORCE_DOMAINS")[0]
# Process: Add Field (5) (Add Field) (management)
all_joined_6_ = arcpy.management.AddField(in_table=all_joined_5_, field_name="index", field_type="FLOAT", field_precision=None, field_scale=None, field_length=None, field_alias="", field_is_nullable="NULLABLE", field_is_required="NON_REQUIRED", field_domain="")[0]
# Process: Calculate Field (5) (Calculate Field) (management)
all_joined_7_ = arcpy.management.CalculateField(in_table=all_joined_6_, field="index", expression="!slope_index!+!soil_type!+!precip!+!elevation!", expression_type="PYTHON3", code_block="", field_type="TEXT", enforce_domains="NO_ENFORCE_DOMAINS")[0]
if __name__ == '__main__':
# Global Environment settings
with arcpy.EnvManager(scratchWorkspace=r"C:\Users\dance\OneDrive\Dokumenty\ArcGIS\Projects\CIBULE\CIBULE.gdb", workspace=r"C:\Users\dance\OneDrive\Dokumenty\ArcGIS\Projects\CIBULE\CIBULE.gdb"):
Model()