Hi.
I would like to write a shapefile from a featureSet created after .query()
As you can see in the picture I have the featureSet, I cast it as a panda.DataFrame but then supposedly I should be able to use the method to_featureclass following the instructions from here: https://developers.arcgis.com/python/guide/introduction-to-the-spatial-dataframe/#Saving-Spatial-DataFrames
But my object does not have this method. I am really confused why it doesn't.
The only question similar to mine that I have found is this one: https://community.esri.com/message/824163-cant-export-spatialdataframe-to-shapefile But I am afraid it didn't help me.
Many thanks for any input.
Greta
Hi Greta,
You may have already figured this out, but it looks like you missed 'spatial' - it should read:
'xyz_sdf.spatial.to_featureclass'
I see this working with the below example script using version 1.6.2 of the API. This particular example maintains the source projection:
<SPAN class="keyword token">from</SPAN> arcgis <SPAN class="keyword token">import</SPAN> GIS <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>features <SPAN class="keyword token">import</SPAN> FeatureLayerCollection<SPAN class="punctuation token">,</SPAN> GeoAccessor<SPAN class="punctuation token">,</SPAN> GeoSeriesAccessor <SPAN class="keyword token">from</SPAN> arcgis<SPAN class="punctuation token">.</SPAN>geometry <SPAN class="keyword token">import</SPAN> SpatialReference <SPAN class="keyword token">import</SPAN> pandas <SPAN class="keyword token">as</SPAN> pd <SPAN class="keyword token">def</SPAN> <SPAN class="token function">search_item</SPAN><SPAN class="punctuation token">(</SPAN>conn<SPAN class="punctuation token">,</SPAN> item_name<SPAN class="punctuation token">,</SPAN> item_type<SPAN class="punctuation token">,</SPAN> flc<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> search_results <SPAN class="operator token">=</SPAN> conn<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>search<SPAN class="punctuation token">(</SPAN>item_name<SPAN class="punctuation token">,</SPAN> item_type<SPAN class="operator token">=</SPAN>item_type<SPAN class="punctuation token">)</SPAN> proper_index <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>i <SPAN class="keyword token">for</SPAN> i<SPAN class="punctuation token">,</SPAN> s <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>search_results<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="string token">'"'</SPAN> <SPAN class="operator token">+</SPAN> item_name <SPAN class="operator token">+</SPAN> <SPAN class="string token">'"'</SPAN> <SPAN class="keyword token">in</SPAN> str<SPAN class="punctuation token">(</SPAN>s<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> found_item <SPAN class="operator token">=</SPAN> search_results<SPAN class="punctuation token">[</SPAN>proper_index<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">if</SPAN> flc <SPAN class="operator token">==</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">:</SPAN> get_item <SPAN class="operator token">=</SPAN> conn<SPAN class="punctuation token">.</SPAN>content<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN>found_item<SPAN class="punctuation token">.</SPAN>id<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> get_item <SPAN class="keyword token">if</SPAN> flc <SPAN class="operator token">==</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">:</SPAN> flc <SPAN class="operator token">=</SPAN> FeatureLayerCollection<SPAN class="punctuation token">.</SPAN>fromitem<SPAN class="punctuation token">(</SPAN>found_item<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">return</SPAN> flc <SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> conn <SPAN class="operator token">=</SPAN> GIS<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"https://www.arcgis.com"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Username"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Password"</SPAN><SPAN class="punctuation token">)</SPAN> flc <SPAN class="operator token">=</SPAN> search_item<SPAN class="punctuation token">(</SPAN>conn<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Name_of_Feature_Layer"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Feature Layer"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN> layer <SPAN class="operator token">=</SPAN> flc<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> epsg_code <SPAN class="operator token">=</SPAN> layer<SPAN class="punctuation token">.</SPAN>properties<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'extent'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'spatialReference'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="string token">'latestWkid'</SPAN><SPAN class="punctuation token">]</SPAN> feature_set <SPAN class="operator token">=</SPAN> layer<SPAN class="punctuation token">.</SPAN>query<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> sdf <SPAN class="operator token">=</SPAN> feature_set<SPAN class="punctuation token">.</SPAN>sdf srs <SPAN class="operator token">=</SPAN> SpatialReference<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">"wkid"</SPAN><SPAN class="punctuation token">:</SPAN>epsg_code<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN> sdf<SPAN class="punctuation token">.</SPAN>spatial<SPAN class="punctuation token">.</SPAN>project<SPAN class="punctuation token">(</SPAN>srs<SPAN class="punctuation token">)</SPAN> sdf<SPAN class="punctuation token">.</SPAN>spatial<SPAN class="punctuation token">.</SPAN>to_featureclass<SPAN class="punctuation token">(</SPAN>location<SPAN class="operator token">=</SPAN><SPAN class="string token">"/home/test.shp"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN> main<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
An alternative approach would be to use the fiona library instead of sdf to write the shapefile. The equivalent workflow is something like this:
Hope this helps!
-Earl
Hi Earl,
Apologies for my confusion and thank you so much for your detailed answer it is super useful!
I have managed to export the data.
Cheers.
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.