Extending a tile source provider

Feedback


The TileSourceProvider class. Custom tile format can be encapsulated as tile source for tiling, which possess the capability of creating and loading tileset and allows users to store tile as the extended format during tiling.

During extension, the TileSourceProvider class must be created under the com.supermap.services.tilesource.impl package. Otherwise, the tile format will not be able to be found during distributed tiling after deployment. The extension is as follows:

@TileSourceProviderAnnotation(infoType = com.supermap.services.tilesource.NationalCacheStandardTileSourceInfo.class, name = "NationalMap", isLocal = true)
public class NationalCacheStandardTileSourceProvider extends AbstractTileSourceProvider<NationalCacheStandardTileSourceInfo> {
}

@TileSourceProviderAnnotation Labels the TileSourceInfo class, name, whether the tile is stored locally or not. infoType indicates tile source corresponding to the current tile provider, which is NationalCacheStandardTileSourceInfo Extended in Previous Section for this example. isLocal represents the storage type, with true indicating local disk storage and false indicating distributed storage.

During extension, unimplemented methods in the AbstractTileSourceProvider class should be noticed, including:

Field/Method Description
protected boolean doConnect(NationalCacheStandardTileSourceInfo tilesourceInfo) Create and connect storage path to store tiles to the path.
protected Tileset<ImageMetaData, ImageTileInfo> doCreateTileset(MetaData metaData) Create a tileset.
public Tileset<ImageMetaData, ImageTileInfo>[] getTilesets() Get the Tileset collection in Provider.
public Tileset<ImageMetaData, ImageTileInfo> getTileset(String name) Get a tileset according to the name.
public void refresh() Refresh the opeation to load Tileset that has not been loaded in folder.
protected boolean doDisConnect() Break the connection to clear all tilesets.

 

  1. Specify to use custom tile format and create tileset of the type, NationalCacheStandardTileset in this example.
        private ConcurrentHashMap<String, NationalCacheStandardTileset> tilesets = new ConcurrentHashMap<String, NationalCacheStandardTileset>();
    @Override
    protected Tileset<ImageMetaData, ImageTileInfo> doCreateTileset(MetaData metaData) {
        if (!(metaData instanceof ImageMetaData)) {
            throw new IllegalArgumentException("NationalCacheStandard TileSource only support ImageMetaData");
        }
        NationalCacheStandardTileset tileset = new NationalCacheStandardTileset(new File(outputFile, metaData.mapName));
        tilesets.put(tileset.getName(), tileset);
        return tileset;
    }
  1. Create tile storage directory according to extended TileSourceInfo class and acquire tilesets of the extended format to the created directory.
    @Override
    protected boolean doConnect(NationalCacheStandardTileSourceInfo tilesourceInfo) {
        String outputPath = tilesourceInfo.getOutputPath();
        if (isEmpty(outputPath)) {
            return false;
        }
        //Create tile storage directory
        outputFile = new File(outputPath);
        if (!outputFile.exists()) {
            outputFile.mkdirs();
        }
        //Store tilesets to the created directory
        File[] files = outputFile.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                NationalCacheStandardTileset tileset = new NationalCacheStandardTileset(file);
                tilesets.put(tileset.getName(), tileset);
            }
        }
        return true;
    }
  1. Get the Tileset collection in Provider
    @Override
    public Tileset<ImageMetaData, ImageTileInfo>[] getTilesets() {
        Collection<NationalCacheStandardTileset> tilesets = this.tilesets.values();
        int size = tilesets.size();
        NationalCacheStandardTileset[] tilesetArray = new NationalCacheStandardTileset[size];
        tilesets.toArray(tilesetArray);
        return tilesetArray;
    }
  1. Get a tileset according to the name
    @Override
    public Tileset<ImageMetaData, ImageTileInfo> getTileset(String name) {
        return tilesets.get(name);
    }
  1. Refresh the opeation to load Tileset that has not been loaded in folder
    @Override
    public void refresh() {
        if (!connected.get()) {
            return;
        }
        try {
            lock.lock();
            File[] files = outputFile.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    NationalCacheStandardTileset tileset = new NationalCacheStandardTileset(file);
                    if (!tilesets.containsKey(tileset.getName())) {
                        tilesets.put(tileset.getName(), tileset);
                    }
                }
            }
        } finally {
            lock.unlock();
        }
    }
  1. Break the connection to clear all tilesets
    @Override
    protected boolean doDisConnect() {
        this.tilesets.clear();
        this.outputFile = null;
        return true;
    }

The complete extension example is as follows:

NationalCacheStandardTileSourceProvider.java