32
Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Embed Size (px)

DESCRIPTION

ArcMap IMXDocument methods Used for getting data: getActiveView() i.e. layout view or data view data. getFocusMap() i.e. currently selected/shown map. getMaps() i.e. all maps. getSelectedItem() i.e. that the user has picked. getSelectedLayer() i.e. that the user has picked. Documents also implement IDocument, the main use of which is programmatically controlling toolbars.

Citation preview

Page 1: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Programming for Geographical Information Analysis:

Advanced Skills

Lecture 3: Arc Data FrameworkDr Andy Evans

Page 2: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

DataGetting maps and layersGetting data points and attributesSorting and searching

Page 3: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

ArcMap IMXDocument methods

Used for getting data:getActiveView() i.e. layout view or data view data.getFocusMap() i.e. currently selected/shown map.getMaps() i.e. all maps.getSelectedItem() i.e. that the user has picked.getSelectedLayer() i.e. that the user has picked.

Documents also implement IDocument, the main use of which is programmatically controlling toolbars.

Page 4: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Data

Getting maps and layersGetting data points and attributesSorting and searching

Page 5: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting data

Map

Feature

LayerFID Data

1 234

2 875

AttributeTable

234 Values

Document (.mxd file)

Page 6: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting a Map

A Map contains all the data and features in the Data View or each Layout View frame.

import com.esri.arcgis.carto.*;

IMxDocument mxDocument = (IMxDocument)app.getDocument();

IMap mxDoc = mxDocument.getFocusMap();

FocusMap is the one visible in data view or selected in layout view.

Page 7: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting all Maps

You can also get an Object containing all the Maps.

IMaps maps = mxDoc.getMaps();

Page 8: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

IMaps

You can loop through all the IMap Interface objects in an IMaps Interface object using its .getCount and .getItem methods.

IMap map = null;for (int i = 0; i < maps.getCount; i++) { map = maps.getItem(i)}

Other IMaps methods include…add(IMap), create(), remove(IMap), removeAt(index), Reset [Remove all].

Page 9: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting dataIt’s rare we want to get data out of a Map. It’s more usual to get data from a Layer ~ (a Coverage, FeatureDataset, Image etc.).

Map

Feature

LayerFID Data

1 234

2 875

AttributeTable

234 Values

Page 10: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting Layers IIf you know what the type of the Layers are, you can get them thus…

// Assuming we've got a IMap object "map".

ILayer layer = null;

for (int i=0; i < map.getLayerCount(); i++) {layer = map.getLayer(i); // Do something

}

Page 11: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Enumerations

Objects containing lists of other objects. Like a 1D array.

Arc uses them to return arrays of data to you.

Have a next method to get the next object.Also a reset method to return to the start.

ArcObject types have different Enumerations.

e.g. IEnumLayer is the Interface for a set of Layers.

Page 12: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Standard use of EnumerationsIEnumSomething enumSomething =

someEnumGettingMethod();enumSomething.reset();

SomeClass variable = enumSomething.next();

while (variable != null) {\\Do stuff with variable

variable = enumSomething.next();}Note we get the first variable first, then do something with it,

before getting the next and checking whether it is null.

Page 13: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting Layers IIGet an Enumeration of Layers

IEnumLayer enumLayer = map.getLayers(null,true);

enumLayer.reset();

ILayer layer = enumLayer.next();

while (layer != null) {\\ Do something with the ILayerlayer = enumLayer.next();

}

Page 14: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Types of LayerRemember however that we can add many things as Layers (images, data, etc.). Main types:

IFeatureLayer

IGeoFeatureLayer

IGraphicsLayerOthers include more specific FeatureLayers, FDOGraphicsLayers (Annotation), TinLayer, RasterLayer, and CoverageAnnotationLayer.If we don’t know the layers in the document we may need to check for a specific type.

Page 15: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

The instanceof keywordYou can check whether an object implements an Interface using Java’s instanceof keyword. For example, if the user’s selected something in ArcMap's tree of contents, you can test whether it’s a GeoFeatureLayer, thus…

// Assuming we’ve got an enumeration of Layers.IGeoFeatureLayer featLayer = null;ILayer layer = enumLayer.next();

while (layer != null) {if (layer instanceof IGeoFeatureLayer) {featLayer = (IGeoFeatureLayer) layer; //Do something with featLayer }layer = enumLayer.next();

}

Page 16: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Data

Getting maps and layers

Getting data points and attributesSorting and searching

Page 17: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting Data/Features from Layers

Assign your Layer to an appropriate Interface.IGeoFeatureLayer : Treat as geographical dataIFeatureLayer : Treat as a general LayerIAttributeTable : Treat as an attribute table

Get the Attribute Table or search the Layer.

Page 18: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting dataOnce we have our Layer, we want to get data from it.

Map

Feature

LayerFID Data

1 234

2 875

AttributeTable

234 Values

Page 19: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting the Attribute TableAssuming we have a Layer Enumeration, we set the Layer to an IAttributeTable.

import com.esri.arcgis.geodatabase.*;

IAttributeTable pAttributeTable =

(IAttributeTable)enumLayer.next(); ITable table = pAttributeTable.getAttributeTable(); IRow row = null; for (int i = 1; i <= table.rowCount(null); i++) {

row = table.getRow(i);

int index = table.findField("School");Object value = row.getValue(index);

}

Page 20: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting the Attribute TableGet Layer as AttributeTable

Get actual Table

Get each row

Get a particular Field

IAttributeTable pAttributeTable = (IAttributeTable) enumLayer.next();

ITable table = pAttributeTable.getAttributeTable(); IRow row = null; for (int i = 1; i <= table.rowCount(null) ; i++) {

row = table.getRow(i);

int index = table.findField("School");Object value = row.getValue(index);

}

Page 21: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting dataAlternative is to get data from a Feature.

Map

Feature

LayerFID Data

1 234

2 875

AttributeTable

234 Values

First though, we need to get only the features we are interested in. We can search for these.

Page 22: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

DataGetting maps and layers

Getting data points and attributes

Sorting and searching

Page 23: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Searching

You need to get a reference to the data and a search cursor (ICursor / IFeatureCursor).

The search cursor jumps between records in a dataset that match some search criteria.

The cursor marks the row/feature in the dataset that you’re currently interested in.

They have a nextSomething method which gets a object appropriate to the dataset.

Page 24: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Search Methods

IFeatureLayer / IGeoFeatureLayer

IFeatureCursor cursor = featureLayer.search(queryFilter, false);

ITable from an IAttributeTable

ICursor cursor = table.search (queryFilter, false);

Where queryFilter is an IQueryFilter ObjectTrue / false determines how the records are allocated to the Cursor – set to False.

Page 25: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

You can get everything by setting these to null.cursor = pFeatureLayer.search(null, false);

IQueryFilter objects store fields you want returned and query strings.

You need to (rare this) make a QueryFilter object from scratch…

IQueryFilter queryFilter = new QueryFilter();

Making a QueryFilter

Page 26: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

IQueryFilter Objects

pQueryFilter.setSubFields("SCHOOL,CITY");pQueryFilter.addField ("POSTCODE");pQueryFilter.setWhereClause =

"CITY = 'Leeds'";

By default the fields are set to “*” i.e. all fields, so if you use just addField you’ll have to setSubFields to “” first.

Page 27: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Sorting data

You can use searching to also sort data. However, it is a bit hit-and-miss.

We’ll look at the more usual TableSort in the practical.

Page 28: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting Data from a Feature

Use the cursor to get the next feature

IFeature feature = featureCursor.nextFeature();while (feature != null) {

//Do stuff to feature feature = featureCursor.nextFeature();

}

The Feature method getValue(i) takes in an integer number equalling the position of the field column.If you don’t know it, there are lookup methods that return integers…feature.getFields().findField("SCHOOL");

Page 29: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Setting/Getting Features Selected

An alternative is to get the selected Features.

To get the Features you need IMap’s getFeatureSelection method, or an ISelectionSet Object.

IMap’s selectFeature method takes in an ILayer and IFeature.Refresh the display using the MxDocument’s refresh.

mxDoc.getActiveView().refresh();

Page 30: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Getting data

Map

Feature

LayerFID Data

1 234

2 875

AttributeTable

234 Values

Document (.mxd file)

Page 31: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Summary

Get the Application’s Document.Get the Maps from it.Pick the one you want or loop through them.Get a Layer from the Map, or Loop through all of them.Generate an Attribute table and use Rows / Fields to get data.Or use the Layer’s search routine to search for Features.

Page 32: Programming for Geographical Information Analysis: Advanced Skills Lecture 3: Arc Data Framework Dr Andy Evans

Next Lecture

Editing data.External applications.

PracticalBuilding a Toolbar.Getting and sorting data.