Select to view content in your preferred language

Alternative way to save Feature Set to Shapefile?

898
13
09-10-2025 07:54 AM
PeterPurnyn
Emerging Contributor

I can't save featuresets to shapefiles, convert between shapely arcgis polygon, calculate area, get enrichment data...

def save_polygon_to_shapefile(
        self, polygon: Polygon, polygon_type: str, filename: str
    ) -> tuple[pathlib.Path, str]:
        """Saves a arcgis.geometry.polygon to a shapefile as a Feature Set.

        Args:
            polygon (Polygon): an arcgis.geometry.polygon object
            polygon_type (str): a string representing the type of polygon as defined in the config file
            filename (str): a string representing the name of the file to save.

        Returns:
            tuple[pathlib.Path, str]: a tuple containing a patlib.path to the file directory and the filename
        """
        file_dir, filename = self.generate_polygon_file_path(
            polygon_type=polygon_type, filename=filename
        )
        f = Feature(geometry=polygon, attributes={})
        fset = FeatureSet([f])
        try:
            """
            Note: 
            Arcgis featureset.save splits by . from the right and takes the first part as the file name, 
            so we must make sure to add a decimal after the filename if the unit in the polygon type has a decimal point
            but the actual extension (shp) added here doesnt actually matter its just for readability.
            """
            fset.save(save_location=file_dir, out_name=f"{filename}.shp")
        except Exception as e:
            self.logger.warning(e, exc_info=True)
            return None, None
        return file_dir, filename
I keep getting this error:
The Product License has not been initialized.

 

How do I save a feature set to a shapefile without depending on Arcgis Pro?

 

0 Kudos
13 Replies
PeterPurnyn
Emerging Contributor

I am running python 3.11.8 but it's not a requirement.
I have arcgis pro 3.3 installed, but I am trying to move away from that.
My current environment has  arcgis=2.3.0=py311_6408 

When I try using `conda install -c esri arcgis' it adds arcgispro.

I will try to install with pip and get back to you.

0 Kudos
Clubdebambos
MVP Regular Contributor

Hi @PeterPurnyn,

How are you accessing ArcGIS Online in the script? 

~ learn.finaldraftmapping.com
0 Kudos
PeterPurnyn
Emerging Contributor
In this script the only import I have are these:
from
arcgis.geometry import Polygon as Polygon
from arcgis.features import Feature, FeatureSet

I do not use a GIS.
0 Kudos
Clubdebambos
MVP Regular Contributor

Hi @PeterPurnyn,

Do you get the same issue with the code below? Just set a folder below.

from arcgis.features import FeatureSet, Feature

## dictionary definition a feature
f_dict = {
    "geometry": {
        "x": -8238318.738276444, "y": 4970309.724235498,
        "spatialReference": {
            "wkid": 102100, "latestWkid": 3857}
        },
    "attributes": {
        "Incident_Type": "Structural-Sidewalk Collapse",
        "Location": "927 Broadway",
        "Borough": "Manhattan",
        "Closed_Date": None,
        "Latitude": 40.7144215406227,
        "Longitude": -74.0060763804198
    }
}

# create a Feature object
f = Feature.from_dict(f_dict)

# create a FeatureSet object
fs = FeatureSet(features=[f])

## set the output folder
folder = r"C:\path\to\output\folder"

## save as a shapefile
fs.save(
    save_location=folder,
    out_name="test.shp"
)

 

~ learn.finaldraftmapping.com
0 Kudos