def download_features(fs_url, date, out_path): #creates a empty GDB arcpy.workspace = cwd out_folder_path = cwd + '/Shapefiles' out_name = "test.gdb" arcpy.CreateFileGDB_management(out_folder_path, out_name) print ("Creating a empty GDB....") '''downloads a hosted service features into a feature class''' agol_securityHandler = AGOLTokenSecurityHandler('Username','Password','http://company.maps.arcgis.com/') agol_org_obj = Administration(securityHandler=agol_securityHandler,initialize=True) fs = FeatureService(url=fs_url,securityHandler=agol_securityHandler,initialize=True) ldf = LayerDefinitionFilter() #CHANGE CONDITION AFTER THE "and" TO QUERY SOMETHING ELSE OTHER THAN DATE #e.g. "ref='EBN_BDP_006_TAP'" #Date format mm/dd/yyyy ldf.addFilter(0, where="1=1" and "date> date\'" + date + "\'") print ("Querying data....") print (fs.query(layerDefsFilter=ldf,returnCountOnly=True)) queryResults = fs.query(layerDefsFilter=ldf,returnCountOnly=False,returnGeometry=True) result = queryResults[0].save(r'in_memory','SampleCities') print ("Saved results....") arcpy.CopyFeatures_management(result,out_path) print ("Features exported") print ("All done!")
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 ArcRestHelperError({ "function": "GetLayerFromFeatureServiceByURL", "line": line, "filename": filename, "synerror": synerror, "arcpyError": arcpy.GetMessages(2), }) except: line, filename, synerror = trace() raise ArcRestHelperError({ "function": "GetLayerFromFeatureServiceByURL", "line": line, "filename": filename, "synerror": synerror, }) finally: fs = None del fs 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()
from arcrest.security import AGOLTokenSecurityHandler from arcrest.agol import FeatureService from arcrest.common.filters import LayerDefinitionFilter if __name__ == "__main__": username = "******" password = "******" url = "<Feature Service URL on AGOL>" proxy_port = None proxy_url = None agolSH = AGOLTokenSecurityHandler(username=username, password=password) fs = FeatureService( url=url, securityHandler=agolSH, proxy_port=proxy_port, proxy_url=proxy_url, initialize=True) ldf = LayerDefinitionFilter() ldf.addFilter(0, where="1=1") print fs.query(layerDefsFilter=ldf, returnCountOnly=True) # should see something like : {'layers': [{'count': 4, 'id': 0}]}
from arcrest.security import AGOLTokenSecurityHandler from arcrest.agol import FeatureService from arcrest.common.filters import LayerDefinitionFilter if __name__ == "__main__": username = "******" password = "******" url = "<Feature Service URL on AGOL>" proxy_port = None proxy_url = None agolSH = AGOLTokenSecurityHandler(username=username, password=password) fs = FeatureService(url=url, securityHandler=agolSH, proxy_port=proxy_port, proxy_url=proxy_url, initialize=True) ldf = LayerDefinitionFilter() ldf.addFilter(0, where="1=1") print fs.query(layerDefsFilter=ldf, returnCountOnly=True) # should see something like : {'layers': [{'count': 4, 'id': 0}]}
def QueryAllFeatures(self, url=None, where="1=1", out_fields="*", timeFilter=None, geometryFilter=None, returnFeatureClass=False, out_fc=None, outSR=None, chunksize=1000, printIndent="", useAGSFeatureService=False): """Performs an SQL query against a hosted feature service layer and returns all features regardless of service limit. Args: url (str): The URL of the feature service layer. where - the selection sql statement out_fields - the attribute fields to return timeFilter - a TimeFilter object where either the start time or start and end time are defined to limit the search results for a given time. The values in the timeFilter should be as UTC timestampes in milliseconds. No checking occurs to see if they are in the right format. geometryFilter - a GeometryFilter object to parse down a given query by another spatial dataset. returnFeatureClass - Default False. If true, query will be returned as feature class chunksize (int): The maximum amount of features to query at a time. Defaults to 1000. out_fc - only valid if returnFeatureClass is set to True. Output location of query. useAGSFeatureService (boolean) - Whether to use an AGOL FeatureLayer (default or AGS-hosted FeatureService. Output: A list of Feature Objects (default) or a path to the output featureclass if returnFeatureClass is set to True. """ if (url is None): return fl = None try: if useAGSFeatureService: fl = FeatureService(url=url, securityHandler=self._securityHandler) qRes = fl.query(where=where, returnIdsOnly=True, timeFilter=timeFilter, geometryFilter=geometryFilter) else: fl = FeatureLayer(url=url, securityHandler=self._securityHandler) qRes = fl.query(where=where, returnIDsOnly=True, timeFilter=timeFilter, geometryFilter=geometryFilter) if 'error' in qRes: if isinstance(qRes, dict): qRes = str(qRes) print(printIndent + qRes) return [] elif 'objectIds' in qRes: oids = qRes['objectIds'] total = len(oids) if total == 0: return fl.query(where=where, returnGeometry=True, out_fields=out_fields, timeFilter=timeFilter, geometryFilter=geometryFilter, outSR=outSR) print(printIndent + "%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, timeFilter=timeFilter, geometryFilter=geometryFilter, outSR=outSR) 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(printIndent + "{:.0%} Completed: {}/{}".format( totalQueried / float(total), totalQueried, total)) else: print(printIndent + results) if returnFeatureClass == True: return combinedResults.save(*os.path.split(out_fc)) else: return combinedResults else: print(printIndent + qRes) except: line, filename, synerror = trace() raise common.ArcRestHelperError({ "function": "QueryAllFeatures", "line": line, "filename": filename, "synerror": synerror, }) finally: fl = None del fl gc.collect()
This sample shows how to create a replica from a FS """ from arcrest.security import AGOLTokenSecurityHandler from arcrest.agol import FeatureService from arcrest.common.filters import LayerDefinitionFilter if __name__ == "__main__": username = "******" password = "******" url = "<Feature Service URL on AGOL>" proxy_port = None proxy_url = None agolSH = AGOLTokenSecurityHandler(username=username, password=password) fs = FeatureService( url=url, securityHandler=agolSH, proxy_port=proxy_port, proxy_url=proxy_url, initialize=True) result = fs.createReplica(replicaName='Demo', layers='0,1,2,3,4', keep_replica=False, layerQueries=None, geometryFilter=None, returnAttachments=True, returnAttachmentDatabyURL=False, returnAsFeatureClass=True, out_path='C:\\temp') print result # should see something like : {'layers': [{'count': 4, 'id': 0}]}
#check for and/or make a folder with the date archive_path = os.path.join(backup_path,timestamp()) if not os.path.exists(archive_path): os.makedirs(archive_path) proxy_port = None proxy_url = None url = backups[k]["url"] msg2 = "Accessing service @ {0}".format(url) logging.info(msg2) print(msg2) fs = FeatureService( url=url, securityHandler=agolSH, proxy_port=proxy_port, proxy_url=proxy_url, initialize=True) print("Copying service to {0}".format(out_path)) result = fs.createReplica( replicaName=backups[k], layers=backups[k]["layers"], keep_replica=False, layerQueries=None, geometryFilter=None, returnAttachments=True, returnAttachmentDatabyURL=False, returnAsFeatureClass=True, out_path=archive_path)
This sample shows how to create a replica from a FS """ from arcrest.security import AGOLTokenSecurityHandler from arcrest.agol import FeatureService from arcrest.common.filters import LayerDefinitionFilter if __name__ == "__main__": username = "******" password = "******" url = "<Feature Service URL on AGOL>" proxy_port = None proxy_url = None agolSH = AGOLTokenSecurityHandler(username=username, password=password) fs = FeatureService(url=url, securityHandler=agolSH, proxy_port=proxy_port, proxy_url=proxy_url, initialize=True) result = fs.createReplica(replicaName='Demo', layers='0,1,2,3,4', keep_replica=False, layerQueries=None, geometryFilter=None, returnAttachments=True, returnAttachmentDatabyURL=False, returnAsFeatureClass=True, out_path='C:\\temp') print result # should see something like : {'layers': [{'count': 4, 'id': 0}]}
from arcrest.common.filters import LayerDefinitionFilter import os, zipfile if __name__ == "__main__": username = "******" password = "******" url = "http://services1.arcgis.com/***/rest/services/yourservice/FeatureServer" proxy_port = None proxy_url = None agolSH = AGOLTokenSecurityHandler(username=username, password=password) repName = 'MyHostedFeatureLayerName' #Name of replica and unzipped file gdb filelocation = 'C:\\Temp' fs = FeatureService(url=url, securityHandler=agolSH, proxy_port=proxy_port, proxy_url=proxy_url, initialize=True) result = fs.createReplica(replicaName=repName, layers=[0, 1, 2, 3, 4, 5, 6, 7, 8], async=True, returnAttachments=True, returnAttachmentsDatabyURL=True, attachmentsSyncDirection='bidirectional', wait=True, dataFormat="filegdb", out_path=filelocation) dzipfile = '{0}'.format(result) parentdirectory = os.path.dirname(os.path.abspath(dzipfile))
""" This sample shows how to create a replica from a FS ArcREST version 3.5.x Python 2/3 """ from __future__ import print_function from arcrest.security import AGOLTokenSecurityHandler from arcrest.agol import FeatureService from arcrest.common.filters import LayerDefinitionFilter if __name__ == "__main__": username = "******" password = "******" url = "<Feature Service URL on AGOL>" proxy_port = None proxy_url = None agolSH = AGOLTokenSecurityHandler(username=username, password=password) fs = FeatureService(url=url, securityHandler=agolSH, proxy_port=proxy_port, proxy_url=proxy_url, initialize=True) result = fs.createReplica(replicaName='Demo', layers=[0, 1, 2, 3], dataFormat="filegdb", out_path='C:\\temp') print(result)