-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtpkxToPortal.py
More file actions
107 lines (75 loc) · 3.28 KB
/
tpkxToPortal.py
File metadata and controls
107 lines (75 loc) · 3.28 KB
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
print("start script")
import arcpy
import os
from arcgis.gis import GIS
from datetime import datetime
arcpy.env.overwriteOutput = True
# Input Variables! #############################################################
rasterType = "TYPE OF RASTER DATA"
subArea = "CITY?"
area = "PROVINCE"
inputPolygons = r"INPUT POLY FEATURES"
inputPolyNameField = 'FIELD IN INPUT POLYS TO USE FOR NAMING'
detail = 16
templateAPRXFile = r"TEMPLATE APRX WITH ONE EMPTY MAP NAMED Map"
inputRasterLyr = r"LYR OR LYRX with RASTER DATA"
outputFolder = r"FOLDER TO DO THE WORK"
enableUploadToPortal = True
################################################################################
# Step 1 Create Temp APRX, add mosaic dataset, and save
aprx = arcpy.mp.ArcGISProject(templateAPRXFile)
aprx.saveACopy(r"{}\temp.aprx".format(outputFolder))
aprx = arcpy.mp.ArcGISProject(r"{}\temp.aprx".format(outputFolder))
map1 = aprx.listMaps("Map")[0]
lyrx1 = arcpy.mp.LayerFile(inputRasterLyr)
map1.addLayer(lyrx1)
map1.spatialReference = arcpy.SpatialReference(102100)
aprx.save()
# Step 2 Loop through poly features and create one tpkx per poly using raster
with arcpy.da.SearchCursor(inputPolygons,inputPolyNameField) as cursor:
for row in cursor:
print("starting {} at {}".format(row[0],datetime.now().strftime('%H:%M')))
query = "{} = '{}'".format(inputPolyNameField,row[0])
outTPKX = "{}\{}_{}_{}_{}.tpkx".format(outputFolder,rasterType,row[0],subArea,area)
arcpy.MakeFeatureLayer_management(inputPolygons,"AREA",query)
arcpy.management.CreateMapTilePackage(
in_map=map1,
service_type="ONLINE",
output_file=outTPKX,
format_type="MIXED",
level_of_detail=detail,
service_file=None,
summary="",
tags="",
extent="DEFAULT",
compression_quality=75,
package_type="tpkx",
min_level_of_detail=0,
area_of_interest="AREA",
create_multiple_packages=None,
output_folder=None)
print("finished {} at {}".format(row[0],datetime.now().strftime('%H:%M')))
# Step 3 upload TPKX to active Portal and delete temp tpkx file ################
if enableUploadToPortal == True:
gis = GIS("home")
folder = 'TPKX {} {}, {}'.format(rasterType,subArea,area)
try:
gis.content.create_folder(folder)
except:
pass
print("uploading {}".format(outTPKX))
upload_properties={
'title':'{} {}, {}, {}'.format(rasterType,row[0],subArea,area),
'summary':'{} {}, {}, {}'.format(rasterType,row[0],subArea,area),
'description':'{} {}, {}, {}'.format(rasterType,row[0],subArea,area),
'tags':'{}, {}, {}, {}'.format(rasterType,row[0],subArea,area)}
tpk_item = gis.content.add(
item_properties=upload_properties,
data=outTPKX,
folder=folder)
print("uploaded tpkx")
os.remove(outTPKX)
else:
pass
os.remove(r"{}\temp.aprx".format(outputFolder))
print("finished script")