Currently I am programming an upload module for GeoServer 2.1-RC2. The user should be able to upload a shape file which is automatically installed as datastore in the default workspace. For this intention I didn’t want to use the REST-API of GeoServer, I wanted to create the data store programmatically.
The following piece of code adds a ShapeFile data store to GeoServer:
[java] private boolean addShapeFileDataStore(String title, String description, String pathToFile) {final Catalog catalog = getCatalog();
DataStoreInfo dsInfo = catalog.getFactory().createDataStore();
dsInfo.setName(title);
dsInfo.setDescription(description);
dsInfo.setEnabled(true);
dsInfo.setType("Shapefile");
dsInfo.getConnectionParameters().put("create spatial index", true);
dsInfo.getConnectionParameters().put("charset", "ISO-8859-1");
dsInfo.getConnectionParameters().put("filetype", "shapefile");
dsInfo.getConnectionParameters().put("cache and reuse memory maps", true);
dsInfo.getConnectionParameters().put("url", "file:" + pathToFile);
dsInfo.getConnectionParameters().put("namespace", "http://www.torres.at/");
try {
catalog.add(dsInfo);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Error adding data store to catalog", e);
}
return true;
}
[/java]
It’s quite simple. With this code you can add every kind of data store to GeoServer – all you need to know are the connection parameters. If you do not know the parameters of your kind of data store just create such a data store and write the connection parameters to your log file. You could use the following function:
[java] private void LogDataStoreData(String dataStoreName) {final Catalog catalog = getCatalog();
DataStoreInfo ds = catalog.getDataStoreByName(dataStoreName);
if (ds!=null)
{
Map<String,Serializable> conMap = ds.getConnectionParameters();
LOGGER.finest("Id: " + ds.getId());
LOGGER.finest("Name: " + ds.getName());
LOGGER.finest("Description: " + ds.getDescription());
LOGGER.finest("Type: " + ds.getType());
LOGGER.finest("Connection parameters:");
for(String key : conMap.keySet())
{
LOGGER.finest("\t" + key + ": " + conMap.get(key));
}
}
}
[/java]
Comments 2
hi, i would like to know if it is possible to load a geodatabase file instead of shapefile?
regards,,,jon
It says that “getCatalog() isn’t defined” and “LOGGER cannot be resolved”… I am not good at Java so could you please explain how to solve these errors ?