Export geometry to file

392
5
Jump to solution
03-09-2021 05:33 AM
Labels (1)
Guglielmo_Cordasco
New Contributor

Hi, I'm using a SOE and I would like to export a geometry to file in order to share it.
I tried with ExportToESRIShape (like in this example) but saving the output buffer in a file I didn't obtain a known format (e.g. shp or zip).
Anyone knows about another method? or an ExportToESRIShape  example to export to file?

Thanks in advance,
Guglielmo.

1 Solution

Accepted Solutions
nicogis
MVP Frequent Contributor

In help you need see class that implement the interface IFeatureClassName

nicogis_0-1616011373470.png

You can write similar code:

IMapServer mapServer = (IMapServer)serverObjectHelper.ServerObject;
            string mapName = mapServer.DefaultMapName;
            IMapServerDataAccess dataAccess = (IMapServerDataAccess)mapServer;
            IFeatureClass sourceFeatureClass = (IFeatureClass)dataAccess.GetDataSource(mapName, 0);

            IDataset sourceDataSet = (IDataset)sourceFeatureClass;
            IWorkspace sourceWorkspace = sourceDataSet.Workspace;

            IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
            IWorkspaceFactory targetWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace targetWorkspace = targetWorkspaceFactory.OpenFromFile($@"c:\temp", 0);
            IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
            IName targetWorkspaceDatasetName = targetWorkspaceDataset.FullName;
            IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDatasetName;
            IFeatureClassName shpFeatureClassName = new FeatureQueryNameClass();
            IDatasetName shpDatasetName = (IDatasetName)shpFeatureClassName;
            shpDatasetName.WorkspaceName = targetWorkspaceName;
            shpDatasetName.Name = $"Test_{Guid.NewGuid()}";

            int shapeFieldPosition = sourceFeatureClass.FindField(sourceFeatureClass.ShapeFieldName);
            IFields inputFields = sourceFeatureClass.Fields;
            IField shapeField = inputFields.get_Field(shapeFieldPosition);

            IGeometryDef geometryDef = (IGeometryDef)((IClone)shapeField.GeometryDef).Clone();

            IFields outFields = (IFields)((IClone)sourceFeatureClass.Fields).Clone();

            featureDataConverter.ConvertFeatureClass((IFeatureClassName)sourceDataSet.FullName, null, null, shpFeatureClassName, geometryDef, outFields, "", 1000, 0);

 

View solution in original post

5 Replies
AlexRodriguez
New Contributor III

First, I don't know what SOE is.  But I don't think it matters.  I'm responding to make sure you know that an ESRI shapefile is not a SINGLE file.  An ESRI Shapefile is a collection of files (minimum 3, maximum like 7).  So when you say you didn't get  a known format (shp or zip) you won't get a single file.  If you export to a shapefile you will get a collection of files with the same root name and each having a different filename extension (ie: .shp, .dbf, .prj, .shx, ...)

0 Kudos
nicogis
MVP Frequent Contributor

if can be useful you can see my soe https://github.com/nicogis/Factory-Utilities-SOE

here you have an operation ('ExtractData') https://github.com/nicogis/Factory-Utilities-SOE/blob/37f3a4868d85435bf03cb3b6648a1a7dedccae40/Facto... where if you can pass a featureset from client you can select parameter dataFormat (SHAPEFILE) to dowload file zipped

nicogis
MVP Frequent Contributor

If you want use sdk you can use FeatureDataConverter class  ( FeatureDataConverter Class (arcgis.com) )

- create a scratch file gdb (try and see if you can use ProConcepts DDL · Esri/arcgis-pro-sdk Wiki · GitHub ) or use a template gdb empty on server

- use method convertfeatureclass ( IFeatureDataConverter2 Interface | ArcGIS Enterprise SDK )

- zip gdb

- download file (see this samples arcgis-enterprise-sdk-resources/Samples/dot net/SOE/DownloadFileRESTSOE at master · Esri/arcgis-ente... )

Guglielmo_Cordasco
New Contributor

Thanks nicogis!


I tried to use your ConvertFeatureClassToShapefile.
ConvertFeatureClassToShapefile takes as input IWorkspace sourceWorkspace and I have created a sourceWorkspace with:

IMapServer mapServer = (IMapServer)serverObjectHelper.ServerObject;
string mapName = mapServer.DefaultMapName;
// Use IMapServerDataAccess to get the data
IMapServerDataAccess dataAccess = (IMapServerDataAccess)mapServer;
// Get access to the source feature class.
IFeatureClass m_fc = (IFeatureClass)dataAccess.GetDataSource(mapName, 0);// Layer 0
IDataset pDataSet = (IDataset)m_fc;
IWorkspace sourceWorkspace = pDataSet.Workspace;


in ConvertFeatureClassToShapefile it is used new FeatureClassNameClass() does not exist in the SDK.
I replaced it with (IFeatureClassName)pDataSet.FullName but calling (IFeatureClass)sourceName.Open() I get System.Runtime.InteropServices.COMException (0x80040351): Exception from HRESULT: 0x80040351 at ESRI.ArcGIS.esriSystem.IName.Open()
Where am I doing wrong?

Thanks again.

0 Kudos
nicogis
MVP Frequent Contributor

In help you need see class that implement the interface IFeatureClassName

nicogis_0-1616011373470.png

You can write similar code:

IMapServer mapServer = (IMapServer)serverObjectHelper.ServerObject;
            string mapName = mapServer.DefaultMapName;
            IMapServerDataAccess dataAccess = (IMapServerDataAccess)mapServer;
            IFeatureClass sourceFeatureClass = (IFeatureClass)dataAccess.GetDataSource(mapName, 0);

            IDataset sourceDataSet = (IDataset)sourceFeatureClass;
            IWorkspace sourceWorkspace = sourceDataSet.Workspace;

            IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
            IWorkspaceFactory targetWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace targetWorkspace = targetWorkspaceFactory.OpenFromFile($@"c:\temp", 0);
            IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
            IName targetWorkspaceDatasetName = targetWorkspaceDataset.FullName;
            IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDatasetName;
            IFeatureClassName shpFeatureClassName = new FeatureQueryNameClass();
            IDatasetName shpDatasetName = (IDatasetName)shpFeatureClassName;
            shpDatasetName.WorkspaceName = targetWorkspaceName;
            shpDatasetName.Name = $"Test_{Guid.NewGuid()}";

            int shapeFieldPosition = sourceFeatureClass.FindField(sourceFeatureClass.ShapeFieldName);
            IFields inputFields = sourceFeatureClass.Fields;
            IField shapeField = inputFields.get_Field(shapeFieldPosition);

            IGeometryDef geometryDef = (IGeometryDef)((IClone)shapeField.GeometryDef).Clone();

            IFields outFields = (IFields)((IClone)sourceFeatureClass.Fields).Clone();

            featureDataConverter.ConvertFeatureClass((IFeatureClassName)sourceDataSet.FullName, null, null, shpFeatureClassName, geometryDef, outFields, "", 1000, 0);

 

View solution in original post