Select to view content in your preferred language

JSON string input coordinate pairs

4044
8
Jump to solution
09-14-2015 10:27 AM
JamesCrandall
MVP Alum

I am unable to properly setup a Toolbox script with a single input parameter to parse a json string.  I will be receiving the JSON as:

{

  
"rings" :

    [

    
[[-117,34],[-116,34],[-117,33],[-117,34]],

    
[[-115,44],[-114,43],[-115,43],[-115,44]]

  
]

  },

  {

  
"rings" :

    [

    
[[32,17],[31,17],[30,17],[30,16],[32,17]]

  
]

  }

So far I have tried to setup the script/tool with a String parameter and attempted to pull in the coordinate pairs and create a polygon feature but it's treating the entire string as a single value and I'm not sure of the best way to parse it into float values.

Any input is appreciated

0 Kudos
1 Solution

Accepted Solutions
BruceHarold
Esri Regular Contributor

Try importing the json module and use json.loads

Regards

View solution in original post

8 Replies
BruceHarold
Esri Regular Contributor

Try importing the json module and use json.loads

Regards

JamesCrandall
MVP Alum

Thanks Bruce!

I've updated the input json string to incorporate a geoJSON format so that it's easier to just convert to polygons.  My task is to return a centroid xy for the input point features (which is why I am attempting to construct a polygon).

The below is failing on the arcpy.AsShape()

feature_info = '{"type": "polygon", "coordinates": [[-117.00,34.00],[-116.00,34.00],[-117.00,33.00],[-117.00,34.00]],"spatialReference" : {"wkid" : 4326}}'
jstr = json.loads(feature_info)
print jstr
inpoly = arcpy.AsShape(jstr) #fails with error: type object argument after * must be a sequence, not a float
pcent = arcpy.Point
pcent = inpoly.centroid
pcx = pcent.X
pcy = pcent.Y
0 Kudos
JamesCrandall
MVP Alum

Oh!  I see my error!

This has the correct JSON format:

feature_info = '{"type": "polygon", "coordinates": [[[-117.00,34.00],[-116.00,34.00],[-117.00,33.00],[-117.00,34.00]]]}'
jstr = json.loads(feature_info)
print jstr
inpoly = arcpy.AsShape(jstr) #fails with error: type object argument after * must be a sequence, not a float
pcent = arcpy.Point
pcent = inpoly.centroid
pcx = pcent.X
pcy = pcent.Y
print str(pcx) + " / " + str(pcy)
0 Kudos
WesMiller
Deactivated User

I did one using the following code(async)

import urllib
import json
URL  = 'your/url'
result = urllib.urlopen(URL).read()
addrData = json.loads(result)
JamesCrandall
MVP Alum

Thanks Wes!

I'm still not entirely certain how this will all be integrated.  I've been asked to create a Geoprocessing Service and my original thought was the calling app would pass in a JSON string (as in my OP).  So at the moment I'm just trying to figure out how to just hardcode this as a python script and then eventually publish it as a service.


Can you suggest the type of input parameter I should consider?  Per your sample, it'd be a "String" representation of the url, correct?

Thanks!

0 Kudos
WesMiller
Deactivated User

Yes you are correct. It is a string representation, in my case i'm passing 'url?'+'Street=' + address + '&outFields=&maxLocations=&outSR='+str(src)+'&searchExtent=&f=Json' to a published geocoder and getting the x,y to determine if the user is in or out of ball districts(soccer, baseball)

BruceHarold
Esri Regular Contributor

If this is all Esri stack then you can publish a feature service of your polygons that you're testing for intersection and use the geocode JSON as input to a Query call and check for distance.

JamesCrandall
MVP Alum

Update:


I successfully published the service and able to execute the task, pass in a well-formatted JSON string and return the centroid coordinates.  As the service publisher, I'm just going to place the requirements on the application developer to perform the necessary formatting.

note: oh how things have changed and now I'm the "configuration specialist" instead of the "app developer"!