Transcript 1. Intriduction to Java Programming for Beginners, Novices
Programming for Geographical Information Analysis: Advanced Skills
Lecture 4: Arc Data Editing Addin Programming Dr Andy Evans
Editing
Communications between addins Event-based communication
Putting Arc into an editing session.
Adding a new field/column.
Changing a value.
Editing Data
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.
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);
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;
Editing sessions
iwe.startEditing(true); iwe.startEditOperation(); iwe.stopEditOperation(); iwe.stopEditing(true); Opens a session Starts a group of edits Ends a group of edits 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.
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.
Editing Data
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.
IFieldEdit fieldEdit = field; fieldEdit.setName("Population"); fieldEdit.setType
(esriFieldType.esriFieldTypeInteger); http://help.arcgis.com/en/sdk/10.0/Java_AO_ADF/api/arco bjects/com/esri/arcgis/geodatabase/esriFieldType.html
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).
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.
Editing Data
Data is set using a FeatureCursor to get the feature: IFeatureCursor fCursor = 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.
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);
Change the value
feature.setValue(
columnIndex, Object
); To change a spatial location you set the shape: IPoint point = new Point(); point.setX(300000.0); point.setY(799000.0); feature.setValue(
shapeColumnIndex,
point); Or, better: feature.setShapeByRef(point);
Shapes
Implement com.esri.arcgis.geometry IGeometry .
Include: Line / Polyline Polygon / MultiPatch Point / Multipoint
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.
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.
Summary: Editing
Editing
Communications between addins
Event-based communication
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);
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
.
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?
Simple Singleton
class Singleton { static Singleton single = null; static Singleton getInstance() { if (single = null) { single = new Singleton(); } return single; } // other methods.
}
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.
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.
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.
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.
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.
AddIn Singleton
class AddIn{ private static AddIn addIn = null; public AddIn() { addIn = this; // Grab our static } // variable as Arc makes it.
public static AddIn getInstance() { return addIn; } // other methods.
} Note the use of “this” to get the object we are currently inside.
Again, then, we can: AddIn a = AddIn.getInstance(); a.whateverMethodInSingletonWeWant();
Use
Editing Communications between addins
Event-based communication
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).
Useful
MxDocument: addIDocumentEventsDispListener Map : addIActiveViewEventsListener Map : addIMapEventsListener FeatureLayer : addIFeatureLayerSelectionEventsListener TIN/Raster/Feature/NetworkLayer : addILayerEventsListener
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.
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.
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.
Closing document
((MxDocument)mxDoc).addIDocumentEventsListener( new IDocumentEventsAdapter(){ @Override public boolean beforeCloseDocument (IDocumentEventsBeforeCloseDocumentEvent e){ // Do something when closing document.
} ); }
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.
IDocumentDirty doc = (IDocumentDirty)app.getDocument(); doc.setDirty(); They will then be asked before final closing.
Of course, you can make your own listeners.
Just add a new class to your project/package.
DIY
Java and Databases Inter-addin communication.