How to get Polygon Centroid / LabelPoint property

1927
4
05-22-2018 06:33 AM
VasileCotovanu1
New Contributor

Check the following Python code

from arcgis.features import FeatureLayer

from arcgis.geometry import Geometry

url = 'https://services.arcgis.com/V6ZHFr6zdgNZuVG0/ArcGIS/rest/services/NYCDemographics1/FeatureServer/0'
layer = FeatureLayer(url)

query = layer.query(
   where = '1=1',
   out_sr = 4326,
   geometry_precision = 6,
   result_record_count = 10,
)
geom = Geometry(query.features[0].geometry)

print (geom.centroid)
print (geom.extent)

I am trying to compute the 'label_point' property of a Polygon object in Python API (1.4)

arcgis.geometry module — arcgis 1.4.1 documentation 

But I get 'None'. Same goes for 'centroid' or other properties, shall I use another constructor for Geometry?

0 Kudos
4 Replies
by Anonymous User
Not applicable

Do you have arcpy or shapely installed in the environment you are using. If not, then many of the geometry operations return None.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Without a GIS object instantiated, geometry operations will be attempted using locally installed geometry libraries, i.e., ArcPy or Shapely.  This is what Aaron is referencing.  If you don't have a local geometry library, the operations will return None (or possibly error).

If you don't want to or can't install a local geometry library, you can use the geometry service from AGOL or an ArcGIS Enterprise.

import arcgis

gis = arcgis.gis.GIS()
url = 'https://services.arcgis.com/V6ZHFr6zdgNZuVG0/ArcGIS/rest/services/NYCDemographics1/FeatureServer/0'


layer = arcgis.features.FeatureLayer(url)
query = layer.query(
   where = '1=1',
   out_sr = 4326,
   geometry_precision = 6,
   result_record_count = 10,
)

label_points = arcgis.geometry.label_points(
    spatial_ref=4326,
    polygons=[feature.geometry for feature in query.features],
    gis = gis
)                                            

print (label_points)
0 Kudos
VasileCotovanu1
New Contributor

Thanks Joshua Bixby‌ and Aaron Pulver‌ - in 'label_points' call I can pass over the GIS object and it works fine

But for 'centroid'? Only via shapely?

My usecase: I am building an API that connects to various ArcGIS REST FeatureServer services, aggregate the content, compute some geometry operations like centroids, label points then spits out GeoJSON. Before implementing all these by myself I want to check if most of these tasks are possible using 3rd party (still maintained) libraries. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Regarding ArcGIS API for Python, yes, you will need a local geometry library (ArcPy or Shapely) installed for it to work.  If you are so inclined, you could retrieve the geometry as GeoJSON and then have some Python library that works on GeoJSON calculate a centroid.  In effect, you could use some other package besides ArcGIS API for Python calculate the centroid of the geometry.

0 Kudos