<?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 Export selection from AGOL Feature Layer in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-selection-from-agol-feature-layer/m-p/1105234#M6717</link>
    <description>&lt;P&gt;I wish to export a selection (date range from January 1 2019 to January 1 2020) from a AGOL feature layer but am having issue with the end results - the attribute table does not match what I would get otherwise if exporting the same feature layer without the selection (as either a Shapefile and a File Geodatabase). Any ideas?&lt;/P&gt;&lt;P&gt;Here's what I've been trying so far:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;from arcgis.gis import GIS&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;gis = GIS(url, user_name, password)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;sql = "CreationDate &amp;gt; date'2019-01-01 00:00:00 AM' AND CreationDate &amp;lt; date'2020-01-01 00:00:00 AM'"&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;gis.content.get(itemid).layers[0].query(sql).save(folder_path, filename, encoding='utf-8')&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="3" color="#008000"&gt;#output is desired range but attributes get twisted (many strings abbreviated, GlobalID as '0's, dates as yyyy-mm-dd format).&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;I've also tried this but have had similar results:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;from arcgis.gis import GIS&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;gis = GIS(url, user_name, password)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;sql = "CreationDate &amp;gt; date'2019-01-01 00:00:00 AM' AND CreationDate &amp;lt; date'2020-01-01 00:00:00 AM'"&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;gis.content.get(itemid).layers[0].query(sql).features.sdf.spatial.to_featurelayer(output)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="3" color="#008000"&gt;#output is similar to previous though GlobalIDs are presereved - however another step would be required to download (export?) the data (gis.content.get(itemid).export(title, export_format).download(folder_path)?) though this will download the entire dataset and not the selection.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Part of this solution is based on what was found on &lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/how-can-i-export-a-feature-layer-in-agol-to/td-p/781687" target="_blank" rel="noopener"&gt;https://community.esri.com/t5/arcgis-api-for-python-questions/how-can-i-export-a-feature-layer-in-agol-to/td-p/781687&lt;/A&gt;. (Though this did not ultimately help me with my issue - mostly from the final line of 'features.sdf.spatial.to_featureclass('./output.shp')' where the output is a string and not an object, so nothing gets saved out.&lt;/P&gt;&lt;P&gt;Some screenshots:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Export as File Geodatabase:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture1.PNG" style="width: 410px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/24662i01375E00C163E1B6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture1.PNG" alt="Capture1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Export from script (notice 'GlobalID'):&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture3.PNG" style="width: 360px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/24660i8708D8B422CC513E/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture3.PNG" alt="Capture3.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Export as File Geodatabase:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture2.PNG" style="width: 271px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/24661i21B734E21EAFC871/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture2.PNG" alt="Capture2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Export from script (notice abbreviations and capitalizations):&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture4.PNG" style="width: 266px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/24659i95BA125C2A800134/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture4.PNG" alt="Capture4.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Oct 2021 16:31:12 GMT</pubDate>
    <dc:creator>TNGStewardship</dc:creator>
    <dc:date>2021-10-06T16:31:12Z</dc:date>
    <item>
      <title>Export selection from AGOL Feature Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-selection-from-agol-feature-layer/m-p/1105234#M6717</link>
      <description>&lt;P&gt;I wish to export a selection (date range from January 1 2019 to January 1 2020) from a AGOL feature layer but am having issue with the end results - the attribute table does not match what I would get otherwise if exporting the same feature layer without the selection (as either a Shapefile and a File Geodatabase). Any ideas?&lt;/P&gt;&lt;P&gt;Here's what I've been trying so far:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;from arcgis.gis import GIS&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;gis = GIS(url, user_name, password)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;sql = "CreationDate &amp;gt; date'2019-01-01 00:00:00 AM' AND CreationDate &amp;lt; date'2020-01-01 00:00:00 AM'"&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;gis.content.get(itemid).layers[0].query(sql).save(folder_path, filename, encoding='utf-8')&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="3" color="#008000"&gt;#output is desired range but attributes get twisted (many strings abbreviated, GlobalID as '0's, dates as yyyy-mm-dd format).&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;I've also tried this but have had similar results:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;from arcgis.gis import GIS&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;gis = GIS(url, user_name, password)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;sql = "CreationDate &amp;gt; date'2019-01-01 00:00:00 AM' AND CreationDate &amp;lt; date'2020-01-01 00:00:00 AM'"&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier" size="3" color="#003366"&gt;gis.content.get(itemid).layers[0].query(sql).features.sdf.spatial.to_featurelayer(output)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier" size="3" color="#008000"&gt;#output is similar to previous though GlobalIDs are presereved - however another step would be required to download (export?) the data (gis.content.get(itemid).export(title, export_format).download(folder_path)?) though this will download the entire dataset and not the selection.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Part of this solution is based on what was found on &lt;A href="https://community.esri.com/t5/arcgis-api-for-python-questions/how-can-i-export-a-feature-layer-in-agol-to/td-p/781687" target="_blank" rel="noopener"&gt;https://community.esri.com/t5/arcgis-api-for-python-questions/how-can-i-export-a-feature-layer-in-agol-to/td-p/781687&lt;/A&gt;. (Though this did not ultimately help me with my issue - mostly from the final line of 'features.sdf.spatial.to_featureclass('./output.shp')' where the output is a string and not an object, so nothing gets saved out.&lt;/P&gt;&lt;P&gt;Some screenshots:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Export as File Geodatabase:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture1.PNG" style="width: 410px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/24662i01375E00C163E1B6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture1.PNG" alt="Capture1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Export from script (notice 'GlobalID'):&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture3.PNG" style="width: 360px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/24660i8708D8B422CC513E/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture3.PNG" alt="Capture3.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Export as File Geodatabase:&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture2.PNG" style="width: 271px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/24661i21B734E21EAFC871/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture2.PNG" alt="Capture2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Export from script (notice abbreviations and capitalizations):&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture4.PNG" style="width: 266px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/24659i95BA125C2A800134/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture4.PNG" alt="Capture4.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Oct 2021 16:31:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-selection-from-agol-feature-layer/m-p/1105234#M6717</guid>
      <dc:creator>TNGStewardship</dc:creator>
      <dc:date>2021-10-06T16:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Export selection from AGOL Feature Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-selection-from-agol-feature-layer/m-p/1106539#M6730</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I broke up the chain so you can better see what needs to happen:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;In your case, you start with an item.&lt;/LI&gt;&lt;LI&gt;You access a layer in that item and query it with your predefined sql.&lt;/LI&gt;&lt;LI&gt;The result is a FeatureSet object.&lt;/LI&gt;&lt;LI&gt;Create an SEDF from the FeatureSet object.&lt;/LI&gt;&lt;LI&gt;Use spatial.tofeatureclass() to save to a shapefile/Feature Class.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you noted, you could actually stop at #3 and use FeatureSet's&amp;nbsp;&lt;EM&gt;&lt;A href="https://developers.arcgis.com/python/api-reference/arcgis.features.toc.html#arcgis.features.FeatureSet.save" target="_self"&gt;save&lt;/A&gt;&amp;nbsp;&lt;/EM&gt;method. In my experience, this method is not as reliable as working through a SEDF.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
gis = GIS(url, user_name, password)
sql = "CreationDate &amp;gt; date'2019-01-01 00:00:00 AM' AND CreationDate &amp;lt; date'2020-01-01 00:00:00 AM'"
item = gis.content.get(itemid)
fs = item.layers[0].query(sql)
sdf = fs.sdf
sdf.spatial.to_featureclass(location=r"c:\output_examples\your.gdb\test")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you tell us what happens when you try the above? It seems this is what you actually want? What I got out of your explanation is that you both want to download the data locally and publish the result as a Feature Layer.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Oct 2021 21:42:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-selection-from-agol-feature-layer/m-p/1106539#M6730</guid>
      <dc:creator>emedina</dc:creator>
      <dc:date>2021-10-11T21:42:39Z</dc:date>
    </item>
    <item>
      <title>Re: Export selection from AGOL Feature Layer</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/export-selection-from-agol-feature-layer/m-p/1107648#M6742</link>
      <description>&lt;P&gt;Hi emedina,&lt;/P&gt;&lt;P&gt;Thanks for your response. Your&amp;nbsp;suggestion almost got me to where I want to be though I'm still having issue with abbreviations ('FEED' instead of 'Feeding') and blank attributes in the output (shapefile) feature class, though I'm thinking that this is what is going to happen no matter what if converting a file geodatabase feature class to a shapefile. (I just did the feature class to shapefile conversion in ArcMap and got the same results, so I guess this is the results I'm stuck with.)&lt;/P&gt;&lt;P&gt;Anyways, thanks again for your response. Let me know if there is an easy way around maintaining the long form attribute information that was present in the file geodatabase.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Oct 2021 18:53:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/export-selection-from-agol-feature-layer/m-p/1107648#M6742</guid>
      <dc:creator>TNGStewardship</dc:creator>
      <dc:date>2021-10-14T18:53:06Z</dc:date>
    </item>
  </channel>
</rss>

