概述
Geoserver服务发布(postgis、tiff、shpfile)
package com.sxqx.geoserver;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.decoder.RESTDataStore;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSGeoTIFFDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSPostGISDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSShapefileDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
/**
* @author : ZhangHan
* @description :
* @time : 2020/7/14 9:47
**/
public class GeoService {
//
private String url="http://localhost:8080/geoserver";
//
//
private String username="admin";
//
//
private String password="geoserver";
private String url;
private String username;
private String password;
//
private GeoProperties.PostGis postGis;
//
@Autowired
//
private GeoProperties properties;
//
//
private String url;
//
//
private String username;
//
//
private String password;
//发布postgis中的数据
public Boolean GeoserverPublishPostGISData(String workSpace, String storeName, String tableName) throws MalformedURLException {
Boolean publish = false;
URL u = new URL(url);
GeoServerRESTManager manager = new GeoServerRESTManager(u, username, password);
GeoServerRESTPublisher publisher = manager.getPublisher() ;
List<String> workspaces = manager.getReader().getWorkspaceNames();
if(!workspaces.contains(workSpace)){
boolean createws = publisher.createWorkspace(workSpace);
System.out.println(" ws : " + createws);
}else {
System.out.println("工作空间已经存在了,ws :" + workSpace);
}
RESTDataStore restStore = manager.getReader().getDatastore(workSpace, storeName);
if(restStore == null){
GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(storeName);
store.setHost(postGis.getHost());
store.setPort(postGis.getPort());
store.setUser(postGis.getUser());
store.setPassword(postGis.getPassword());
store.setDatabase(postGis.getDatabase());
store.setSchema(postGis.getSchame());
store.setConnectionTimeout(20);
store.setMaxConnections(20);
store.setMinConnections(1);
store.setExposePrimaryKeys(true);
boolean createStore = manager.getStoreManager().create(workSpace, store);
System.out.println("create store : " + createStore);
} else {
System.out.println("数据存储已经存在了,store:" + storeName);
}
//判断图层是否已经存在,不存在则创建并发布
RESTLayer layer = manager.getReader().getLayer(workSpace, tableName);
if(layer == null){
GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
pds.setTitle(tableName);
pds.setName(tableName);
pds.setSRS("EPSG:4326");
GSLayerEncoder layerEncoder = new GSLayerEncoder();
publish = manager.getPublisher().publishDBLayer(workSpace, storeName,
pds, layerEncoder);
System.out.println("publish : " + publish);
}else {
System.out.println("表已经发布过了,table:" + tableName);
}
return publish;
}
//发布shapefile数据
public Boolean GeoserverPublishShapefileData(String zipFilePath, String layerName, String urlDataStore, String workSpace, String storeName) throws MalformedURLException, FileNotFoundException {
boolean publish = false;
File zipFile = new File(zipFilePath);
//判断工作区(workspace)是否存在,不存在则创建
URL u = new URL(url);
//获取管理对象
GeoServerRESTManager manager = new GeoServerRESTManager(u, username, password);
//获取发布对象
GeoServerRESTPublisher publisher = manager.getPublisher() ;
//获取所有的工作空间名称
List<String> workspaces = manager.getReader().getWorkspaceNames();
//判断工作空间是否存在
if(!workspaces.contains(workspaces)){
//创建一个新的存储空间
boolean createws = publisher.createWorkspace(workSpace);
System.out.println("create ws : " + createws);
}else {
System.out.println("workspace已经存在了,ws :" + workSpace);
}
//判断数据存储(datastore)是否已经存在,不存在则创建
URL urlShapefile = new URL(urlDataStore);
RESTDataStore restStore = manager.getReader().getDatastore(workSpace, storeName);
if(restStore == null){
//创建shape文件存储
GSShapefileDatastoreEncoder store = new GSShapefileDatastoreEncoder(storeName, urlShapefile);
boolean createStore = manager.getStoreManager().create(workSpace, store);
System.out.println("create store : " + createStore);
} else {
System.out.println("数据存储已经存在了,store:" + storeName);
}
//判断图层是否已经存在,不存在则创建并发布
RESTLayer layer = manager.getReader().getLayer(workSpace, layerName);
if(layer == null){
//发布图层
String srs = "EPSG:4326";
publish = manager.getPublisher().publishShp(workSpace, storeName, layerName, zipFile, srs);
System.out.println("publish : " + publish);
}else {
System.out.println("表已经发布过了,table:" + storeName);
}
return publish;
}
//发布tiff数据
public Boolean GeoserverPublishTiffData(String zipFilePath, String workSpace, String storeName) throws MalformedURLException, FileNotFoundException {
boolean publish = false;
File zipFile = new File(zipFilePath);
//判断工作区(workspace)是否存在,不存在则创建
System.err.println(url);
URL u = new URL(url);
//获取管理对象
GeoServerRESTManager manager = new GeoServerRESTManager(u, username, password);
//获取发布对象
GeoServerRESTPublisher publisher = manager.getPublisher() ;
//获取所有的工作空间名称
List<String> workspaces = manager.getReader().getWorkspaceNames();
//判断工作空间是否存在
if(!workspaces.contains(workspaces)){
//创建一个新的存储空间
boolean createws = publisher.createWorkspace(workSpace);
System.out.println("create ws : " + createws);
}else {
System.out.println("workspace已经存在了,ws :" + workSpace);
}
RESTDataStore restStore = manager.getReader().getDatastore(workSpace, storeName);
if (restStore == null) {
GSGeoTIFFDatastoreEncoder gsGeoTIFFDatastoreEncoder = new GSGeoTIFFDatastoreEncoder(storeName);
gsGeoTIFFDatastoreEncoder.setWorkspaceName(workSpace);
gsGeoTIFFDatastoreEncoder.setUrl(new URL("file:" + zipFile));
boolean createStore = manager.getStoreManager().create(workSpace, gsGeoTIFFDatastoreEncoder);
System.out.println("create store (TIFF文件创建状态) : " + createStore);
publish = manager.getPublisher().publishGeoTIFF(workSpace, storeName, zipFile);
//
Boolean publish1 = manager.getStyleManager().publishStyle(new File("D:\project_data\sxqx\basic\style\succeed\dem.sld"));
System.out.println("publish (TIFF文件发布状态) : " + publish);
GSLayerEncoder layerEncoder = new GSLayerEncoder();
layerEncoder.setDefaultStyle("dem");
GeoServerRESTPublisher geoServerRESTPublisher = new GeoServerRESTPublisher(url,username,password);
geoServerRESTPublisher.configureLayer(workSpace,storeName,layerEncoder);
//
System.out.println("publish (TIFF文件样式状态) : " + publish1);
} else {
System.out.println("数据存储已经存在了,store:" + storeName);
}
System.err.println(manager.getStructuredGridCoverageReader());
return publish;
}
private GSLayerEncoder configureDefaultStyle(String defaultStyle) {
GSLayerEncoder layerEncoder = new GSLayerEncoder();
if (defaultStyle != null && !defaultStyle.isEmpty()) {
if (defaultStyle.indexOf(":") != -1) {
String[] wsAndName = defaultStyle.split(":");
layerEncoder.setDefaultStyle(wsAndName[0], wsAndName[1]);
} else {
layerEncoder.setDefaultStyle(defaultStyle);
}
}
return layerEncoder;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public GeoProperties.PostGis getPostGis() {
return postGis;
}
public void setPostGis(GeoProperties.PostGis postGis) {
this.postGis = postGis;
}
}
最后
以上就是沉静钥匙为你收集整理的Geoserver服务发布(postgis、tiff、shpfile)的全部内容,希望文章能够帮你解决Geoserver服务发布(postgis、tiff、shpfile)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复