1 / 39

Programming for Geographical Information Analysis: Advanced Skills

Programming for Geographical Information Analysis: Advanced Skills. Lecture 4: Arc Data Editing Addin Programming Dr Andy Evans. Editing Communications between addins Event-based communication. Editing Data. Putting Arc into an editing session. Adding a new field/column.

toyah
Download Presentation

Programming for Geographical Information Analysis: Advanced Skills

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming for Geographical Information Analysis:Advanced Skills Lecture 4: Arc Data Editing Addin Programming Dr Andy Evans

  2. Editing Communications between addins Event-based communication

  3. Editing Data Putting Arc into an editing session. Adding a new field/column. Changing a value.

  4. Editing sessions Although some cursors can be used directly in some circumstances to edit data, it is usually worth opening an editing session programmatically. To do this, we need the workspace.

  5. Workspaces Open a workspace (casts/try-catches removed throughout): IWorkspaceFactory wsF = new DataSourcesGDB.FileGDBWorkspaceFactoryClass(); IWorkspace ws = wsF.OpenFromFile(geodatabasePath, 0); Find current (note difficulties with proxy classes): IFeatureDatasetProxy ifdp = iGeoFeaturelayer.getFeatureClass(). getFeatureDataset(); IWorkspaceProxy iwp = ifdp.getWorkspace(); IWorkspaceFactory wf = iwp.getWorkspaceFactory(); IWorkspace ws = wf.openFromFile (iwp.getPathName(), 0);

  6. Workspaces Make one: IWorkspaceName wsn = workspaceFactory.create ("c:\temp","tempGDB",null,0); IName name = wsn; IWorkspace iWorkspace = name.open(); Once we have an iWorkspace, cast to IWorkspaceEdit : IWorkspaceEdit iwe = (IWorkspaceEdit) iWorkspace;

  7. Editing sessions iwe.startEditing(true); Opens a session iwe.startEditOperation(); Starts a group of edits iwe.stopEditOperation(); Ends a group of edits iwe.stopEditing(true); Closes session You can do multiple edit operations within a session. They are only needed for operations on features that are part of a Topology or Geometric Network, but are good practice.

  8. Editing Data Putting Arc into an editing session. Adding a new field/column. Changing a field. Adding a field: First make a new Field. Then set it up. Then add it.

  9. Making a field/column IField field = new Field(); Note rare making of a new object Note that the IField label is used to make the object, but we need an IFieldEdit view on it to edit things like the name. IFieldEditfieldEdit = field; fieldEdit.setName("Population"); fieldEdit.setType (esriFieldType.esriFieldTypeInteger); http://help.arcgis.com/en/sdk/10.0/Java_AO_ADF/api/arcobjects/com/esri/arcgis/geodatabase/esriFieldType.html

  10. Adding a Field Get a FeatureClass from the IFeatureLayer IFeatureClass fClass = featurelayer.getFeatureClass(); Add the field to the existing fields. IFields fields = fClass.getFields(); fClass.addField(field); Might want to check the field doesn’t exist with fClass.findField("columnName") first (returns -1 when none found).

  11. Editing data Use a cursor to find the features to edit. Get a feature. Edit its value for a specific column. Tell the cursor to store the changes back into the original database. Release the cursor resources.

  12. Editing Data Data is set using a FeatureCursor to get the feature: IFeatureCursorfCursor = null; Three types: fCursor = fClass.IFeatureClass_update(null,false) fCursor = fClass.IFeatureClass_insert(false) fCursor = fClass.search(null, false) IFeature feature = pFCursor.nextFeature(); Cursors also have methods for adding and deleting rows.

  13. Cursors “Recycling” cursors can reuse resources allocated to a row. We don’t want this, as these temporarily store changes we want. Therefore, we need to use non-recycling cursors: fCursor = fClass.IFeatureClass_insert(false) It also means we must be extra-careful to release resources at the end of editing. To do this we use the ESRI Cleaner class after we’ve finished with the cursor/editing: Cleaner.release(cursor);

  14. Change the value feature.setValue(columnIndex, Object); To change a spatial location you set the shape: IPointpoint = new Point(); point.setX(300000.0); point.setY(799000.0); feature.setValue(shapeColumnIndex,point); Or, better: feature.setShapeByRef(point);

  15. Shapes Implement com.esri.arcgis.geometry IGeometry. Include: Line / Polyline Polygon / MultiPatch Point / Multipoint

  16. Fix the value For an Update/Insert Cursor: fCursor.updateFeature(feature); For a Search Cursor: feature.store(); Note the different objects the methods are in. Note also that because IFeatures actually inherit from IRow, similar things can be done to table rows. See IFeatureClass docs for info. Note that under some circumstances editing using Update and Insert cursors is possible outside of an editing session, but isn’t advised.

  17. Summary: Editing Open an edit session. Open an edit operation. Get a cursor of features to edit. Edit the features. Tell the cursor to fix the changes. Free up cursor resources. Close the edit operation. Close the edit session.

  18. Editing Communications between addins Event-based communication

  19. Communication between addins There are various ways of getting hold of other addins, built into the system. However, these are addin specific. Eg. IDockableWindowManager dwm = new IDockableWindowManagerProxy(app); UID uid = new UID(); uid.setValue(uk.ac.leeds.geog.geog5790.OurWindow); IDockableWindow tableWin = dwm.getDockableWindow(uid);

  20. More generic method Every addin is held as a static variable within ArcGIS. That is, there is only one copy of it. We could get hold of this, if only we had a method to do so. To understand how we can build such a method, we need to understand Singletons.

  21. Singletons Singletons are both a class and a static variable. Because they are static, there is only ever one copy of them. However, they are not troubled by the problems of containing static code, as they are also perfectly normal classes. How is this amazing trick done?

  22. Simple Singleton class Singleton { static Singleton single = null; static Singleton getInstance() { if (single = null) { single = new Singleton(); } return single; } // other methods. }

  23. Use Note that as getInstance is static, we can call it using the class: Singleton s = Singleton.getInstance(); But it returns to us the class as an object we can use: s.whateverMethodInSingletonWeWant(); But the object is static, so if we call getInstance somewhere else, we get exactly the same object, including any changes we’ve made to it in other code.

  24. Simple Singleton However, we want to make sure no one does this: Singleton s = new Singleton(); Let alone this: s.single = someOtherSingleton; So the constructor (unusually) and the variable are set to private, so no one outside the Singleton class can use them. We must include the empty (or otherwise) constructor, to force it to be private.

  25. Simple Singleton class Singleton { private static Singleton single = null; private Singleton() {} public static Singleton getInstance() { if (single = null) { single = new Singleton(); } return single; } // other methods. } Here the constructor is called from within the class, so it works fine, even though the constructor is private.

  26. Uses Wherever you need one specific version of something, e.g. for storage, that everything else can get at. Wherever you need to communicate between different code running on the JVM. e.g. between Applets running in different webpage frames. Note, however, that which can see it will depend on how the JVM classloader is activated.

  27. AddIns As addins are static variables in Arc, if we build them to be Singletons, we can use the Class’ getInstance() method to get hold of them in other code. Note, however, that as Arc is making the static variable, from the class, we shouldn’t. We don’t need to call the constructor. Arc, however, does need access to it, so it must be public.

  28. AddIn Singleton class AddIn{ private static AddInaddIn = null; public AddIn() { addIn = this; // Grab our static } // variable as Arc makes it. public static AddIngetInstance() { return addIn; } // other methods. } Note the use of “this” to get the object we are currently inside.

  29. Use Again, then, we can: AddIn a = AddIn.getInstance(); a.whateverMethodInSingletonWeWant();

  30. Editing Communications between addins Event-based communication

  31. Event communication Arc is, plainly, set up for Event Based Programming. You can register listeners with most GUI components and many non-GUI components (for example, for data changes).

  32. Useful MxDocument: addIDocumentEventsDispListener Map : addIActiveViewEventsListener Map : addIMapEventsListener FeatureLayer : addIFeatureLayerSelectionEventsListener TIN/Raster/Feature/NetworkLayer : addILayerEventsListener

  33. Adapters We saw that java.awt supplies Adapter classes. These are classes that match an interface, but with nothing in the methods. You can extend the class, and then override the methods you want to, without having to do all of them. However, we saw them in anonymous inner classes.

  34. Shutting a Window myFrame.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); Arc has a number of Adapters you can use like this for key jobs. Check what classes implement the Listeners in the docs.

  35. New document ((MxDocument)mxDoc).addIDocumentEventsListener( new IDocumentEventsAdapter(){ @Override public void newDocument (IDocumentEventsNewDocumentEvent e){ // Do something when new document. } } ); NB: This needs some additional try-catch blocks.

  36. Closing document ((MxDocument)mxDoc).addIDocumentEventsListener( new IDocumentEventsAdapter(){ @Override public boolean beforeCloseDocument (IDocumentEventsBeforeCloseDocumentEvent e){ // Do something when closing document. } } );

  37. Dirty, dirty, document If you close a document but have made programmatic changes to it, this can be lost unless the user saves the map. To ask the user if they want to, set the document as “dirty”, e.g. at the end of the beforeCloseDocument method. IDocumentDirtydoc = (IDocumentDirty)app.getDocument(); doc.setDirty(); They will then be asked before final closing.

  38. DIY Of course, you can make your own listeners. Just add a new class to your project/package.

  39. Next Lecture Java and Databases Practical Inter-addin communication.

More Related