再战GeoServer和WFS的实践经验

前言

最近搞了一波geoserver,使用geoServer发布WFS,使用其RestAPI进行空间库的操作。这次我把一些重要信息记录在这,以防后面忘记。也给其他有需要的人一些提示吧。
PS网上的资料都是绝大多数都是复制粘贴的。。。。

GeoServer简介

GeoServer is a Java-based software server that allows users to view and edit geospatial data. Using open standards set forth by the Open Geospatial Consortium (OGC), GeoServer allows for great flexibility in map creation and data sharing.
关键字:开源 标准
在github上开源。

内容

本文设计到的内容都是在GeoServer 2.15.1上进行的。
与GeoServer 的WFS进行基于Rest交互关键就在于请求参数,值得注意的是这些请求最好采用POST方法发送。
查询可以采用json,但增加,删除,修改都只能采用XML形式。

GeoServer提供的一个演示的工具是非常好的 参考地方,请善于使用。
在这里插入图片描述

增加API

<wfs:Transaction service="WFS" version="1.0.0"    xmlns:opengis="http://www.geo_road.com"    xmlns:wfs="http://www.opengis.net/wfs"    xmlns:ogc="http://www.opengis.net/ogc"   xmlns:gml="http://www.opengis.net/gml"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.opengis.net/wfs   http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
    <wfs:Insert handle="geo_road">
        <opengis:geo_roads>
            <opengis:geom>
                <gml:LineString srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
                    <gml:coordinates decimal="." cs="," ts=" "> 39.871086,-72.91787 41.871086,-72.91787</gml:coordinates>
                </gml:LineString>
            </opengis:geom>
            <opengis:name>123</opengis:name>
            <opengis:subType>null</opengis:subType>
            <opengis:type>road</opengis:type>
        </opengis:geo_roads>
    </wfs:Insert>
</wfs:Transaction>

注意xmlns:opengis后面那个值是工作区对应的命名空间的名字。
在这里插入图片描述

删除API

<Transaction xmlns="http://www.opengis.net/wfs" service="WFS" version="1.0.0"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://www.opengis.net/wfs   http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
    <Delete typeName="geo_road:geo_roads">
        <Filter xmlns="http://www.opengis.net/ogc">
            <FeatureId fid="geo_roads.8345"/>
        </Filter>
    </Delete>
</Transaction>

修改API

注意xmlns:geo_road后面那个值是工作区对应的命名空间的名字。


<wfs:Transaction service="WFS" version="1.0.0"    xmlns:geo_road="http://www.geo_road.com"    xmlns:wfs="http://www.opengis.net/wfs"    xmlns:ogc="http://www.opengis.net/ogc"    xmlns:gml="http://www.opengis.net/gml"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.opengis.net/wfs   http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">
    <wfs:Update typeName="geo_road:geo_roads">
        <wfs:Property>
            <wfs:Name>geom</wfs:Name>
            <wfs:Value>
                <gml:LineString>
                    <gml:coordinates decimal="." cs="," ts=" "> 39.871086,119.91787 41.871086,120.91787</gml:coordinates>
                </gml:LineString>
            </wfs:Value>
        </wfs:Property>
        <wfs:Property>
            <wfs:Name>name    </wfs:Name>
            <wfs:Value>zw8345    </wfs:Value>
        </wfs:Property>
        <ogc:Filter>
            <ogc:FeatureId fid="geo_roads.8345"/>
        </ogc:Filter>
    </wfs:Update>
</wfs:Transaction>

查询API

根据ID查询

这个请求可以通过下面的方式

 Client client = Client.create();
        WebResource resource;
        postParams.add("version", "1.1.0");
        postParams.add("service", "wfs");
        postParams.add("outputFormat", "json");
        postParams.add("FEATUREID", uuid);
        resource = client.resource(url).queryParams(postParams);

具体参数

{"service":["wfs"],"FEATUREID":["geo_roads.8344"],"version":["1.1.0"],"outputFormat":["json"]}

空间查询

发送方式跟上面一样。

{"FILTER":["<Filter><And><BBOX><PropertyName>geom<\/PropertyName><Envelope srsName='EPSG:4326'><lowerCorner>-72.91787 41.871086<\/lowerCorner><upperCorner>-74.91787 39.871086<\/upperCorner><\/Envelope><\/BBOX><\/And><\/Filter>"],"service":["wfs"],"version":["1.1.0"],"outputFormat":["json"]}

总结

geoServer确实坑很多,至今还遇到了不少大坑,比如可怕的magic nubmer问题。如果有哪位大神看到并解决了可以私信我。
但是带来的收益也是巨大的,不用担心使用破解版遭遇诉讼赔的倾家荡产,也不用每个项目投入40万+的巨额资金。
实际上,只要在这个技术上投入少于400人天那肯定是赚翻了。

发布了142 篇原创文章 · 获赞 70 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/zhaoenweiex/article/details/91042710