擴展GeoServer數據源

    今天咱們來說講怎麼擴展GeoServer(簡稱GS)的數據源。你們都知道,GS支持多種數據源,並且都提供了友好的界面供操做。下面咱們就來簡單介紹一下,如何把自定義的數據源增長到GS中,讓咱們能夠在統一風格的界面上愉快的操做。html

要完成這個任務,須要以下四個步驟(以矢量數據爲例):java

1 建立一個類實現接口org.geotools.data.DataStoreFactorySpi(柵格數據實現org.geotools.coverage.grid.io.GridFormatFactorySpi);web

2 在目錄META-INF/services/下增長文件org.geotools.data.DataStoreFactorySpi,內容爲剛建立的類的徹底類名;app

3 將編譯好的classes複製到<GeoServer install path>\webapps\geoserver\WEB-INF\classes下,或者複製打包的jar文件到<GeoServer install path>\webapps\geoserver\WEB-INF\lib目錄下;webapp

4 重啓GS服務。url

這裏提供一個例子:spa

package wj.demo.geocsv;

import java.awt.RenderingHints.Key;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.logging.Logger;

import org.geotools.data.AbstractDataStoreFactory;
import org.geotools.data.DataAccessFactory;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.DataUtilities;
import org.geotools.util.KVP;
import org.geotools.util.logging.Logging;

public class GeoCSVFileDataStoreFactory extends AbstractDataStoreFactory
        implements DataStoreFactorySpi {

    static final Logger LOGGER = Logging.getLogger("wj.demo.geosvc");

    public static final DataAccessFactory.Param URLP = new DataAccessFactory.Param(
            "url", URL.class, "url to a .csv file", true, null, new KVP(
                    new Object[] { "ext", "csv" }));

    public static final DataAccessFactory.Param GEO_FIELINDEX = new DataAccessFactory.Param(
            "geofield", Integer.class, "Index of geometry field.", true, "0",
            new KVP(new Object[] { "level", "advanced" }));

    public boolean canProcess(Map params) {
        if (!super.canProcess(params))
            return false;
        try {
            URL url = (URL) URLP.lookUp(params);
            if (canProcess(url)) {
                return true;
            }

            Object geofld = GEO_FIELINDEX.lookUp(params);
            File dir = DataUtilities.urlToFile(url);

            return (dir.isDirectory())
                    && ((geofld == null) || (geofld instanceof Integer));
        } catch (IOException e) {
        }
        return false;
    }

    public boolean canProcess(URL f) {
        return (f != null) && (f.getFile().toUpperCase().endsWith("CSV"));
    }

    public String getDescription() {
        return "Comma Separated Values(CSV) file (*.csv)";
    }

    public String getDisplayName() {
        return "CSV file";
    }

    public Param[] getParametersInfo() {
        return new DataAccessFactory.Param[] { URLP, GEO_FIELINDEX };
    }

    public boolean isAvailable() {
        return true;
    }

    public Map<Key, ?> getImplementationHints() {
        return Collections.EMPTY_MAP;
    }

    public DataStore createDataStore(Map<String, Serializable> arg0)
            throws IOException {
        // TODO Auto-generated method stub
        return null;
    }

    public DataStore createNewDataStore(Map<String, Serializable> arg0)
            throws IOException {
        // TODO Auto-generated method stub
        return null;
    }

}
檢驗成果:
用數據源建立界面的先後對比照來講明:
以前:
image 

以後:.net

image

image

後記:3d

GS還提供了自定義錄入界面的方法,這個之後再說。code

相關文章
相關標籤/搜索