def GetLayerFromFeatureServiceByURL(self, url, layerName="", returnURLOnly=False): fs = None try: fs = FeatureService(url=url, securityHandler=self._securityHandler) return self.GetLayerFromFeatureService(fs=fs, layerName=layerName, returnURLOnly=returnURLOnly) except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "GetLayerFromFeatureServiceByURL", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), }) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "GetLayerFromFeatureServiceByURL", "line": line, "filename": filename, "synerror": synerror, }) finally: fs = None del fs gc.collect()
def DeleteFeaturesFromFeatureLayer(self, url, sql): fl = None try: fl = FeatureLayer( url=url, securityHandler=self._securityHandler, ) return fl.deleteFeatures(where=sql) except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "DeleteFeaturesFromFeatureLayer", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), }) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "DeleteFeaturesFromFeatureLayer", "line": line, "filename": filename, "synerror": synerror, }) finally: fl = None del fl gc.collect()
def AddFeaturesToFeatureLayer(self, url, pathToFeatureClass): fl = None try: fl = FeatureLayer(url=url, securityHandler=self._securityHandler) return fl.addFeatures(fc=pathToFeatureClass) except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), }) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": line, "filename": filename, "synerror": synerror, }) finally: fl = None del fl gc.collect()
def GetLayerFromFeatureService(self, fs, layerName="", returnURLOnly=False): layers = None table = None layer = None sublayer = None try: layers = fs.layers for layer in layers: if layer.name == layerName: if returnURLOnly: return fs.url + '/' + str(layer.id) else: return layer elif not layer.subLayers is None: for sublayer in layer.subLayers: if sublayer == layerName: return sublayer for table in fs.tables: if table.name == layerName: if returnURLOnly: return fs.url + '/' + str(layer.id) else: return table return None except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "GetLayerFromFeatureService", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), }) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "GetLayerFromFeatureService", "line": line, "filename": filename, "synerror": synerror, }) finally: layers = None table = None layer = None sublayer = None del layers del table del layer del sublayer gc.collect()
def createGroup(self, title, tags, description="", snippet="", phone="", access="org", sortField="title", sortOrder="asc", isViewOnly=False, isInvitationOnly=False, thumbnail=None): admin = None userCommunity = None try: admin = arcrest.manageorg.Administration( securityHandler=self._securityHandler) userCommunity = admin.community return userCommunity.createGroup(title=title, tags=tags, description=description, snippet=snippet, phone=phone, access=access, sortField=sortField, sortOrder=sortOrder, isViewOnly=isViewOnly, isInvitationOnly=isInvitationOnly, thumbnail=thumbnail) except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "createGroup", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), }) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "createGroup", "line": line, "filename": filename, "synerror": synerror, }) finally: admin = None userCommunity = None del admin del userCommunity gc.collect()
def GetFeatureService(self, itemId, returnURLOnly=False): admin = None item = None try: admin = arcrest.manageorg.Administration( securityHandler=self._securityHandler) if self._securityHandler.valid == False: self._valid = self._securityHandler.valid self._message = self._securityHandler.message return None item = admin.content.item(itemId=itemId) if item.itemType == "Feature Service": if returnURLOnly: return item.url else: return FeatureService( url=item.url, securityHandler=self._securityHandler) return None except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "GetFeatureService", "line": line, "filename": filename, "synerror": synerror, }) finally: admin = None item = None del item del admin gc.collect()
def getThumbnailForItem(self,itemId,fileName,filePath): admin = None item = None try: admin = arcrest.manageorg.Administration(securityHandler=self._securityHandler) item = admin.content.getItem(itemId = itemId) return item.saveThumbnail(fileName=fileName,filePath=filePath) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "getThumbnailForItem", "line": line, "filename": filename, "synerror": synerror, } ) finally: admin = None item = None del admin del item gc.collect()
def createRole(self, name, description="", privileges=None): admin = None portal = None setPrivResults = None roleID = None createResults = None try: admin = arcrest.manageorg.Administration( securityHandler=self._securityHandler) portal = admin.portals.portalSelf try: roleID = portal.roles.findRoleID(name) if roleID is None: createResults = portal.createRole(name=name, description=description) if 'success' in createResults: if createResults['success'] == True: setPrivResults = portal.roles.setPrivileges( createResults['id'], privileges) if 'success' in setPrivResults: print("%s role created" % name) else: print(setPrivResults) else: print(createResults) else: print(createResults) else: print("%s role already exist" % name) except Exception as e: print(e) return None except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "createGroup", "line": line, "filename": filename, "synerror": synerror, }) finally: admin = None portal = None setPrivResults = None roleID = None createResults = None del admin del portal del setPrivResults del roleID del createResults gc.collect()
def getGroupContentItems(self,groupName): admin = None userCommunity = None groupIds = None groupId = None groupContent = None result = None item = None items = [] try: admin = arcrest.manageorg.Administration(securityHandler=self._securityHandler) userCommunity = admin.community groupIds = userCommunity.getGroupIDs(groupNames=groupName) #groupContent = admin.query(q="group:" + group_ids , bbox=None, start=1, num=100, sortField=None, #sortOrder="asc") if not groupIds is None: for groupId in groupIds: groupContent = admin.content.groupContent(groupId=groupId) if 'error' in groupContent: print groupContent else: for result in groupContent['items']: item = admin.content.getItem(itemId = result['id']) items.append(item) return items except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "getGroupContent", "line": line, "filename": filename, "synerror": synerror, } ) finally: admin = None userCommunity = None groupIds = None groupId = None groupContent = None result = None item = None del admin del userCommunity del groupIds del groupId del groupContent del result del item gc.collect()
def createGroup(self, title, tags, description="", snippet="", phone="", access="org", sortField="title", sortOrder="asc", isViewOnly=False, isInvitationOnly=False, thumbnail=None): admin = None userCommunity = None try: admin = arcrest.manageorg.Administration( securityHandler=self._securityHandler) userCommunity = admin.community try: groupExist = userCommunity.getGroupIDs(groupNames=title) if len(groupExist) > 0: print("Group %s already exist" % title) return None return userCommunity.createGroup( title=title, tags=tags, description=description, snippet=snippet, phone=phone, access=access, sortField=sortField, sortOrder=sortOrder, isViewOnly=isViewOnly, isInvitationOnly=isInvitationOnly, thumbnail=thumbnail) except Exception as e: print(e) return None except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "createGroup", "line": line, "filename": filename, "synerror": synerror, }) finally: admin = None userCommunity = None del admin del userCommunity gc.collect()
def GetLayerFromFeatureService(self, fs, layerName="", returnURLOnly=False): layers = None table = None layer = None sublayer = None try: layers = fs.layers if (layers is None or len(layers) == 0) and fs.url is not None: fs = arcrest.ags.FeatureService(url=fs.url) layers = fs.layers if layers is not None: for layer in layers: if layer.name == layerName: if returnURLOnly: return fs.url + '/' + str(layer.id) else: return layer elif not layer.subLayers is None: for sublayer in layer.subLayers: if sublayer == layerName: return sublayer if fs.tables is not None: for table in fs.tables: if table.name == layerName: if returnURLOnly: return fs.url + '/' + str(layer.id) else: return table return None except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "GetLayerFromFeatureService", "line": line, "filename": filename, "synerror": synerror, }) finally: layers = None table = None layer = None sublayer = None del layers del table del layer del sublayer gc.collect()
def getGroupContent(self, groupName): admin = None userCommunity = None groupIds = None groupId = None try: admin = arcrest.manageorg.Administration( securityHandler=self._securityHandler) userCommunity = admin.community groupIds = userCommunity.getGroupIDs(groupNames=groupName) if not groupIds is None: for groupId in groupIds: return admin.query(q="group:" + groupId, bbox=None, start=1, num=100, sortField=None, sortOrder="asc") except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "getGroupContent", "line": line, "filename": filename, "synerror": synerror, }) finally: admin = None userCommunity = None groupIds = None groupId = None groupContent = None result = None item = None del admin del userCommunity del groupIds del groupId del groupContent del result del item gc.collect()
def getGroupContent(self,groupName,onlyInOrg,onlyInUser): admin = None groups = None q = None results = None res = None try: admin = arcrest.manageorg.Administration(securityHandler=self._securityHandler) groups = admin.community.groups q = groupName if onlyInOrg == True: q = q + " orgid: %s" % admin.portals.portalSelf.id if onlyInUser == True: q = q + " owner: %s" % self._securityHandler.username results = groups.search(q = q) if 'total' in results and 'results' in results: if results['total'] > 0: for res in results['results']: group = admin.content.group(groupId=res['id']) return group.items return None except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "getGroupContent", "line": line, "filename": filename, "synerror": synerror, } ) finally: admin = None groups = None q = None results = None res = None del admin del groups del q del results del res gc.collect()
def AddFeaturesToFeatureLayer(self, url, pathToFeatureClass, chunksize=0, lowerCaseFieldNames=False): if arcpyFound == False: raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": inspect.currentframe().f_back.f_lineno, "filename": 'featureservicetools', "synerror": "ArcPy required for this function" }) fl = None try: fl = FeatureLayer(url=url, securityHandler=self._securityHandler) if chunksize > 0: fc = os.path.basename(pathToFeatureClass) inDesc = arcpy.Describe(pathToFeatureClass) oidName = arcpy.AddFieldDelimiters(pathToFeatureClass, inDesc.oidFieldName) arr = arcpy.da.FeatureClassToNumPyArray( pathToFeatureClass, (oidName)) syncSoFar = 0 messages = {'addResults': [], 'errors': []} total = len(arr) errorCount = 0 if total == '0': print("0 features in %s" % pathToFeatureClass) return "0 features in %s" % pathToFeatureClass print("%s features in layer" % (total)) arcpy.env.overwriteOutput = True if int(total) < int(chunksize): return fl.addFeatures( fc=pathToFeatureClass, lowerCaseFieldNames=lowerCaseFieldNames) else: newArr = chunklist(arr, chunksize) exprList = [ "{0} >= {1} AND {0} <= {2}".format( oidName, nArr[0][0], nArr[len(nArr) - 1][0]) for nArr in newArr ] for expr in exprList: UploadLayer = arcpy.MakeFeatureLayer_management( pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0) #print(arcpy.GetCount_management(in_rows=UploadLayer).getOutput(0) + " features in the chunk") results = fl.addFeatures( fc=UploadLayer, lowerCaseFieldNames=lowerCaseFieldNames) chunkCount = arcpy.GetCount_management( in_rows=UploadLayer).getOutput(0) print(chunkCount + " features in the chunk") if chunkCount > 0: if results is not None and 'addResults' in results and results[ 'addResults'] is not None: featSucces = 0 for result in results['addResults']: if 'success' in result: if result['success'] == False: if 'error' in result: errorCount = errorCount + 1 print("\tError info: %s" % (result)) else: featSucces = featSucces + 1 syncSoFar = syncSoFar + featSucces print("%s features added in this chunk" % (featSucces)) print("%s/%s features added, %s errors" % (syncSoFar, total, errorCount)) if 'addResults' in messages: messages['addResults'] = messages[ 'addResults'] + results['addResults'] else: messages['addResults'] = results[ 'addResults'] else: messages['errors'] = result return messages else: return fl.addFeatures(fc=pathToFeatureClass, lowerCaseFieldNames=lowerCaseFieldNames) except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), }) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": line, "filename": filename, "synerror": synerror, }) finally: fl = None del fl gc.collect()
def QueryAllFeatures(self, url, sql, out_fields="*", chunksize=1000, saveLocation="", outName=""): fl = None try: fl = FeatureLayer(url=url, securityHandler=self._securityHandler) qRes = fl.query(where=sql, returnIDsOnly=True) if 'error' in qRes: print(qRes) return qRes elif 'objectIds' in qRes: oids = qRes['objectIds'] total = len(oids) if total == 0: return { 'success': True, 'message': "No features matched the query" } print("%s features to be downloaded" % total) chunksize = min(chunksize, fl.maxRecordCount) combinedResults = None totalQueried = 0 for chunk in chunklist(l=oids, n=chunksize): oidsQuery = ",".join(map(str, chunk)) if not oidsQuery: continue else: results = fl.query(objectIds=oidsQuery, returnGeometry=True, out_fields=out_fields) if isinstance(results, FeatureSet): if combinedResults is None: combinedResults = results else: for feature in results.features: combinedResults.features.append(feature) totalQueried += len(results.features) print("{:.0%} Completed: {}/{}".format( totalQueried / float(total), totalQueried, total)) else: print(results) if saveLocation == "" or outName == "": return combinedResults else: return combinedResults.save(saveLocation=saveLocation, outName=outName) else: print(qRes) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "QueryFeatureLayer", "line": line, "filename": filename, "synerror": synerror, }) finally: fl = None del fl gc.collect()
def __init__(self, securityinfo): """Constructor A number of different security handlers are suppoted by ArcGIS, this function simplifies the creation of the handlers. A dict with the type of handler and information to created it is required to generate the handlers Inputs: securityinfo - The dict with details to create the handler. The valid parameters are: - security_type: Required, valid values are Portal, NTLM, PKI, OAUTH, LDAP - proxy_url: Optional, url for proxy server - proxy_port: Optional, port for proxy server - referer_url: Optional, url for referer - token_url: Optional, url for token server - username: Optional, Username for log in, not required for all security types - password: Optional, Password for log in, not required for all security types - certificatefile: Only required for PKI - keyfile: Only required for PKI - client_id: Only required for OAuth - secret_id: Only required for OAuth """ try: if not securityinfo is None: if isinstance(securityinfo, securityhandlerhelper): self._securityHandler = securityinfo.securityhandler self._username = securityinfo._username self._password = securityinfo._password self._proxy_url = securityinfo._proxy_url self._proxy_port = securityinfo._proxy_port self._token_url = securityinfo._token_url self._security_type = securityinfo._security_type self._featureServiceFieldCase = securityinfo._featureServiceFieldCase self._keyfile = securityinfo._keyfile self._certificatefile = securityinfo._certificatefile self._referer_url = securityinfo._referer_url self._client_id = securityinfo._client_id self._secret_id = securityinfo._secret_id self._is_portal = securityinfo._is_portal self._message = securityinfo._message self._valid = securityinfo._valid #self._securityHandler = securityinfo return else: pass if isinstance(securityinfo, str) and os.path.isfile(securityinfo): securityinfo = common.init_config_json( config_file=securityinfo) if 'Credentials' in securityinfo: securityinfo = securityinfo['Credentials'] if 'security_type' in securityinfo: self._security_type = securityinfo['security_type'] else: self._security_type = 'Portal' if not any(self._security_type in s for s in self._supported_types): self._message = 'Security type not supported: ' + self._security_type self._valid = False return if 'proxy_url' in securityinfo: self._proxy_url = securityinfo['proxy_url'] if 'proxy_port' in securityinfo: self._proxy_port = securityinfo['proxy_port'] if 'referer_url' in securityinfo: self._referer_url = securityinfo['referer_url'] if 'token_url' in securityinfo and securityinfo[ 'token_url'] is not None: self._token_url = securityinfo['token_url'] if not self._token_url.startswith('http://') and \ not self._token_url.startswith('https://'): self._token_url = 'https://' + self._token_url if 'org_url' in securityinfo and securityinfo[ 'org_url'] is not None: self._org_url = securityinfo['org_url'] if not self._org_url.startswith( 'http://') and not self._org_url.startswith( 'https://'): self._org_url = 'http://' + self._org_url if 'username' in securityinfo: self._username = securityinfo['username'] if 'password' in securityinfo: self._password = securityinfo['password'] if 'certificatefile' in securityinfo: self._certificatefile = securityinfo['certificatefile'] if 'keyfile' in securityinfo: self._keyfile = securityinfo['keyfile'] if 'client_id' in securityinfo: self._client_id = securityinfo['client_id'] if 'secret_id' in securityinfo: self._secret_id = securityinfo['secret_id'] if str(self._security_type).upper() == 'ArcGIS'.upper(): self._securityHandler = security.ArcGISTokenSecurityHandler( proxy_url=self._proxy_url, proxy_port=self._proxy_port) self._org_url = self._securityHandler.org_url self._username = self._securityHandler.username self._valid = True self._message = "ArcGIS security handler created" elif str(self._security_type).upper() == 'Portal'.upper() or \ str(self._security_type).upper() == 'AGOL'.upper(): if self._org_url is None or self._org_url == '': self._org_url = 'http://www.arcgis.com' if self._username is None or self._username == '' or \ self._password is None or self._password == '': self._message = "No username or password, no security handler generated" self._valid = True else: if self._org_url is None or '.arcgis.com' in self._org_url: self._securityHandler = security.AGOLTokenSecurityHandler( username=self._username, password=self._password, org_url=self._org_url, token_url=self._token_url, proxy_url=self._proxy_url, proxy_port=self._proxy_port) self._org_url = self._securityHandler.org_url self._message = "ArcGIS Online security handler created" else: self._securityHandler = security.PortalTokenSecurityHandler( username=self._username, password=self._password, org_url=self._org_url, proxy_url=self._proxy_url, proxy_port=self._proxy_port) self._message = "Portal security handler created" elif str(self._security_type).upper() == 'NTLM'.upper(): if self._username is None or self._username == '' or \ self._password is None or self._password == '': self._message = "Username and password required for NTLM" self._valid = False else: self._securityHandler = security.NTLMSecurityHandler( username=self._username, password=self._password, org_url=self._org_url, proxy_url=self._proxy_url, proxy_port=self._proxy_port, referer_url=self._referer_url) self._message = "NTLM security handler created" elif str(self._security_type).upper() == 'LDAP'.upper(): if self._username is None or self._username == '' or \ self._password is None or self._password == '': self._message = "Username and password required for LDAP" self._valid = False else: self._securityHandler = security.LDAPSecurityHandler( username=self._username, password=self._password, org_url=self._org_url, proxy_url=self._proxy_url, proxy_port=self._proxy_port, referer_url=self._referer_url) self._message = "LDAP security handler created" elif str(self._security_type).upper() == 'PKI'.upper(): if self._keyfile is None or self._keyfile == '' or \ self._certificatefile is None or self._certificatefile == '': self._message = "Key file and certification file required for PKI" self._valid = False else: self._securityHandler = security.PKISecurityHandler( keyfile=self._keyfile, certificatefile=self._certificatefile, org_url=self._org_url, proxy_url=self._proxy_url, proxy_port=self._proxy_port, referer_url=self._referer_url) self._message = "PKI security handler created" elif str(securityinfo['security_type']).upper( ) == 'OAUTH'.upper(): if self._secret_id is None or self._secret_id == '' or \ self._client_id is None or self._client_id == '': self._message = "client_id and secret_id required for OAUTH" self._valid = False else: self._securityHandler = security.OAuthSecurityHandler( client_id=self._client_id, secret_id=self._secret_id, org_url=self._org_url, proxy_url=self._proxy_url, proxy_port=self._proxy_port) self._message = "OAuth security handler created" else: print("No valid security type set") self._message = "No valid security type set" if self._securityHandler is not None: admin = Administration( url=self._org_url, securityHandler=self._securityHandler) try: portal = admin.portals.portalSelf if portal.featureServers is not None: for hostingServer in portal.featureServers: if hostingServer is not None: if isinstance(hostingServer, AGSAdministration): try: serData = hostingServer.data dataItems = serData.rootDataItems if dataItems is not None: if 'rootItems' in dataItems: for rootItem in dataItems[ 'rootItems']: if rootItem == '/enterpriseDatabases': rootItems = serData.findDataItems( ancestorPath= rootItem, type='fgdb,egdb' ) if not rootItems is None and 'items' in rootItems: for item in rootItems[ 'items']: if 'info' in item: if 'isManaged' in item[ 'info'] and item[ 'info'][ 'isManaged'] == True: conStrDic = {} conStr = item[ 'info'][ 'connectionString'].split( ";" ) for conStrValue in conStr: spltval = conStrValue.split( "=" ) conStrDic[spltval[ 0]] = spltval[ 1] if 'DBCLIENT' in conStrDic: if str( conStrDic[ 'DBCLIENT'] ).upper( ) == 'postgresql'.upper( ): self._featureServiceFieldCase = 'lower' except HTTPError as err: if err.code == 403: print( "Admistrative access denied, unable to check if hosting servers" ) else: print(err) except Exception as e: print(e) except HTTPError as err: if err.code == 403: print( "Admistrative access denied, unable to check if hosting servers" ) else: print(err) except Exception as e: print(e) if 'error' in self._securityHandler.message: self._message = self._securityHandler.message self._valid = False else: if self._securityHandler.message is not None: self._message = self._securityHandler.message self._valid = True else: self._message = 'Security info not set' self._valid = True except ValueError as e: raise e except Exception as e: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "securityhandlerhelper_init", "line": line, "filename": filename, "synerror": synerror, })
def shareItemsToGroup(self, shareToGroupName, items=None, groups=None): admin = None userCommunity = None group_ids = None results = None item = None res = None group = None groupContent = None result = None try: admin = arcrest.manageorg.Administration( securityHandler=self._securityHandler) userCommunity = admin.community group_ids = userCommunity.getGroupIDs(groupNames=shareToGroupName) results = [] if not items is None: for item in items: item = admin.content.item(itemId=item) res = item.shareItem(",".join(group_ids), everyone=False, org=False) if 'error' in res: print res else: print "%s shared with %s" % (item.title, shareToGroupName) results.append(res) if not groups is None: for group in groups: groupContent = admin.content.groupContent(groupId=group) if 'error' in groupContent: print groupContent else: for result in groupContent['items']: item = admin.content.item(itemId=result['id']) res = item.shareItem(",".join(group_ids), everyone=False, org=False) if 'error' in res: print res else: print "%s shared with %s" % (result['title'], shareToGroupName) results.append(res) except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "shareItemsToGroup", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), }) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "shareItemsToGroup", "line": line, "filename": filename, "synerror": synerror, }) finally: admin = None userCommunity = None group_ids = None item = None res = None group = None groupContent = None result = None del admin del userCommunity del group_ids del item del res del group del groupContent del result gc.collect()
def QueryAllFeatures(self,url,sql,chunksize=0,saveLocation="",outName=""): fl = None try: fl = FeatureLayer( url=url, securityHandler=self._securityHandler) totalQueried = 0 if chunksize > 0: qRes = fl.query(where=sql, returnIDsOnly=True) if 'error' in qRes: print qRes return qRes elif 'objectIds' in qRes: oids = qRes['objectIds'] total = len(oids) if total == 0: return {'success':'true','message': "No features matched the query"} minId = min(oids) maxId = max(oids) i = 0 print "%s features to be downloaded" % total combinedResults = None while(i <= len(oids)): oidsQuery = ','.join(str(e) for e in oids[i:i+chunksize]) if oidsQuery == '': continue else: results = fl.query(objectIds=oidsQuery, returnGeometry=True, out_fields='*') if isinstance(results,FeatureSet): if combinedResults is None: combinedResults = results else: for feature in results.features: combinedResults.features.append(feature) totalQueried += len(results.features) print "%s%% Completed: %s/%s " % (int(totalQueried / float(total) *100), totalQueried, total) i += chunksize else: print results print combinedResults.save(saveLocation=saveLocation, outName=outName) else: print qRes else: return fl.query(where=sql, returnFeatureClass=True, returnGeometry=True, out_fields='*', out_fc=os.path.join(saveLocation,outName) ) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "QueryFeatureLayer", "line": line, "filename": filename, "synerror": synerror, } ) finally: fl = None del fl gc.collect()
def removeUserGroups(self,users=None): """ This function deletes all groups for the list of users, if no users are specified, all users in the org are queried and their groups is deleted. Inputs: users - Comma delimted list of user names """ admin = None userCommunity = None portal = None groupAdmin = None user = None userCommData = None group = None try: admin = arcrest.manageorg.Administration(securityHandler=self._securityHandler) if users is None: print ("You have selected to remove all users groups, you must modify the code to do this") usersObj = [] commUsers = admin.portals.portalSelf.users(start=1, num=100) usersObj = commUsers['users'] return else: usersObj = [] userStr = users.split(',') for user in userStr: try: user = admin.community.users.user(str(user).strip()) usersObj.append(user) except: print ("%s does not exist" % str(user).strip()) if usersObj: for userCommData in usersObj: print ("Loading groups for user: %s" % userCommData.username) if userCommData.groups: for group in userCommData.groups: groupObj = admin.community.groups.group(groupId=group['id']) if groupObj.owner == userCommData.username: print (groupObj.delete()) else: print ("No Groups Found") except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "removeUserGroups", "line": line, "filename": filename, "synerror": synerror, } ) finally: admin = None userCommunity = None portal = None groupAdmin = None user = None userCommData = None group = None del admin del userCommunity del portal del groupAdmin del user del userCommData del group gc.collect()
def DeleteFeaturesFromFeatureLayer(self,url,sql,chunksize=0): fl = None try: fl = FeatureLayer( url=url, securityHandler=self._securityHandler) totalDeleted = 0 if chunksize > 0: qRes = fl.query(where=sql, returnIDsOnly=True) if 'error' in qRes: print qRes return qRes elif 'objectIds' in qRes: oids = qRes['objectIds'] total = len(oids) if total == 0: return {'success':'true','message': "No features matched the query"} minId = min(oids) maxId = max(oids) i = 0 print "%s features to be deleted" % total while(i <= len(oids)): oidsDelete = ','.join(str(e) for e in oids[i:i+chunksize]) if oidsDelete == '': continue else: results = fl.deleteFeatures(objectIds=oidsDelete) if 'deleteResults' in results: totalDeleted += len(results['deleteResults']) print "%s%% Completed: %s/%s " % (int(totalDeleted / float(total) *100), totalDeleted, total) i += chunksize else: print results return {'success':'true','message': "%s deleted" % totalDeleted} qRes = fl.query(where=sql, returnIDsOnly=True) if 'objectIds' in qRes: oids = qRes['objectIds'] if len(oids)> 0 : print "%s features to be deleted" % len(oids) results = fl.deleteFeatures(where=sql) if 'deleteResults' in results: totalDeleted += len(results['deleteResults']) return {'success':'true','message': "%s deleted" % totalDeleted} else: return results return {'success':'true','message': "%s deleted" % totalDeleted} else: print qRes else: results = fl.deleteFeatures(where=sql) if 'deleteResults' in results: return {'success':'true','message': totalDeleted + len(results['deleteResults'])} else: return results except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "DeleteFeaturesFromFeatureLayer", "line": line, "filename": filename, "synerror": synerror, } ) finally: fl = None del fl gc.collect()
def RemoveAndAddFeatures(self, url, pathToFeatureClass,id_field,chunksize=1000): fl = None try: arcpy.env.overwriteOutput = True tempaddlayer= 'ewtdwedfew' if not arcpy.Exists(pathToFeatureClass): raise common.ArcRestHelperError({ "function": "RemoveAndAddFeatures", "line": inspect.currentframe().f_back.f_lineno, "filename": 'featureservicetools', "synerror": "%s does not exist" % pathToFeatureClass } ) fields = arcpy.ListFields(pathToFeatureClass,wild_card=id_field) if len(fields) == 0: raise common.ArcRestHelperError({ "function": "RemoveAndAddFeatures", "line": inspect.currentframe().f_back.f_lineno, "filename": 'featureservicetools', "synerror": "%s field does not exist" % id_field }) strFld = True if fields[0].type != 'String': strFld = False fl = FeatureLayer( url=url, securityHandler=self._securityHandler) id_field_local = arcpy.AddFieldDelimiters(pathToFeatureClass, id_field) idlist = [] print arcpy.GetCount_management(in_rows=pathToFeatureClass).getOutput(0) + " features in the layer" with arcpy.da.SearchCursor(pathToFeatureClass, (id_field)) as cursor: allidlist = [] for row in cursor: if (strFld): idlist.append("'" + row[0] +"'") else: idlist.append(row[0]) if len(idlist) >= chunksize: allidlist.append(idlist) idlist = [] if len(idlist) > 0: allidlist.append(idlist) for idlist in allidlist: idstring = ' in (' + ','.join(idlist) + ')' sql = id_field + idstring sqlLocalFC = id_field_local + idstring results = fl.deleteFeatures(where=sql, rollbackOnFailure=True) if 'error' in results: raise common.ArcRestHelperError({ "function": "RemoveAndAddFeatures", "line": inspect.currentframe().f_back.f_lineno, "filename": 'featureservicetools', "synerror":results['error'] }) elif 'deleteResults' in results: print "%s features deleted" % len(results['deleteResults']) for itm in results['deleteResults']: if itm['success'] != True: print itm else: print results arcpy.MakeFeatureLayer_management(pathToFeatureClass,tempaddlayer,sqlLocalFC) results = fl.addFeatures(fc=tempaddlayer) if 'error' in results: raise common.ArcRestHelperError({ "function": "RemoveAndAddFeatures", "line": inspect.currentframe().f_back.f_lineno, "filename": 'featureservicetools', "synerror":results['error'] }) elif 'addResults' in results: print "%s features added" % len(results['addResults']) for itm in results['addResults']: if itm['success'] != True: print itm else: print results idlist = [] if 'error' in results: raise common.ArcRestHelperError({ "function": "RemoveAndAddFeatures", "line": inspect.currentframe().f_back.f_lineno, "filename": 'featureservicetools', "synerror":results['error'] }) else: print results except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "create_report_layers_using_config", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), } ) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": line, "filename": filename, "synerror": synerror, } ) finally: gc.collect()
def AddFeaturesToFeatureLayer(self,url,pathToFeatureClass,chunksize=0): fl = None try: fl = FeatureLayer( url=url, securityHandler=self._securityHandler) if chunksize > 0: messages = {'addResults':[]} total = arcpy.GetCount_management(pathToFeatureClass).getOutput(0) arcpy.env.overwriteOutput = True inDesc = arcpy.Describe(pathToFeatureClass) oidName = arcpy.AddFieldDelimiters(pathToFeatureClass,inDesc.oidFieldName) sql = '%s = (select min(%s) from %s)' % (oidName,oidName,os.path.basename(pathToFeatureClass)) cur = arcpy.da.SearchCursor(pathToFeatureClass,[inDesc.oidFieldName],sql) minOID = cur.next()[0] del cur, sql sql = '%s = (select max(%s) from %s)' % (oidName,oidName,os.path.basename(pathToFeatureClass)) cur = arcpy.da.SearchCursor(pathToFeatureClass,[inDesc.oidFieldName],sql) maxOID = cur.next()[0] del cur, sql breaks = range(minOID,maxOID)[0:-1:chunksize] breaks.append(maxOID+1) exprList = [oidName + ' >= ' + str(breaks[b]) + ' and ' + \ oidName + ' < ' + str(breaks[b+1]) for b in range(len(breaks)-1)] for expr in exprList: UploadLayer = arcpy.MakeFeatureLayer_management(pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0) result = fl.addFeatures(fc=UploadLayer) if messages is None: messages = result else: if 'addResults' in result: if 'addResults' in messages: messages['addResults'] = messages['addResults'] + result['addResults'] print "%s/%s features added" % (len(messages['addResults']),total) else: messages['addResults'] = result['addResults'] print "%s/%s features added" % (len(messages['addResults']),total) else: messages['errors'] = result return messages else: return fl.addFeatures(fc=pathToFeatureClass) except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "create_report_layers_using_config", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), } ) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": line, "filename": filename, "synerror": synerror, } ) finally: fl = None del fl gc.collect()
def removeUserData(self,users=None): """ This function deletes content for the list of users, if no users are specified, all users in the org are queried and their content is deleted. Inputs: users - Comma delimited list of user names """ admin = None portal = None user = None adminusercontent = None userFolder = None userContent = None userItem = None folderContent = None try: admin = arcrest.manageorg.Administration(securityHandler=self._securityHandler) if users is None: print ("You have selected to remove all users data, you must modify the code to do this") usersObj = [] commUsers = admin.portals.portalSelf.users(start=1, num=100) commUsers = commUsers['users'] for user in commUsers: usersObj.append(user.userContent) return else: usersObj = [] userStr = users.split(',') for user in userStr: try: user = admin.content.users.user(str(user).strip()) usersObj.append(user) except: print ("%s does not exist" % str(user).strip()) if usersObj: for user in usersObj: print ("Loading content for user: %s" % user.username) itemsToDel = [] for userItem in user.items: itemsToDel.append(userItem.id) if len(itemsToDel) > 0: print (user.deleteItems(items=",".join(itemsToDel))) if user.folders: for userFolder in user.folders: if (user.currentFolder['title'] != userFolder['title']): user.currentFolder = userFolder['title'] itemsToDel = [] for userItem in user.items: itemsToDel.append(userItem.id) if len(itemsToDel) > 0: print (user.deleteItems(items=",".join(itemsToDel))) print (user.deleteFolder()) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "removeUserData", "line": line, "filename": filename, "synerror": synerror, } ) finally: admin = None portal = None user = None adminusercontent = None userFolder = None userContent = None userItem = None folderContent = None del admin del portal del user del adminusercontent del userFolder del userContent del userItem del folderContent gc.collect()
def AddFeaturesToFeatureLayer(self, url, pathToFeatureClass, chunksize=0, lowerCaseFieldNames=False): if arcpyFound == False: raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": inspect.currentframe().f_back.f_lineno, "filename": 'featureservicetools', "synerror": "ArcPy required for this function" }) fl = None try: fl = FeatureLayer(url=url, securityHandler=self._securityHandler) if chunksize > 0: messages = {'addResults': []} total = arcpy.GetCount_management( pathToFeatureClass).getOutput(0) if total == '0': print("0 features in %s" % pathToFeatureClass) return "0 features in %s" % pathToFeatureClass arcpy.env.overwriteOutput = True if int(total) < int(chunksize): return fl.addFeatures( fc=pathToFeatureClass, lowerCaseFieldNames=lowerCaseFieldNames) else: inDesc = arcpy.Describe(pathToFeatureClass) oidName = arcpy.AddFieldDelimiters(pathToFeatureClass, inDesc.oidFieldName) fc = os.path.basename(pathToFeatureClass) sql = "{0} IN ((SELECT MIN({0}) FROM {1}), (SELECT MAX({0}) FROM {1}))".format( oidName, fc) minOID, maxOID = list( zip(*arcpy.da.SearchCursor(pathToFeatureClass, "OID@", sql)))[0] breaks = list(range(minOID, maxOID))[0:-1:chunksize] breaks.append(maxOID + 1) exprList = [ "{0} >= {1} AND {0} < {2}".format( oidName, breaks[b], breaks[b + 1]) for b in range(len(breaks) - 1) ] for expr in exprList: UploadLayer = arcpy.MakeFeatureLayer_management( pathToFeatureClass, 'TEMPCOPY', expr).getOutput(0) result = fl.addFeatures( fc=UploadLayer, lowerCaseFieldNames=lowerCaseFieldNames) if messages is None: messages = result else: if result is not None and 'addResults' in result: if 'addResults' in messages: messages['addResults'] = messages[ 'addResults'] + result['addResults'] print("%s/%s features added" % (len(messages['addResults']), total)) else: messages['addResults'] = result[ 'addResults'] print("%s/%s features added" % (len(messages['addResults']), total)) else: messages['errors'] = result return messages else: return fl.addFeatures(fc=pathToFeatureClass, lowerCaseFieldNames=lowerCaseFieldNames) except arcpy.ExecuteError: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), }) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "AddFeaturesToFeatureLayer", "line": line, "filename": filename, "synerror": synerror, }) finally: fl = None del fl gc.collect()
def shareItemsToGroup(self,shareToGroupName,items=None,groups=None): admin = None userCommunity = None group_ids = None results = None item = None res = None group = None groupContent = None result = None try: admin = arcrest.manageorg.Administration(securityHandler=self._securityHandler) userCommunity = admin.community group_ids = userCommunity.getGroupIDs(groupNames=shareToGroupName) results = [] if not items is None: for item in items: item = admin.content.getItem(itemId = item) res = item.shareItem(",".join(group_ids),everyone=False,org=False) if 'error' in res: print res else: print "%s shared with %s" % (item.title,shareToGroupName) results.append(res) if not groups is None: for group in groups: group = admin.content.group(groupId=group) if group is None: print "Group not found" else: for itemJSON in group.items: if 'id' in itemJSON: item = admin.content.getItem(itemId = itemJSON['id']) res = item.shareItem(",".join(group_ids),everyone=False,org=False) if 'error' in res: print res elif 'notSharedWith' in res: print "%s shared with %s, not Shared With %s" % \ (item.title,shareToGroupName,",".join(res['notSharedWith'])) else: print "%s shared with %s" % (item.title,shareToGroupName) results.append(res) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "shareItemsToGroup", "line": line, "filename": filename, "synerror": synerror, } ) finally: admin = None userCommunity = None group_ids = None item = None res = None group = None groupContent = None result = None del admin del userCommunity del group_ids del item del res del group del groupContent del result gc.collect()
def removeUserGroups(self, users=None): """ This function deletes all groups for the list of users, if no users are specified, all users in the org are queried and their groups is deleted. Inputs: users - Comma delimted list of user names """ admin = None userCommunity = None portal = None groupAdmin = None user = None userCommData = None group = None try: admin = arcrest.manageorg.Administration( securityHandler=self._securityHandler) if users is None: usersObj = admin.portals.portalSelf.users(start=1, num=100) else: usersObj = [] userStr = users.split(',') for user in userStr: usersObj.append(admin.content.users.user(user)) if usersObj: for user in usersObj: print "Loading groups for user: %s" % user.username userCommData = admin.community.users.user(user.username) if userCommData.groups: for group in userCommData.groups: groupObj = admin.community.groups.group( groupId=group['id']) if groupObj.owner == user.username: print groupObj.delete() else: print "No Groups Found" except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "DeleteFeaturesFromFeatureLayer", "line": line, "filename": filename, "synerror": synerror, }) finally: admin = None userCommunity = None portal = None groupAdmin = None user = None userCommData = None group = None del admin del userCommunity del portal del groupAdmin del user del userCommData del group gc.collect()