Upload
sarah-patrick
View
221
Download
0
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
Programming for Geographical Information Analysis:
Advanced Skills
Lecture 3: Arc Data FrameworkDr Andy Evans
DataGetting maps and layersGetting data points and attributesSorting and searching
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.
Data
Getting maps and layersGetting data points and attributesSorting and searching
Getting data
Map
Feature
LayerFID Data
1 234
2 875
AttributeTable
234 Values
Document (.mxd file)
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.
Getting all Maps
You can also get an Object containing all the Maps.
IMaps maps = mxDoc.getMaps();
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].
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
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
}
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.
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.
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();
}
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.
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();
}
Data
Getting maps and layers
Getting data points and attributesSorting and searching
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.
Getting dataOnce we have our Layer, we want to get data from it.
Map
Feature
LayerFID Data
1 234
2 875
AttributeTable
234 Values
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);
}
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);
}
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.
DataGetting maps and layers
Getting data points and attributes
Sorting and searching
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.
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.
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
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.
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.
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");
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();
Getting data
Map
Feature
LayerFID Data
1 234
2 875
AttributeTable
234 Values
Document (.mxd file)
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.
Next Lecture
Editing data.External applications.
PracticalBuilding a Toolbar.Getting and sorting data.