2

The title kind of saids it all. I am attempting to export a featurelayer to a shapefile and am not having any luck finding a library which includes this functionality or relevant code to see where to get started here. Is this possible? If so, any ideas on how to accomplish the task?

I currently have made a button to make a featurelayer from a selection as follows:

protected void doActionPerformed(ActionEvent e) {
 IFeatureSelection featureSelection = getFeatureSelection();
 if (!(featureSelection instanceof IFeatureLayer)) {
 return;
 }
 FeatureLayer featureLayer;
 try {
 featureLayer = Casting.cast(featureSelection, FeatureLayer.class);
 Date currentDate = new Date();
 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd hh:mm:ss");
 String dateString = dateFormat.format(currentDate);
 IFeatureLayer selectionLayer = featureLayer.createSelectionLayer(featureLayer.getName() + " " + dateString,
 true,
 "",
 "");
 ActiveMapContainer mapContainer = ActiveMapContainer.getInstance();
 mapContainer.getMap().addLayer(selectionLayer);
 } catch (Exception e1) {
 Log.error("CreateLayerFromSelectionAction.class doActionPerformed(ActionEvent e)", e1);
 e1.printStackTrace();
 }
}
asked Jul 14, 2015 at 17:22
1
  • 1
    Look at this (gis.stackexchange.com/questions/26058/…). It is written for .NET, but ESRI is pretty good about using the same names so you should be able to translate it well enough. Commented Jul 14, 2015 at 17:34

1 Answer 1

1

I have solved this issue now and for those interested, the code is posted below... I help you all find this helpful:

public final class ExportLayerToShapefileAction extends SelectionAction {
public ExportLayerToShapefileAction() {
 super("gsExportToShapefile");
}
@Override
protected void doActionPerformed(ActionEvent e) {
 IFeatureSelection featureSelection = getFeatureSelection();
 if (!(featureSelection instanceof IFeatureLayer)) {
 return;
 }
 FeatureLayer featureLayer;
 try {
 featureLayer = Casting.cast(featureSelection, FeatureLayer.class);
 Date currentDate = new Date();
 SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd_hh-mm-ss");
 String dateString = dateFormat.format(currentDate);
 IFeatureLayer selectionLayer = featureLayer.createSelectionLayer(featureLayer.getName() + " " + dateString,
 true,
 "",
 "");
 JFileChooser fileChooser = new JFileChooser();
 fileChooser.setDialogTitle("Select a directory to export to.");
 fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
 int userSelection = fileChooser.showSaveDialog(WinMgr.getInstance().getSelectedNavigator());
 if (!(userSelection == JFileChooser.APPROVE_OPTION)) {
 return;
 }
 FileUtil.createFileIfNotExists(new File(fileChooser.getSelectedFile()
 .getAbsolutePath() + "\\Layer_Export_" + dateString));
 exportLayerToShapefile(fileChooser.getSelectedFile().getAbsolutePath(),
 "Layer_Export_" + dateString,
 selectionLayer,
 null,
 null);
 } catch (Exception e1) {
 e1.printStackTrace();
 }
}
public static boolean exportLayerToShapefile(
 String shapePath,
 String shapeName,
 ILayer source,
 IEnumFieldError[] fieldErrors,
 IEnumInvalidObject invalidObjects) {
 try {
 IFeatureLayer sfeatlayer = (IFeatureLayer) source;
 IFeatureClass sfeatClass = sfeatlayer.getFeatureClass();
 IDataset sdataset = Casting.cast(sfeatClass, IDataset.class);
 IDatasetName sdatasetName = (IDatasetName) sdataset.getFullName();
 ISelectionSet sSelectionSet = (
 (ITable) source).select(new QueryFilter(),
 esriSelectionType.esriSelectionTypeHybrid,
 esriSelectionOption.esriSelectionOptionNormal,
 sdataset.getWorkspace());
 IWorkspaceFactory factory = new ShapefileWorkspaceFactory();
 IWorkspace targetWorkspace = factory.openFromFile(shapePath, 0);
 IDataset targetDataset = (IDataset) targetWorkspace;
 IName targetWorkspaceName = targetDataset.getFullName();
 IWorkspaceName tWorkspaceName = (IWorkspaceName) targetWorkspaceName;
 IFeatureClassName tFeatClassname = new FeatureClassName();
 IDatasetName tDatasetName = (IDatasetName) tFeatClassname;
 tDatasetName.setName(shapeName);
 tDatasetName.setWorkspaceNameByRef(tWorkspaceName);
 IFieldChecker fieldChecker = new FieldChecker();
 IFields sFields = sfeatClass.getFields();
 fieldChecker.setInputWorkspace(sdataset.getWorkspace());
 fieldChecker.setValidateWorkspaceByRef(targetWorkspace);
 fieldChecker.validate(sFields, fieldErrors, null);
 if (fieldErrors != null) {
 System.out.println("Errors encountered during field validation");
 }
 String shapefieldName = sfeatClass.getShapeFieldName();
 int shapeFieldIndex = sfeatClass.findField(shapefieldName);
 IField shapefield = sFields.getField(shapeFieldIndex);
 IGeometryDef geomDef = shapefield.getGeometryDef();
 IClone geomDefClone = (IClone) geomDef;
 IClone targetGeomDefClone = geomDefClone.esri_clone();
 IGeometryDef tGeomDef = (IGeometryDef) targetGeomDefClone;
 IFeatureDataConverter2 featDataConverter = (IFeatureDataConverter2) new FeatureDataConverter();
 invalidObjects = featDataConverter.convertFeatureClass(
 sdatasetName,
 null,
 sSelectionSet,
 null,
 tFeatClassname,
 tGeomDef,
 null,
 "",
 1000, 0);
 return true;
 } catch (IOException | Casting.CastException e) {
 e.printStackTrace();
 }
 return false;
}
}
answered Jul 17, 2015 at 22:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.