Geoserver自动化上传Shapefile

geosever提供了一个rest接口,可以通过这个接口实现一些功能,再通过相关的代码,就可以实现部分功能自动化。

这里介绍shapefile数据实现上传自动化的几种方法。

官方API https://docs.geoserver.org/stable/en/user/rest/stores.html

(1)直接上传ZIP,来发布服务

解析代码:

pro.zip(上传的shp压缩文件,该文件直接将shp数据压缩成文件,不要放置在文件夹下再压缩文件夹!)

curl -v -u admin:geoserver //用户名 密码
-XPUT -H "Content-type: application/zip" //文件类型
--data-binary @C:\Users\111111\Desktop\shapefile_test\pro.zip //文件路径
http://localhost:8888/geoserver/rest/workspaces/HZP/datastores/pro/file.shp?charset=GBK

curl可操作demo

curl -v -u admin:geoserver -XPUT -H "Content-type: application/zip" --data-binary @C:\Users\111111\Desktop\shapefile_test\pro.zip http://localhost:8888/geoserver/rest/workspaces/HZP/datastores/pro/file.shp?charset=GBK

写成java形式来调用Curl发布

String filePath;
String fileName;
String workspaceName;

//获取文件名称 ,用户上传其本地的zip,需要先知道文件
//的地址,即filepath,再对filepath处理     
//得到filename 
/*File tempFile =new File( filepath.trim());  
/*String fileName = tempFile.getName(); 

String CurlDOS = "curl -v -u admin:geoserver -XPUT -H \"Content-type: application/zip\" --data-binary @"+filePath+" http://localhost:8888/geoserver/rest/workspaces/"+workspaceName+"/datastores/"+fileName+"/file.shp?charset=GBK"

(2)通过调用XML的方法来达到发布服务。

(发布指定文件夹中的矢量数据)

//2中的workspace需要和4中的一致,

//4中的datastores后的参数需要与XML中的name一致。

//4中的<featureType><name>所对应的是上传的shp的文件名。

Curl直接操作demo:

//先执行2
curl -u admin:geoserver -XPOST -H "Content-type:text/xml" -d @C:\Users\111111\Desktop\shapefile_test\pro.xml http://localhost:8888/geoserver/rest/workspaces/HZP/datastores.xml
//再执行4 
curl -u admin:geoserver -XPOST -H "Content-type:text/xml" -d "<featureType><name>pro</name></featureType>"  http://localhost:8888/geoserver/rest/workspaces/HZP/datastores/pro/featuretypes

XML

<dataStore>
  <name>pro</name> //此处的name指的是存储空间的名称
  <type>Shapefile</type>
  <enabled>true</enabled>
  <connectionParameters>
    <entry key="url">file:/C:/Users/111111/Desktop/shapefile_test/pro</entry>
	<entry key="charset">GBK</entry>
  </connectionParameters>
  <__default>false</__default>
</dataStore>

参考版XML

<dataStore>
    <name>pro</name>
    <description>tiger counties created from REST</description>
    <!-- 上传数据存储的类型 -->
    <type>Shapefile</type>
    <!-- 数据存储是否可用-->
    <enabled>true</enabled>
    <connectionParameters>
        <!-- 使用内存映射的缓冲区 -->
        <entry key="memory mapped buffer">false</entry>
        <!-- 如果缺少空间索引或者空间索引过时,重新建立空间索引 -->
        <entry key="create spatial index">true</entry>
        <!-- DBF的字符集 -->
        <entry key="charset">GBK</entry>
        <!-- 数据文件类型 -->
        <entry key="filetype">shapefile</entry>
        <!-- 如果缺少空间索引或者空间索引过时,重新建立空间索引 -->
        <entry key="cache and reuse memory maps">true</entry>
        <!-- 上传数据文件的路径 -->
        <entry key="url">C:\Users\111111\Desktop\shapefile_test\pro\pro.shp</entry>
        <entry key="namespace">http://HZP</entry>
    </connectionParameters>
    <__default>false</__default>
</dataStore>

(3)使用curl完成数据Postgis中数据的发布

方法一(失败)

1.新建工作空间

curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "<namespace><prefix>PostGis_test</prefix><uri>http://localhost:8888/POSTGIS</uri></namespace>" http://localhost:8888/geoserver/rest/namespaces

方法二:

1.先定义好上传的数据库。

2.数据更改后,再添加数据即可。

name为文件名。

curl -u admin:geoserver -XPOST -H "Content-type:text/xml" -d "<featureType><name>50m_admin_0_countries</name></featureType>"  http://localhost:8888/geoserver/rest/workspaces/PostGis_pro/datastores/postgis/featuretypes

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我会尽力回答您的问题。首先需要了解一下GeoServer的架构和工作原理,以及shapefile图层的相关知识。 GeoServer是一个基于Java的开源地理信息系统软件,它可以将地理空间数据发布为Web服务。它的工作原理是将地理空间数据存储在数据库中,然后将数据通过WMS、WFS等协议发布为Web服务。 shapefile是一种常见的地理信息数据格式,它由三个文件组成:.shp、.dbf和.shx文件。其中.shp文件包含了地理要素的几何形状信息,.dbf文件包含了地理要素的属性信息,.shx文件是用来提高访问速度的索引文件。 为了自动发布shapefile图层,可以考虑编写一个插件来实现。具体步骤如下: 1. 创建一个新的GeoServer插件项目,并添加依赖项。 2. 实现一个自定义的发布工具,用来将shapefile文件上传GeoServer中,并创建对应的图层。 3. 实现一个自定义的数据存储,用来管理shapefile图层的数据。 4. 实现一个自定义的样式管理器,用来管理shapefile图层的样式。 5. 扩展GeoServer的REST API,以便我们能够在UI中访问我们的自定义插件。 6. 编写一些测试用例,确保我们的插件能够正常工作。 以上是大致的步骤,具体实现方法需要根据您的需求进行调整。如果您需要更详细的信息,可以参考GeoServer官方文档中的扩展开发部分,或者在CSDN上搜索相关的教程和例子。 希望能对您有所帮助,如果您还有其他问题,可以随时问我。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值