Need help with creating a Polygon from JSON

2430
2
Jump to solution
05-07-2019 08:30 AM
JeremyRead1
New Contributor III

I am using a Python script to query an ArcGIS Online feature service, and then I want to create records in an on-premise feature class based on the results. I've done this many, many times with point features, but never with polygon features. I cannot seem to figure out how to create a polygon shape from the JSON parameters (coordinates/rings).

for feat in agolQueryJson['features']:
   feature_coords = feat['geometry']['rings']

For the first record, the value of feature_coords is as follows:

[[[3119080.82716037, 10062393.2323891], [3119167.33715791, 10062378.1910806], [3119165.12029902, 10062150.6820212], [3119136.04194509, 10062115.4521047], [3119109.98523858, 10062106.2047479], [3119106.03478733, 10062060.0765594], [3119080.82716037, 10062393.2323891]], [[3119080.82716037, 10062393.2323891], [3119079.78123078, 10062393.4141472], [3119079.72545653, 10062407.7937117], [3119080.82716037, 10062393.2323891]]]

I have not figured out how to take this and turn it into a polygon shape that can be used in an insert cursor. I've tried using arcpy.AsShape and just arcpy.Polygon, but no luck with either method:

poly = arcpy.AsShape(feature_coords)

poly = arcpy.Polygon(arcpy.Array([arcpy.Point(*coords) for coords in feature_coords]))

Any suggestions would be most appreciated!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

You were close... you have a multipart shape (could be a hole or a separate piece)

 z
 
[[[3119080.82716037, 10062393.2323891],
  [3119167.33715791, 10062378.1910806],
  [3119165.12029902, 10062150.6820212],
  [3119136.04194509, 10062115.4521047],
  [3119109.98523858, 10062106.2047479],
  [3119106.03478733, 10062060.0765594],
  [3119080.82716037, 10062393.2323891]],
 [[3119080.82716037, 10062393.2323891],
  [3119079.78123078, 10062393.4141472],
  [3119079.72545653, 10062407.7937117],
  [3119080.82716037, 10062393.2323891]]]

poly = arcpy.Polygon(arcpy.Array([arcpy.Point(*coords) for part in z for coords in part]))

poly
<Polygon object at 0x1a9f648e588[0x1a9f6475dc8]>

poly.partCount
2

poly.area
19990.167093683034

View solution in original post

2 Replies
DanPatterson_Retired
MVP Emeritus

You were close... you have a multipart shape (could be a hole or a separate piece)

 z
 
[[[3119080.82716037, 10062393.2323891],
  [3119167.33715791, 10062378.1910806],
  [3119165.12029902, 10062150.6820212],
  [3119136.04194509, 10062115.4521047],
  [3119109.98523858, 10062106.2047479],
  [3119106.03478733, 10062060.0765594],
  [3119080.82716037, 10062393.2323891]],
 [[3119080.82716037, 10062393.2323891],
  [3119079.78123078, 10062393.4141472],
  [3119079.72545653, 10062407.7937117],
  [3119080.82716037, 10062393.2323891]]]

poly = arcpy.Polygon(arcpy.Array([arcpy.Point(*coords) for part in z for coords in part]))

poly
<Polygon object at 0x1a9f648e588[0x1a9f6475dc8]>

poly.partCount
2

poly.area
19990.167093683034
JeremyRead1
New Contributor III

That did it! Thank you!

0 Kudos