Select to view content in your preferred language

Help with first notebook

111
1
3 weeks ago
Labels (1)
Mike_Koutnik
Regular Contributor

Hi, I am a Notebook newbie. I have a simple task. I need to schedule a task to calculate acres for a polygon feature layer in ArcGIS Online. I just need to access the area of the geometry, convert to acres, and set the Acres field. Below is what I have so far in my first-ever notebook. I'm not sure I'm even taking the right approach. I am currently stuck on how to get at the area of the feature shape... Any guidance is appreciated.

from arcgis import GIS
from arcgis.geometry import lengths, areas_and_lengths, project
from arcgis.geometry import Point, Polyline, Polygon, Geometry
from IPython.display import display
import pandas as pd
from arcgis.features import GeoAccessor, GeoSeriesAccessor

## connect to AGOL
agol = GIS("home")

## access the feature service item
item = agol.content.get("xyz")

## get the layer of interest as a FeatureLayer
## the example here is the first layer in the Feature Service
lyr = item.layers[0]
fset = lyr.query()
features = fset.features

for f in features:
    print(f.area)
    f.attributes['Acres'] = 1

 

0 Kudos
1 Reply
MobiusSnake
MVP Regular Contributor

Few things to get you rolling:

  • When you get an item from the content manager, it's an Item, not a FeatureLayerCollection, so you can't access the "layers" property right away.  You can use the FeatureLayerCollection.fromitem() class method to get a layer collection from an item, however.  Alternatively, use the FeatureLayerCollection(url) or FeatureLayer(url) constructors - they take the URLs to the service or layer (respectively), rather than an Item ID.
  • Features don't have an area property.  You can get area a few different ways (from the geometry if you have ArcPy access, from the shape-area field, from geoaccessors, from "areas_and_lengths", etc.), but depending how you do it, you'll want to make sure that your coordinate system is appropriate.  For example, if you use the shape-area field and you're using Web Mercator, the area likely won't be accurate.
  • When you assign a value to the acres field, you need to push it back to the service.  Setting it on the feature as you're doing will set the value in memory, but won't edit the service.  You'll want to look into the FeatureLayer.edit_features() method, this pushes a feature set to the service, saving the changes you've made.
0 Kudos