ArcGIS API for Python Session 2

Review of what we've learned

importied and connected to ArcGIS Online using GIS()

created a Map object and changed the basemap with the Map.basemap property

geocoded POIs (Points Of Interest) and added the results to our map with the geocode() and draw() methods

search for layers within ArcGIS Online and add it to map via add_layer() method

create layer from geocoded features and add layer to map

Searching for data

Let's talk about gis.content.search() method some more.

We want to find hurricane data from curated and vetted resources.

How can we guarantee this happens using the search() method?

Copy and paste the following code into new cells (divided by the lines).

from arcgis import GIS

gis = GIS()

hurricanes_search = gis.content.search("title: hurricanes", outside_org=True, item_type="Feature *")

# hurricanes_search = gis.content.search("title: hurricanes, groupdesignations: livingatlas", outside_org=True, item_type="Feature *")

for idx, itm in enumerate(hurricanes_search):

display(itm)

print(f"[{idx:<2}] {itm.title:55}")

print(f"{itm.id}\t{itm.type:20}\t{itm.owner:20}\t{itm.groupDesignations}\n")

Let's talk to our best friend again (<shift>+<tab>) about what type of data is the variable hurricanes_search.

Put your cursor on the variable hurricanes_search within the notebook cell then do the help key shortcut.

The results from the search() method is returned as a python list of Item objects

Note: Doing <shift>+<tab>+<tab> extends the help vertically (Don't need to click on the '+' sign in the help window).

Each Item object returned from a search has property fields associated with it.

Within our search() method call we can query on these property fields to filter our search results.

We're already querying using the title field. Now let's add a field called groupdesignations to our query.

Now we have only results from the Living Atlas repository.

If for some reason your search results are not producing the expected results you can either

Do the search within ArcGIS Pro which has a category for searching the Living Atlas directly.

Go to the Living Atlas site in a browser: Living Atlas. There, you can issue a search in the search bar.

Once you find the wanted feature service get the Item ID and issue gis.content.get(<ItemID>) with

Item ID.

By clicking on the feature service image we can go to the feature service (which is just a FeatureLayerCollection that's available online), webpage within ArcGIS and see the details of the feature service.

Being that the feature service is from the Living Atlas there should always be accurate details associated with it.

Let's choose the Recent Hurricanes, Cyclones and Typhoons feature service.

Copy and paste the following code into a new cell.

rh_flyrs = hurricanes_search[4].layers # rh_flyrs stands for recent hurricanes feature lyrs

for idx, lyr in enumerate(rh_flyrs):

print(f"[{idx:2}] {lyr.properties.name:50}")

Looks like there are 3 layers to this feature service and we want to add them all to a map.

Let's create a feature set for each layer and have it contain only a hurricane of a particular name.

op_fset = rh_flyrs[0].query(where="STORMNAME='Ida'")

# help(FeatureLayer.query)

display(len(op_fset))

ot_fset = rh_flyrs[1].query(where="STORMNAME='Ida'")

display(len(ot_fset))

ows_fset = rh_flyrs[2].query(where="STORMNAME='Ida'")

display(len(ows_fset))

Copy and paste the following code into a new cell.

storm_map = gis.map("USA", zoomlevel=3)

for lyr in recent_hurricanes_fservice.layers:

storm_map.add_layer(lyr)

storm_map

If for some reason you're not getting the results you're expecting