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!")
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()