赞
踩
geoserver支持的数据存储种类多样,客户的空间数据种类更杂,比如dwg,excel,kml,mysql等等,针对多样的空间文件,一般系统会统一解析保存空间属性,很容易就能转换成geojson,所以大部分空间数据文件的发布,都可以转为geojson的发布,业务系统保存属性信息的同时空间信息也能顺便发布出去,遇到重新发布时就很方便,不需要一直保存用户上传的文件,省去许多文件IO操作的麻烦
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${spring-data-mongodb.version}</version>
</dependency>
<dependency>
<groupId>it.geosolutions</groupId>
<artifactId>geoserver-manager</artifactId>
<version>${geoserver.version}</version>
</dependency>
import com.cqdh.group.its.db.util.GSMongodbDatastoreEncoder;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import it.geosolutions.geoserver.rest.GeoServerRESTManager;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTReader;
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.feature.GSFeatureTypeEncoder;
import org.bson.Document;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
/**
* @Description 发布geojson文件到geoserver
* @Date 2023/3/2 16:08
* @Author 余乐
**/
public class GeoJsonToGeoServer {
public static void main(String[] args) throws IOException, JSONException {
String jsonFilePath = "C:\\Users\\CDLX\\Desktop\\geojson\\TEST004_SHP_SHUIYU.geojson";
String url = "http://127.0.0.1:8089/geoserver";
String username = "admin";
String passwd = "geoserver";
String ws = "yule";
String storeName = "151mongodb";
String mongodbUrl = "mongodb://192.168.1.1:27017/its-table";
String db = "its-table";
String collectname = "TEST004_GEOJSON_SHUIYU";
GeoJsonToGeoServer toGeoServer = new GeoJsonToGeoServer();
//1保存geojson空间数据到mongodb
toGeoServer.saveToMongodb(jsonFilePath, mongodbUrl, db, collectname);
//2发布mongodb空间表到geoserver
toGeoServer.publicMongodb(url, username, passwd, collectname, mongodbUrl, ws, storeName);
}
/**
* 保存json数据到mongodb
*
* @param filePath geojson全路径
* @param mongodbUrl mongodb地址
* @param db 数据库
* @param collect 表名
* @return
* @throws IOException
* @throws JSONException
*/
public void saveToMongodb(String filePath, String mongodbUrl, String db, String collect) throws IOException, JSONException {
// 1. 读取 GeoJSON 文件
BufferedReader reader = new BufferedReader(new FileReader(filePath));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
reader.close();
String geoJsonString = stringBuilder.toString();
// 2. 解析 GeoJSON 数据
JSONTokener tokener = new JSONTokener(geoJsonString);
JSONObject geoJson = new JSONObject(tokener);
// 3. 连接 MongoDB
MongoClient mongoClient = MongoClients.create(mongodbUrl);
MongoDatabase mongoDatabase = mongoClient.getDatabase(db);
MongoCollection<Document> mongoCollection = mongoDatabase.getCollection(collect);
//先清空表再写入
mongoCollection.drop();
//创建索引,否则geoserver找不到该表,导致发布失败
mongoCollection.createIndex(new Document("geometry", "2dsphere"));
// 4. 将解析后的 GeoJSON 数据插入 MongoDB
JSONArray features = geoJson.getJSONArray("features");
for (int i = 0; i < features.length(); i++) {
JSONObject feature = features.getJSONObject(i);
Document document = Document.parse(feature.toString());
mongoCollection.insertOne(document);
}
// 5. 关闭 MongoDB 连接
mongoClient.close();
}
/**
* 发布mongodb表到geoserver
*
* @param url geoserverUrl
* @param username geoserver用户名
* @param passwd geoserver密码
* @param tale_name 空间表名
* @param mongodbUrl mongodb地址
* @param ws geoserver工作空间
* @param store_name geoserver存储仓库
* @throws MalformedURLException
*/
public void publicMongodb(String url, String username, String passwd, String tale_name, String mongodbUrl, String ws, String store_name) throws MalformedURLException {
String srs = "EPSG:4490";
//判断工作区(workspace)是否存在,不存在则创建
URL restURL = new URL(url);
//获取管理对象
GeoServerRESTManager manager = new GeoServerRESTManager(restURL, 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) {
//创建momgodb数据存储
GSMongodbDatastoreEncoder store = new GSMongodbDatastoreEncoder(store_name);
store.setSchema(mongodbUrl);
store.setData_store(mongodbUrl);
boolean createStore = manager.getStoreManager().create(ws, store);
System.out.println("create store " + store_name + ":" + createStore);
} else {
System.out.println("数据存储已经存在了,store:" + store_name);
}
//判断图层是否已经存在,不存在则创建并发布
GeoServerRESTReader reader = new GeoServerRESTReader(url, username, passwd);
RESTLayer layer = reader.getLayer(ws, tale_name);
if (layer == null) {
GSFeatureTypeEncoder pds = new GSFeatureTypeEncoder();
pds.setTitle(tale_name);
pds.setName(tale_name);
//设置坐标系,预览有图片位置不对都是这个参数引起的
pds.setSRS(srs);
GSLayerEncoder layerEncoder = new GSLayerEncoder();
boolean publish = manager.getPublisher().publishDBLayer(ws, store_name, pds, layerEncoder);
System.out.println("publish : " + publish);
} else {
System.out.println("表已经发布过了,table:" + tale_name);
}
}
}
workspace已经存在了,ws :yule
数据存储已经存在了,store:151mongodb
publish : true
检查数据库
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。