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-Dat...
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
Solved! Go to Solution.
Hi Greta,
You may have already figured this out, but it looks like you missed 'spatial' - it should read:
'xx_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:
from arcgis import GIS
from arcgis.features import FeatureLayerCollection, GeoAccessor, GeoSeriesAccessor
from arcgis.geometry import SpatialReference
import pandas as pd
def search_item(conn, item_name, item_type, flc=False):
search_results = conn.content.search(item_name, item_type=item_type)
proper_index = [i for i, s in enumerate(search_results) if '"' + item_name + '"' in str(s)]
found_item = search_results[proper_index[0]]
if flc == False:
get_item = conn.content.get(found_item.id)
return get_item
if flc == True:
flc = FeatureLayerCollection.fromitem(found_item)
return flc
def main():
conn = GIS("https://www.arcgis.com", "Username", "Password")
flc = search_item(conn, "Name_of_Feature_Layer", "Feature Layer", True)
layer = flc.layers[0]
epsg_code = layer.properties['extent']['spatialReference']['latestWkid']
feature_set = layer.query()
sdf = feature_set.sdf
srs = SpatialReference({"wkid":epsg_code})
sdf.spatial.project(srs)
sdf.spatial.to_featureclass(location="/home/test.shp")
if __name__ == '__main__':
main()
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 Greta,
You may have already figured this out, but it looks like you missed 'spatial' - it should read:
'xx_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:
from arcgis import GIS
from arcgis.features import FeatureLayerCollection, GeoAccessor, GeoSeriesAccessor
from arcgis.geometry import SpatialReference
import pandas as pd
def search_item(conn, item_name, item_type, flc=False):
search_results = conn.content.search(item_name, item_type=item_type)
proper_index = [i for i, s in enumerate(search_results) if '"' + item_name + '"' in str(s)]
found_item = search_results[proper_index[0]]
if flc == False:
get_item = conn.content.get(found_item.id)
return get_item
if flc == True:
flc = FeatureLayerCollection.fromitem(found_item)
return flc
def main():
conn = GIS("https://www.arcgis.com", "Username", "Password")
flc = search_item(conn, "Name_of_Feature_Layer", "Feature Layer", True)
layer = flc.layers[0]
epsg_code = layer.properties['extent']['spatialReference']['latestWkid']
feature_set = layer.query()
sdf = feature_set.sdf
srs = SpatialReference({"wkid":epsg_code})
sdf.spatial.project(srs)
sdf.spatial.to_featureclass(location="/home/test.shp")
if __name__ == '__main__':
main()
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.
Greta