Guest User

Untitled

a guest
Jan 25th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. from arcgis.gis import GIS
  2. gis = GIS()
  3. web_map = gis.content.get('<GUID value>')#where GUID value is the GUID I have in a list
  4.  
  5. web_map_layers = webMap(web_map)
  6. layers = web_map_layers.layers
  7.  
  8. import arcpy
  9. from arcpy import env
  10. import timeit
  11. import uuid
  12. from arcgis.gis import GIS
  13. from arcgis import features
  14. from arcgis.mapping import WebMap
  15. from collections import OrderedDict
  16. import json
  17. import urllib
  18. import http.client
  19.  
  20. start_time = timeit.default_timer()
  21.  
  22. #Constant values
  23. SURVEY_DB_WORKSPACE = r'C:Userssurvey_gdb.sde'
  24. #survey table server instance and owner prefix values
  25. SURVEY_DB_INSTANCE_AND_OWNER = 'mydb.dbo.'
  26. GEO_PROC_WORKSPACE = r"C:Usersgeo_proc_gdb.sde"
  27. #geo-proc server instance and owner prefix values
  28. GEO_PROC_DB_INSTANCE_AND_OWNER = 'mydb_gdb.DBO.'
  29. #rest services credentials
  30. USERNAME = 'user'
  31. PASSWORD = 'password'
  32. PORTAL_URL = 'https://mywebsite/portal'
  33. SERVERNAME = "mywebsite"
  34. PORT = ''#if using web adaptor, leave empty (modify tokenURL)
  35. TOKEN_URL = "https://mywebsite/server/admin/generateToken"
  36.  
  37. def main():
  38.  
  39. #set environment to the Survey database, loop through surveys table, grab WebMap GUID
  40. workspace = SURVEY_DB_WORKSPACE
  41. arcpy.env.workspace = workspace
  42. arcpy.env.overwriteOutput = True
  43.  
  44. surveys = SURVEY_DB_INSTANCE_AND_OWNER + 'Surveys'
  45. where_clause = "active = 1"
  46. fields = ['OID@', 'Name', 'Id', 'Progress']
  47. web_map_ids = OrderedDict()
  48.  
  49. with arcpy.da.SearchCursor(surveys, fields) as cursor:
  50. for row in cursor:
  51. web_map_ids[row[0]] = [row[1], row[2], row[3]]
  52.  
  53. #change environment to the geo-processing database
  54. workspace = GEO_PROC_WORKSPACE
  55.  
  56. #connect to db and set point and line features
  57. field = 'state'
  58. lines_backup = GEO_PROC_DB_INSTANCE_AND_OWNER + 'lines_backup'
  59. points_backup = GEO_PROC_DB_INSTANCE_AND_OWNER + 'points_backup'
  60. tmp_lines_split = GEO_PROC_DB_INSTANCE_AND_OWNER + 'tmp_lines_split'
  61. lines_split = GEO_PROC_DB_INSTANCE_AND_OWNER + 'lines_split'
  62. arcpy.env.workspace = workspace
  63. arcpy.env.overwriteOutput = True
  64.  
  65. #Define how to acquire a rest services token
  66. #from https://community.esri.com/thread/83654
  67.  
  68. def getToken(username, password, serverName, serverPort):
  69. params = urllib.parse.urlencode({'username': USERNAME, 'password': PASSWORD,'client': 'requestip', 'f': 'json'})
  70. headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
  71.  
  72. # Connect to URL and post parameters
  73. httpConn = http.client.HTTPSConnection(serverName, serverPort)
  74. httpConn.request("POST", TOKEN_URL, params, headers)
  75.  
  76. # Read response
  77. response = httpConn.getresponse()
  78. print(response.status)
  79. if (response.status != 200):
  80. httpConn.close()
  81. print("Error while fetching tokens from admin URL. Please check the URL and try again.")
  82. return
  83. else:
  84. data = response.read()
  85. httpConn.close()
  86.  
  87. # Check that data returned is not an error object
  88. if not assertJsonSuccess(data):
  89. return
  90.  
  91. # Extract the token from it
  92. token = json.loads(data)
  93. return token['token']
  94.  
  95. ###End getToken function
  96.  
  97. # A function that checks that the input JSON object is not an error object.
  98. def assertJsonSuccess(data):
  99. obj = json.loads(data)
  100. if 'status' in obj and obj['status'] == "error":
  101. print("Error: JSON object returns an error. " + str(obj))
  102. return False
  103. else:
  104. return True
  105.  
  106. ### End jsonSuccess function
  107. """
  108. This is the Main loop through WebMap values, processing and reporting back to survey, then continuing
  109. """
  110. #loop through the survey web map guids for processing-this is where the GUID values come from
  111. current_survey = ''
  112. for k,v in web_map_ids.items():
  113. if v[1] is None:
  114. continue
  115. current_survey = v[0]
  116.  
  117. #get WebMap
  118. gis = GIS(PORTAL_URL, username=USERNAME, password=PASSWORD)
  119. web_map = gis.content.get(v[1])
  120. web_Map = WebMap(web_map)
  121. web_map_layers = web_Map.layers
  122.  
  123. #extract the feature service name that contains the Point feature class
  124. num = 0
  125. for item in web_map_layers:
  126. for tag in item:
  127. if tag =='title':
  128. if item[tag] == 'Point':
  129. num = web_map_layers.index(item)
  130. else:
  131. continue
  132. else:
  133. continue
  134.  
  135. point_url = web_map_layers[num]['url']
  136.  
  137. #from https://www.esri.com/arcgis-blog/products/arcgis-desktop/analytics/quick-tips-consuming-feature-services-with-geoprocessing/
  138. #Extract Points from feature service to feature class
  139. baseURL = point_url
  140. where = '1=1'
  141. fields = '*'
  142. token = 'getToken(USERNAME, PASSWORD, SERVERNAME, PORT)'
  143. query = "?where={}&outFields={}&returnGeometry=true&f=json&token={}".format(where, fields, token)
  144. # See http://services1.arcgis.com/help/index.html?fsQuery.html for more info on FS-Query
  145. fsURL = baseURL + query
  146. fs = arcpy.FeatureSet()
  147. fs.load(fsURL)
  148.  
  149. #clean up prior to re-creating backups
  150. if arcpy.Exists(lines_backup):
  151. arcpy.Delete_management(lines_backup)
  152. if arcpy.Exists(points_backup):
  153. arcpy.Delete_management(points_backup)
  154.  
  155. #copy Points from feature service to feature class, for geo-processing
  156. arcpy.CopyFeatures_management(fs, points_backup)
Add Comment
Please, Sign In to add comment