-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharcpyDownloadMapService.py
More file actions
148 lines (100 loc) · 4.75 KB
/
arcpyDownloadMapService.py
File metadata and controls
148 lines (100 loc) · 4.75 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
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
################################################################################
#
# Version: 1.0
#
# Request a feature class from a arcgis rest api map service. Takes a URL and
# output feature class as input
#
# Author: https://github.com/jtgis
#
# Date: 19 mar 2021
#
################################################################################
import json
import arcpy
import requests
import tempfile
import shutil
url = "www.somemapserviceurl.com/1"
def downloadRestFeatures(url,queryLayer,query,outLocation,outName):
"""
#https://gis.stackexchange.com/questions/324513/converting-rest-service-to-file-geodatabase-feature-class
can export a map service to fc optionally add a query or selection layer
to limit ouput or leave those as "" to get the whole thing
returns the new fc
"""
if not query:
query = '1=1'
data1 = requests.get("{}/query".format(url), {"where": query,
"f": "json",
"returnCountOnly": True}).json()
print(data1['count'])
data2 = requests.get("{}/query".format(url), {"where": query,
"f": "json",
"returnIdsOnly": True}).json()
oidList = data2['objectIds']
oidFieldName = data2['objectIdFieldName']
data3 = requests.get(url, {"where": query,
"f": "json",
"returnIdsOnly": True}).json()
n = data3['maxRecordCount']
print(n)
oidList.sort()
list_of_groups = izip_longest(*(iter(oidList),) * n)
x=1
for group in list_of_groups:
time.sleep(2)
print(x)
x=x+1
group = [i for i in group if i is not None]
firstNUMB = group[0]
lastNUMB = group[-1]
queryParam = '{} BETWEEN {} AND {}'.format(oidFieldName,firstNUMB,lastNUMB)
params = {'where': queryParam,
'outFields': '*',
'f': 'pjson',
'returnGeometry': True}
if queryLayer:
spatial_ref = arcpy.Describe(queryLayer).spatialReference
dissolved = arcpy.Dissolve_management(queryLayer,"dissolved")
arcpy.AddGeometryAttributes_management(dissolved,
"EXTENT")
with arcpy.da.SearchCursor(dissolved, ["OID@", "EXT_MIN_X", "EXT_MIN_Y","EXT_MAX_X", "EXT_MAX_Y"]) as sCur:
for row in sCur:
minX, minY, maxX, maxY = row[1], row[2], row[3], row[4]
extent = (str(minX) +","+ str(minY) +","+ str(maxX) +","+ str(maxY))
params = {'where': queryParam,
'geometry': extent,
'geometryType':
'esriGeometryEnvelope ',
'inSR': spatial_ref,
'spatialRel':
'esriSpatialRelIntersects',
'outFields': '*',
'f': 'pjson',
'returnGeometry': True}
r = requests.get("{}/query".format(url), params)
data = r.json()
dirpath = tempfile.mkdtemp()
json_path = r"{}\mapService.json".format(dirpath)
with open(json_path, 'w') as f:
json.dump(data, f)
f.close()
r.close()
arcpy.JSONToFeatures_conversion(json_path,
"in_memory/singleFeature")
if arcpy.Exists("in_memory/allFeatures"):
arcpy.management.Append(["in_memory/singleFeature"],
"in_memory/allFeatures","TEST")
else:
arcpy.conversion.FeatureClassToFeatureClass("in_memory/singleFeature",
"in_memory",
"allFeatures")
arcpy.Delete_management("in_memory/singleFeature")
arcpy.conversion.FeatureClassToFeatureClass("in_memory/allFeatures",
outLocation,outName)
shutil.rmtree(dirpath)
arcpy.Delete_management("in_memory/allFeatures")
return "{}\{}".format(outLocation,outName)
tempFc = downloadRestFeatures(url,"","","in_memory","test")
print [f.name for f in arcpy.ListFields(tempFc)]