<jupyter_start><jupyter_text>Using Feature LayersThe feature layer is the primary concept for working with features in a GIS.Users create, import, export, analyze, edit, and visualize features, i.e. “entities in space” as feature layers.Feature layers can be added to and visualized using maps. They act as inputs to and outputs from feature analysis tools.Feature layers are created by publishing feature data to a GIS, and are exposed as a broader resource (Item) in the GIS. Feature layer instances can be obtained through the layers attribute on feature layer collection Items in the GIS. A feature layer collection is a collection of feature layers and tables, with the associated relationships among the entities. A feature layer collection is backed by a [feature service](http://server.arcgis.com/en/server/latest/publish-services/windows/what-is-a-feature-service-.htm) in a web GIS.  Accessing Feature LayersFeature layer collection items are available as content in the GIS. You can search the GIS for feature layer collection items, or get them using their item id. Feature layers are available through the layers attribute on feature layer collection Items in the GIS.  Searching the GIS for feature layers You can search the GIS for feature layer collections by specifying the item type as 'Feature Layer Collection' or 'Feature Layer'.Note: A feature layer collection can be considered a type of feature layer such as a group feature layer. Hence, you can specify the item type as 'Feature Layer' and still get back feature layer collection items as results.The examples below will clarify this further:<jupyter_code># Establish a connection to your GIS.
from arcgis.gis import GIS
from IPython.display import display
gis = GIS() # anonymous connection to www.arcgis.com
# Search for 'USA major cities' feature layer collection
search_results = gis.content.search('title: USA Major Cities',
                                    'Feature Layer')

# Access the first Item that's returned
major_cities_item = search_results[0]

major_cities_item<jupyter_output><empty_output><jupyter_text>Note that the major_cities_item is a 'Feature Layer Collection' item.Since this item is a Feature Layer Collection, accessing the `layers` property will give us a list of `FeatureLayer` objects.<jupyter_code>major_cities_layers = major_cities_item.layers
major_cities_layers<jupyter_output><empty_output><jupyter_text>Accessing feature layers using item idFeature layer collection items are available as content in the GIS. You can get them using their item id, and query their layers property to get to the feature layers:<jupyter_code>freeways = gis.content.get('91c6a5f6410b4991ab0db1d7c26daacb')
freeways<jupyter_output><empty_output><jupyter_text>Since freeways is a Feature Layer Collection item, accessing the layers property will give us a list of FeatureLayer objects. This item has two layers:<jupyter_code>freeways.layers<jupyter_output><empty_output><jupyter_text>The code below cycles through the layers and prints their names:<jupyter_code>for lyr in freeways.layers:
    print(lyr.properties.name)<jupyter_output>USA Freeway System (over 1:500k)
USA Freeway System (below 1:500k)<jupyter_text>Accessing feature layers and tables from feature servicesA feature service serves a collection of feature layers and tables, with the associated relationships among the entities. It is represented by `arcgis.features.FeatureLayerCollection` in the ArcGIS Python API.Instances of FeatureLayerCollection can be constructed using a feature service url, as shown below:<jupyter_code>from arcgis.features import FeatureLayerCollection
fs_url = 'http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer'
sanfran = FeatureLayerCollection(fs_url)<jupyter_output><empty_output><jupyter_text>The collection of layers and tables in a FeatureLayerCollection can be accessed using the layers and tables properties respectively:<jupyter_code>sanfran.layers<jupyter_output><empty_output><jupyter_text>**Tables** represent entity classes with uniform properties. In addition to working with “entities with location” as features, the GIS can also work with non-spatial entities as rows in tables. Working with tables is similar to working with feature layers, except that the rows (Features) in a table do not have a geometry, and tables ignore any geometry related operation. The sanfran feature layer collection also has a table that can be obtained using its tables property:<jupyter_code>sanfran.tables<jupyter_output><empty_output><jupyter_text>Accessing feature layers from a feature layer urlInstances of FeatureLayers can also be constructed using a url to the REST endpoint of a feature layer:<jupyter_code>from arcgis.features import FeatureLayer
lyr_url = 'http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0'

layer = FeatureLayer(lyr_url)
layer<jupyter_output><empty_output><jupyter_text>Properties of FeatureLayerIn this section, let us take a closer look at the properties of a `FeatureLayer` object. We will use the `major_cities_layers` object created earlier<jupyter_code>feature_layer = major_cities_layers[0]
feature_layer<jupyter_output><empty_output><jupyter_text>The `properties` field on a `FeatureLayer` object provides a dictionary representation of all its properties. However you can access individual properties as fields as well:<jupyter_code>feature_layer.properties.extent<jupyter_output><empty_output><jupyter_text>The `capabilities` property is useful to know what kinds of edits and operations be performed on the feature layer<jupyter_code>feature_layer.properties.capabilities<jupyter_output><empty_output><jupyter_text>You can access the rendering information from the `drawingInfo` property<jupyter_code>feature_layer.properties.drawingInfo.renderer.type<jupyter_output><empty_output><jupyter_text>Querying feature layersQuerying is a powerful operation that can be performed on a `FeatureLayer` object. Let is take a closer look here. To write meaningful queries, we need to know the names of fields present in the layer. This can be determined by calling the `fields` property:<jupyter_code>for f in feature_layer.properties.fields:
    print(f['name'])<jupyter_output>FID
OBJECTID
NAME
CLASS
ST
STFIPS
PLACEFIPS
CAPITAL
POP_CLASS
POPULATION
POP2010
WHITE
BLACK
AMERI_ES
ASIAN
HAWN_PI
HISPANIC
OTHER
MULT_RACE
MALES
FEMALES
AGE_UNDER5
AGE_5_9
AGE_10_14
AGE_15_19
AGE_20_24
AGE_25_34
AGE_35_44
AGE_45_54
AGE_55_64
AGE_65_74
AGE_75_84
AGE_85_UP
MED_AGE
MED_AGE_M
MED_AGE_F
HOUSEHOLDS
AVE_HH_SZ
HSEHLD_1_M
HSEHLD_1_F
MARHH_CHD
MARHH_NO_C
MHH_CHILD
FHH_CHILD
FAMILIES
AVE_FAM_SZ
HSE_UNITS
VACANT
OWNER_OCC
RENTER_OCC<jupyter_text>The query method has a number of parameters that allow you to refine and transform the results. Since the processing is performed on the server, this operation is not restricted by the capacity of the client computer. For instance, let us select all the cities whose population in the year 2007 was greater than 1 million. Instead of returning all the fields, let us get only population related fields<jupyter_code>query_result1 = feature_layer.query(where='POP2010>1000000', 
                                    out_fields='WHITE,BLACK,MULT_RACE,HISPANIC')
len(query_result1.features)
query_result1.fields<jupyter_output><empty_output><jupyter_text>If we are only interested in the count, we could save bandwidth by setting the `return_count_only` to `True`<jupyter_code>feature_layer.query(where='POP2010>1000000', return_count_only=True)<jupyter_output><empty_output><jupyter_text>Querying features using a different spatial reference<jupyter_code>query_result1.spatial_reference<jupyter_output><empty_output><jupyter_text>By default, the query results are in the same spatial reference as the source layer. However you can use the `out_sr` parameter to reproject the result into a desired spatial reference. The projection happens on the server and on all the resulting features.In the example above, we obtained data in wkid:3857, a well known id for 'Web Mercator' projection. We can observe how the coordinates look like below:<jupyter_code>query_result1.features[0].geometry<jupyter_output><empty_output><jupyter_text>The coordinates are in projected coordinate system as expected. If we wish to have this data in latitude and longitude instead, we could do so by changing the `out_sr` to wkid:4326  FeatureSet propertiesAs seen previously, a `FeatureSet` is returned by a `query()` operation. The `FeatureSet` object packs a bunch of useful properties that help us discern useful information about the features under access One of the important properties is the `spatial_reference` as you saw earlier. Below, we are using the same `query_result1` `FeatureSet` from earlier query operation.<jupyter_code>query_result1.spatial_reference<jupyter_output><empty_output><jupyter_text>One of the most powerful operation on a `FeatureSet` is accessing the features not as `Feature` objects, but as pandas dataframe objects. The `df` property, returns a dataframe object:<jupyter_code>query2 = feature_layer.query(where="POP2010 > 1000000")
query2.sdf<jupyter_output><empty_output><jupyter_text>Accessing the features as a dataframe makes if easier to analyze the data statistically.  Accessing Features from query results<jupyter_code>query_geographic = feature_layer.query(where='POP2010 > 1000000', out_sr='4326')
query_geographic.features[0].geometry<jupyter_output><empty_output><jupyter_text>We can execute the `query()` method on the first `FeatureLayer` object and get a `FeatureSet`. Let us query and access the first 10 features in this layer<jupyter_code>major_cities_l1 = major_cities_layers[0]
major_cities_l1_fset = major_cities_l1.query(where= 'FID < 11')
type(major_cities_l1_fset)<jupyter_output><empty_output><jupyter_text>Now, accessing the `features` property of the above `FeatureSet` object will provide us the individual point `Features`.<jupyter_code>major_cities_l1_features = major_cities_l1_fset.features
len(major_cities_l1_features)<jupyter_output><empty_output><jupyter_text>Accessing Feature geometry and attributesAs mentioned earlier, the `Feature` object is a fine grained representation of spatial information. Two important properties of a `Feature` object are its `geometry` and `attributes`: Let us display the geometry and attributes of the first feature<jupyter_code>major_cities_l1_features[0].geometry
major_cities_l1_features[0].attributes<jupyter_output><empty_output><jupyter_text>Using feature collectionsSimilar to feature layers, [feature collections](http://doc.arcgis.com/en/arcgis-online/reference/feature-collections.htm) can also be used to store features. With a feature collection, a service is not created to serve out feature data. Instead it is stored as json data with the item. Feature collections can be added to maps as layers, passed as input to feature analysis tools and queried for feature data.Feature collections are shared in the GIS as items. Feature Collection Items can be searched by specifying 'Feature Collection' as the item_type.Let us search for a feature collection published by Iowa Dept. of Transportation as an example:<jupyter_code>search_fc = gis.content.search("title:AVL_Direct_FC", item_type='Feature Collection')
iowa_fc_item = search_fc[0]
iowa_fc_item<jupyter_output><empty_output><jupyter_text>Accessing the `layers` property on a feature collection item returns a list of `FeatureCollection` objects<jupyter_code>iowa_fc_item.layers
iowa_fc = iowa_fc_item.layers[0]<jupyter_output><empty_output><jupyter_text>You can call the `query()` method on a `FeatureCollection` object to get a `FeatureSet`.<jupyter_code>iowa_fset = iowa_fc.query()<jupyter_output><empty_output><jupyter_text>Once you have a `FeatureSet` object, you can access the `features` property to get a list of `Feature` objects as seen earlier<jupyter_code>iowa_features = iowa_fset.features
iowa_features[0].geometry<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0586527293844367, 'fraction_numerical': 0.01443504230960677, 'mean_word_length': 6.663699936427209, 'pattern_counts': {'":': 0, '<': 112, '<?xml version=': 0, '>': 115, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '28', 'dir': 'jupyter-structured-clean-dedup', 'id': '303967', 'license': 'Apache-2.0', 'n_tokens_mistral': 3570, 'n_tokens_neox': 3403, 'n_words': 1300, 'path': 'guide/04-feature-data-and-analysis/working-with-feature-layers-and-features.ipynb', 'repo_name': 'randybondsjr/arcgis-python-api', 'size': '12054'}