Ex_features_module

Exercise - add new cities and to the cities over100k FeatureLayer

Goal:

  • Add 4 new cities ‘Jackson, WY’, ‘Bend, OR’, ‘Cedar Falls, IA’, ‘Palm Springs, CA’ to the layer cities over100k.
    • Make up your own values for their population. Better yet, get the 4 quantiles of population column and use them for the population of these 4 cities.
  • Add a new field called male_femal_ratio, float as data type
  • Generate random number between bounds [0.7 - 1.3] and assign values for the male_female_ratio for all rows
  • Update the service description and the item description with a string ‘hands on exercise completed’
  • Explore if you can edit HTML popup for this layer for use when rendered in map widget

Hints

- Publish the service definition datasets/fortune_500_styled_wfl.sd to your account. This will give you the feature layers to play with

  • Read the item as a FeatureLayer object and use edit_features() method to add new rows - cities
    • for calculating the quantiles, read the attribute information as a pandas data frame. To do this,
    • call query() without any parameters
  • Get the FeatureLayerManager object from the FeatureLayer and use add_to_definition() to create the new field
  • Create random values using numpy package random function
  • In a loop add the ratio value to all features
  • Once again call the edit_features() now with updates argument to feed the sex ratio values
In [ ]:
# connect to your GIS
from arcgis.gis import *
from getpass import getpass
password = getpass()
In [ ]:
gis = GIS('url','username',password)
In [ ]:
# publish the sd file
fortune_500_item = gis.content.add({},data='../../datasets/fortune_500_styled_wfl.sd')
fortune_500_item
In [ ]:
# get all layers from the item
fortune_500_item.layers
In [ ]:
# find which layer is cities_over100k
cities_flayer  = fortune_500_item.layers[<appropriate index here>]

Get the attribute table as a data frame

In [ ]:
# create cities_fset by running the query() on cities_flayer
In [ ]:
cities_fset.df

If you get an error about pandas not found, then save notebook, close all notebooks. Stop the kernel in terminal. Install pandas using the followin command:

conda install pandas

Then resume your work, start notebooks. Run the above cell to view the table

In [ ]:
pop_series = cities_fset.df.<name of population column here>
pop_quantiles = pop_series.quantile([0,0.25,0.5,0.75]) # to get 4 population values at equal intervals
pop_quantiles

The above cell give you 4 new population values. Now create a list of dictionaries for 4 new cities

In [ ]:
cities_features = cities_fset.features
cities_features[0] # you are viewing the first city

Import geocoding module and find the coordinates, of the 4 new cities

Adding new features

In [ ]:
#from arcgis import ....

# find the x, y of the 4 new cities
In [ ]:
# create 4 deep copies of the first feature to use as a template

# edit each copy to feed the x,y, name and population.

# read population as pop_quantiles[0], then as pop_quantiles[0.25] and so on for 0.5 and 0.75
In [ ]:
# compose a list of these 4 features
In [ ]:
# call edit_features, pass the list of featuers as parameters to `adds` parameter

Scroll up, rerun the cell with query() on FeatureLayer object and the cell that prints the data frame. Find if your new features are listed

Adding new fields

In [ ]:
cities_flayer_mgr = cities_flayer.manager
cities_flayer_mgr.properties.fields
In [ ]:
template_field = cities_flayer_mgr.properties.fields[0]
template_field
In [ ]:
new_field = copy.deepcopy(template_field)
# edit the name, alias, data type, nullable, of this field to match the requirements for male_to_female column
In [ ]:
#cities_flayer_mgr.add_to_definition(pass the new fields)

Calculating field values

In [ ]:
import numpy as np
In [ ]:
# rand1 = np.random.uniform(low=0.7, high=1.3, size=<fill with number of features in your featureset> )
len(list(rand1))
In [ ]:
rand1[0]
In [ ]:
count=0
for f in cities_fset:
    f.attributes['male_to_female'] = int(rand1[count])
    count+=1
In [ ]:
#print f to view the last feature.
f
In [ ]:
cities_flayer.edit_features(updates=cities_fset)

Re run the cell that queries the featuer layer and the cell that prints the data frame to find if the new column and values are applied

In [ ]: