Key Ideas
[Part 3 of 12]


In this part, we see two different routes to getting data: either treating it as rows in an attribute table, or treating it as geographical features.

  1. In the following code, we get the FocusMap (the one visible in data view or selected in layout view). We then loop through the layers and keep the last GeofeatureLayer only. This is just for clarity; normally we'd process each GeofeatureLayer inside the loop that finds them. Note also we've taken out unnecessary explicit casts. Finally, note that we've also move the code getting the index of the column we're after outside the loop getting the data for efficiency (in the lecture notes it was in the loop just so we could delimit the functionality of bits of code easily).

    @Override
    public void init(IApplication app){

       // Get the gateway objects.
       IMxDocument mxDoc = app.getDocument();
       IMap mxDoc = mxDocument.getFocusMap();


       // Get a suitable layer.
       IGeoFeatureLayer featLayer = null;
       ILayer layer = enumLayer.next();


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


       // Get the data from the attribute table.
       IAttributeTable pAttributeTable = enumLayer.next();
       ITable table = pAttributeTable.getAttributeTable();
       int index = table.findField("SCHOOL");
       IRow row = null;

       for (int i = 1; i <= table.rowCount(null); i++) {
          row = table.getRow(i);
          Object value = row.getValue(index);
       }
    }

  2. The following code shows two additional options. The first is the use of a query filter, and the second is the direct use of a geographical feature, rather than treating it as a row in an attribute table.

    @Override
    public void init(IApplication app){

       // Get the gateway objects.
       IMxDocument mxDoc = app.getDocument();
       IMap mxDoc = mxDocument.getFocusMap();


       // Get a suitable layer.
       IGeoFeatureLayer featLayer = null;
       ILayer layer = enumLayer.next();


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


       // Get the data using a query filter.
       IAttributeTable pAttributeTable = enumLayer.next();
       ITable table = pAttributeTable.getAttributeTable();


       IQueryFilter queryFilter = new QueryFilter();
       pQueryFilter.setSubFields("SCHOOL,CITY");
       pQueryFilter.addField ("POSTCODE");
       pQueryFilter.setWhereClause = "CITY = 'Leeds'";

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

       // Get each feature in turn from the cursor
       // and get attribute values.
       IFeature feature = featureCursor.nextFeature();
       int index = -1;
       if (feature != null) {
          index = feature.getFields().findField("SCHOOL");
       }

       while (feature != null) {
          feature = featureCursor.nextFeature();
          Object value = feature.getValue(index);
       }
    }

  3. A variety of alternative routes into these data structures exist, including:

    IMXDocument.getFocusMap()
    IMXDocument.getMaps()
    IMXDocument.getSelectedItem()
    IMXDocument.getSelectedLayer()
    IMap.getFeatureSelection()