Select to view content in your preferred language

ArcGIS Online Notebook how to create buffers and append against existing layer

980
2
Jump to solution
09-24-2024 06:02 PM
Labels (1)
ChristopherCounsell
MVP Regular Contributor

Hi,

I have a Hosted Feature Layer in ArcGIS Online, with a point layer and a polygon layer.

I am looking to run an ArcGIS Notebook as a task that will:

  • Query the point layer where processed=no
  • For each point, buffer by a point attribute ("distance_field")
  • Take the buffer and append to the polygon layer, passing through the distance_field value, a type field and a globalid into parentglobalid. This is required for subsequent processing (and deleting related buffers when the point is updated).

I am having difficulty with the buffer() and use_proximity.create_buffers(). No matter what I do the input geometry seems to be recognized as invalid. Typical error messages are invalid geometry or 

an error occured with:list indices must be integers or slices, not str
Here is a basic script:
 

 

from arcgis.gis import GIS
from arcgis.features import FeatureLayer
from arcgis.geometry import buffer
from arcgis.features import use_proximity

gis = GIS("home")

# Get our layers
layer_item = gis.content.get("xyzxyzxyz")
point_sublayer = layer_item.layers[1]
polygon_sublayer = layer_item.layers[2]

# Query our point layer
# Can set our_sr to 4326 or 3857. WGS84 or projected.
points = point_sublayer.query(where="1=1", out_fields="*", return_geometry=True,out_sr = 4326)

try:
    for point in points:
        p = point.geometry
        print(type(p))
        buffers = buffer(geometries=[p], distances=[5], unit='meters',out_sr=4326) # fails   
        # extra bit would go here to append against polygon
except Exception as e:
    print(f"an error occured with:{str(e)}")

    
 

 

 

I believe it is because the input is a feature, not a featurecollection or other valid type but I'm not sure how to handle it otherwise.

1 Solution

Accepted Solutions
Clubdebambos
MVP Regular Contributor

Hi @ChristopherCounsell 

I got the same error from your code above. See code snippet below for using the geometry.buffer to return the geometry for the buffer polygon. Most notably use the LengthUnits enum and the Point geometry.

Let me know if this works on your end.

 

from arcgis.geometry import LengthUnits
from arcgis.geometry import Point

...

for point in points:
    try:
        p = Point(point.geometry)
        buffers = buffer(geometries=[p], distances=[5], unit=LengthUnits.METER.value, in_sr=4326)
        print(buffers)
    except Exception as e:
        print(f"an error occured with: {str(e)}")

 

You have inspired this blog post.

All the best,

Glen

~ learn.finaldraftmapping.com

View solution in original post

2 Replies
Clubdebambos
MVP Regular Contributor

Hi @ChristopherCounsell 

I got the same error from your code above. See code snippet below for using the geometry.buffer to return the geometry for the buffer polygon. Most notably use the LengthUnits enum and the Point geometry.

Let me know if this works on your end.

 

from arcgis.geometry import LengthUnits
from arcgis.geometry import Point

...

for point in points:
    try:
        p = Point(point.geometry)
        buffers = buffer(geometries=[p], distances=[5], unit=LengthUnits.METER.value, in_sr=4326)
        print(buffers)
    except Exception as e:
        print(f"an error occured with: {str(e)}")

 

You have inspired this blog post.

All the best,

Glen

~ learn.finaldraftmapping.com
ChristopherCounsell
MVP Regular Contributor

Thanks @Clubdebambos . The amended geometry / units resolved the issue.

For each point, as we only have one buffer, here's the extra piece to create a polygon feature:

        if buffers:
            new_polygon= {
                "geometry": buffers[0],
                "attributes": {
                    "parentglobalid": point.attributes["globalid"]
                    # more attributes here
                }
            }
            # Append the new feature to the polygon layer
            polygon_layer.edit_features(adds=[new_polygon])