arcgissamples\geometry\CreateGraphicElements.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package arcgissamples.geometry; import java.io.File; import java.io.IOException; import java.net.UnknownHostException; import com.esri.arcgis.carto.FeatureLayer; import com.esri.arcgis.carto.IEnumLayer; import com.esri.arcgis.carto.ILayer; import com.esri.arcgis.carto.Map; import com.esri.arcgis.carto.MapServer; import com.esri.arcgis.carto.PolygonElement; import com.esri.arcgis.geodatabase.IFeature; import com.esri.arcgis.geodatabase.IFeatureClass; import com.esri.arcgis.geodatabase.IFeatureCursor; import com.esri.arcgis.geometry.ITopologicalOperator; import com.esri.arcgis.interop.AutomationException; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.UID; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; public class CreateGraphicElements { public CreateGraphicElements() { } public static void main(String[] args) { System.out.println("Starting CreateBuffer - An ArcObjects Java SDK Developer Sample"); try { // Initialize the engine and licenses. EngineInitializer.initializeEngine(); AoInitialize aoInit = new AoInitialize(); initializeArcGISLicenses(aoInit); //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); String mxd = devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "mxds" + File.separator + "brazil.mxd"; MapServer mapServer = new MapServer(); mapServer.connect(mxd); CreateGraphicElements cb = new CreateGraphicElements(); cb.bufferAndCreateElements((Map) mapServer.getMap("Geography"), 4.00); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * This demonstrates buffering off selected elements in a map and adding the buffers as graphics to the graphics * container * * @param map * - this map must have selected features * @param bufferDistance * - the distance in map units to buffer off of features */ public void bufferAndCreateElements(Map map, double bufferDistance) { try { FeatureLayer layer = (FeatureLayer) getLayer(map, "Valves"); IFeatureClass fc = layer.getFeatureClass(); IFeatureCursor featureCursor = fc.search(null, false); IFeature feature = featureCursor.nextFeature(); /* * If you plan to use this code with features that are already selected in a map, * then the following snippet will be useful for that scenario * * if (map.getSelectionCount() < 1) { System.out.println("Your map did not have anything selected"); return; } // get the selected features into a MapSelection to allow for looping MapSelection mapSelection = new MapSelection(map.getFeatureSelection()); // Set the MapSelection at the beginning of the Enumeration mapSelection.reset(); IFeature feature = mapSelection.next(); */ while (feature != null) { System.out.println(feature.getFeatureType()); // use the "interface" here because we don't want to restrict // the type of feature (line, point, polygon) to buffer ITopologicalOperator topologicalOperator = (ITopologicalOperator) feature.getShape(); PolygonElement polygonElement = new PolygonElement(); // Set the geometry for the polygonElement = to the result of // the buffer operation polygonElement.setGeometry(topologicalOperator.buffer(bufferDistance)); // now add the polygon element to the graphics container map.addElement(polygonElement, 0); // get the next element //feature = mapSelection.next(); feature = featureCursor.nextFeature(); } } catch (Exception e) { System.out.println("Threw an exception in bufferToGraphics "); e.printStackTrace(); } } /** * Retrieves ID of a layer in MapEJB based on its name * * @param mapServer * @param layerName * @return */ private ILayer getLayer(Map map, String layerName) throws IOException, AutomationException { UID uid = new UID(); uid.setValue("{40A9E885-5533-11d0-98BE-00805F7CED21}");//for feature layers IEnumLayer enumLayers = map.getLayers(uid, true); ILayer layer = enumLayers.next(); while(layer != null) { if (layerName.equalsIgnoreCase(layer.getName())) { return layer; } layer = enumLayers.next(); } return null; } /** * Initializes the lowest available ArcGIS License */ private static void initializeArcGISLicenses(AoInitialize aoInit) { try { if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) { aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); } else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) { aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); } else { System.err.println("Could not initialize an Engine or ArcView license. Exiting application."); System.exit(-1); } } catch (Exception e) { e.printStackTrace(); } } }