Shapefile load-on-demand

The Maps package provides classes to perform load-on-demand on Shapefiles. This is achieved by the use of specific spatial index files. These files, usually having an . idx extension, store relations between tiles and object identifiers that belong to these tiles. A class and a tool example are provided to generate these spatial index files. A generic tile loader is also provided to minimize the amount of code needed to implement the load-on-demand mechanism using Shapefiles.
The load-on-demand mechanism involves two classes in addition to the shape reader and the dbf reader: the IlvShapeFileIndex class and the IlvShapeSpatialIndex class. A utility class is also provided to generate the spatial index for a given Shapefile: the IlvShapeFileTiler class.
The mechanism used to store and retrieve objects by tiles is illustrated in the following diagram:
mapsprg_intro6.gif
The Spatial Index file holds objects identifiers for each tile. Objects identifiers are their ordinal place in the IndexFile. Geometries are retrieved in the Shapefile using the IndexFile. In the following example, the tile [2, 1] (tiles indices begin at 0) contains identifiers 2, 5 and 9 referring to the geometries g2 g5 and g9.
The classes used to perform load-on-demand on the Shapefiles are:

The IlvShapeFileIndex Class

This class allows you to directly access geometries in a Shapefile. The spatial index and the Shapefile must correspond to the same theme:
// Open the index file.
IlvShapeFileIndex index = new IlvShapeFileIndex("example.shx");
// Open the corresponding Shapefile.
IlvSHPReader shape = new IlvSHPReader("example.shp");
// Retrieve the feature for each index.
int count = index.getRecordCount();
for(int i = 0; i < count; i++)
    IlvMapFeature f = shape.getFeatureAt(i);

The IlvShapeSpatialIndex Class

This class stores tile information: tile size and count, and identifiers of the objects belonging to each tile. To retrieve objects from a tile specified by its row and column, use the getIdArray method:
// Open the spatial index file.
IlvShapeSpatialIndex spatialIndex =
    new IlvShapeSpatialIndex("example.shx");
// Loop on all columns and rows.
for(int c = 0; c < spatialindex.getColumnCount(); c++) {
    for(int r = 0; r < spatialindex.getRowCount(); r++) {
        // Retrieve the IDs of objects belonging to the tile at row ‘r’ and 
        // column ‘c’.
        int[] ids = spatialindex.getIdArray(c, r);
        // Loop on these IDs and get the corresponding map feature.
        for(int i =0; i < ids.length; i++) {
            IlvMapFeature f = shape.getFeatureAt(i);
        }
    }
}

The IlvShapeFileTiler Class

This class is used to generate tiling information from a given Shapefile. To use this class you have to provide the Shapefile to tile, the SpatialIndexFile to write to, and either the tile size or the number of rows and columns.
IlvShapeFileTiler.CreateShapeSpatialIndex("example.shp", 
                                          "example.idx", 
                                                     5., 10.);              
The above code extract produces a SpatialIndexFile named example.idx with a tile size of width 5 and height 10.
IlvShapeFileTiler.CreateShapeSpatialIndex("example.shp", 
                                          "example.idx", 
                                          20, 30);
The above code extract produces a SpatialIndexFile of 600 tiles, 20 columns and 30 rows.