<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic python script - select by json in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-script-select-by-json/m-p/241435#M18786</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI!&lt;/P&gt;&lt;P&gt;I´m a bit stuck with this easy trouble... I need to make a geoprocessing service, it will take JSON geometry, select features in database and answer questions like "are there any features/how many features/witch features are in that JSON polygon. Im not able to find out how to write python script to select features by JSON... Any advise? Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 11 Sep 2014 15:51:08 GMT</pubDate>
    <dc:creator>J_B__K_</dc:creator>
    <dc:date>2014-09-11T15:51:08Z</dc:date>
    <item>
      <title>python script - select by json</title>
      <link>https://community.esri.com/t5/python-questions/python-script-select-by-json/m-p/241435#M18786</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI!&lt;/P&gt;&lt;P&gt;I´m a bit stuck with this easy trouble... I need to make a geoprocessing service, it will take JSON geometry, select features in database and answer questions like "are there any features/how many features/witch features are in that JSON polygon. Im not able to find out how to write python script to select features by JSON... Any advise? Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Sep 2014 15:51:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-select-by-json/m-p/241435#M18786</guid>
      <dc:creator>J_B__K_</dc:creator>
      <dc:date>2014-09-11T15:51:08Z</dc:date>
    </item>
    <item>
      <title>Re: python script - select by json</title>
      <link>https://community.esri.com/t5/python-questions/python-script-select-by-json/m-p/241436#M18787</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You'll want to make the input JSON a string parameter as use it as an argument to arcpy.AsShape().&lt;/P&gt;&lt;P&gt;The JSON will need to be well formed GeoJSON or EsriJSON, and if the former don;t forget to set a spatial reference to the result.&amp;nbsp; Also don't forget the boolean second argument to AsShape.&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 11 Sep 2014 18:35:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-select-by-json/m-p/241436#M18787</guid>
      <dc:creator>BruceHarold</dc:creator>
      <dc:date>2014-09-11T18:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: python script - select by json</title>
      <link>https://community.esri.com/t5/python-questions/python-script-select-by-json/m-p/241437#M18788</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you. I´ve tested arcpy.AsShape(), but I don´t know how to select features with that polygon... ??? I know it must be something easy, but I can´t solve it...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 12 Sep 2014 07:15:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-select-by-json/m-p/241437#M18788</guid>
      <dc:creator>J_B__K_</dc:creator>
      <dc:date>2014-09-12T07:15:41Z</dc:date>
    </item>
    <item>
      <title>Re: python script - select by json</title>
      <link>https://community.esri.com/t5/python-questions/python-script-select-by-json/m-p/241438#M18789</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;To send a geometry, the geometry has to be specified in JSON format.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;As from ArcGIS 10.1 SP1 you can use the json token &lt;STRONG&gt;SHAPE@JSON&lt;/STRONG&gt; on a feature during a search cursor.&lt;/LI&gt;&lt;LI&gt;In ArcGIS 10.1 the arcpy.Polygon object has a property called "JSON" (&lt;STRONG&gt;arcpy.Polygon.JSON&lt;/STRONG&gt;) which will do this for you.&lt;/LI&gt;&lt;LI&gt;In ArcGIS 10.0 you have to go a little deeper and use the private property &lt;STRONG&gt;arcpy.Polygon.__geo_interface__&lt;/STRONG&gt; &lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Now, what might be a challenge, is (or was) the fact that the Python JSON flavor is not the same flavor ArcGIS Server consumes. This means that you will have to do some manual conversion to obtain the correct format. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14160179378669671" jivemacro_uid="_14160179378669671"&gt;
&lt;P&gt;# let's say you have an arcpy.Polygon object. No get the JSON:&lt;/P&gt;
&lt;P&gt;polygonJSON = polygon.__geo_interface__ # 10.0 syntax&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# convert this to the correct JSON format for ArcGIS Server&lt;/P&gt;
&lt;P&gt;data_string = str(polygonJSON)&lt;/P&gt;
&lt;P&gt;data_string = data_string.replace("'type': 'Polygon', 'coordinates':","'rings':")&lt;/P&gt;
&lt;P&gt;data_string = data_string.replace("(", "[")&lt;/P&gt;
&lt;P&gt;data_string = data_string.replace(")", "]")&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You can use the urllib library to encode all your parameters like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14160179463508488 jive_text_macro" jivemacro_uid="_14160179463508488"&gt;
&lt;P&gt;params = urllib.urlencode({'f': 'json', 'geometryType': 'esriGeometryPolygon', &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'geometry': data_string, 'spatialRel': 'esriSpatialRelIntersects', &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'where': '1=1', 'outFields': '', 'returnGeometry': 'true'})&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you know the service URL to query:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;A class="jive-link-external-small" href="http://someserver/ArcGIS/rest/services/aFolder/sService/MapServer/0" rel="nofollow" target="_blank"&gt;http://someserver/ArcGIS/rest/services/aFolder/sService/MapServer/0&lt;/A&gt;&lt;/EM&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This actual url to connect to will be&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;A class="jive-link-external-small" href="http://someserver/ArcGIS/rest/services/aFolder/sService/MapServer/0" rel="nofollow" target="_blank"&gt;http://someserver/ArcGIS/rest/services/aFolder/sService/MapServer/0&lt;/A&gt;&lt;STRONG&gt;/query&lt;/STRONG&gt; &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To force the POST request you will have to do the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14160180571039727 jive_text_macro" jivemacro_uid="_14160180571039727"&gt;
&lt;P&gt;# assuming this query URL&lt;/P&gt;
&lt;P&gt;queryURL = "&lt;EM&gt;&lt;A class="jive-link-external-small" href="http://someserver/ArcGIS/rest/services/aFolder/sService/MapServer/0" rel="nofollow" target="_blank"&gt;http://someserver/ArcGIS/rest/services/aFolder/sService/MapServer/0&lt;/A&gt;&lt;STRONG&gt;/query&lt;/STRONG&gt;&lt;/EM&gt;"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# use the request method on the urllib2 library&lt;/P&gt;
&lt;P&gt;req = urllib2.Request(queryURL, params) # forces POST request&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# get the response&lt;/P&gt;
&lt;P&gt;response = urllib2.urlopen(req)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The response will be file based GeoJSON. To create a JSON object you will need to do the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_1416018084039664" jivemacro_uid="_1416018084039664"&gt;
&lt;P&gt;# assuming this query URL&lt;/P&gt;
&lt;P&gt;jsonResult = json.load(response)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Next, process the JSON with the results...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Kind regards, Xander&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 15 Nov 2014 02:21:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-select-by-json/m-p/241438#M18789</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2014-11-15T02:21:59Z</dc:date>
    </item>
  </channel>
</rss>

