<jupyter_start><jupyter_text>Analysis tools This sample notebook shows how you can access the spatial analysis tools available via your ArcGIS Online organization. The documentation for the service is available at [ArcGIS REST API: Spatial Analysis Service](https://developers.arcgis.com/rest/analysis/api-reference/getting-started.htm). First, you would need to establish a connection to your organization which could be an ArcGIS Online organization or an ArcGIS Enterprise. To be able to run the code in this sample notebook, you would need to provide credentials of a user within an ArcGIS Online organization.<jupyter_code>import pandas as pd
from arcgis.gis import GIS
import arcgis.network as network
import arcgis.features as features
import arcgis.features.use_proximity as use_proximity
import arcgis.features.find_locations as find_locations

user_name = 'arcgis_python'
password = '<PASSWORD>'
my_gis = GIS('https://www.arcgis.com', user_name, password)<jupyter_output><empty_output><jupyter_text>Find nearestThe Find Nearest task measures the straight-line distance, driving distance, or driving time from features in the analysis layer to features in the near layer, and copies the nearest features in the near layer to a new layer. Connecting lines showing the measured path are returned as well. To get all possible values for the `measurement_type` parameter, you can call the `retrieve_travel_modes()` method of the `network.RouteLayer` object. The default value is `StraightLine` which will generate the Euclidean distance lines connecting the locations.<jupyter_code>route_service = network.RouteLayer(my_gis.properties.helperServices.route.url, gis=my_gis)
print(sorted([i['name'] for i in route_service.retrieve_travel_modes()['supportedTravelModes']]))<jupyter_output>['Driving Distance', 'Driving Time', 'Rural Driving Distance', 'Rural Driving Time', 'Trucking Distance', 'Trucking Time', 'Walking Distance', 'Walking Time']<jupyter_text>In this example, we will find the distance from a point to a few other points in another feature collection. The input features parameter can be a URL to a feature service layer with an optional filter to select specific features, or a feature collection. In this example, we will construct a feature collection from a Python dictionary.<jupyter_code>near_layer_fcol_def = {
    "layerDefinition": {        
        "geometryType": "esriGeometryPoint",
        "fields": [
            {
                "alias": "OBJECTID",
                "name": "OBJECTID",
                "type": "esriFieldTypeOID",
                "editable": False
            },
            {
                "alias": "NAME",
                "name": "NAME",
                "length": 255,
                "type": "esriFieldTypeString",
                "editable": True
            }            
        ]
    },
    "featureSet": {
        "geometryType": "esriGeometryPoint",
        "features": [
            {
                "geometry": {
                    "x": -13036019.880334152,
                    "y": 3864814.036269334,
                    "spatialReference": {
                        "wkid": 102100,
                        "latestWkid": 3857
                    }
                },
                "attributes": {
                    "NAME": "Point1",
                    "OBJECTID": 0
                }
            },
            {
                "geometry": {
                    "x": -13034185.391655311,
                    "y": 3863705.6993592004,
                    "spatialReference": {
                        "wkid": 102100,
                        "latestWkid": 3857
                    }
                },
                "attributes": {
                    "NAME": "Point2",
                    "OBJECTID": 1
                }
            }
        ]
    }
}

near_feat_col = features.FeatureCollection(near_layer_fcol_def)
analysis_layer_fcol_def = {
    "layerDefinition": {
        "geometryType": "esriGeometryPoint",
        "fields": [
            {
                "alias": "OBJECTID",
                "name": "OBJECTID",
                "type": "esriFieldTypeOID",
                "editable": False
            },
            {
                "alias": "NAME",
                "name": "NAME",
                "length": 255,
                "type": "esriFieldTypeString",
                "editable": True
            }
        ]
    },   
    "featureSet": {
        "geometryType": "esriGeometryPoint",
        "features": [
            {
                "geometry": {
                    "x": -13032694.869603751,
                    "y": 3865368.204724401,
                    "spatialReference": {
                        "wkid": 102100,
                        "latestWkid": 3857
                    }
                },
                "attributes": {
                    "NAME": "AnalysisPoint",
                    "OBJECTID": 0
                }
            }
        ]
    }
}

analysis_feat_col = features.FeatureCollection(analysis_layer_fcol_def)
car_mode = [i for i in route_service.retrieve_travel_modes()['supportedTravelModes'] 
            if i['name'] == 'Driving Time'][0]
result = use_proximity.find_nearest(analysis_feat_col, near_feat_col, measurement_type=car_mode,
                                    context={'outSR': {"wkid": 4326}})
df = pd.DataFrame.from_records([i['attributes'] for i in 
                                result['connecting_lines_layer'].layer.featureSet.features])
df[['RouteName', 'Total_Kilometers', 'Total_Minutes']]<jupyter_output><empty_output><jupyter_text>Plan routes The Plan Routes task determines how to efficiently divide tasks among a mobile workforce.You provide the input, which includes a set of stops and the number of vehicles available to visit the stops, and the tool assigns the stops to vehicles and returns routes showing how each vehicle can reach their assigned stops in the least amount of time.In this example, we will use features from a hosted feature service, apply a filter to it to retrieve only a few cities, and then find out the best sequence to travel through them.<jupyter_code>sample_cities = my_gis.content.search('title:"USA Major Cities" type:Feature Service owner:esri', 
                                      outside_org=True)[0]
stops_cities = ['San Francisco', 'San Jose', 'Sacramento', 
                'Los Angeles', 'San Diego']

values = "'" + "', '".join(stops_cities) + "'"
stops_layer = {'url': sample_cities.layers[0].url, 
               'filter': "ST in ('CA', 'NV')  AND NAME IN ({0})".format(values)}
start_layer = {'url': sample_cities.layers[0].url, 
               'filter': "NAME = 'Fresno'"}

result = use_proximity.plan_routes(stops_layer=stops_layer, route_count=1, 
                                   max_stops_per_route=10, route_start_time=1413964800000,
                                   start_layer=start_layer, context={'outSR': {"wkid": 4326}})
df = pd.DataFrame.from_records([i['attributes'] for i in 
                                result['routes_layer'].layer.featureSet.features])
df[['RouteName', 'StopCount', 'TotalTime', 'Total_Kilometers']]<jupyter_output><empty_output><jupyter_text>Create Drive-Time Areas The Create Drive-Time Areas task creates areas that can be reached within a given drive time or drive distance. In this example, we will generate a drive time area of 15 min around the city of Riverside, CA.<jupyter_code>sample_cities = my_gis.content.search('title:"USA Major Cities" type:Feature Service owner:esri', 
                                      outside_org=True)[0]
input_layer = {'url': sample_cities.layers[0].url, 
               'filter': "NAME = 'Riverside' AND ST = 'CA'"}

result = use_proximity.create_drive_time_areas(input_layer=input_layer, break_values=[15], 
                                               context={'outSR': {"wkid": 4326}})<jupyter_output><empty_output><jupyter_text>When creating drive-time areas, the output polygons will be enriched with various demographics data. For brevity, we show just a few columns.<jupyter_code>df = pd.DataFrame.from_records([i['attributes'] for i in 
                                result.layer.featureSet.features])
df[['Name', 'AnalysisArea', 'AGE_5_17', 'AGE_18_21', 'AGE_22_29', 'AGE_30_39', 'AGE_40_49', 'AGE_50_64', 'AGE_65_UP']]<jupyter_output><empty_output><jupyter_text>Connect Origins to Destinations The Connect Origins to Destinations task measures the travel time or distance between pairs of points. You provide starting and ending points, and the tool returns a layer containing route lines, including measurements, between the paired origins and destinations.In this example, we will find the travel distance between multiple cities which are loaded from a hosted feature service.<jupyter_code>sample_cities = my_gis.content.search('title:"USA Major Cities" type:Feature Service owner:esri', 
                                      outside_org=True)[0]
origins_layer = {'url': sample_cities.layers[0].url, 
                 'filter': "ST = 'CA' AND NAME in ('Riverside', 'Los Angeles') "}
destinations_layer = {'url': sample_cities.layers[0].url, 
                      'filter': "ST = 'CA' AND NAME = 'Victorville' "}

result = use_proximity.connect_origins_to_destinations(origins_layer=origins_layer, 
                                                       destinations_layer=destinations_layer,
                                                       context={'outSR': {"wkid": 4326}})
df = pd.DataFrame.from_records([i['attributes'] for i in 
                                result['routes_layer'].layer.featureSet.features])
df[['RouteName', 'Total_Kilometers', 'Total_Miles', 'Total_Minutes']]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10002055921052631, 'fraction_numerical': 0.021792763157894735, 'mean_word_length': 2.3548275862068966, 'pattern_counts': {'":': 61, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '535245', 'license': 'Apache-2.0', 'n_tokens_mistral': 2839, 'n_tokens_neox': 2552, 'n_words': 754, 'path': 'guide/11-performing-network-analyses/using-network-analysis-tools.ipynb', 'repo_name': 'fossabot/arcgis-python-api_test', 'size': '9729'}