-
Notifications
You must be signed in to change notification settings - Fork 3
/
OSMM_prep.py
148 lines (122 loc) · 6.12 KB
/
OSMM_prep.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
# Merges OSMM tiles from different folders and splits into LADs
# -----------------------------------------------------------------------------------------------------------------
import time
import arcpy
import os
import MyFunctions
arcpy.CheckOutExtension("Spatial")
print(''.join(["## Started on : ", time.ctime()]))
arcpy.env.overwriteOutput = True # Overwrites files
arcpy.env.qualifiedFieldNames = False # Joined fields will be exported without the join table name
arcpy.env.XYTolerance = "0.001 Meters"
# Location of the input tiles
# tile_folder = r"D:\cenv0389\OxCamArc\OSMM_update"
# tile_folder = r"D:\cenv0389\Oxon_GIS\GIS_data\2020_OSMM"
tile_folder = r"M:\urban_development_natural_capital\osmm_northern_powerhouse"
# Target feature class name
fc_name = "TopographicArea"
# Typo in name!
# fc_name = "Topgraphicarea"
# Define location for output gdb, which should be created in advance
# OSMM_out_gdb = r"D:\cenv0389\OxCamArc\OSMM.gdb"
OSMM_out_gdb = r"M:\urban_development_natural_capital\osmm_northern_powerhouse\OSMM_large_areas.gdb"
# Either copy the output (OSMM split into LADs) into a single gdb (LAD_gdb), or copy into separate gdbs for each LAD, in LAD_gdb_folder
separate_LAD_gdbs = True
LAD_gdb = r"D:\cenv0389\OxCamArc\OSMM_update.gdb"
LAD_gdb_folder = r"M:\urban_development_natural_capital\LADs"
# new_fc = "OSMM_Oxon_Aug2020"
new_fc = "OSMM_Overlaps"
OSMM_fc = new_fc
LAD_table = r"M:\urban_development_natural_capital\data.gdb\LADs"
LAD_name_field = "LAD_name_simple"
County_name_field = "County"
# LADs_included = ["Northumberland", "Allerdale", "Carlisle", "Newcastle upon Tyne", "North Tyneside", "South Tyneside", "Sunderland",
# "Gateshead", "County Durham", "Copeland", "Darlington", "Stockton-on-Tees", "Hartlepool", "Middlesbrough",
# "Redcar and Cleveland"]
# LADs_included = ["Pendle", "Burnley", "Rossendale", "Rochdale", "Manchester", "Trafford", "Salford", "Bury", "Hyndburn",
# "Ribble Valley", "Lancaster", "Wyre", "Barrow-in-Furness", "Blackpool", "Fylde", "Preston",
# "South Ribble", "Blackburn with Darwen", "Chorley", "Wigan", "West Lancashire", "Bolton", "Warrington",
# "Halton", "St Helens", "Knowsley", "Liverpool", "Wirral", "Sefton", "Cheshire West and Chester"]
# LADs_included = ["North Lincolnshire", "North East Lincolnshire", "East Riding of Yorkshire", "York", "Selby", "Wakefield",
# "Barnsley", "Doncaster", "Rotherham", "Sheffield"]
LADs_included = [ "Oldham", "Cheshire East", "Stockport", "Tameside", "Pendle", "Craven",
"South Lakeland", "Eden", "Scarborough", "Richmondshire", "Harrogate", "Ryedale", "Hambleton"]
# boundary = r"D:\cenv0389\Oxon_GIS\GIS_data\Oxfordshire.shp"
boundary = r"M:\urban_development_natural_capital\data.gdb\NP_boundary"
collate_tiles = False
merge_tiles = False
' Split into LADs OR trim to county boundary'
split_LADs = True
trim_county = False
delete_overlaps = True
if collate_tiles:
arcpy.env.workspace = tile_folder
folders = arcpy.ListFiles()
i = 0
for folder in folders:
print("Folder " + folder)
arcpy.env.workspace = os.path.join(tile_folder, folder)
subfolders = arcpy.ListFiles("mastermap*")
for subfolder in subfolders:
print (" Subfolder " + subfolder)
arcpy.env.workspace = os.path.join(tile_folder, folder, subfolder)
in_gdb_list = arcpy.ListFiles("*.gdb")
in_gdb = in_gdb_list[0]
print (" gdb " + in_gdb)
arcpy.env.workspace = in_gdb
i = i + 1
out_fc = os.path.join(OSMM_out_gdb, "OSMM_tile_" + folder)
print (" Copying to " + out_fc)
arcpy.CopyFeatures_management(fc_name, out_fc)
if merge_tiles:
print ("Merging tiles")
arcpy.env.workspace = OSMM_out_gdb
in_fcs = arcpy.ListFeatureClasses()
arcpy.Merge_management(in_fcs, new_fc)
print(''.join(["## Finished merge on : ", time.ctime()]))
# Split into LADS
# ------------------
if split_LADs:
LAD_names = []
LADs = arcpy.SearchCursor(LAD_table)
arcpy.env.workspace = OSMM_out_gdb
i=0
for LAD in LADs:
LAD_full_name = LAD.getValue(LAD_name_field)
if LAD_full_name in LADs_included:
i=i+1
print "Processing LAD number " + str(i) + " out of " + str(len(LADs_included))
LAD_name = LAD_full_name.replace(" ", "")
LAD_names.append(LAD_name)
print("Clipping OSMM for " + LAD_name)
arcpy.MakeFeatureLayer_management(LAD_table, "LAD_lyr")
arcpy.SelectLayerByAttribute_management("LAD_lyr", where_clause= LAD_name_field + "= '" + LAD_full_name + "'")
arcpy.MakeFeatureLayer_management(OSMM_fc, "OSMM_lyr")
arcpy.SelectLayerByLocation_management("OSMM_lyr", "INTERSECT", "LAD_lyr")
if separate_LAD_gdbs:
# Option for copying direct to LAD gdb (gdb needs to be set up first)
out_file = os.path.join(LAD_gdb_folder, LAD_name + ".gdb", "OSMM")
else:
# Option for copying to a single folder
out_file = os.path.join(LAD_gdb, "OSMM_" + LAD_name)
arcpy.Clip_analysis("OSMM_lyr", "LAD_lyr", out_file)
arcpy.Delete_management("LAD_lyr")
arcpy.Delete_management("OSMM_lyr")
if delete_overlaps:
print "Deleting overlaps"
arcpy.DeleteIdentical_management(out_file, ["Shape"])
MyFunctions.check_and_repair(out_file)
print(''.join(["## Finished ", LAD_full_name, " on : ", time.ctime()]))
if trim_county:
print("Clipping to " + boundary)
arcpy.MakeFeatureLayer_management(new_fc, "lyr")
arcpy.SelectLayerByLocation_management("lyr", "INTERSECT", boundary)
out_file = os.path.join(OSMM_out_gdb, new_fc + "_clip")
arcpy.Clip_analysis("lyr", boundary, out_file)
arcpy.Delete_management("lyr")
if delete_overlaps:
print "Deleting overlaps"
arcpy.DeleteIdentical_management(out_file, ["Shape"])
MyFunctions.check_and_repair(out_file)
print(''.join(["## Finished on : ", time.ctime()]))
exit()