def summarize_crashes(buffs, out_features=arcpy.CreateUniqueName( 'crash_summary', 'in_memory'), crash_service_url=DEFAULT_CRASH_URL): """downloads and summarizes crash information within buffered intersections Required: buffs -- intersection buffers out_features -- output features crash_service_url -- IOWA DOT Crash REST Service url """ # dissolve buffers for REST API query dis = arcpy.CreateUniqueName('buff_dis', 'in_memory') arcpy.management.Dissolve(buffs, dis) # custom module: get layer from REST service lyr = restapi.MapServiceLayer(crash_service_url) # get geometry as JSON g = restapi.Geometry(dis) # query REST Service fs = lyr.query(geometry=g) crashes = arcpy.CreateUniqueName('crashes', 'in_memory') # export feature set restapi.exportFeatureSet(fs, crashes) # crash summary fields crash_sum_fields = [ 'FATALITIES', 'INJURIES', 'MAJINJURY', 'MININJURY', 'POSSINJURY', 'UNKINJURY', 'PROPDMG', 'VEHICLES', 'TOCCUPANTS' ] # create field map for spatial join fms = arcpy.FieldMappings() fms.addTable(buffs) fms.addTable(crashes) # set field map merge option to 'Sum' to summarize crash results per intersection buffer for field_name in crash_sum_fields: fi = fms.findFieldMapIndex(field_name) field = fms.getFieldMap(fi) field.mergeRule = 'Sum' # update field map fms.replaceFieldMap(fi, field) # do spatial join arcpy.analysis.SpatialJoin(buffs, crashes, out_features, field_mapping=fms) return out_features
# Test Geoprocessing Service gp_url = 'http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons' gp = restapi.GPTask(gp_url) # get a list of gp parameters (so we know what to pass in as kwargs) print '\nGP Task "{}" parameters:\n'.format(gp.name) for p in gp.parameters: print '\t', p.name, p.dataType point = {"geometryType":"esriGeometryPoint", "features":[ {"geometry":{"x":-10603050.16225853,"y":4715351.1473399615, "spatialReference":{"wkid":102100,"latestWkid":3857}}}], "sr":{"wkid":102100,"latestWkid":3857}} # run task, passing in gp parameters as keyword arguments (**kwargs) gp_res = gp.run(Input_Location=str(point), Drive_Times = '1 2 3', inSR = 102100) # returns a GPResult() object, can get at the first result by indexing (usually only one result) # can test if there are results by __nonzero__() if gp_res: result = gp_res.results[0] # this returned a GPFeatureRecordSetLayer as an outputParameter, so we can export this to polygons print '\nOutput Result: "{}", data type: {}\n'.format(result.paramName, result.dataType) # now export the result value to fc (use the value property of the GPResult object from run()) drive_times = os.path.join(folder, 'drive_times.shp') restapi.exportFeatureSet(drive_times, gp_res.value)