Python Summzarize_within attribute error?

313
3
Jump to solution
10-15-2019 03:25 PM
Highlighted
New Contributor

In arcgis online i have a webmap where i did a sumamrize within of a bunch of points into polygons to give those polygons density colours based on the number of points that are within them, now I want to do the same thing but solely in python. my points are the "QFESFireStations", and my polygons are the "Drought Affected Shires; QLD". My code is below. Also, I dont get why the summary_polygons layer is optional, because the tool NEEDS two layers, so how can only one layer be required for an input??? See the links below.

For some reason when i run this below i get (AttributeError: 'Item' object has no attribute 'summarize_within')

from arcgis.geoanalytics.summarize_data import summarize_within
webmap.summarize_within(summarized_layer="QFESFireStations", output_name = "SEE_ME",
polygon_or_bin = "POLYGONS", bin_type = "SQUARE",
summary_polygons="Drought Affected Shires; QLD", sum_shape = True)


https://esri.github.io/arcgis-python-api/apidoc/html/arcgis.geoanalytics.summarize_data.html#summari...

----------------------------------------------------------

Also, this is just a note I have three variable types

webmap = item
NewWebMap = MapView 
usable = WebMap

---------------------------------------------------------

I tried the samething using arcpy but also didnt work.

# https://pro.arcgis.com/en/pro-app/tool-reference/big-data-analytics/summarize-within.htm

import arcpy
drought = gis.content.get('4e73c854013a43beb258150d4be7e135')
Fire_station = gis.content.get('84fead439e2444a299d75296c365aba7')
outFS = 'SEE_ME1'
dataStore = "SEE_ME_DataStore1"

# Execute SummarizeWithin
arcpy.geoanalytics.SummarizeWithin(Fire_station, outFS, "POLYGON", None, None, drought, "ADD_SUMMARY", dataStore)

This gives the error (RuntimeError: Object: Error in executing tool)

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Contributor

Hi Garas Lewis,

Thank you for reaching out with your questions regarding GeoAnalytics Server tools.

First, I'll clarify why summary_polygons is an optional parameter. Note that the tool Summarize Within has two options for aggregation:

  1. Aggregate by polygons - In this case the polygon layer is required and bin parameters are not
  2. Aggregate by bins - In this case the bin type and bin size parameters are required, and summary polygons are not

So, the summary_polygons parameter is optional since choosing to summarize into bins does not require a summary_polygons layer. 

Second, let's solve the error (AttributeError: 'Item' object has no attribute 'summarize_within') when trying to run your ArcGIS Python API script. It looks like your code is trying to call the tool from the webmap (like "webmap.summarize_within"), but it should be calling it from the geoanalytics module (like "geoanalytics.summarize_within").

Before we move onto fixing up the scripts, it's important to note that there are a few ways to run Summarize Within:

  1. In ArcGIS Online using the ArcGIS API for Python. See the documentation and examples here: arcgis.features.analysis module — arcgis 1.6.2 documentation 
  2. In ArcGIS Enterprise using standard analysis tools or GeoAnalytics tools. GeoAnalytics tools require a GeoAnalytics Server.
  3. Using the same tools through arcpy (GeoAnalytics tools or standard tools)

I have outlined each of these methods below, but please ask me if for further clarification if you are still unsure which one you should be using!

Solution 1) In ArcGIS Online using the ArcGIS API for Python

Below is a sample script to get the ArcGIS Online tool working using the ArcGIS Python API. For more help, see the documentation here: arcgis.features.analysis module — arcgis 1.6.2 documentation 

# Run the ArcGIS Online tool tool using ArcGIS Python API scripting

# Import modules
from arcgis.gis import GIS
from arcgis.features.summarize_data import summarize_within

# Use your Online connection to specify where you want to run the tool
gis = GIS("https://www.arcgis.com/home/", "your_username", "your.password")

# Set the parameters to run the ArcGIS Online Summarize Within tool with
sum_within_layer = "https://services1.arcgis.com/abc123/arcgis/rest/services/Drought Affected Shires/FeatureServer/0"
summary_layer = "https://services1.arcgis.com/abc123/arcgis/rest/services/QFESFireStations/FeatureServer/0"
output_name = "SEE_ME_arcgisOnline_pythonAPI"
sum_shape=True
shape_units=None
summary_fields=[]
group_by_field=None
minority_majority=False
percent_shape=False

# Run the ArcGIS Online Summarize Within tool
summarized_result = summarize_within(sum_within_layer, summary_layer, sum_shape, shape_units, summary_fields,
group_by_field, minority_majority, percent_shape,
output_name)

Solution 2) In ArcGIS Enterprise using standard analysis tools or GeoAnalytics tools.

Below is a sample script to get the GeoAnalytics tool working using the ArcGIS Python API. For more help, see the documentation here: arcgis.geoanalytics.summarize_data module — arcgis 1.6.2 documentation 

# Run the ArcGIS Enterprise tool tool using ArcGIS Python API scripting

# Import modules
import arcgis
from arcgis.gis import GIS

# Import the GeoAnalytics Server tool you want to run
from arcgis.geoanalytics.summarize_data import summarize_within

# Use your Portal connection to specify where you want to run the tool
gis = GIS("https://<my_domain>.com/portal", "your_username", "your.password")

# Check that your Portal is GeoAnalytics enabled
# If this returns false, please see the end of this post for a standard analysis solution!
check_geoanalytics = arcgis.geoanalytics.is_supported()
print("The check for GeoAnalytics returned {}".format(check_geoanalytics))

# Set tool variables
summarized_layer = "https://<my_domain>.com/server/rest/services/Hosted/QFESFireStations/0"
summary_polygons = "https://<my_domain>.com/server/rest/services/Hosted/Drought Affected Shires/0"
output_name = "SEE_ME_geoanalytics_pythonAPI"

# Run the tool using the ArcGIS Python API
summarize_result = summarize_within(summarized_layer, summary_polygons,
bin_type=None,bin_size=None,
bin_size_unit=None, standard_summary_fields=None,
weighted_summary_fields=None, sum_shape=False, shape_units=None,
group_by_field=None, minority_majority=False, percent_shape=False,
output_name)

To get the scripts above to work, you will have to replace the URL with your domain and double check that the rest endpoints for the layers are correct. 

Solution 3) Using the same tools through arcpy (GeoAnalytics tools or standard tools)

Finally, to solve the arcpy error (RuntimeError: Object: Error in executing tool), you can modify and run the sample scripts provided below in your ArcGIS Pro python environment. This should solve the error.

If you intended to use the ArcGIS Enterprise GeoAnalytics tool via arcpy, below is a sample script to complete the workflow. For more information, see the documentation here: https://pro.arcgis.com/en/pro-app/tool-reference/big-data-analytics/summarize-within.htm.

# Run the GeoAnalytics tool using ArcGIS Pro arcpy scripting

# import modules
import arcpy
from arcpy import geoanalytics

# Set the parameters you want to use in the GeoAnalytics Server tool
summarized_layer = "QFESFireStations"
summary_polygons = "Drought Affected Shires; QLD"
output_name = "SEE_ME_geoanalytics_arcpy"
polygon_or_bin = "POLYGON"

# Run the GeoAnalytics Server tool using arcpy
summarize_result = arcpy.geoanalytics.SummarizeWithin(summarized_layer, output_name, polygon_or_bin,
'', None, summary_polygons, "NO_SUMMARY", '', None, None,
"SPATIOTEMPORAL_DATA_STORE", None, "NO_MIN_MAJ", None)

If you intended to use the ArcGIS Enterprise Standard tool via arcpy, here is a sample script to complete the workflow. For more information, see the documentation here: Summarize Within—Standard Feature Analysis tools | ArcGIS Desktop 

# Run the Standard tool using ArcGIS Pro arcpy scripting

# Import modules

import arcpy

from arcpy import sfa

# Set the parameters you want to use in the standard analysis tool

summarized_layer = "https://<my_domain>.com/server/rest/services/Hosted/QFESFireStations/0" 
summary_polygons = "https://<my_domain>.com/server/rest/services/Hosted/Drought Affected Shires/0" 

# Run the standard analysis tool via arcpy

summarize_result = arcpy.sfa.SummarizeWithin(summary_polygons, summarized_layer, "SEE_ME_standardAnalysis_arcpy", "ADD_SHAPE_SUM", '', None, None, "NO_MIN_MAJ", "NO_PERCENT")

For the above scripts to work, you may have to replace summarized_layer and summary_polygons with full URL pathways to the layer, like in solution 2.

Please let me know if you have any questions regarding these scripts, or anything else related to your workflow. I hope this helps!

Cheers,

Bethany

View solution in original post

Reply
0 Kudos
3 Replies
Highlighted
Esri Contributor

Hi Garas Lewis,

Thank you for reaching out with your questions regarding GeoAnalytics Server tools.

First, I'll clarify why summary_polygons is an optional parameter. Note that the tool Summarize Within has two options for aggregation:

  1. Aggregate by polygons - In this case the polygon layer is required and bin parameters are not
  2. Aggregate by bins - In this case the bin type and bin size parameters are required, and summary polygons are not

So, the summary_polygons parameter is optional since choosing to summarize into bins does not require a summary_polygons layer. 

Second, let's solve the error (AttributeError: 'Item' object has no attribute 'summarize_within') when trying to run your ArcGIS Python API script. It looks like your code is trying to call the tool from the webmap (like "webmap.summarize_within"), but it should be calling it from the geoanalytics module (like "geoanalytics.summarize_within").

Before we move onto fixing up the scripts, it's important to note that there are a few ways to run Summarize Within:

  1. In ArcGIS Online using the ArcGIS API for Python. See the documentation and examples here: arcgis.features.analysis module — arcgis 1.6.2 documentation 
  2. In ArcGIS Enterprise using standard analysis tools or GeoAnalytics tools. GeoAnalytics tools require a GeoAnalytics Server.
  3. Using the same tools through arcpy (GeoAnalytics tools or standard tools)

I have outlined each of these methods below, but please ask me if for further clarification if you are still unsure which one you should be using!

Solution 1) In ArcGIS Online using the ArcGIS API for Python

Below is a sample script to get the ArcGIS Online tool working using the ArcGIS Python API. For more help, see the documentation here: arcgis.features.analysis module — arcgis 1.6.2 documentation 

# Run the ArcGIS Online tool tool using ArcGIS Python API scripting

# Import modules
from arcgis.gis import GIS
from arcgis.features.summarize_data import summarize_within

# Use your Online connection to specify where you want to run the tool
gis = GIS("https://www.arcgis.com/home/", "your_username", "your.password")

# Set the parameters to run the ArcGIS Online Summarize Within tool with
sum_within_layer = "https://services1.arcgis.com/abc123/arcgis/rest/services/Drought Affected Shires/FeatureServer/0"
summary_layer = "https://services1.arcgis.com/abc123/arcgis/rest/services/QFESFireStations/FeatureServer/0"
output_name = "SEE_ME_arcgisOnline_pythonAPI"
sum_shape=True
shape_units=None
summary_fields=[]
group_by_field=None
minority_majority=False
percent_shape=False

# Run the ArcGIS Online Summarize Within tool
summarized_result = summarize_within(sum_within_layer, summary_layer, sum_shape, shape_units, summary_fields,
group_by_field, minority_majority, percent_shape,
output_name)

Solution 2) In ArcGIS Enterprise using standard analysis tools or GeoAnalytics tools.

Below is a sample script to get the GeoAnalytics tool working using the ArcGIS Python API. For more help, see the documentation here: arcgis.geoanalytics.summarize_data module — arcgis 1.6.2 documentation 

# Run the ArcGIS Enterprise tool tool using ArcGIS Python API scripting

# Import modules
import arcgis
from arcgis.gis import GIS

# Import the GeoAnalytics Server tool you want to run
from arcgis.geoanalytics.summarize_data import summarize_within

# Use your Portal connection to specify where you want to run the tool
gis = GIS("https://<my_domain>.com/portal", "your_username", "your.password")

# Check that your Portal is GeoAnalytics enabled
# If this returns false, please see the end of this post for a standard analysis solution!
check_geoanalytics = arcgis.geoanalytics.is_supported()
print("The check for GeoAnalytics returned {}".format(check_geoanalytics))

# Set tool variables
summarized_layer = "https://<my_domain>.com/server/rest/services/Hosted/QFESFireStations/0"
summary_polygons = "https://<my_domain>.com/server/rest/services/Hosted/Drought Affected Shires/0"
output_name = "SEE_ME_geoanalytics_pythonAPI"

# Run the tool using the ArcGIS Python API
summarize_result = summarize_within(summarized_layer, summary_polygons,
bin_type=None,bin_size=None,
bin_size_unit=None, standard_summary_fields=None,
weighted_summary_fields=None, sum_shape=False, shape_units=None,
group_by_field=None, minority_majority=False, percent_shape=False,
output_name)

To get the scripts above to work, you will have to replace the URL with your domain and double check that the rest endpoints for the layers are correct. 

Solution 3) Using the same tools through arcpy (GeoAnalytics tools or standard tools)

Finally, to solve the arcpy error (RuntimeError: Object: Error in executing tool), you can modify and run the sample scripts provided below in your ArcGIS Pro python environment. This should solve the error.

If you intended to use the ArcGIS Enterprise GeoAnalytics tool via arcpy, below is a sample script to complete the workflow. For more information, see the documentation here: https://pro.arcgis.com/en/pro-app/tool-reference/big-data-analytics/summarize-within.htm.

# Run the GeoAnalytics tool using ArcGIS Pro arcpy scripting

# import modules
import arcpy
from arcpy import geoanalytics

# Set the parameters you want to use in the GeoAnalytics Server tool
summarized_layer = "QFESFireStations"
summary_polygons = "Drought Affected Shires; QLD"
output_name = "SEE_ME_geoanalytics_arcpy"
polygon_or_bin = "POLYGON"

# Run the GeoAnalytics Server tool using arcpy
summarize_result = arcpy.geoanalytics.SummarizeWithin(summarized_layer, output_name, polygon_or_bin,
'', None, summary_polygons, "NO_SUMMARY", '', None, None,
"SPATIOTEMPORAL_DATA_STORE", None, "NO_MIN_MAJ", None)

If you intended to use the ArcGIS Enterprise Standard tool via arcpy, here is a sample script to complete the workflow. For more information, see the documentation here: Summarize Within—Standard Feature Analysis tools | ArcGIS Desktop 

# Run the Standard tool using ArcGIS Pro arcpy scripting

# Import modules

import arcpy

from arcpy import sfa

# Set the parameters you want to use in the standard analysis tool

summarized_layer = "https://<my_domain>.com/server/rest/services/Hosted/QFESFireStations/0" 
summary_polygons = "https://<my_domain>.com/server/rest/services/Hosted/Drought Affected Shires/0" 

# Run the standard analysis tool via arcpy

summarize_result = arcpy.sfa.SummarizeWithin(summary_polygons, summarized_layer, "SEE_ME_standardAnalysis_arcpy", "ADD_SHAPE_SUM", '', None, None, "NO_MIN_MAJ", "NO_PERCENT")

For the above scripts to work, you may have to replace summarized_layer and summary_polygons with full URL pathways to the layer, like in solution 2.

Please let me know if you have any questions regarding these scripts, or anything else related to your workflow. I hope this helps!

Cheers,

Bethany

View solution in original post

Reply
0 Kudos
Highlighted
New Contributor

Thank you so much!

Also justed wanted to ask:

(1)

how do i add another layer and change when it turns on based on zoom? Like if i zoom out I want a layer to deactivate(hide), and when i zoom in i want to be able to see it again. Like i have a bunch of layers here for example and i dont want the map to be cluttered. And how do i get the legend to appear, i found ".legend" but it doesn't work?

WebMap = gis.map('Queensland')
WebMap.add_layer(analysis_layer)
WebMap.add_layer(point_layer)
WebMap.legend = True
WebMap

(2)

Is there a way to aggregate points? for example if i have a point layer with lots of points can i merge those points together based on some distance? for that layer?

--------------------------NOTES FOR OTHERS-----------------------------

Heres a couple more points I just learned to add.

(1)

# Change a mapserver layer to a featurelayer in case someone needs to know how

from arcgis.features import FeatureLayer
drought_polygons = FeatureLayer(url="https://gisservices.information.qld.gov.au/arcgis/rest/services/Farming/DroughtDeclarations/MapServer/0")

Thank you so much AGAIN!

Reply
0 Kudos
Highlighted
Esri Contributor

Hi Garas Lewis‌!

I'm happy to see the scripts were helpful. 

The best steps to get the appropriate answers to your follow up questions would be:
 - Please open two new GeoNet questions; one with each of the questions you asked above. I work with GeoAnalytics analysis, but someone from the appropriate part of the software will be glad to help answer your new questions.  

- In your new question 1, could you please clarify if you are using the Python API for scripting, and where you are trying to make the legend visible (i.e. in a custom app, in Jupyter notebooks, etc)?

Feel free to post links to your new questions in this thread so we can forward them on to the correct contact.  

Thank you! 

Bethany

Reply
0 Kudos