GeoServer系列-java发布shp文件

pom引入

        <dependency>
            <groupId>it.geosolutions</groupId>
            <artifactId>geoserver-manager</artifactId>
            <version>1.7.0</version>
        </dependency>

单元测试



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.datastore.GSShapefileDatastoreEncoder;
import org.apache.hc.core5.http.message.BasicNameValuePair;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;

public class GeoShpTest {
    public static void main(String[] args) throws IOException {
        //GeoServer的连接配置
        //String url = "http://192.168.10.151:8993/geoserver";
        String url = "http://127.0.0.1:8016/geoserver";
        String username = "admin";
        String passwd = "geoserver";
        //RESTManager 发布shp图层
        GeoserverPublishShapefileData(url, username, passwd);
    }


    public static void GeoserverPublishShapefileData(String url, String username, String passwd) throws IOException {
		//工作空间        
		String ws = "test";
		//存储目录
        String store_name = "yule";
		//申明坐标系
        String srs = "EPSG:4490";
        //压缩文件的完整路径,用户本地目录,压缩包和解压后的文件不能包含中文
        File zipFile = new File("C:\\Users\\CDLX\\Desktop\\cad\\224cadtest\\224cadtest\\224cadtest.zip");
		//图层名称
        String layername = "224cadtest";
        // shp文件所在的位置,geoserver 服务器所在目录,这个目录具体未知可以参考web界面上上传时显示的地址
        String urlDatastore = "file:data/test/yule/224cadtest.shp";
        //判断工作区(workspace)是否存在,不存在则创建cccc
        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);
        }

        //判断数据存储(datastore)是否已经存在,不存在则创建
        URL urlShapefile = new URL(urlDatastore);
        RESTDataStore restStore = manager.getReader().getDatastore(ws, store_name);
//        System.out.println(restStore);
        if (restStore == null) {
            //创建shape文件存储 , 设置编码支持中文
            GSShapefileDatastoreEncoder store = new GSShapefileDatastoreEncoder(store_name, urlShapefile);
            store.setCharset(Charset.forName("gb2312"));
//            System.out.println("ddd"+store.getCharset());
            boolean createStore = manager.getStoreManager().create(ws, store);
            System.out.println("create store : " + createStore);
        } else {
            System.out.println("数据存储已经存在了,store:" + store_name);
        }

        //判断图层是否已经存在,不存在则创建并发布
        GeoServerRESTReader reader = new GeoServerRESTReader(url, username, passwd);
        RESTLayer layer = reader.getLayer(ws, layername);
        if (layer == null) {
            //发布shp文件
            boolean publish = publisher.publishShp(ws, store_name, layername, zipFile, srs, new BasicNameValuePair("charset", "gbk"));
            System.out.println("发布shp文件 : " + layername + ":" + publish);
        } else {
            System.out.println("shp已经发布过了,layer:" + layername);
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
你可以使用 GeoTools 库来实现将 SHP 文件发布为 GeoJSON 格式的服务。下面是一个示例代码: ```java import org.geoserver.catalog.Catalog; import org.geoserver.catalog.CoverageStoreInfo; import org.geoserver.catalog.DataStoreInfo; import org.geoserver.catalog.FeatureTypeInfo; import org.geoserver.catalog.NamespaceInfo; import org.geoserver.catalog.StoreInfo; import org.geoserver.catalog.WorkspaceInfo; import org.geoserver.catalog.impl.CatalogImpl; import org.geoserver.data.util.IOUtils; import org.geoserver.platform.GeoServerExtensions; import org.geoserver.wfs.WFSServiceImpl; import org.geotools.data.DataStore; import org.geotools.data.DataStoreFinder; import org.geotools.data.FeatureSource; import org.geotools.data.FeatureStore; import org.geotools.data.Transaction; import org.geotools.data.simple.SimpleFeatureCollection; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.data.simple.SimpleFeatureStore; import org.geotools.feature.FeatureCollection; import org.geotools.geojson.feature.FeatureJSON; import org.geotools.geometry.jts.ReferencedEnvelope; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.opengis.feature.Property; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.filter.Filter; import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Map; @RestController @RequestMapping("/geojson") public class GeoJSONController { private Catalog catalog; public GeoJSONController() { this.catalog = new CatalogImpl(); } @RequestMapping(value = "/{workspace}/{datastore}/{layer}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<String> getGeoJSON(@PathVariable("workspace") String workspaceName, @PathVariable("datastore") String datastoreName, @PathVariable("layer") String layerName, HttpServletRequest request) { try { WorkspaceInfo workspace = catalog.getWorkspaceByName(workspaceName); DataStoreInfo dataStore = catalog.getDataStoreByName(datastoreName, workspace); FeatureTypeInfo featureType = catalog.getFeatureTypeByName(workspaceName, layerName); DataStore datastore = DataStoreFinder.getDataStore(dataStore.getConnectionParameters()); SimpleFeatureSource featureSource = datastore.getFeatureSource(featureType.getName()); FeatureCollection<SimpleFeatureType, SimpleFeature> collection = featureSource.getFeatures(); SimpleFeatureCollection features = (SimpleFeatureCollection) collection; // Convert SimpleFeatureCollection to GeoJSON FeatureJSON featureJSON = new FeatureJSON(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); featureJSON.writeFeatureCollection(features, outputStream); return ResponseEntity.ok(outputStream.toString()); } catch (Exception e) { return ResponseEntity.status(500).body("Error: " + e.getMessage()); } } } ``` 在此代码中,我们首先获取了指定的工作空间、数据存储和要素类型。然后,我们使用 `DataStoreFinder` 类创建一个 `DataStore` 实例,并从中获取要素源。接下来,我们使用 GeoTools 库的 `FeatureJSON` 类将 `SimpleFeatureCollection` 转换为 GeoJSON 字符串,并返回该字符串作为响应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

占星安啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值