时间:2020-12-23 作者:悬浮的青春 分类: gis二次开发
不啰嗦直接贴代码
pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.test</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>it.geosolutions</groupId> <artifactId>geoserver-manager</artifactId> <version>1.7.0</version> </dependency> </dependencies> <repositories> <repository> <id>GeoSolutions</id> <url>http://maven.geo-solutions.it/</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
class 代码。
直接使用的springboot简单做了实现。
package com.test.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.GSPostGISDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.datastore.GSShapefileDatastoreEncoder;
import it.geosolutions.geoserver.rest.encoder.feature.GSFeatureTypeEncoder;
import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoder;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
import it.geosolutions.geoserver.rest.decoder.RESTDataStoreList;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws IOException {
//GeoServer的连接配置
String url = "http://localhost:8080/geoserver" ;
String username = "admin" ;
String passwd = "geoserver" ;
//GeoserverPublishPostGISData(url, username, passwd);
GeoserverPublishTiffData(url, username, passwd);
//GeoserverPublishTiffData(url, username, passwd); //发布tiff数据
// SpringApplication.run(DemoApplication.class, args);
}
//发布postgis中的数据
public static void GeoserverPublishPostGISData(String url,String username,String passwd) throws IOException{
//postgis连接配置
String postgisHost = "localhost" ;
int postgisPort = 5432 ;//端口号
String postgisUser = "postgres" ;//用户名
String postgisPassword = "postgres" ;//用户密码
String postgisDatabase = "sqlView" ;//数据库名称
String ws = "testNew" ; //待创建和发布图层的工作区名称workspace
String store_name = "testGeoserver" ; //待创建和发布图层的数据存储名称store
String table_name = "roa_4m" ; // 数据库要发布的表名称,后面图层名称和表名保持一致
//判断工作区(workspace)是否存在,不存在则创建
URL u = new URL(url);
GeoServerRESTManager manager = new GeoServerRESTManager(u, username, passwd);
GeoServerRESTPublisher publisher = manager.getPublisher() ;
List<String> workspaces = manager.getReader().getWorkspaceNames();
if(!workspaces.contains(ws)){
boolean createws = publisher.createWorkspace(ws);
System.out.println("create ws : " + createws);
}else {
System.out.println("workspace已经存在了,ws :" + ws);
}
//判断数据存储(datastore)是否已经存在,不存在则创建
RESTDataStore restStore = manager.getReader().getDatastore(ws, store_name);
if(restStore == null){
GSPostGISDatastoreEncoder store = new GSPostGISDatastoreEncoder(store_name);
store.setHost(postgisHost);//设置url
store.setPort(postgisPort);//设置端口
store.setUser(postgisUser);// 数据库的用户名
store.setPassword(postgisPassword);// 数据库的密码
store.setDatabase(postgisDatabase);// 那个数据库;
store.setSchema("public"); //当前先默认使用public这个schema
store.setConnectionTimeout(20);// 超时设置
//store.setName(schema);
store.setMaxConnections(20); // 最大连接数
store.setMinConnections(1); // 最小连接数
store.setExposePrimaryKeys(true);
boolean createStore = manager.getStoreManager().create(ws, store);
System.out.println("create store : " + createStore);
} else {
System.out.println("数据存储已经存在了,store:" + store_name);
}
//判断图层是否已经存在,不存在则创建并发布
RESTLayer layer = manager.getReader().getLayer(ws, table_name);
if(layer == null){
GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
pds.setTitle(table_name);
pds.setName(table_name);
pds.setSRS("EPSG:4326");
GSLayerEncoder layerEncoder = new GSLayerEncoder();
boolean publish = manager.getPublisher().publishDBLayer(ws, store_name, pds, layerEncoder);
System.out.println("publish : " + publish);
}else {
System.out.println("表已经发布过了,table:" + table_name);
}
}
public static void GeoserverPublishTiffData(String url,String username,String passwd) throws IOException {
GeoServerRESTPublisher geoServerRESTPublisher = new GeoServerRESTPublisher(url,username,passwd);
GeoServerRESTReader geoServerRESTReader = new GeoServerRESTReader(url,username,passwd);
String workspace = "bbb";
String storeName = "resttestdem";
String coverageName = "resttestdem";
File geotiff = new File("D:\\11\\11.tif");
//workspace是否存在,不存在则新建。
List<String> workspacesList = geoServerRESTReader.getWorkspaceNames();
boolean wsNull = !workspacesList.contains(workspace);
if(wsNull){
geoServerRESTPublisher.createWorkspace(workspace);
}
//store是否存在,不存在则新建并发布数据。
RESTDataStoreList datastoresList = geoServerRESTReader.getDatastores(workspace);
List<String> datastoreNameList = datastoresList.getNames();
boolean storeNull = !datastoreNameList.contains(storeName);
if(storeNull){
boolean result = geoServerRESTPublisher.publishGeoTIFF(workspace,storeName,coverageName,geotiff);
System.out.println("数据发布是否成功:"+result);
}else{
System.out.println("数据已经发布过了,不能重复发布!");
}
}
//发布shapefile数据
public static void GeoserverPublishShapefileData(String url,String username,String passwd) throws IOException{
String ws = "testshape" ; //待创建和发布图层的工作区名称workspace
String store_name = "testShapeStore" ; //待创建和发布图层的数据存储名称store
String srs="EPSG:4326";
//压缩文件的完整路径
File zipFile=new File("D:/软件安装/geoserver/GeoServer 2.14.0/data_dir/data/shapefiles/states.zip");
String layername="states";//图层名称
//shp文件所在的位置
String urlDatastore="file:data/shapefiles/states.shp";
//判断工作区(workspace)是否存在,不存在则创建
URL u = new URL(url);
//获取管理对象
GeoServerRESTManager manager = new GeoServerRESTManager(u, username, passwd);
//获取发布对象
GeoServerRESTPublisher publisher = manager.getPublisher() ;
//获取所有的工作空间名称
List<String> workspaces = manager.getReader().getWorkspaceNames();
//判断工作空间是否存在
if(!workspaces.contains(ws)){
//创建一个新的存储空间
boolean createws = publisher.createWorkspace(ws);
System.out.println("create ws : " + createws);
}else {
System.out.println("workspace已经存在了,ws :" + ws);
}
//判断数据存储(datastore)是否已经存在,不存在则创建
URL urlShapefile = new URL(urlDatastore);
RESTDataStore restStore = manager.getReader().getDatastore(ws, store_name);
if(restStore == null){
//创建shape文件存储
GSShapefileDatastoreEncoder store = new GSShapefileDatastoreEncoder(store_name, urlShapefile);
boolean createStore = manager.getStoreManager().create(ws, store);
System.out.println("create store : " + createStore);
} else {
System.out.println("数据存储已经存在了,store:" + store_name);
}
//判断图层是否已经存在,不存在则创建并发布
RESTLayer layer = manager.getReader().getLayer(ws, layername);
if(layer == null){
//发布图层
boolean publish = manager.getPublisher().publishShp(ws, store_name, layername, zipFile, srs);
System.out.println("publish : " + publish);
}else {
System.out.println("表已经发布过了,table:" + store_name);
}
}
}
标签: geoserver