地图编辑 - 基于Java语言ArcEngine平台开发


//基于 java 语言开发的以 arcgis Engine 为平台的地图编辑程序,开以平台为 Eclipse J2EE, jdk1.7,ArcEngine10. 0,以下为主程序。 package arcgissamples.editing; //引用包: import java.awt.BorderLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import javax.swing.UIManager; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.carto.FeatureLayer; import com.esri.arcgis.carto.IFeatureLayer; import com.esri.arcgis.carto.ILayer; import com.esri.arcgis.controls.ControlsEditingSketchContextMenu; import com.esri.arcgis.controls.ControlsEditingTaskToolControl; import com.esri.arcgis.controls.ControlsEditingToolbar; import com.esri.arcgis.controls.ControlsMapNavigationToolbar; import com.esri.arcgis.controls.ControlsRedoCommand; import com.esri.arcgis.controls.ControlsUndoCommand; import com.esri.arcgis.controls.EngineEditor; import com.esri.arcgis.controls.IMapControlEvents2Adapter; import com.esri.arcgis.controls.IMapControlEvents2OnMouseDownEvent; import com.esri.arcgis.controls.ToolbarMenu; import com.esri.arcgis.controls.esriToolbarOrientation; import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory; import com.esri.arcgis.geodatabase.IFeatureClass; import com.esri.arcgis.geodatabase.IFeatureWorkspace; import com.esri.arcgis.geodatabase.IWorkspace; import com.esri.arcgis.geodatabase.IWorkspaceFactory; import com.esri.arcgis.interop.AutomationException; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; import com.esri.arcgis.systemUI.ControlsOperationStack; import com.esri.arcgis.systemUI.esriCommandStyles; public class EditingApplication extends JFrame{ private static final long serialVersionUID = 1L; private MapBean map; private ToolbarBean editToolbar, navToolbar; private ToolbarMenu menu; public static void main(String[] args) throws Exception { // initialize the interop initializeInterop(); // initialize to a license level initializeArcGISLicenses(); // set native look and feel UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // start the sample final EditingApplication thisClass = new EditingApplication(); thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SwingUtilities.invokeLater(new Runnable() { public void run() { thisClass.initializeUI(); thisClass.setVisible(true); } }); } public void initializeUI() { try { this.setTitle("捕捉编辑器"); this.add(getMap(), BorderLayout.CENTER); //添加地图页面 this.add(getEditToolbar(), BorderLayout.NORTH); //添加编辑工具 this.add(getNavToolbar(), BorderLayout.WEST); //添加导航工具 this.add(new EventsPane(), BorderLayout.EAST); //添加事件面板 this.setSize(800, 700); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { EngineEditor editor = new EngineEditor(); //Save or discard outstanding edits if(editor.hasEdits()){ int result = JOptionPane.showConfirmDialog(null, "Do you want to save your edits?","Edits", JOptionPane.YES_NO_OPTION); editor.stopEditing((result==JOptionPane.YES_OPTION?true:false)); } //Always cleanly shutdown the interop new AoInitialize().shutdown(); } catch (Exception swallow) { // dont care } } }); } catch (Exception e) { e.printStackTrace(); } } public MapBean getMap() throws Exception { if (map == null) { map = new MapBean(); // Add a layer to the map to perform some edits on map.addLayer(getHighwayLayer(), 0); // Add a listener to listen to the mouse down event map.addIMapControlEvents2Listener(new IMapControlEvents2Adapter() { private static final long serialVersionUID = 1L; public void onMouseDown(IMapControlEvents2OnMouseDownEvent event) throws IOException, AutomationException { // display the Editing menu on right-click if (event.getButton() == 2) { try { getMenu().popupMenu(event.getX(), event.getY(), getMap().getHWnd()); } catch (Exception e) { e.printStackTrace(); } } } }); } return map; } private ILayer getHighwayLayer() throws Exception { //Get DEVKITHOME Home final String devKitHome = System.getenv("AGSDEVKITJAVA"); if (devKitHome == null) { System.out.println("The developer kit environment variable is not set, exiting application"); System.exit(-1); } // Get the ushigh.shp shapefile from sample data and return it as ILayer IWorkspaceFactory factory = new ShapefileWorkspaceFactory(); IWorkspace workspace = factory.openFromFile(devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa", 0); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace) workspace; IFeatureClass featureClass = featureWorkspace .openFeatureClass("ushigh"); ILayer layer = new FeatureLayer(); layer.setName("Highways"); ((IFeatureLayer) layer).setFeatureClassByRef(featureClass); return layer; } public ToolbarMenu getMenu() throws Exception { if (menu == null) { menu = new ToolbarMenu(); //Set the hook for the context menu, otherwise it will appear diabled menu.setHook(getEditToolbar()); menu.addItem(new ControlsEditingSketchContextMenu(), 0, 0, false, esriCommandStyles.esriCommandStyleIconOnly); menu.setCommandPoolByRef(getEditToolbar().getCommandPool()); } return menu; } public ToolbarBean getNavToolbar() throws Exception { if (navToolbar == null) { navToolbar = new ToolbarBean(); navToolbar.setBuddyControl(getMap()); navToolbar.addItem(new ControlsMapNavigationToolbar(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); navToolbar .setOrientation(esriToolbarOrientation.esriToolbarOrientationVertical); navToolbar.setCommandPoolByRef(getEditToolbar().getCommandPool()); navToolbar.setSize(100, 25); } return navToolbar; } public ToolbarBean getEditToolbar() throws Exception { if (editToolbar == null) { editToolbar = new ToolbarBean(); editToolbar.setBuddyControl(getMap()); editToolbar.setOperationStackByRef(new ControlsOperationStack()); editToolbar.addItem(new ControlsUndoCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); editToolbar.addItem(new ControlsRedoCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); editToolbar.addItem(new ControlsEditingTaskToolControl(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); editToolbar.addItem(new ControlsEditingToolbar(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); } return editToolbar; } static void initializeInterop() { // Visual beans mode required for multi-threaded applciations like // Swing. EngineInitializer.initializeVisualBeans(); } static void initializeArcGISLicenses() { try { AoInitialize ao = new AoInitialize(); if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcEditor) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor); else { System.err .println("Could not initialize an Engine or ArcEditor license. Exiting application."); System.exit(-1); } } catch (Exception e) { e.printStackTrace(); } } } 调用的事件面板类,其扩展了 JTabbePane。 package arcgissamples.editing; 引用包: import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Comparator; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.JTextPane; import com.esri.arcgis.controls.EngineEditor; import com.esri.arcgis.controls.IEngineEditEvents; public class EventsPane extends JTabbedPane implements ActionListener { private EngineEditor editor; public EventsPane() { super(); initializeUI(); //初始化 UI try{ editor = new EngineEditor(); //Register the sample's event listener final EditEventsListener listener = new EditEventsListener(this); editor.addIEngineEditEventsListener(listener); //Add checkboxes for each Editing event Method[] events = IEngineEditEvents.class.getMethods(); //Sort the array based on event names 按事件名排序 Arrays.sort(events, new Comparator(){ public int compare(Method o1, Method o2) { return o1.getName().compareTo(o2.getName()); } }); //For each event, add a checkbox to the UI 为每一个事件添加 checkbox 到 UI for (final Method event : events) { final JCheckBox chkBox = new JCheckBox(event.getName()); getPanelInput().add(chkBox); //Update the event listener whenever the user selects or clears the checkbox chkBox.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e) { listener.update(event.getName(),chkBox.isSelected()); } }); } }catch(Exception ex){ ex.printStackTrace(); } } //respond to user actions public void actionPerformed(ActionEvent e) { if(e.getSource()==btnClearOutput){ //Clear the output getTextOutput().setText(""); }else if(e.getSource()==btnClearAll){ //Clear the output getTextOutput().setText(""); //Clear all event checkboxes for(int i=0;i shouldListen = new HashMap(); public EditEventsListener(EventsPane pane) { //EventsPane will be used to print the events this.pane = pane; } public void update(String eventName, boolean isInterested) { shouldListen.put(eventName, isInterested); } private void print(final String event) { //if user is interested in the event if (shouldListen.get(event)!=null&&shouldListen.get(event)==true){ //print it on the EventsPane through Swing's EDT (event dispatching thread) SwingUtilities.invokeLater(new Runnable(){ public void run() { pane.getTextOutput().setText( pane.getTextOutput().getText()+ "- "+ event + "()\n"); } }); } } public void onAbort(IEngineEditEventsOnAbortEvent arg0) throws IOException, AutomationException { print("onAbort"); } public void onAfterDrawSketch(IEngineEditEventsOnAfterDrawSketchEvent arg0) throws IOException, AutomationException { print("onAbort"); } public void onBeforeStopEditing( IEngineEditEventsOnBeforeStopEditingEvent arg0) throws IOException, AutomationException { print("onBeforeStopEditing"); } public void onBeforeStopOperation( IEngineEditEventsOnBeforeStopOperationEvent arg0) throws IOException, AutomationException { print("onBeforeStopOperation"); } public void onChangeFeature(IEngineEditEventsOnChangeFeatureEvent arg0) throws IOException, AutomationException { print("onChangeFeature"); } public void onConflictsDetected( IEngineEditEventsOnConflictsDetectedEvent arg0) throws IOException, AutomationException { print("onConflictsDetected"); } public void onCreateFeature(IEngineEditEventsOnCreateFeatureEvent arg0) throws IOException, AutomationException { print("onCreateFeature"); } public void onCurrentTaskChanged( IEngineEditEventsOnCurrentTaskChangedEvent arg0) throws IOException, AutomationException { print("onCurrentTaskChanged"); } public void onCurrentZChanged(IEngineEditEventsOnCurrentZChangedEvent arg0) throws IOException, AutomationException { print("onCurrentZChanged"); } public void onDeleteFeature(IEngineEditEventsOnDeleteFeatureEvent arg0) throws IOException, AutomationException { print("onDeleteFeature"); } public void onSaveEdits(IEngineEditEventsOnSaveEditsEvent arg0) throws IOException, AutomationException { print("onSaveEdits"); } public void onSelectionChanged(IEngineEditEventsOnSelectionChangedEvent arg0) throws IOException, AutomationException { print("onSelectionChanged"); } public void onSketchFinished(IEngineEditEventsOnSketchFinishedEvent arg0) throws IOException, AutomationException { print("onSketchFinished"); } public void onSketchModified(IEngineEditEventsOnSketchModifiedEvent arg0) throws IOException, AutomationException { print("onSketchModified"); } public void onStartEditing(IEngineEditEventsOnStartEditingEvent arg0) throws IOException, AutomationException { print("onStartEditing"); } public void onStartOperation(IEngineEditEventsOnStartOperationEvent arg0) throws IOException, AutomationException { print("onStartOperation"); } public void onStopEditing(IEngineEditEventsOnStopEditingEvent arg0) throws IOException, AutomationException { print("onStopEditing"); } public void onStopOperation(IEngineEditEventsOnStopOperationEvent arg0) throws IOException, AutomationException { print("onStopOperation"); } public void onTargetLayerChanged( IEngineEditEventsOnTargetLayerChangedEvent arg0) throws IOException, AutomationException { print("onTargetLayerChanged"); } public void onVertexAdded(IEngineEditEventsOnVertexAddedEvent arg0) throws IOException, AutomationException { print("onVertexAdded"); } public void onVertexDeleted(IEngineEditEventsOnVertexDeletedEvent arg0) throws IOException, AutomationException { print("onVertexDeleted"); } public void onVertexMoved(IEngineEditEventsOnVertexMovedEvent arg0) throws IOException, AutomationException { print("onVertexMoved"); } }
还剩15页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享pdf获得金币 ] 1 人已下载

下载pdf

pdf贡献者

learner

贡献于2014-06-29

下载需要 5 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf