Programming for Geographical Information Analysis: Advanced Skills

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

description

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. - PowerPoint PPT Presentation

Transcript of Programming for Geographical Information Analysis: Advanced Skills

Page 1: Programming for  Geographical Information Analysis: Advanced Skills

Programming for Geographical Information Analysis:

Advanced Skills

Lecture 4: Arc Data EditingAddin Programming

Dr Andy Evans

Page 2: Programming for  Geographical Information Analysis: Advanced Skills

EditingCommunications between addinsEvent-based communication

Page 3: Programming for  Geographical Information Analysis: Advanced Skills

Editing Data

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

Page 4: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 5: Programming for  Geographical Information Analysis: Advanced Skills

WorkspacesOpen 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);

Page 6: Programming for  Geographical Information Analysis: Advanced Skills

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;

Page 7: Programming for  Geographical Information Analysis: Advanced Skills

Editing sessions

iwe.startEditing(true); Opens a sessioniwe.startEditOperation(); Starts a group of editsiwe.stopEditOperation(); Ends a group of editsiwe.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.

Page 8: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 9: Programming for  Geographical Information Analysis: Advanced Skills

Making a field/columnIField field = new Field(); Note rare

making of a new objectNote 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/arcobjects/com/esri/arcgis/geodatabase/esriFieldType.html

Page 10: Programming for  Geographical Information Analysis: Advanced Skills

Adding a Field

Get a FeatureClass from the IFeatureLayerIFeatureClass 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).

Page 11: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 12: Programming for  Geographical Information Analysis: Advanced Skills

Editing DataData 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.

Page 13: Programming for  Geographical Information Analysis: Advanced Skills

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);

Page 14: Programming for  Geographical Information Analysis: Advanced Skills

Change the value

feature.setValue(columnIndex, Object);

To change a spatial location you set the shape column “SHAPE”:

IPoint point = new Point();point.setX(300000.0);point.setY(799000.0);feature.setValue(shapeColumnIndex,point);Or, better:feature.setShapeByRef(point);

Page 15: Programming for  Geographical Information Analysis: Advanced Skills

Shapes

Implement com.esri.arcgis.geometry IGeometry.

Include:Line / PolylinePolygon / MultiPatchPoint / Multipoint

Page 16: Programming for  Geographical Information Analysis: Advanced Skills

Fix the valueFor 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.

Page 17: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 18: Programming for  Geographical Information Analysis: Advanced Skills

Editing

Communications between addinsEvent-based communication

Page 19: Programming for  Geographical Information Analysis: Advanced Skills

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);

Page 20: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 21: Programming for  Geographical Information Analysis: Advanced Skills

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?

Page 22: Programming for  Geographical Information Analysis: Advanced Skills

Simple Singleton

class Singleton {static Singleton single = null;

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

}

Page 23: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 24: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 25: Programming for  Geographical Information Analysis: Advanced Skills

Simple Singletonclass 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.

Page 26: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 27: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 28: Programming for  Geographical Information Analysis: Advanced Skills

AddIn Singletonclass 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.

Page 29: Programming for  Geographical Information Analysis: Advanced Skills

Use

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

Page 30: Programming for  Geographical Information Analysis: Advanced Skills

Editing

Communications between addins

Event-based communication

Page 31: Programming for  Geographical Information Analysis: Advanced Skills

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).

Page 32: Programming for  Geographical Information Analysis: Advanced Skills

Useful

MxDocument: addIDocumentEventsDispListener

Map : addIActiveViewEventsListenerMap : addIMapEventsListener

FeatureLayer : addIFeatureLayerSelectionEventsListener

TIN/Raster/Feature/NetworkLayer : addILayerEventsListener

Page 33: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 34: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 35: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 36: Programming for  Geographical Information Analysis: Advanced Skills

Closing document

((MxDocument)mxDoc).addIDocumentEventsListener(new IDocumentEventsAdapter(){

@Override public boolean beforeCloseDocument (IDocumentEventsBeforeCloseDocumentEvent e){

// Do something when closing document.}

});

Page 37: Programming for  Geographical Information Analysis: Advanced Skills

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.

Page 38: Programming for  Geographical Information Analysis: Advanced Skills

DIY

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

Page 39: Programming for  Geographical Information Analysis: Advanced Skills

Next Lecture

Java and Databases

PracticalInter-addin communication.