<?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 Geographic REST API query in ArcGIS REST APIs and Services Questions</title>
    <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/geographic-rest-api-query/m-p/1311855#M4518</link>
    <description>&lt;P&gt;Hello, I am looking to query a dataset of US roads to just New York State. Here is the dataset in question: &lt;A href="https://connecticut.maps.arcgis.com/home/item.html?id=f42ecc08a3634182b8678514af35fac3" target="_blank"&gt;Transportation - Overview (arcgis.com).&amp;nbsp;&lt;/A&gt;The "Local Roads" layer does not contain a state identifier. Rather than downloading all 17.6 million records, I'd like to only return and download those for New York State. Below is a sample code I have used in the past to extract features from ArcGIS Online. Any suggestions on how to adapt it to query based on geography (envelope or shapefile) would be greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.env.overwriteOutput = True
baseURL = "https://services2.arcgis.com/FiaPA4ga0iQKduv3/arcgis/rest/services/Transportation_v1/FeatureServer/8"
fields = "*"
outdata = "/NYS_Roads.gdb/Local_roads"
# Get record extract limit
urlstring = baseURL + "?f=json"
j = urllib.request.urlopen(urlstring)
js = json.load(j)
maxrc = int(js["maxRecordCount"])
print("Record extract limit: {}".format(maxrc))

# Get object ids of features
where = "1=1"
urlstring = baseURL + "/query?where={}&amp;amp;returnIdsOnly=true&amp;amp;f=json".format(where)
j = urllib.request.urlopen(urlstring)
js = json.load(j)
idfield = js["objectIdFieldName"]
idlist = js["objectIds"]
idlist.sort()
numrec = len(idlist)
print("Number of target records: {}".format(numrec))

# Gather features
print ("Gathering records...")
fs = dict()
for i in range(0, numrec, maxrc):
  torec = i + (maxrc - 1)
  if torec &amp;gt; numrec:
    torec = numrec - 1
  fromid = idlist[i]
  toid = idlist[torec]
  where = "{} &amp;gt;= {} and {} &amp;lt;= {}".format(idfield, fromid, idfield, toid)
  print("  {}".format(where))
  urlstring = baseURL + "/query?where={}&amp;amp;returnGeometry=true&amp;amp;outFields={}&amp;amp;f=json".format(where,fields)
  fs[i] = arcpy.FeatureSet()
  fs[i].load(urlstring)

# Save features
print ("Saving features...")
fslist = []
for key,value in list(fs.items()):
  fslist.append(value)
arcpy.Merge_management(fslist, outdata)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 26 Jul 2023 02:00:42 GMT</pubDate>
    <dc:creator>adam_gallaher</dc:creator>
    <dc:date>2023-07-26T02:00:42Z</dc:date>
    <item>
      <title>Geographic REST API query</title>
      <link>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/geographic-rest-api-query/m-p/1311855#M4518</link>
      <description>&lt;P&gt;Hello, I am looking to query a dataset of US roads to just New York State. Here is the dataset in question: &lt;A href="https://connecticut.maps.arcgis.com/home/item.html?id=f42ecc08a3634182b8678514af35fac3" target="_blank"&gt;Transportation - Overview (arcgis.com).&amp;nbsp;&lt;/A&gt;The "Local Roads" layer does not contain a state identifier. Rather than downloading all 17.6 million records, I'd like to only return and download those for New York State. Below is a sample code I have used in the past to extract features from ArcGIS Online. Any suggestions on how to adapt it to query based on geography (envelope or shapefile) would be greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Adam&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.env.overwriteOutput = True
baseURL = "https://services2.arcgis.com/FiaPA4ga0iQKduv3/arcgis/rest/services/Transportation_v1/FeatureServer/8"
fields = "*"
outdata = "/NYS_Roads.gdb/Local_roads"
# Get record extract limit
urlstring = baseURL + "?f=json"
j = urllib.request.urlopen(urlstring)
js = json.load(j)
maxrc = int(js["maxRecordCount"])
print("Record extract limit: {}".format(maxrc))

# Get object ids of features
where = "1=1"
urlstring = baseURL + "/query?where={}&amp;amp;returnIdsOnly=true&amp;amp;f=json".format(where)
j = urllib.request.urlopen(urlstring)
js = json.load(j)
idfield = js["objectIdFieldName"]
idlist = js["objectIds"]
idlist.sort()
numrec = len(idlist)
print("Number of target records: {}".format(numrec))

# Gather features
print ("Gathering records...")
fs = dict()
for i in range(0, numrec, maxrc):
  torec = i + (maxrc - 1)
  if torec &amp;gt; numrec:
    torec = numrec - 1
  fromid = idlist[i]
  toid = idlist[torec]
  where = "{} &amp;gt;= {} and {} &amp;lt;= {}".format(idfield, fromid, idfield, toid)
  print("  {}".format(where))
  urlstring = baseURL + "/query?where={}&amp;amp;returnGeometry=true&amp;amp;outFields={}&amp;amp;f=json".format(where,fields)
  fs[i] = arcpy.FeatureSet()
  fs[i].load(urlstring)

# Save features
print ("Saving features...")
fslist = []
for key,value in list(fs.items()):
  fslist.append(value)
arcpy.Merge_management(fslist, outdata)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 02:00:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-rest-apis-and-services-questions/geographic-rest-api-query/m-p/1311855#M4518</guid>
      <dc:creator>adam_gallaher</dc:creator>
      <dc:date>2023-07-26T02:00:42Z</dc:date>
    </item>
  </channel>
</rss>

